|
| 1 | +import { |
| 2 | + Wormhole, |
| 3 | + routes, |
| 4 | + type Network, |
| 5 | +} from "@wormhole-foundation/sdk-connect"; |
| 6 | + |
| 7 | +import "@wormhole-foundation/sdk-definitions-ntt"; |
| 8 | +import "@wormhole-foundation/sdk-evm-ntt"; |
| 9 | +import "@wormhole-foundation/sdk-solana-ntt"; |
| 10 | + |
| 11 | +import { EvmPlatform } from "@wormhole-foundation/sdk-evm"; |
| 12 | +import { SolanaPlatform } from "@wormhole-foundation/sdk-solana"; |
| 13 | +import { |
| 14 | + nttExecutorRoute, |
| 15 | + NttExecutorRoute, |
| 16 | +} from "../src/executor/executor.js"; |
| 17 | + |
| 18 | +const SOL_TOKEN = "EetppHswYvV1jjRWoQKC1hejdeBDHR9NNzNtCyRQfrrQ"; |
| 19 | +const SEPOLIA_TOKEN = "0x738141EFf659625F2eAD4feECDfCD94155C67f18"; |
| 20 | + |
| 21 | +const nttConfig: NttExecutorRoute.Config["ntt"] = { |
| 22 | + tokens: { |
| 23 | + TestToken: [ |
| 24 | + { |
| 25 | + chain: "Solana", |
| 26 | + token: SOL_TOKEN, |
| 27 | + manager: "NTtAaoDJhkeHeaVUHnyhwbPNAN6WgBpHkHBTc6d7vLK", |
| 28 | + transceiver: [ |
| 29 | + { |
| 30 | + type: "wormhole", |
| 31 | + address: "ExVbjD8inGXkt7Cx8jVr4GF175sQy1MeqgfaY53Ah8as", |
| 32 | + }, |
| 33 | + ], |
| 34 | + quoter: "Nqd6XqA8LbsCuG8MLWWuP865NV6jR1MbXeKxD4HLKDJ", |
| 35 | + }, |
| 36 | + { |
| 37 | + chain: "Sepolia", |
| 38 | + token: SEPOLIA_TOKEN, |
| 39 | + manager: "0x649fF7B32C2DE771043ea105c4aAb2D724497238", |
| 40 | + transceiver: [ |
| 41 | + { |
| 42 | + type: "wormhole", |
| 43 | + address: "0x06413c42e913327Bc9a08B7C1E362BAE7C0b9598", |
| 44 | + }, |
| 45 | + ], |
| 46 | + }, |
| 47 | + ], |
| 48 | + }, |
| 49 | +}; |
| 50 | + |
| 51 | +const wh = new Wormhole("Testnet", [SolanaPlatform, EvmPlatform]); |
| 52 | + |
| 53 | +function createRoute(config: NttExecutorRoute.Config) { |
| 54 | + const Route = nttExecutorRoute(config); |
| 55 | + return new Route(wh); |
| 56 | +} |
| 57 | + |
| 58 | +describe("NttExecutorRoute referrer fee", () => { |
| 59 | + let request: routes.RouteTransferRequest<"Testnet">; |
| 60 | + |
| 61 | + beforeAll(async () => { |
| 62 | + request = await routes.RouteTransferRequest.create(wh, { |
| 63 | + source: Wormhole.tokenId("Solana", SOL_TOKEN), |
| 64 | + destination: Wormhole.tokenId("Sepolia", SEPOLIA_TOKEN), |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it("uses getReferrerFee callback when defined", async () => { |
| 69 | + const getReferrerFee = jest.fn(async () => ({ |
| 70 | + feeDbps: 42n, |
| 71 | + referrerAddress: "0x9b2A3B92b1D86938D3Ed37B0519952C227bA6D09", |
| 72 | + })); |
| 73 | + const route = createRoute({ ntt: nttConfig, getReferrerFee }); |
| 74 | + |
| 75 | + const result = await route.validate(request, { amount: "1.0" }); |
| 76 | + expect(result.valid).toBe(true); |
| 77 | + if (!result.valid) throw new Error("unexpected"); |
| 78 | + |
| 79 | + expect(getReferrerFee).toHaveBeenCalledWith({ |
| 80 | + sourceChain: "Solana", |
| 81 | + sourceToken: SOL_TOKEN, |
| 82 | + destinationChain: "Sepolia", |
| 83 | + destinationToken: SEPOLIA_TOKEN, |
| 84 | + }); |
| 85 | + const params = result.params as NttExecutorRoute.ValidatedParams; |
| 86 | + expect(params.normalizedParams.referrerFeeDbps).toBe(42n); |
| 87 | + }); |
| 88 | + |
| 89 | + it("uses static referrerFee when getReferrerFee is not defined", async () => { |
| 90 | + const route = createRoute({ |
| 91 | + ntt: nttConfig, |
| 92 | + referrerFee: { feeDbps: 100n }, |
| 93 | + }); |
| 94 | + |
| 95 | + const result = await route.validate(request, { amount: "1.0" }); |
| 96 | + expect(result.valid).toBe(true); |
| 97 | + if (!result.valid) throw new Error("unexpected"); |
| 98 | + |
| 99 | + const params = result.params as NttExecutorRoute.ValidatedParams; |
| 100 | + expect(params.normalizedParams.referrerFeeDbps).toBe(100n); |
| 101 | + }); |
| 102 | + |
| 103 | + it("getReferrerFee takes priority over static referrerFee", async () => { |
| 104 | + const route = createRoute({ |
| 105 | + ntt: nttConfig, |
| 106 | + referrerFee: { feeDbps: 100n }, |
| 107 | + getReferrerFee: async () => ({ |
| 108 | + feeDbps: 77n, |
| 109 | + referrerAddress: "0x9b2A3B92b1D86938D3Ed37B0519952C227bA6D09", |
| 110 | + }), |
| 111 | + }); |
| 112 | + |
| 113 | + const result = await route.validate(request, { amount: "1.0" }); |
| 114 | + expect(result.valid).toBe(true); |
| 115 | + if (!result.valid) throw new Error("unexpected"); |
| 116 | + |
| 117 | + const params = result.params as NttExecutorRoute.ValidatedParams; |
| 118 | + expect(params.normalizedParams.referrerFeeDbps).toBe(77n); |
| 119 | + }); |
| 120 | + |
| 121 | + it("defaults to 0 when neither is defined", async () => { |
| 122 | + const route = createRoute({ ntt: nttConfig }); |
| 123 | + |
| 124 | + const result = await route.validate(request, { amount: "1.0" }); |
| 125 | + expect(result.valid).toBe(true); |
| 126 | + if (!result.valid) throw new Error("unexpected"); |
| 127 | + |
| 128 | + const params = result.params as NttExecutorRoute.ValidatedParams; |
| 129 | + expect(params.normalizedParams.referrerFeeDbps).toBe(0n); |
| 130 | + }); |
| 131 | + |
| 132 | + it("resolves per-token feeDbps override from static config", async () => { |
| 133 | + const route = createRoute({ |
| 134 | + ntt: nttConfig, |
| 135 | + referrerFee: { |
| 136 | + feeDbps: 10n, |
| 137 | + perTokenOverrides: { |
| 138 | + Solana: { |
| 139 | + [SOL_TOKEN]: { referrerFeeDbps: 50n }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }); |
| 144 | + |
| 145 | + const result = await route.validate(request, { amount: "1.0" }); |
| 146 | + expect(result.valid).toBe(true); |
| 147 | + if (!result.valid) throw new Error("unexpected"); |
| 148 | + |
| 149 | + const params = result.params as NttExecutorRoute.ValidatedParams; |
| 150 | + expect(params.normalizedParams.referrerFeeDbps).toBe(50n); |
| 151 | + }); |
| 152 | +}); |
0 commit comments