|
| 1 | +import { toWei } from "src/utils/units.js"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { TEST_CLIENT } from "~test/test-clients.js"; |
| 4 | +import * as Onramp from "./Onramp.js"; |
| 5 | + |
| 6 | +// Use the same receiver address as other bridge tests |
| 7 | +const RECEIVER_ADDRESS = "0x2a4f24F935Eb178e3e7BA9B53A5Ee6d8407C0709"; |
| 8 | +const NATIVE_TOKEN_ADDRESS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; |
| 9 | + |
| 10 | +/** |
| 11 | + * These tests call the real Bridge Onramp API. They are executed only when a |
| 12 | + * `TW_SECRET_KEY` environment variable is present, mirroring the behaviour of |
| 13 | + * the other bridge tests in this package. |
| 14 | + */ |
| 15 | +describe.runIf(process.env.TW_SECRET_KEY)("Bridge.Onramp.prepare", () => { |
| 16 | + it("should prepare an onramp successfully", async () => { |
| 17 | + const prepared = await Onramp.prepare({ |
| 18 | + client: TEST_CLIENT, |
| 19 | + onramp: "stripe", |
| 20 | + chainId: 1, |
| 21 | + tokenAddress: NATIVE_TOKEN_ADDRESS, |
| 22 | + receiver: RECEIVER_ADDRESS, |
| 23 | + amount: toWei("0.01"), |
| 24 | + }); |
| 25 | + |
| 26 | + expect(prepared).toBeDefined(); |
| 27 | + |
| 28 | + // The destinationAmount should be a bigint and greater than zero |
| 29 | + expect(typeof prepared.destinationAmount).toBe("bigint"); |
| 30 | + expect(prepared.destinationAmount > 0n).toBe(true); |
| 31 | + |
| 32 | + // A redirect link for the user should be provided |
| 33 | + expect(prepared.link).toBeDefined(); |
| 34 | + expect(typeof prepared.link).toBe("string"); |
| 35 | + |
| 36 | + // Intent must be present and reference the correct receiver |
| 37 | + expect(prepared.intent).toBeDefined(); |
| 38 | + expect(prepared.intent.receiver.toLowerCase()).toBe( |
| 39 | + RECEIVER_ADDRESS.toLowerCase(), |
| 40 | + ); |
| 41 | + |
| 42 | + // Steps array should be defined (it may be empty if the provider supports the destination token natively) |
| 43 | + expect(Array.isArray(prepared.steps)).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should surface any errors", async () => { |
| 47 | + await expect( |
| 48 | + Onramp.prepare({ |
| 49 | + client: TEST_CLIENT, |
| 50 | + onramp: "stripe", |
| 51 | + chainId: 444, // Unsupported chain ID |
| 52 | + tokenAddress: NATIVE_TOKEN_ADDRESS, |
| 53 | + receiver: RECEIVER_ADDRESS, |
| 54 | + amount: toWei("0.01"), |
| 55 | + }), |
| 56 | + ).rejects.toThrowError(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should prepare a Coinbase onramp successfully", async () => { |
| 60 | + const prepared = await Onramp.prepare({ |
| 61 | + client: TEST_CLIENT, |
| 62 | + onramp: "coinbase", |
| 63 | + chainId: 1, |
| 64 | + tokenAddress: NATIVE_TOKEN_ADDRESS, |
| 65 | + receiver: RECEIVER_ADDRESS, |
| 66 | + amount: toWei("0.01"), |
| 67 | + }); |
| 68 | + |
| 69 | + expect(prepared).toBeDefined(); |
| 70 | + |
| 71 | + // The destinationAmount should be a bigint and greater than zero |
| 72 | + expect(typeof prepared.destinationAmount).toBe("bigint"); |
| 73 | + expect(prepared.destinationAmount > 0n).toBe(true); |
| 74 | + |
| 75 | + // A redirect link for the user should be provided |
| 76 | + expect(prepared.link).toBeDefined(); |
| 77 | + expect(typeof prepared.link).toBe("string"); |
| 78 | + |
| 79 | + // Intent must be present and reference the correct receiver |
| 80 | + expect(prepared.intent).toBeDefined(); |
| 81 | + expect(prepared.intent.receiver.toLowerCase()).toBe( |
| 82 | + RECEIVER_ADDRESS.toLowerCase(), |
| 83 | + ); |
| 84 | + |
| 85 | + // Steps array should be defined (it may be empty if the provider supports the destination token natively) |
| 86 | + expect(Array.isArray(prepared.steps)).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should prepare a Transak onramp successfully", async () => { |
| 90 | + const prepared = await Onramp.prepare({ |
| 91 | + client: TEST_CLIENT, |
| 92 | + onramp: "transak", |
| 93 | + chainId: 1, |
| 94 | + tokenAddress: NATIVE_TOKEN_ADDRESS, |
| 95 | + receiver: RECEIVER_ADDRESS, |
| 96 | + amount: toWei("0.01"), |
| 97 | + }); |
| 98 | + |
| 99 | + expect(prepared).toBeDefined(); |
| 100 | + |
| 101 | + // The destinationAmount should be a bigint and greater than zero |
| 102 | + expect(typeof prepared.destinationAmount).toBe("bigint"); |
| 103 | + expect(prepared.destinationAmount > 0n).toBe(true); |
| 104 | + |
| 105 | + // A redirect link for the user should be provided |
| 106 | + expect(prepared.link).toBeDefined(); |
| 107 | + expect(typeof prepared.link).toBe("string"); |
| 108 | + |
| 109 | + // Intent must be present and reference the correct receiver |
| 110 | + expect(prepared.intent).toBeDefined(); |
| 111 | + expect(prepared.intent.receiver.toLowerCase()).toBe( |
| 112 | + RECEIVER_ADDRESS.toLowerCase(), |
| 113 | + ); |
| 114 | + |
| 115 | + // Steps array should be defined (it may be empty if the provider supports the destination token natively) |
| 116 | + expect(Array.isArray(prepared.steps)).toBe(true); |
| 117 | + }); |
| 118 | +}); |
0 commit comments