Skip to content

Commit

Permalink
v13.6.10: fix solana nonces
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNe0x1 committed Jan 4, 2024
1 parent 0acce83 commit 3a6a7ae
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 269 deletions.
72 changes: 34 additions & 38 deletions dist/esm/index.evm.js
Original file line number Diff line number Diff line change
Expand Up @@ -44595,9 +44595,6 @@ Token.solana = {
...instructions
};

let currentDeadline;
let currentNonce;

const getWSolSenderAccountKeypairIfNeeded = async ({ paymentRoute })=> {

if(
Expand Down Expand Up @@ -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;
Expand All @@ -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({
Expand All @@ -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 });
Expand All @@ -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({
Expand All @@ -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 });
Expand Down Expand Up @@ -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({
Expand All @@ -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;
Expand Down Expand Up @@ -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({
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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({
Expand All @@ -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 });
Expand Down Expand Up @@ -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,
Expand All @@ -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({
Expand All @@ -45475,37 +45468,40 @@ 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 });

}

};

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 });

Expand All @@ -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();
Expand All @@ -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
};
Expand Down
Loading

0 comments on commit 3a6a7ae

Please sign in to comment.