|
1 | 1 | import { assert, assertType, describe, test } from "vitest"; |
2 | 2 | import { type SwapApprovalApiResponse } from "../../../src/api/swap-approval.js"; |
3 | 3 | import { getSwapQuote } from "../../../src/actions/getSwapQuote.js"; |
4 | | -import { parseEther } from "viem"; |
| 4 | +import { Hex, parseEther } from "viem"; |
| 5 | +import { hasIntegratorIdAppended } from "../../../src/utils/hex.js"; |
| 6 | +import { mainnetTestClient } from "../../common/sdk.js"; |
5 | 7 |
|
6 | 8 | // Mainnet WETH |
7 | 9 | const inputToken = { |
@@ -68,4 +70,68 @@ describe("getSwapQuote", () => { |
68 | 70 | assert(quote, "No swap quote returned for the provided parameters"); |
69 | 71 | assertType<SwapApprovalApiResponse>(quote); |
70 | 72 | }); |
| 73 | + |
| 74 | + test("swap approval calldata has integrator id appended", async () => { |
| 75 | + const integratorId: Hex = "0xdead"; |
| 76 | + |
| 77 | + const quote = await getSwapQuote({ |
| 78 | + amount: parseEther(inputAmount), |
| 79 | + route: { |
| 80 | + originChainId: 1, |
| 81 | + inputToken: inputToken.address, |
| 82 | + destinationChainId: 10, |
| 83 | + outputToken: outputToken.address, |
| 84 | + }, |
| 85 | + depositor: testRecipient, |
| 86 | + recipient: testRecipient, |
| 87 | + integratorId, |
| 88 | + }); |
| 89 | + |
| 90 | + assert(quote.swapTx, "swapTx missing in swap approval response"); |
| 91 | + let data: Hex; |
| 92 | + if ("eip712" in quote.swapTx) { |
| 93 | + data = quote.swapTx.swapTx.data as Hex; |
| 94 | + } else { |
| 95 | + const simple = quote.swapTx as { data: string }; |
| 96 | + data = simple.data as Hex; |
| 97 | + } |
| 98 | + |
| 99 | + assert( |
| 100 | + hasIntegratorIdAppended(data, integratorId, { |
| 101 | + isSwap: true, |
| 102 | + }), |
| 103 | + "Expected swap calldata to have integrator id suffix", |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + test("client injects integratorId when omitted", async () => { |
| 108 | + const quote = await mainnetTestClient.getSwapQuote({ |
| 109 | + amount: parseEther(inputAmount), |
| 110 | + route: { |
| 111 | + originChainId: 1, |
| 112 | + inputToken: inputToken.address, |
| 113 | + destinationChainId: 10, |
| 114 | + outputToken: outputToken.address, |
| 115 | + }, |
| 116 | + depositor: testRecipient, |
| 117 | + recipient: testRecipient, |
| 118 | + // no integrator ID |
| 119 | + }); |
| 120 | + |
| 121 | + assert(quote.swapTx, "swapTx missing in swap approval response"); |
| 122 | + let data: Hex; |
| 123 | + if ("eip712" in quote.swapTx) { |
| 124 | + data = quote.swapTx.swapTx.data as Hex; |
| 125 | + } else { |
| 126 | + const simple = quote.swapTx as { data: string }; |
| 127 | + data = simple.data as Hex; |
| 128 | + } |
| 129 | + |
| 130 | + // Client default integratorId is 0xdead when not configured explicitly |
| 131 | + const expectedIntegratorId: Hex = "0xdead"; |
| 132 | + assert( |
| 133 | + hasIntegratorIdAppended(data, expectedIntegratorId, { isSwap: true }), |
| 134 | + "Expected swap calldata to include client's integrator id when omitted in params", |
| 135 | + ); |
| 136 | + }); |
71 | 137 | }); |
0 commit comments