|
1 | | -import { Connection } from '@solana/web3.js'; |
2 | 1 | import { ActionComponent, type Action } from './Action.ts'; |
3 | 2 |
|
4 | 3 | export interface ActionContext { |
@@ -30,66 +29,3 @@ export interface ActionAdapter { |
30 | 29 | context: ActionContext, |
31 | 30 | ) => Promise<void>; |
32 | 31 | } |
33 | | - |
34 | | -export class ActionConfig implements ActionAdapter { |
35 | | - private static readonly CONFIRM_TIMEOUT_MS = 60000 * 1.2; // 20% extra time |
36 | | - private connection: Connection; |
37 | | - |
38 | | - constructor( |
39 | | - rpcUrl: string, |
40 | | - private adapter: IncomingActionConfig['adapter'], |
41 | | - ) { |
42 | | - if (!rpcUrl) { |
43 | | - throw new Error('rpcUrl is required'); |
44 | | - } |
45 | | - |
46 | | - this.connection = new Connection(rpcUrl, 'confirmed'); |
47 | | - } |
48 | | - |
49 | | - async connect(context: ActionContext) { |
50 | | - try { |
51 | | - return await this.adapter.connect(context); |
52 | | - } catch { |
53 | | - return null; |
54 | | - } |
55 | | - } |
56 | | - |
57 | | - signTransaction(tx: string, context: ActionContext) { |
58 | | - return this.adapter.signTransaction(tx, context); |
59 | | - } |
60 | | - |
61 | | - confirmTransaction(signature: string): Promise<void> { |
62 | | - return new Promise<void>((res, rej) => { |
63 | | - const start = Date.now(); |
64 | | - |
65 | | - const confirm = async () => { |
66 | | - if (Date.now() - start >= ActionConfig.CONFIRM_TIMEOUT_MS) { |
67 | | - rej(new Error('Unable to confirm transaction')); |
68 | | - return; |
69 | | - } |
70 | | - |
71 | | - try { |
72 | | - const status = await this.connection.getSignatureStatus(signature); |
73 | | - |
74 | | - // if error present, transaction failed |
75 | | - if (status.value?.err) { |
76 | | - rej(new Error('Transaction execution failed')); |
77 | | - return; |
78 | | - } |
79 | | - |
80 | | - // if has confirmations, transaction is successful |
81 | | - if (status.value && status.value.confirmations !== null) { |
82 | | - res(); |
83 | | - return; |
84 | | - } |
85 | | - } catch (e) { |
86 | | - console.error('Error confirming transaction', e); |
87 | | - } |
88 | | - |
89 | | - setTimeout(confirm, 3000); |
90 | | - }; |
91 | | - |
92 | | - confirm(); |
93 | | - }); |
94 | | - } |
95 | | -} |
0 commit comments