diff --git a/dist/esm/index.evm.js b/dist/esm/index.evm.js index dfb31dc..e74da1b 100644 --- a/dist/esm/index.evm.js +++ b/dist/esm/index.evm.js @@ -44595,9 +44595,6 @@ Token.solana = { ...instructions }; -let currentDeadline; -let currentNonce; - const getWSolSenderAccountKeypairIfNeeded = async ({ paymentRoute })=> { if( @@ -45071,18 +45068,14 @@ const getPaymentMethod = ({ paymentRoute })=>{ }; const getDeadline = ()=>{ - if(currentDeadline) { return currentDeadline } - currentDeadline = Math.ceil(new Date().getTime()/1000)+1800; // 30 Minutes (lower causes wallet simulation issues) - return currentDeadline + return Math.ceil(new Date().getTime()/1000)+1800 // 30 Minutes (lower causes wallet simulation issues) }; const getNonce = (paymentsAccountData)=>{ - if(currentNonce) { return currentNonce } - currentNonce = paymentsAccountData ? paymentsAccountData.nonce : new BN('0'); - return currentNonce + return paymentsAccountData ? paymentsAccountData.nonce : new BN('0') }; -const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ +const routeSol = async({ paymentRoute, nonce, deadline }) =>{ const paymentReceiverPublicKey = new PublicKey(paymentRoute.toAddress); const feeReceiverPublicKey = paymentRoute.fee ? new PublicKey(paymentRoute.fee.receiver) : paymentReceiverPublicKey; @@ -45098,10 +45091,10 @@ const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ const data = Buffer.alloc(routers$2.solana.api.routeSol.layout.span); routers$2.solana.api.routeSol.layout.encode({ anchorDiscriminator: routers$2.solana.api.routeSol.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -45111,7 +45104,7 @@ const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ }) }; -const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ +const routeToken = async({ paymentRoute, nonce, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }); const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }); @@ -45129,10 +45122,10 @@ const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ const data = Buffer.alloc(routers$2.solana.api.routeToken.layout.span); routers$2.solana.api.routeToken.layout.encode({ anchorDiscriminator: routers$2.solana.api.routeToken.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -45142,7 +45135,7 @@ const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ }) }; -const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ +const routeOrcaSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = wSolSenderAccountKeypair ? wSolSenderAccountKeypair.publicKey : await getPaymentSenderTokenAccountAddress({ paymentRoute }); const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }); @@ -45197,14 +45190,14 @@ const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccou const data = Buffer.alloc(routers$2.solana.api.routeOrcaSwap.layout.span); routers$2.solana.api.routeOrcaSwap.layout.encode({ anchorDiscriminator: routers$2.solana.api.routeOrcaSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -45214,7 +45207,7 @@ const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccou }) }; -const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ +const routeOrcaSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }); const escrowOutWsolPublicKey = wSolEscrowAccountKeypair.publicKey; @@ -45271,14 +45264,14 @@ const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscro const data = Buffer.alloc(routers$2.solana.api.routeOrcaSwapSolOut.layout.span); routers$2.solana.api.routeOrcaSwapSolOut.layout.encode({ anchorDiscriminator: routers$2.solana.api.routeOrcaSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -45288,7 +45281,7 @@ const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscro }) }; -const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ +const routeOrcaTwoHopSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const paymentReceiverTokenAccountPublicKey = new PublicKey(await getPaymentReceiverTokenAccountAddress({ paymentRoute })); const feeReceiverTokenAccountPublicKey = paymentRoute.fee ? new PublicKey(await getFeeReceiverTokenAccountAddress({ paymentRoute })) : paymentReceiverTokenAccountPublicKey; @@ -45362,7 +45355,7 @@ const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSende const data = Buffer.alloc(routers$2.solana.api.routeOrcaTwoHopSwap.layout.span); routers$2.solana.api.routeOrcaTwoHopSwap.layout.encode({ anchorDiscriminator: routers$2.solana.api.routeOrcaTwoHopSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -45371,7 +45364,7 @@ const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSende sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -45381,7 +45374,7 @@ const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSende }) }; -const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ +const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const middleTokenAccountPublicKey = new PublicKey(await getMiddleTokenAccountAddress({ paymentRoute })); const exchangeRouteTransaction = await paymentRoute.exchangeRoutes[0].getTransaction({ account: paymentRoute.fromAddress }); @@ -45456,7 +45449,7 @@ const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSo const data = Buffer.alloc(routers$2.solana.api.routeOrcaTwoHopSwapSolOut.layout.span); routers$2.solana.api.routeOrcaTwoHopSwapSolOut.layout.encode({ anchorDiscriminator: routers$2.solana.api.routeOrcaTwoHopSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -45465,7 +45458,7 @@ const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSo sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -45475,30 +45468,29 @@ const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSo }) }; -const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair })=> { +const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline })=> { - const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }); const paymentMethod = getPaymentMethod({ paymentRoute }); switch(paymentMethod){ case 'routeSol': - return await routeSol({ paymentRoute, paymentsAccountData }); + return await routeSol({ paymentRoute, nonce, deadline }); case 'routeToken': - return await routeToken({ paymentRoute, paymentsAccountData }); + return await routeToken({ paymentRoute, nonce, deadline }); case 'routeOrcaSwap': - return await routeOrcaSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaSwapSolOut': - return await routeOrcaSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); case 'routeOrcaTwoHopSwap': - return await routeOrcaTwoHopSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaTwoHopSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaTwoHopSwapSolOut': - return await routeOrcaTwoHopSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaTwoHopSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); } @@ -45506,6 +45498,10 @@ const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccoun const getTransaction$3 = async({ paymentRoute })=> { + const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }); + const deadline = getDeadline(); + const nonce = getNonce(paymentsAccountData); + const wSolSenderAccountKeypair = await getWSolSenderAccountKeypairIfNeeded({ paymentRoute }); const wSolEscrowAccountKeypair = await getWSolEscrowAccountKeypairIfNeeded({ paymentRoute }); @@ -45519,7 +45515,7 @@ const getTransaction$3 = async({ paymentRoute })=> { createEscrowOutSolAccount({ paymentRoute }), // needs to happen before createEscrowOutWSolAccount createEscrowOutWSolAccount({ paymentRoute, wSolEscrowAccountKeypair }), createEscrowOutTokenAccount({ paymentRoute }), - payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair }), + payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline }), closeWSolSenderAccount({ paymentRoute, wSolSenderAccountKeypair }), ]) ).filter(Boolean).flat(); @@ -45533,8 +45529,8 @@ const getTransaction$3 = async({ paymentRoute })=> { // debug(transaction, paymentRoute) - transaction.deadline = currentDeadline; - transaction.nonce = currentNonce.toString(); + transaction.deadline = deadline; + transaction.nonce = nonce.toString(); return transaction }; diff --git a/dist/esm/index.js b/dist/esm/index.js index 4ecc8b1..b5aef21 100644 --- a/dist/esm/index.js +++ b/dist/esm/index.js @@ -901,9 +901,6 @@ const request = async function (url, options) { }) }; -let currentDeadline; -let currentNonce; - const getWSolSenderAccountKeypairIfNeeded = async ({ paymentRoute })=> { if( @@ -1377,18 +1374,14 @@ const getPaymentMethod = ({ paymentRoute })=>{ }; const getDeadline = ()=>{ - if(currentDeadline) { return currentDeadline } - currentDeadline = Math.ceil(new Date().getTime()/1000)+1800; // 30 Minutes (lower causes wallet simulation issues) - return currentDeadline + return Math.ceil(new Date().getTime()/1000)+1800 // 30 Minutes (lower causes wallet simulation issues) }; const getNonce = (paymentsAccountData)=>{ - if(currentNonce) { return currentNonce } - currentNonce = paymentsAccountData ? paymentsAccountData.nonce : new BN('0'); - return currentNonce + return paymentsAccountData ? paymentsAccountData.nonce : new BN('0') }; -const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ +const routeSol = async({ paymentRoute, nonce, deadline }) =>{ const paymentReceiverPublicKey = new PublicKey(paymentRoute.toAddress); const feeReceiverPublicKey = paymentRoute.fee ? new PublicKey(paymentRoute.fee.receiver) : paymentReceiverPublicKey; @@ -1404,10 +1397,10 @@ const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ const data = Buffer.alloc(solanaRouters.solana.api.routeSol.layout.span); solanaRouters.solana.api.routeSol.layout.encode({ anchorDiscriminator: solanaRouters.solana.api.routeSol.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -1417,7 +1410,7 @@ const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ }) }; -const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ +const routeToken = async({ paymentRoute, nonce, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }); const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }); @@ -1435,10 +1428,10 @@ const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ const data = Buffer.alloc(solanaRouters.solana.api.routeToken.layout.span); solanaRouters.solana.api.routeToken.layout.encode({ anchorDiscriminator: solanaRouters.solana.api.routeToken.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -1448,7 +1441,7 @@ const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ }) }; -const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ +const routeOrcaSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = wSolSenderAccountKeypair ? wSolSenderAccountKeypair.publicKey : await getPaymentSenderTokenAccountAddress({ paymentRoute }); const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }); @@ -1503,14 +1496,14 @@ const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccou const data = Buffer.alloc(solanaRouters.solana.api.routeOrcaSwap.layout.span); solanaRouters.solana.api.routeOrcaSwap.layout.encode({ anchorDiscriminator: solanaRouters.solana.api.routeOrcaSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -1520,7 +1513,7 @@ const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccou }) }; -const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ +const routeOrcaSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }); const escrowOutWsolPublicKey = wSolEscrowAccountKeypair.publicKey; @@ -1577,14 +1570,14 @@ const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscro const data = Buffer.alloc(solanaRouters.solana.api.routeOrcaSwapSolOut.layout.span); solanaRouters.solana.api.routeOrcaSwapSolOut.layout.encode({ anchorDiscriminator: solanaRouters.solana.api.routeOrcaSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -1594,7 +1587,7 @@ const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscro }) }; -const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ +const routeOrcaTwoHopSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const paymentReceiverTokenAccountPublicKey = new PublicKey(await getPaymentReceiverTokenAccountAddress({ paymentRoute })); const feeReceiverTokenAccountPublicKey = paymentRoute.fee ? new PublicKey(await getFeeReceiverTokenAccountAddress({ paymentRoute })) : paymentReceiverTokenAccountPublicKey; @@ -1668,7 +1661,7 @@ const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSende const data = Buffer.alloc(solanaRouters.solana.api.routeOrcaTwoHopSwap.layout.span); solanaRouters.solana.api.routeOrcaTwoHopSwap.layout.encode({ anchorDiscriminator: solanaRouters.solana.api.routeOrcaTwoHopSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -1677,7 +1670,7 @@ const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSende sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -1687,7 +1680,7 @@ const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSende }) }; -const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ +const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const middleTokenAccountPublicKey = new PublicKey(await getMiddleTokenAccountAddress({ paymentRoute })); const exchangeRouteTransaction = await paymentRoute.exchangeRoutes[0].getTransaction({ account: paymentRoute.fromAddress }); @@ -1762,7 +1755,7 @@ const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSo const data = Buffer.alloc(solanaRouters.solana.api.routeOrcaTwoHopSwapSolOut.layout.span); solanaRouters.solana.api.routeOrcaTwoHopSwapSolOut.layout.encode({ anchorDiscriminator: solanaRouters.solana.api.routeOrcaTwoHopSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -1771,7 +1764,7 @@ const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSo sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -1781,30 +1774,29 @@ const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSo }) }; -const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair })=> { +const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline })=> { - const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }); const paymentMethod = getPaymentMethod({ paymentRoute }); switch(paymentMethod){ case 'routeSol': - return await routeSol({ paymentRoute, paymentsAccountData }); + return await routeSol({ paymentRoute, nonce, deadline }); case 'routeToken': - return await routeToken({ paymentRoute, paymentsAccountData }); + return await routeToken({ paymentRoute, nonce, deadline }); case 'routeOrcaSwap': - return await routeOrcaSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaSwapSolOut': - return await routeOrcaSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); case 'routeOrcaTwoHopSwap': - return await routeOrcaTwoHopSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaTwoHopSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaTwoHopSwapSolOut': - return await routeOrcaTwoHopSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaTwoHopSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); } @@ -1812,6 +1804,10 @@ const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccoun const getTransaction$3 = async({ paymentRoute })=> { + const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }); + const deadline = getDeadline(); + const nonce = getNonce(paymentsAccountData); + const wSolSenderAccountKeypair = await getWSolSenderAccountKeypairIfNeeded({ paymentRoute }); const wSolEscrowAccountKeypair = await getWSolEscrowAccountKeypairIfNeeded({ paymentRoute }); @@ -1825,7 +1821,7 @@ const getTransaction$3 = async({ paymentRoute })=> { createEscrowOutSolAccount({ paymentRoute }), // needs to happen before createEscrowOutWSolAccount createEscrowOutWSolAccount({ paymentRoute, wSolEscrowAccountKeypair }), createEscrowOutTokenAccount({ paymentRoute }), - payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair }), + payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline }), closeWSolSenderAccount({ paymentRoute, wSolSenderAccountKeypair }), ]) ).filter(Boolean).flat(); @@ -1839,8 +1835,8 @@ const getTransaction$3 = async({ paymentRoute })=> { // debug(transaction, paymentRoute) - transaction.deadline = currentDeadline; - transaction.nonce = currentNonce.toString(); + transaction.deadline = deadline; + transaction.nonce = nonce.toString(); return transaction }; diff --git a/dist/esm/index.solana.js b/dist/esm/index.solana.js index 11d4018..23c0920 100644 --- a/dist/esm/index.solana.js +++ b/dist/esm/index.solana.js @@ -2199,9 +2199,6 @@ Token.solana = { ...instructions }; -let currentDeadline; -let currentNonce; - const getWSolSenderAccountKeypairIfNeeded = async ({ paymentRoute })=> { if( @@ -2675,18 +2672,14 @@ const getPaymentMethod = ({ paymentRoute })=>{ }; const getDeadline = ()=>{ - if(currentDeadline) { return currentDeadline } - currentDeadline = Math.ceil(new Date().getTime()/1000)+1800; // 30 Minutes (lower causes wallet simulation issues) - return currentDeadline + return Math.ceil(new Date().getTime()/1000)+1800 // 30 Minutes (lower causes wallet simulation issues) }; const getNonce = (paymentsAccountData)=>{ - if(currentNonce) { return currentNonce } - currentNonce = paymentsAccountData ? paymentsAccountData.nonce : new BN('0'); - return currentNonce + return paymentsAccountData ? paymentsAccountData.nonce : new BN('0') }; -const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ +const routeSol = async({ paymentRoute, nonce, deadline }) =>{ const paymentReceiverPublicKey = new PublicKey(paymentRoute.toAddress); const feeReceiverPublicKey = paymentRoute.fee ? new PublicKey(paymentRoute.fee.receiver) : paymentReceiverPublicKey; @@ -2702,10 +2695,10 @@ const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ const data = Buffer.alloc(routers$1.solana.api.routeSol.layout.span); routers$1.solana.api.routeSol.layout.encode({ anchorDiscriminator: routers$1.solana.api.routeSol.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -2715,7 +2708,7 @@ const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ }) }; -const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ +const routeToken = async({ paymentRoute, nonce, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }); const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }); @@ -2733,10 +2726,10 @@ const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ const data = Buffer.alloc(routers$1.solana.api.routeToken.layout.span); routers$1.solana.api.routeToken.layout.encode({ anchorDiscriminator: routers$1.solana.api.routeToken.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -2746,7 +2739,7 @@ const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ }) }; -const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ +const routeOrcaSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = wSolSenderAccountKeypair ? wSolSenderAccountKeypair.publicKey : await getPaymentSenderTokenAccountAddress({ paymentRoute }); const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }); @@ -2801,14 +2794,14 @@ const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccou const data = Buffer.alloc(routers$1.solana.api.routeOrcaSwap.layout.span); routers$1.solana.api.routeOrcaSwap.layout.encode({ anchorDiscriminator: routers$1.solana.api.routeOrcaSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -2818,7 +2811,7 @@ const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccou }) }; -const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ +const routeOrcaSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }); const escrowOutWsolPublicKey = wSolEscrowAccountKeypair.publicKey; @@ -2875,14 +2868,14 @@ const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscro const data = Buffer.alloc(routers$1.solana.api.routeOrcaSwapSolOut.layout.span); routers$1.solana.api.routeOrcaSwapSolOut.layout.encode({ anchorDiscriminator: routers$1.solana.api.routeOrcaSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -2892,7 +2885,7 @@ const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscro }) }; -const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ +const routeOrcaTwoHopSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const paymentReceiverTokenAccountPublicKey = new PublicKey(await getPaymentReceiverTokenAccountAddress({ paymentRoute })); const feeReceiverTokenAccountPublicKey = paymentRoute.fee ? new PublicKey(await getFeeReceiverTokenAccountAddress({ paymentRoute })) : paymentReceiverTokenAccountPublicKey; @@ -2966,7 +2959,7 @@ const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSende const data = Buffer.alloc(routers$1.solana.api.routeOrcaTwoHopSwap.layout.span); routers$1.solana.api.routeOrcaTwoHopSwap.layout.encode({ anchorDiscriminator: routers$1.solana.api.routeOrcaTwoHopSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -2975,7 +2968,7 @@ const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSende sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -2985,7 +2978,7 @@ const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSende }) }; -const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ +const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const middleTokenAccountPublicKey = new PublicKey(await getMiddleTokenAccountAddress({ paymentRoute })); const exchangeRouteTransaction = await paymentRoute.exchangeRoutes[0].getTransaction({ account: paymentRoute.fromAddress }); @@ -3060,7 +3053,7 @@ const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSo const data = Buffer.alloc(routers$1.solana.api.routeOrcaTwoHopSwapSolOut.layout.span); routers$1.solana.api.routeOrcaTwoHopSwapSolOut.layout.encode({ anchorDiscriminator: routers$1.solana.api.routeOrcaTwoHopSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -3069,7 +3062,7 @@ const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSo sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -3079,30 +3072,29 @@ const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSo }) }; -const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair })=> { +const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline })=> { - const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }); const paymentMethod = getPaymentMethod({ paymentRoute }); switch(paymentMethod){ case 'routeSol': - return await routeSol({ paymentRoute, paymentsAccountData }); + return await routeSol({ paymentRoute, nonce, deadline }); case 'routeToken': - return await routeToken({ paymentRoute, paymentsAccountData }); + return await routeToken({ paymentRoute, nonce, deadline }); case 'routeOrcaSwap': - return await routeOrcaSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaSwapSolOut': - return await routeOrcaSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); case 'routeOrcaTwoHopSwap': - return await routeOrcaTwoHopSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaTwoHopSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaTwoHopSwapSolOut': - return await routeOrcaTwoHopSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaTwoHopSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); } @@ -3110,6 +3102,10 @@ const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccoun const getTransaction$2 = async({ paymentRoute })=> { + const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }); + const deadline = getDeadline(); + const nonce = getNonce(paymentsAccountData); + const wSolSenderAccountKeypair = await getWSolSenderAccountKeypairIfNeeded({ paymentRoute }); const wSolEscrowAccountKeypair = await getWSolEscrowAccountKeypairIfNeeded({ paymentRoute }); @@ -3123,7 +3119,7 @@ const getTransaction$2 = async({ paymentRoute })=> { createEscrowOutSolAccount({ paymentRoute }), // needs to happen before createEscrowOutWSolAccount createEscrowOutWSolAccount({ paymentRoute, wSolEscrowAccountKeypair }), createEscrowOutTokenAccount({ paymentRoute }), - payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair }), + payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline }), closeWSolSenderAccount({ paymentRoute, wSolSenderAccountKeypair }), ]) ).filter(Boolean).flat(); @@ -3137,8 +3133,8 @@ const getTransaction$2 = async({ paymentRoute })=> { // debug(transaction, paymentRoute) - transaction.deadline = currentDeadline; - transaction.nonce = currentNonce.toString(); + transaction.deadline = deadline; + transaction.nonce = nonce.toString(); return transaction }; diff --git a/dist/umd/index.evm.js b/dist/umd/index.evm.js index e52bbe4..abc5c19 100644 --- a/dist/umd/index.evm.js +++ b/dist/umd/index.evm.js @@ -44601,9 +44601,6 @@ ...instructions }; - let currentDeadline; - let currentNonce; - const getWSolSenderAccountKeypairIfNeeded = async ({ paymentRoute })=> { if( @@ -45077,18 +45074,14 @@ }; const getDeadline = ()=>{ - if(currentDeadline) { return currentDeadline } - currentDeadline = Math.ceil(new Date().getTime()/1000)+1800; // 30 Minutes (lower causes wallet simulation issues) - return currentDeadline + return Math.ceil(new Date().getTime()/1000)+1800 // 30 Minutes (lower causes wallet simulation issues) }; const getNonce = (paymentsAccountData)=>{ - if(currentNonce) { return currentNonce } - currentNonce = paymentsAccountData ? paymentsAccountData.nonce : new BN('0'); - return currentNonce + return paymentsAccountData ? paymentsAccountData.nonce : new BN('0') }; - const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ + const routeSol = async({ paymentRoute, nonce, deadline }) =>{ const paymentReceiverPublicKey = new PublicKey(paymentRoute.toAddress); const feeReceiverPublicKey = paymentRoute.fee ? new PublicKey(paymentRoute.fee.receiver) : paymentReceiverPublicKey; @@ -45104,10 +45097,10 @@ const data = Buffer.alloc(routers$2.solana.api.routeSol.layout.span); routers$2.solana.api.routeSol.layout.encode({ anchorDiscriminator: routers$2.solana.api.routeSol.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -45117,7 +45110,7 @@ }) }; - const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ + const routeToken = async({ paymentRoute, nonce, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }); const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }); @@ -45135,10 +45128,10 @@ const data = Buffer.alloc(routers$2.solana.api.routeToken.layout.span); routers$2.solana.api.routeToken.layout.encode({ anchorDiscriminator: routers$2.solana.api.routeToken.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -45148,7 +45141,7 @@ }) }; - const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ + const routeOrcaSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = wSolSenderAccountKeypair ? wSolSenderAccountKeypair.publicKey : await getPaymentSenderTokenAccountAddress({ paymentRoute }); const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }); @@ -45203,14 +45196,14 @@ const data = Buffer.alloc(routers$2.solana.api.routeOrcaSwap.layout.span); routers$2.solana.api.routeOrcaSwap.layout.encode({ anchorDiscriminator: routers$2.solana.api.routeOrcaSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -45220,7 +45213,7 @@ }) }; - const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ + const routeOrcaSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }); const escrowOutWsolPublicKey = wSolEscrowAccountKeypair.publicKey; @@ -45277,14 +45270,14 @@ const data = Buffer.alloc(routers$2.solana.api.routeOrcaSwapSolOut.layout.span); routers$2.solana.api.routeOrcaSwapSolOut.layout.encode({ anchorDiscriminator: routers$2.solana.api.routeOrcaSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -45294,7 +45287,7 @@ }) }; - const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ + const routeOrcaTwoHopSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const paymentReceiverTokenAccountPublicKey = new PublicKey(await getPaymentReceiverTokenAccountAddress({ paymentRoute })); const feeReceiverTokenAccountPublicKey = paymentRoute.fee ? new PublicKey(await getFeeReceiverTokenAccountAddress({ paymentRoute })) : paymentReceiverTokenAccountPublicKey; @@ -45368,7 +45361,7 @@ const data = Buffer.alloc(routers$2.solana.api.routeOrcaTwoHopSwap.layout.span); routers$2.solana.api.routeOrcaTwoHopSwap.layout.encode({ anchorDiscriminator: routers$2.solana.api.routeOrcaTwoHopSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -45377,7 +45370,7 @@ sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -45387,7 +45380,7 @@ }) }; - const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ + const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const middleTokenAccountPublicKey = new PublicKey(await getMiddleTokenAccountAddress({ paymentRoute })); const exchangeRouteTransaction = await paymentRoute.exchangeRoutes[0].getTransaction({ account: paymentRoute.fromAddress }); @@ -45462,7 +45455,7 @@ const data = Buffer.alloc(routers$2.solana.api.routeOrcaTwoHopSwapSolOut.layout.span); routers$2.solana.api.routeOrcaTwoHopSwapSolOut.layout.encode({ anchorDiscriminator: routers$2.solana.api.routeOrcaTwoHopSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -45471,7 +45464,7 @@ sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data); return new TransactionInstruction({ @@ -45481,30 +45474,29 @@ }) }; - const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair })=> { + const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline })=> { - const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }); const paymentMethod = getPaymentMethod({ paymentRoute }); switch(paymentMethod){ case 'routeSol': - return await routeSol({ paymentRoute, paymentsAccountData }); + return await routeSol({ paymentRoute, nonce, deadline }); case 'routeToken': - return await routeToken({ paymentRoute, paymentsAccountData }); + return await routeToken({ paymentRoute, nonce, deadline }); case 'routeOrcaSwap': - return await routeOrcaSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaSwapSolOut': - return await routeOrcaSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); case 'routeOrcaTwoHopSwap': - return await routeOrcaTwoHopSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaTwoHopSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaTwoHopSwapSolOut': - return await routeOrcaTwoHopSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaTwoHopSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); } @@ -45512,6 +45504,10 @@ const getTransaction$3 = async({ paymentRoute })=> { + const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }); + const deadline = getDeadline(); + const nonce = getNonce(paymentsAccountData); + const wSolSenderAccountKeypair = await getWSolSenderAccountKeypairIfNeeded({ paymentRoute }); const wSolEscrowAccountKeypair = await getWSolEscrowAccountKeypairIfNeeded({ paymentRoute }); @@ -45525,7 +45521,7 @@ createEscrowOutSolAccount({ paymentRoute }), // needs to happen before createEscrowOutWSolAccount createEscrowOutWSolAccount({ paymentRoute, wSolEscrowAccountKeypair }), createEscrowOutTokenAccount({ paymentRoute }), - payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair }), + payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline }), closeWSolSenderAccount({ paymentRoute, wSolSenderAccountKeypair }), ]) ).filter(Boolean).flat(); @@ -45539,8 +45535,8 @@ // debug(transaction, paymentRoute) - transaction.deadline = currentDeadline; - transaction.nonce = currentNonce.toString(); + transaction.deadline = deadline; + transaction.nonce = nonce.toString(); return transaction }; diff --git a/dist/umd/index.js b/dist/umd/index.js index f577867..22a44e7 100644 --- a/dist/umd/index.js +++ b/dist/umd/index.js @@ -906,9 +906,6 @@ }) }; - let currentDeadline; - let currentNonce; - const getWSolSenderAccountKeypairIfNeeded = async ({ paymentRoute })=> { if( @@ -1382,18 +1379,14 @@ }; const getDeadline = ()=>{ - if(currentDeadline) { return currentDeadline } - currentDeadline = Math.ceil(new Date().getTime()/1000)+1800; // 30 Minutes (lower causes wallet simulation issues) - return currentDeadline + return Math.ceil(new Date().getTime()/1000)+1800 // 30 Minutes (lower causes wallet simulation issues) }; const getNonce = (paymentsAccountData)=>{ - if(currentNonce) { return currentNonce } - currentNonce = paymentsAccountData ? paymentsAccountData.nonce : new solanaWeb3_js.BN('0'); - return currentNonce + return paymentsAccountData ? paymentsAccountData.nonce : new solanaWeb3_js.BN('0') }; - const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ + const routeSol = async({ paymentRoute, nonce, deadline }) =>{ const paymentReceiverPublicKey = new solanaWeb3_js.PublicKey(paymentRoute.toAddress); const feeReceiverPublicKey = paymentRoute.fee ? new solanaWeb3_js.PublicKey(paymentRoute.fee.receiver) : paymentReceiverPublicKey; @@ -1409,10 +1402,10 @@ const data = solanaWeb3_js.Buffer.alloc(solanaRouters.solana.api.routeSol.layout.span); solanaRouters.solana.api.routeSol.layout.encode({ anchorDiscriminator: solanaRouters.solana.api.routeSol.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new solanaWeb3_js.BN(paymentRoute.toAmount.toString()), feeAmount: new solanaWeb3_js.BN((paymentRoute.feeAmount || '0').toString()), - deadline: new solanaWeb3_js.BN(getDeadline()), + deadline: new solanaWeb3_js.BN(deadline), }, data); return new solanaWeb3_js.TransactionInstruction({ @@ -1422,7 +1415,7 @@ }) }; - const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ + const routeToken = async({ paymentRoute, nonce, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }); const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }); @@ -1440,10 +1433,10 @@ const data = solanaWeb3_js.Buffer.alloc(solanaRouters.solana.api.routeToken.layout.span); solanaRouters.solana.api.routeToken.layout.encode({ anchorDiscriminator: solanaRouters.solana.api.routeToken.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new solanaWeb3_js.BN(paymentRoute.toAmount.toString()), feeAmount: new solanaWeb3_js.BN((paymentRoute.feeAmount || '0').toString()), - deadline: new solanaWeb3_js.BN(getDeadline()), + deadline: new solanaWeb3_js.BN(deadline), }, data); return new solanaWeb3_js.TransactionInstruction({ @@ -1453,7 +1446,7 @@ }) }; - const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ + const routeOrcaSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = wSolSenderAccountKeypair ? wSolSenderAccountKeypair.publicKey : await getPaymentSenderTokenAccountAddress({ paymentRoute }); const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }); @@ -1508,14 +1501,14 @@ const data = solanaWeb3_js.Buffer.alloc(solanaRouters.solana.api.routeOrcaSwap.layout.span); solanaRouters.solana.api.routeOrcaSwap.layout.encode({ anchorDiscriminator: solanaRouters.solana.api.routeOrcaSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new solanaWeb3_js.BN(paymentRoute.toAmount.toString()), feeAmount: new solanaWeb3_js.BN((paymentRoute.feeAmount || '0').toString()), - deadline: new solanaWeb3_js.BN(getDeadline()), + deadline: new solanaWeb3_js.BN(deadline), }, data); return new solanaWeb3_js.TransactionInstruction({ @@ -1525,7 +1518,7 @@ }) }; - const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ + const routeOrcaSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }); const escrowOutWsolPublicKey = wSolEscrowAccountKeypair.publicKey; @@ -1582,14 +1575,14 @@ const data = solanaWeb3_js.Buffer.alloc(solanaRouters.solana.api.routeOrcaSwapSolOut.layout.span); solanaRouters.solana.api.routeOrcaSwapSolOut.layout.encode({ anchorDiscriminator: solanaRouters.solana.api.routeOrcaSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new solanaWeb3_js.BN(paymentRoute.toAmount.toString()), feeAmount: new solanaWeb3_js.BN((paymentRoute.feeAmount || '0').toString()), - deadline: new solanaWeb3_js.BN(getDeadline()), + deadline: new solanaWeb3_js.BN(deadline), }, data); return new solanaWeb3_js.TransactionInstruction({ @@ -1599,7 +1592,7 @@ }) }; - const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ + const routeOrcaTwoHopSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const paymentReceiverTokenAccountPublicKey = new solanaWeb3_js.PublicKey(await getPaymentReceiverTokenAccountAddress({ paymentRoute })); const feeReceiverTokenAccountPublicKey = paymentRoute.fee ? new solanaWeb3_js.PublicKey(await getFeeReceiverTokenAccountAddress({ paymentRoute })) : paymentReceiverTokenAccountPublicKey; @@ -1673,7 +1666,7 @@ const data = solanaWeb3_js.Buffer.alloc(solanaRouters.solana.api.routeOrcaTwoHopSwap.layout.span); solanaRouters.solana.api.routeOrcaTwoHopSwap.layout.encode({ anchorDiscriminator: solanaRouters.solana.api.routeOrcaTwoHopSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -1682,7 +1675,7 @@ sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new solanaWeb3_js.BN(paymentRoute.toAmount.toString()), feeAmount: new solanaWeb3_js.BN((paymentRoute.feeAmount || '0').toString()), - deadline: new solanaWeb3_js.BN(getDeadline()), + deadline: new solanaWeb3_js.BN(deadline), }, data); return new solanaWeb3_js.TransactionInstruction({ @@ -1692,7 +1685,7 @@ }) }; - const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ + const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const middleTokenAccountPublicKey = new solanaWeb3_js.PublicKey(await getMiddleTokenAccountAddress({ paymentRoute })); const exchangeRouteTransaction = await paymentRoute.exchangeRoutes[0].getTransaction({ account: paymentRoute.fromAddress }); @@ -1767,7 +1760,7 @@ const data = solanaWeb3_js.Buffer.alloc(solanaRouters.solana.api.routeOrcaTwoHopSwapSolOut.layout.span); solanaRouters.solana.api.routeOrcaTwoHopSwapSolOut.layout.encode({ anchorDiscriminator: solanaRouters.solana.api.routeOrcaTwoHopSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -1776,7 +1769,7 @@ sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new solanaWeb3_js.BN(paymentRoute.toAmount.toString()), feeAmount: new solanaWeb3_js.BN((paymentRoute.feeAmount || '0').toString()), - deadline: new solanaWeb3_js.BN(getDeadline()), + deadline: new solanaWeb3_js.BN(deadline), }, data); return new solanaWeb3_js.TransactionInstruction({ @@ -1786,30 +1779,29 @@ }) }; - const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair })=> { + const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline })=> { - const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }); const paymentMethod = getPaymentMethod({ paymentRoute }); switch(paymentMethod){ case 'routeSol': - return await routeSol({ paymentRoute, paymentsAccountData }); + return await routeSol({ paymentRoute, nonce, deadline }); case 'routeToken': - return await routeToken({ paymentRoute, paymentsAccountData }); + return await routeToken({ paymentRoute, nonce, deadline }); case 'routeOrcaSwap': - return await routeOrcaSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaSwapSolOut': - return await routeOrcaSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); case 'routeOrcaTwoHopSwap': - return await routeOrcaTwoHopSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaTwoHopSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaTwoHopSwapSolOut': - return await routeOrcaTwoHopSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaTwoHopSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); } @@ -1817,6 +1809,10 @@ const getTransaction$3 = async({ paymentRoute })=> { + const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }); + const deadline = getDeadline(); + const nonce = getNonce(paymentsAccountData); + const wSolSenderAccountKeypair = await getWSolSenderAccountKeypairIfNeeded({ paymentRoute }); const wSolEscrowAccountKeypair = await getWSolEscrowAccountKeypairIfNeeded({ paymentRoute }); @@ -1830,7 +1826,7 @@ createEscrowOutSolAccount({ paymentRoute }), // needs to happen before createEscrowOutWSolAccount createEscrowOutWSolAccount({ paymentRoute, wSolEscrowAccountKeypair }), createEscrowOutTokenAccount({ paymentRoute }), - payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair }), + payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline }), closeWSolSenderAccount({ paymentRoute, wSolSenderAccountKeypair }), ]) ).filter(Boolean).flat(); @@ -1844,8 +1840,8 @@ // debug(transaction, paymentRoute) - transaction.deadline = currentDeadline; - transaction.nonce = currentNonce.toString(); + transaction.deadline = deadline; + transaction.nonce = nonce.toString(); return transaction }; diff --git a/dist/umd/index.solana.js b/dist/umd/index.solana.js index cee01bb..b49965e 100644 --- a/dist/umd/index.solana.js +++ b/dist/umd/index.solana.js @@ -2204,9 +2204,6 @@ ...instructions }; - let currentDeadline; - let currentNonce; - const getWSolSenderAccountKeypairIfNeeded = async ({ paymentRoute })=> { if( @@ -2680,18 +2677,14 @@ }; const getDeadline = ()=>{ - if(currentDeadline) { return currentDeadline } - currentDeadline = Math.ceil(new Date().getTime()/1000)+1800; // 30 Minutes (lower causes wallet simulation issues) - return currentDeadline + return Math.ceil(new Date().getTime()/1000)+1800 // 30 Minutes (lower causes wallet simulation issues) }; const getNonce = (paymentsAccountData)=>{ - if(currentNonce) { return currentNonce } - currentNonce = paymentsAccountData ? paymentsAccountData.nonce : new solanaWeb3_js.BN('0'); - return currentNonce + return paymentsAccountData ? paymentsAccountData.nonce : new solanaWeb3_js.BN('0') }; - const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ + const routeSol = async({ paymentRoute, nonce, deadline }) =>{ const paymentReceiverPublicKey = new solanaWeb3_js.PublicKey(paymentRoute.toAddress); const feeReceiverPublicKey = paymentRoute.fee ? new solanaWeb3_js.PublicKey(paymentRoute.fee.receiver) : paymentReceiverPublicKey; @@ -2707,10 +2700,10 @@ const data = solanaWeb3_js.Buffer.alloc(routers$1.solana.api.routeSol.layout.span); routers$1.solana.api.routeSol.layout.encode({ anchorDiscriminator: routers$1.solana.api.routeSol.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new solanaWeb3_js.BN(paymentRoute.toAmount.toString()), feeAmount: new solanaWeb3_js.BN((paymentRoute.feeAmount || '0').toString()), - deadline: new solanaWeb3_js.BN(getDeadline()), + deadline: new solanaWeb3_js.BN(deadline), }, data); return new solanaWeb3_js.TransactionInstruction({ @@ -2720,7 +2713,7 @@ }) }; - const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ + const routeToken = async({ paymentRoute, nonce, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }); const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }); @@ -2738,10 +2731,10 @@ const data = solanaWeb3_js.Buffer.alloc(routers$1.solana.api.routeToken.layout.span); routers$1.solana.api.routeToken.layout.encode({ anchorDiscriminator: routers$1.solana.api.routeToken.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new solanaWeb3_js.BN(paymentRoute.toAmount.toString()), feeAmount: new solanaWeb3_js.BN((paymentRoute.feeAmount || '0').toString()), - deadline: new solanaWeb3_js.BN(getDeadline()), + deadline: new solanaWeb3_js.BN(deadline), }, data); return new solanaWeb3_js.TransactionInstruction({ @@ -2751,7 +2744,7 @@ }) }; - const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ + const routeOrcaSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = wSolSenderAccountKeypair ? wSolSenderAccountKeypair.publicKey : await getPaymentSenderTokenAccountAddress({ paymentRoute }); const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }); @@ -2806,14 +2799,14 @@ const data = solanaWeb3_js.Buffer.alloc(routers$1.solana.api.routeOrcaSwap.layout.span); routers$1.solana.api.routeOrcaSwap.layout.encode({ anchorDiscriminator: routers$1.solana.api.routeOrcaSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new solanaWeb3_js.BN(paymentRoute.toAmount.toString()), feeAmount: new solanaWeb3_js.BN((paymentRoute.feeAmount || '0').toString()), - deadline: new solanaWeb3_js.BN(getDeadline()), + deadline: new solanaWeb3_js.BN(deadline), }, data); return new solanaWeb3_js.TransactionInstruction({ @@ -2823,7 +2816,7 @@ }) }; - const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ + const routeOrcaSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }); const escrowOutWsolPublicKey = wSolEscrowAccountKeypair.publicKey; @@ -2880,14 +2873,14 @@ const data = solanaWeb3_js.Buffer.alloc(routers$1.solana.api.routeOrcaSwapSolOut.layout.span); routers$1.solana.api.routeOrcaSwapSolOut.layout.encode({ anchorDiscriminator: routers$1.solana.api.routeOrcaSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new solanaWeb3_js.BN(paymentRoute.toAmount.toString()), feeAmount: new solanaWeb3_js.BN((paymentRoute.feeAmount || '0').toString()), - deadline: new solanaWeb3_js.BN(getDeadline()), + deadline: new solanaWeb3_js.BN(deadline), }, data); return new solanaWeb3_js.TransactionInstruction({ @@ -2897,7 +2890,7 @@ }) }; - const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ + const routeOrcaTwoHopSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const paymentReceiverTokenAccountPublicKey = new solanaWeb3_js.PublicKey(await getPaymentReceiverTokenAccountAddress({ paymentRoute })); const feeReceiverTokenAccountPublicKey = paymentRoute.fee ? new solanaWeb3_js.PublicKey(await getFeeReceiverTokenAccountAddress({ paymentRoute })) : paymentReceiverTokenAccountPublicKey; @@ -2971,7 +2964,7 @@ const data = solanaWeb3_js.Buffer.alloc(routers$1.solana.api.routeOrcaTwoHopSwap.layout.span); routers$1.solana.api.routeOrcaTwoHopSwap.layout.encode({ anchorDiscriminator: routers$1.solana.api.routeOrcaTwoHopSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -2980,7 +2973,7 @@ sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new solanaWeb3_js.BN(paymentRoute.toAmount.toString()), feeAmount: new solanaWeb3_js.BN((paymentRoute.feeAmount || '0').toString()), - deadline: new solanaWeb3_js.BN(getDeadline()), + deadline: new solanaWeb3_js.BN(deadline), }, data); return new solanaWeb3_js.TransactionInstruction({ @@ -2990,7 +2983,7 @@ }) }; - const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ + const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const middleTokenAccountPublicKey = new solanaWeb3_js.PublicKey(await getMiddleTokenAccountAddress({ paymentRoute })); const exchangeRouteTransaction = await paymentRoute.exchangeRoutes[0].getTransaction({ account: paymentRoute.fromAddress }); @@ -3065,7 +3058,7 @@ const data = solanaWeb3_js.Buffer.alloc(routers$1.solana.api.routeOrcaTwoHopSwapSolOut.layout.span); routers$1.solana.api.routeOrcaTwoHopSwapSolOut.layout.encode({ anchorDiscriminator: routers$1.solana.api.routeOrcaTwoHopSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -3074,7 +3067,7 @@ sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new solanaWeb3_js.BN(paymentRoute.toAmount.toString()), feeAmount: new solanaWeb3_js.BN((paymentRoute.feeAmount || '0').toString()), - deadline: new solanaWeb3_js.BN(getDeadline()), + deadline: new solanaWeb3_js.BN(deadline), }, data); return new solanaWeb3_js.TransactionInstruction({ @@ -3084,30 +3077,29 @@ }) }; - const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair })=> { + const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline })=> { - const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }); const paymentMethod = getPaymentMethod({ paymentRoute }); switch(paymentMethod){ case 'routeSol': - return await routeSol({ paymentRoute, paymentsAccountData }); + return await routeSol({ paymentRoute, nonce, deadline }); case 'routeToken': - return await routeToken({ paymentRoute, paymentsAccountData }); + return await routeToken({ paymentRoute, nonce, deadline }); case 'routeOrcaSwap': - return await routeOrcaSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaSwapSolOut': - return await routeOrcaSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); case 'routeOrcaTwoHopSwap': - return await routeOrcaTwoHopSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaTwoHopSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaTwoHopSwapSolOut': - return await routeOrcaTwoHopSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaTwoHopSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); } @@ -3115,6 +3107,10 @@ const getTransaction$2 = async({ paymentRoute })=> { + const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }); + const deadline = getDeadline(); + const nonce = getNonce(paymentsAccountData); + const wSolSenderAccountKeypair = await getWSolSenderAccountKeypairIfNeeded({ paymentRoute }); const wSolEscrowAccountKeypair = await getWSolEscrowAccountKeypairIfNeeded({ paymentRoute }); @@ -3128,7 +3124,7 @@ createEscrowOutSolAccount({ paymentRoute }), // needs to happen before createEscrowOutWSolAccount createEscrowOutWSolAccount({ paymentRoute, wSolEscrowAccountKeypair }), createEscrowOutTokenAccount({ paymentRoute }), - payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair }), + payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline }), closeWSolSenderAccount({ paymentRoute, wSolSenderAccountKeypair }), ]) ).filter(Boolean).flat(); @@ -3142,8 +3138,8 @@ // debug(transaction, paymentRoute) - transaction.deadline = currentDeadline; - transaction.nonce = currentNonce.toString(); + transaction.deadline = deadline; + transaction.nonce = nonce.toString(); return transaction }; diff --git a/package.evm.json b/package.evm.json index 560e36b..a680f80 100644 --- a/package.evm.json +++ b/package.evm.json @@ -1,7 +1,7 @@ { "name": "@depay/web3-payments-evm", "moduleName": "Web3Payments", - "version": "13.6.9", + "version": "13.6.10", "description": "JavaScript library to scan crypto wallets for liquefiable assets and perform cost-effective, auto-converted payments on-chain.", "main": "dist/umd/index.evm.js", "module": "dist/esm/index.evm.js", diff --git a/package.json b/package.json index c580afd..2426179 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@depay/web3-payments", "moduleName": "Web3Payments", - "version": "13.6.9", + "version": "13.6.10", "description": "JavaScript library to scan crypto wallets for liquefiable assets and perform cost-effective, auto-converted payments on-chain.", "main": "dist/umd/index.js", "module": "dist/esm/index.js", diff --git a/package.solana.json b/package.solana.json index 7c459c7..7093077 100644 --- a/package.solana.json +++ b/package.solana.json @@ -1,7 +1,7 @@ { "name": "@depay/web3-payments-solana", "moduleName": "Web3Payments", - "version": "13.6.9", + "version": "13.6.10", "description": "JavaScript library to scan crypto wallets for liquefiable assets and perform cost-effective, auto-converted payments on-chain.", "main": "dist/umd/index.solana.js", "module": "dist/esm/index.solana.js", diff --git a/src/platforms/solana/transaction.js b/src/platforms/solana/transaction.js index e3e204b..ad4164b 100644 --- a/src/platforms/solana/transaction.js +++ b/src/platforms/solana/transaction.js @@ -4,9 +4,6 @@ import { BN, PublicKey, Buffer, TransactionMessage, VersionedTransaction, Transa import { request, getProvider } from '@depay/web3-client' import Token from '@depay/web3-tokens' -let currentDeadline -let currentNonce - const getWSolSenderAccountKeypairIfNeeded = async ({ paymentRoute })=> { if( @@ -480,18 +477,14 @@ const getPaymentMethod = ({ paymentRoute })=>{ } const getDeadline = ()=>{ - if(currentDeadline) { return currentDeadline } - currentDeadline = Math.ceil(new Date().getTime()/1000)+1800 // 30 Minutes (lower causes wallet simulation issues) - return currentDeadline + return Math.ceil(new Date().getTime()/1000)+1800 // 30 Minutes (lower causes wallet simulation issues) } const getNonce = (paymentsAccountData)=>{ - if(currentNonce) { return currentNonce } - currentNonce = paymentsAccountData ? paymentsAccountData.nonce : new BN('0') - return currentNonce + return paymentsAccountData ? paymentsAccountData.nonce : new BN('0') } -const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ +const routeSol = async({ paymentRoute, nonce, deadline }) =>{ const paymentReceiverPublicKey = new PublicKey(paymentRoute.toAddress) const feeReceiverPublicKey = paymentRoute.fee ? new PublicKey(paymentRoute.fee.receiver) : paymentReceiverPublicKey @@ -507,10 +500,10 @@ const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ const data = Buffer.alloc(routers.solana.api.routeSol.layout.span) routers.solana.api.routeSol.layout.encode({ anchorDiscriminator: routers.solana.api.routeSol.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data) return new TransactionInstruction({ @@ -520,7 +513,7 @@ const routeSol = async({ paymentRoute, paymentsAccountData }) =>{ }) } -const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ +const routeToken = async({ paymentRoute, nonce, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }) const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }) @@ -538,10 +531,10 @@ const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ const data = Buffer.alloc(routers.solana.api.routeToken.layout.span) routers.solana.api.routeToken.layout.encode({ anchorDiscriminator: routers.solana.api.routeToken.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data) return new TransactionInstruction({ @@ -551,7 +544,7 @@ const routeToken = async({ paymentRoute, paymentsAccountData }) =>{ }) } -const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ +const routeOrcaSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = wSolSenderAccountKeypair ? wSolSenderAccountKeypair.publicKey : await getPaymentSenderTokenAccountAddress({ paymentRoute }) const paymentReceiverTokenAccountAddress = await getPaymentReceiverTokenAccountAddress({ paymentRoute }) @@ -606,14 +599,14 @@ const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccou const data = Buffer.alloc(routers.solana.api.routeOrcaSwap.layout.span) routers.solana.api.routeOrcaSwap.layout.encode({ anchorDiscriminator: routers.solana.api.routeOrcaSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data) return new TransactionInstruction({ @@ -623,7 +616,7 @@ const routeOrcaSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccou }) } -const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ +const routeOrcaSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const senderTokenAccountAddress = await getPaymentSenderTokenAccountAddress({ paymentRoute }) const escrowOutWsolPublicKey = wSolEscrowAccountKeypair.publicKey @@ -680,14 +673,14 @@ const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscro const data = Buffer.alloc(routers.solana.api.routeOrcaSwapSolOut.layout.span) routers.solana.api.routeOrcaSwapSolOut.layout.encode({ anchorDiscriminator: routers.solana.api.routeOrcaSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, sqrtPriceLimit: exchangeRouteSwapInstructionData.sqrtPriceLimit, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToB: exchangeRouteSwapInstructionData.aToB, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data) return new TransactionInstruction({ @@ -697,7 +690,7 @@ const routeOrcaSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscro }) } -const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }) =>{ +const routeOrcaTwoHopSwap = async({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }) =>{ const paymentReceiverTokenAccountPublicKey = new PublicKey(await getPaymentReceiverTokenAccountAddress({ paymentRoute })) const feeReceiverTokenAccountPublicKey = paymentRoute.fee ? new PublicKey(await getFeeReceiverTokenAccountAddress({ paymentRoute })) : paymentReceiverTokenAccountPublicKey @@ -771,7 +764,7 @@ const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSende const data = Buffer.alloc(routers.solana.api.routeOrcaTwoHopSwap.layout.span) routers.solana.api.routeOrcaTwoHopSwap.layout.encode({ anchorDiscriminator: routers.solana.api.routeOrcaTwoHopSwap.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -780,7 +773,7 @@ const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSende sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data) return new TransactionInstruction({ @@ -790,7 +783,7 @@ const routeOrcaTwoHopSwap = async({ paymentRoute, paymentsAccountData, wSolSende }) } -const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }) =>{ +const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }) =>{ const middleTokenAccountPublicKey = new PublicKey(await getMiddleTokenAccountAddress({ paymentRoute })) const exchangeRouteTransaction = await paymentRoute.exchangeRoutes[0].getTransaction({ account: paymentRoute.fromAddress }) @@ -865,7 +858,7 @@ const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSo const data = Buffer.alloc(routers.solana.api.routeOrcaTwoHopSwapSolOut.layout.span) routers.solana.api.routeOrcaTwoHopSwapSolOut.layout.encode({ anchorDiscriminator: routers.solana.api.routeOrcaTwoHopSwapSolOut.anchorDiscriminator, - nonce: getNonce(paymentsAccountData), + nonce, amountIn: exchangeRouteSwapInstructionData.amount, amountSpecifiedIsInput: exchangeRouteSwapInstructionData.amountSpecifiedIsInput, aToBOne: exchangeRouteSwapInstructionData.aToBOne, @@ -874,7 +867,7 @@ const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSo sqrtPriceLimitTwo: exchangeRouteSwapInstructionData.sqrtPriceLimitTwo, paymentAmount: new BN(paymentRoute.toAmount.toString()), feeAmount: new BN((paymentRoute.feeAmount || '0').toString()), - deadline: new BN(getDeadline()), + deadline: new BN(deadline), }, data) return new TransactionInstruction({ @@ -884,30 +877,29 @@ const routeOrcaTwoHopSwapSolOut = async({ paymentRoute, paymentsAccountData, wSo }) } -const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair })=> { +const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline })=> { - const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }) const paymentMethod = getPaymentMethod({ paymentRoute }) switch(paymentMethod){ case 'routeSol': - return await routeSol({ paymentRoute, paymentsAccountData }); + return await routeSol({ paymentRoute, nonce, deadline }); case 'routeToken': - return await routeToken({ paymentRoute, paymentsAccountData }); + return await routeToken({ paymentRoute, nonce, deadline }); case 'routeOrcaSwap': - return await routeOrcaSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaSwapSolOut': - return await routeOrcaSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); case 'routeOrcaTwoHopSwap': - return await routeOrcaTwoHopSwap({ paymentRoute, paymentsAccountData, wSolSenderAccountKeypair }); + return await routeOrcaTwoHopSwap({ paymentRoute, nonce, wSolSenderAccountKeypair, deadline }); case 'routeOrcaTwoHopSwapSolOut': - return await routeOrcaTwoHopSwapSolOut({ paymentRoute, paymentsAccountData, wSolEscrowAccountKeypair }); + return await routeOrcaTwoHopSwapSolOut({ paymentRoute, nonce, wSolEscrowAccountKeypair, deadline }); } @@ -915,6 +907,10 @@ const payment = async({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccoun const getTransaction = async({ paymentRoute })=> { + const paymentsAccountData = await getPaymentsAccountData({ from: paymentRoute.fromAddress }) + const deadline = getDeadline() + const nonce = getNonce(paymentsAccountData) + const wSolSenderAccountKeypair = await getWSolSenderAccountKeypairIfNeeded({ paymentRoute }) const wSolEscrowAccountKeypair = await getWSolEscrowAccountKeypairIfNeeded({ paymentRoute }) @@ -928,7 +924,7 @@ const getTransaction = async({ paymentRoute })=> { createEscrowOutSolAccount({ paymentRoute }), // needs to happen before createEscrowOutWSolAccount createEscrowOutWSolAccount({ paymentRoute, wSolEscrowAccountKeypair }), createEscrowOutTokenAccount({ paymentRoute }), - payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair }), + payment({ paymentRoute, wSolSenderAccountKeypair, wSolEscrowAccountKeypair, nonce, deadline }), closeWSolSenderAccount({ paymentRoute, wSolSenderAccountKeypair }), ]) ).filter(Boolean).flat() @@ -942,8 +938,8 @@ const getTransaction = async({ paymentRoute })=> { // debug(transaction, paymentRoute) - transaction.deadline = currentDeadline - transaction.nonce = currentNonce.toString() + transaction.deadline = deadline + transaction.nonce = nonce.toString() return transaction }