Skip to content

Commit 31965ad

Browse files
committed
feat: updated magic-router tx confirmation from polling to websocket subscription signatureSubscribe
1 parent 6fcb73e commit 31965ad

File tree

1 file changed

+57
-40
lines changed

1 file changed

+57
-40
lines changed

ts/src/magic-router.ts

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export function getWritableAccounts(transaction: Transaction) {
3535
/**
3636
* Get the closest validator info from the router connection.
3737
*/
38-
export async function getClosestValidator(routerConnection: Connection) : Promise<{ identity: string; fqdn?: string }> {
38+
export async function getClosestValidator(
39+
routerConnection: Connection,
40+
): Promise<{ identity: string; fqdn?: string }> {
3941
const response = await fetch(routerConnection.rpcEndpoint, {
4042
method: "POST",
4143
headers: { "Content-Type": "application/json" },
@@ -49,11 +51,10 @@ export async function getClosestValidator(routerConnection: Connection) : Promis
4951

5052
const identityData = (await response.json())?.result;
5153

52-
if ((identityData == null) || (identityData.identity == null)) {
54+
if (identityData?.identity == null) {
5355
throw new Error("Invalid response");
5456
}
5557

56-
5758
return identityData;
5859
}
5960

@@ -182,46 +183,62 @@ export async function sendAndConfirmMagicTransaction(
182183
options,
183184
);
184185
let status;
185-
const { recentBlockhash, lastValidBlockHeight, minNonceContextSlot, nonceInfo } = transaction;
186-
if (
187-
recentBlockhash !== undefined && lastValidBlockHeight !== undefined
188-
) {
189-
status = (await connection.confirmTransaction({
190-
abortSignal: options?.abortSignal,
191-
signature: signature,
192-
blockhash: recentBlockhash,
193-
lastValidBlockHeight: lastValidBlockHeight
194-
}, options?.commitment)).value;
195-
} else if (
196-
minNonceContextSlot !== undefined &&
197-
nonceInfo !== undefined
198-
) {
199-
const {
200-
nonceInstruction
201-
} = nonceInfo;
202-
const nonceAccountPubkey = nonceInstruction.keys[0].pubkey;
203-
status = (await connection.confirmTransaction({
204-
abortSignal: options?.abortSignal,
205-
minContextSlot: minNonceContextSlot,
206-
nonceAccountPubkey,
207-
nonceValue: nonceInfo.nonce,
208-
signature
209-
}, options?.commitment)).value;
186+
const {
187+
recentBlockhash,
188+
lastValidBlockHeight,
189+
minNonceContextSlot,
190+
nonceInfo,
191+
} = transaction;
192+
if (recentBlockhash !== undefined && lastValidBlockHeight !== undefined) {
193+
status = (
194+
await connection.confirmTransaction(
195+
{
196+
abortSignal: options?.abortSignal,
197+
signature,
198+
blockhash: recentBlockhash,
199+
lastValidBlockHeight,
200+
},
201+
options?.commitment,
202+
)
203+
).value;
204+
} else if (minNonceContextSlot !== undefined && nonceInfo !== undefined) {
205+
const { nonceInstruction } = nonceInfo;
206+
const nonceAccountPubkey = nonceInstruction.keys[0].pubkey;
207+
status = (
208+
await connection.confirmTransaction(
209+
{
210+
abortSignal: options?.abortSignal,
211+
minContextSlot: minNonceContextSlot,
212+
nonceAccountPubkey,
213+
nonceValue: nonceInfo.nonce,
214+
signature,
215+
},
216+
options?.commitment,
217+
)
218+
).value;
210219
} else {
211-
if (options?.abortSignal !== null) {
212-
console.warn('sendAndConfirmTransaction(): A transaction with a deprecated confirmation strategy was ' + 'supplied along with an `abortSignal`. Only transactions having `lastValidBlockHeight` ' + 'or a combination of `nonceInfo` and `minNonceContextSlot` are abortable.');
213-
}
214-
status = (await connection.confirmTransaction(signature, options?.commitment)).value;
220+
if (options?.abortSignal !== null) {
221+
console.warn(
222+
"sendAndConfirmTransaction(): A transaction with a deprecated confirmation strategy was " +
223+
"supplied along with an `abortSignal`. Only transactions having `lastValidBlockHeight` " +
224+
"or a combination of `nonceInfo` and `minNonceContextSlot` are abortable.",
225+
);
226+
}
227+
status = (
228+
await connection.confirmTransaction(signature, options?.commitment)
229+
).value;
215230
}
216-
if (status.err) {
217-
if (signature !== null) {
231+
if (status.err != null) {
232+
if (signature !== null) {
218233
throw new SendTransactionError({
219-
action: 'send',
220-
signature: signature,
221-
transactionMessage: `Status: (${JSON.stringify(status)})`
234+
action: "send",
235+
signature,
236+
transactionMessage: `Status: (${JSON.stringify(status)})`,
222237
});
223-
}
224-
throw new Error(`Transaction ${signature} failed (${JSON.stringify(status)})`);
238+
}
239+
throw new Error(
240+
`Transaction ${signature} failed (${JSON.stringify(status)})`,
241+
);
225242
}
226243
return signature;
227-
}
244+
}

0 commit comments

Comments
 (0)