|
1 | 1 | import { |
2 | 2 | ABIType, |
3 | | - decodeAddress, |
4 | 3 | encodeAddress, |
5 | 4 | getApplicationAddress, |
6 | 5 | OnApplicationComplete, |
@@ -33,40 +32,53 @@ export function checkSwapTransactions( |
33 | 32 | const swapEndTxn = unsignedTxns[unsignedTxns.length - 1]!; |
34 | 33 |
|
35 | 34 | // send algo/asset |
36 | | - if (sendAssetTxn.reKeyTo !== undefined) throw Error("Unexpected rekey"); |
37 | | - if (sendAssetTxn.closeRemainderTo !== undefined) throw Error("Unexpected close remainder to"); |
38 | | - if (encodeAddress(sendAssetTxn.from.publicKey) !== userAddress) throw Error("Incorrect sender"); |
39 | | - if (encodeAddress(sendAssetTxn.to.publicKey) !== folksRouterAddr) throw Error("Incorrect receiver"); |
| 35 | + if (sendAssetTxn.rekeyTo !== undefined) throw Error("Unexpected rekey"); |
| 36 | + if (sendAssetTxn.payment?.closeRemainderTo !== undefined) throw Error("Unexpected close remainder to"); |
| 37 | + if (encodeAddress(sendAssetTxn.sender.publicKey) !== userAddress) throw Error("Incorrect sender"); |
| 38 | + if (sendAssetTxn.payment && encodeAddress(sendAssetTxn.payment.receiver.publicKey) !== folksRouterAddr.toString()) |
| 39 | + throw Error("Incorrect receiver"); |
| 40 | + if ( |
| 41 | + sendAssetTxn.assetTransfer && |
| 42 | + encodeAddress(sendAssetTxn.assetTransfer.receiver.publicKey) !== folksRouterAddr.toString() |
| 43 | + ) |
| 44 | + throw Error("Incorrect receiver"); |
40 | 45 | if ( |
41 | 46 | !(fromAssetId === 0 && sendAssetTxn.type == TransactionType.pay) && |
42 | | - !(fromAssetId === sendAssetTxn.assetIndex && sendAssetTxn.type === TransactionType.axfer) |
| 47 | + !( |
| 48 | + sendAssetTxn.type === TransactionType.axfer && |
| 49 | + sendAssetTxn.assetTransfer && |
| 50 | + BigInt(fromAssetId) === sendAssetTxn.assetTransfer.assetIndex |
| 51 | + ) |
43 | 52 | ) |
44 | 53 | throw Error("Sending incorrect algo/asset"); |
45 | | - const sendAmount = BigInt(sendAssetTxn.amount); |
| 54 | + const sendAmount = sendAssetTxn.payment ? sendAssetTxn.payment.amount : sendAssetTxn.assetTransfer!.amount; |
46 | 55 |
|
47 | 56 | // swap forward txns |
48 | 57 | swapForwardTxns.forEach((txn, i) => { |
49 | | - if (encodeAddress(txn.from.publicKey) !== userAddress) throw Error("Incorrect sender"); |
50 | | - if (txn.appIndex !== folksRouterAppId) throw Error("Incorrect application index"); |
51 | | - if (txn.type !== TransactionType.appl && txn.appOnComplete !== OnApplicationComplete.NoOpOC) |
| 58 | + if (encodeAddress(txn.sender.publicKey) !== userAddress) throw Error("Incorrect sender"); |
| 59 | + if (txn.applicationCall?.appIndex !== BigInt(folksRouterAppId)) throw Error("Incorrect application index"); |
| 60 | + if (txn.type !== TransactionType.appl && txn.applicationCall.onComplete !== OnApplicationComplete.NoOpOC) |
52 | 61 | throw Error("Incorrect transaction type"); |
53 | | - const swapForwardSelector = uint8ArrayToHex(txn.appArgs!.at(0)!); |
| 62 | + const swapForwardSelector = uint8ArrayToHex(txn.applicationCall.appArgs.at(0)!); |
54 | 63 | if (swapForwardSelector !== getHexSelector("swap_forward")) throw Error("Incorrect selector"); |
55 | 64 | }); |
56 | 65 |
|
57 | 66 | // receive algo/asset |
58 | | - if (encodeAddress(swapEndTxn.from.publicKey) !== userAddress) throw Error("Incorrect sender"); |
59 | | - if (swapEndTxn.appIndex !== folksRouterAppId) throw Error("Incorrect application index"); |
60 | | - if (swapEndTxn.type !== TransactionType.appl && swapEndTxn.appOnComplete !== OnApplicationComplete.NoOpOC) |
| 67 | + if (encodeAddress(swapEndTxn.sender.publicKey) !== userAddress) throw Error("Incorrect sender"); |
| 68 | + if (swapEndTxn.applicationCall?.appIndex !== BigInt(folksRouterAppId)) throw Error("Incorrect application index"); |
| 69 | + if ( |
| 70 | + swapEndTxn.type !== TransactionType.appl && |
| 71 | + swapEndTxn.applicationCall.onComplete !== OnApplicationComplete.NoOpOC |
| 72 | + ) |
61 | 73 | throw Error("Incorrect transaction type"); |
62 | | - const swapEndSelector = uint8ArrayToHex(swapEndTxn.appArgs!.at(0)!); |
| 74 | + const swapEndSelector = uint8ArrayToHex(swapEndTxn.applicationCall.appArgs.at(0)!); |
63 | 75 | const isFixedInput = swapEndSelector === getHexSelector("fi_end_swap"); |
64 | 76 | const isFixedOutput = swapEndSelector === getHexSelector("fo_end_swap"); |
65 | 77 | if ((isFixedInput && swapMode !== SwapMode.FIXED_INPUT) || (isFixedOutput && swapMode !== SwapMode.FIXED_OUTPUT)) |
66 | 78 | throw Error("Incorrect swap mode"); |
67 | | - if (ABIType.from("uint64").decode(swapEndTxn.appArgs!.at(1)!) !== BigInt(toAssetId)) |
| 79 | + if (ABIType.from("uint64").decode(swapEndTxn.applicationCall.appArgs.at(1)!) !== BigInt(toAssetId)) |
68 | 80 | throw Error("Receiving incorrect algo/asset"); |
69 | | - const receiveAmount = ABIType.from("uint64").decode(swapEndTxn.appArgs!.at(2)!) as bigint; |
| 81 | + const receiveAmount = ABIType.from("uint64").decode(swapEndTxn.applicationCall.appArgs.at(2)!) as bigint; |
70 | 82 |
|
71 | 83 | // check amounts |
72 | 84 | const slippageAmount = mulScale(swapQuote.quoteAmount, BigInt(slippageBps), ONE_4_DP); |
|
0 commit comments