From 01422866d1395dc78c5efbe8ca67c09411420ef6 Mon Sep 17 00:00:00 2001 From: Gerhard Steenkamp Date: Mon, 30 Dec 2024 19:12:49 +0200 Subject: [PATCH] add test script --- scripts/tests/swap-auth.ts | 64 +++++++++++++++++++++++++++++++++--- scripts/tests/swap-permit.ts | 10 +++++- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/scripts/tests/swap-auth.ts b/scripts/tests/swap-auth.ts index ae676ba54..a772aad6e 100644 --- a/scripts/tests/swap-auth.ts +++ b/scripts/tests/swap-auth.ts @@ -1,11 +1,65 @@ -import { swap } from "./_swap-utils"; +import { Wallet } from "ethers"; -async function swapWithAuthorization() { - console.log("Swapping with authorization..."); - await swap("auth"); +import { getProvider } from "../../api/_utils"; +import { fetchSwapQuote, SWAP_API_BASE_URL } from "./_swap-utils"; +import { buildAuthTxPayload } from "../../api/swap/auth/_utils"; +import axios from "axios"; + +type AuthPayload = Awaited>; + +async function swapWithPermit() { + console.log("Swapping with auth..."); + const swapQuote = await fetchSwapQuote("auth"); + + if (!swapQuote) { + console.log("No Quote"); + return; + } + + if (process.env.DEV_WALLET_PK) { + const wallet = new Wallet(process.env.DEV_WALLET_PK!).connect( + getProvider(swapQuote.swapTx.chainId) + ); + + // sign permit + deposit + const permitSig = await wallet._signTypedData( + swapQuote.eip712.transferWithAuthorization.domain, + swapQuote.eip712.transferWithAuthorization.types, + swapQuote.eip712.transferWithAuthorization.message + ); + console.log("Signed permit:", permitSig); + + const depositSig = await wallet._signTypedData( + swapQuote.eip712.deposit.domain, + swapQuote.eip712.deposit.types, + swapQuote.eip712.deposit.message + ); + console.log("Signed deposit:", depositSig); + + // relay + const relayResponse = await axios.post(`${SWAP_API_BASE_URL}/api/relay`, { + ...swapQuote.swapTx, + signatures: { permit: permitSig, deposit: depositSig }, + }); + console.log("Relay response:", relayResponse.data); + + // track relay + while (true) { + const relayStatusResponse = await axios.get( + `${SWAP_API_BASE_URL}/api/relay/status?requestHash=${relayResponse.data.requestHash}` + ); + console.log("Relay status response:", relayStatusResponse.data); + + if (relayStatusResponse.data.status === "success") { + break; + } + + await new Promise((resolve) => setTimeout(resolve, 1_000)); + } + } } -swapWithAuthorization() +swapWithPermit() .then(() => console.log("Done")) .catch((e) => { console.error(e); diff --git a/scripts/tests/swap-permit.ts b/scripts/tests/swap-permit.ts index c9c7ab86b..ce3c4e409 100644 --- a/scripts/tests/swap-permit.ts +++ b/scripts/tests/swap-permit.ts @@ -2,11 +2,19 @@ import { Wallet } from "ethers"; import { getProvider } from "../../api/_utils"; import { fetchSwapQuote, SWAP_API_BASE_URL } from "./_swap-utils"; +import { buildPermitTxPayload } from "../../api/swap/permit/_utils"; import axios from "axios"; +type PermitPayload = Awaited>; + async function swapWithPermit() { console.log("Swapping with permit..."); - const swapQuote = await fetchSwapQuote("permit"); + const swapQuote = await fetchSwapQuote("permit"); + + if (!swapQuote) { + console.log("No Quote"); + return; + } if (process.env.DEV_WALLET_PK) { const wallet = new Wallet(process.env.DEV_WALLET_PK!).connect(