Skip to content

Commit

Permalink
add test script
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteenkamp89 committed Dec 30, 2024
1 parent 75fbc44 commit 0142286
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
64 changes: 59 additions & 5 deletions scripts/tests/swap-auth.ts
Original file line number Diff line number Diff line change
@@ -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<ReturnType<typeof buildAuthTxPayload>>;

async function swapWithPermit() {
console.log("Swapping with auth...");
const swapQuote = await fetchSwapQuote<AuthPayload>("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);
Expand Down
10 changes: 9 additions & 1 deletion scripts/tests/swap-permit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<typeof buildPermitTxPayload>>;

async function swapWithPermit() {
console.log("Swapping with permit...");
const swapQuote = await fetchSwapQuote<any>("permit");
const swapQuote = await fetchSwapQuote<PermitPayload>("permit");

if (!swapQuote) {
console.log("No Quote");
return;
}

if (process.env.DEV_WALLET_PK) {
const wallet = new Wallet(process.env.DEV_WALLET_PK!).connect(
Expand Down

0 comments on commit 0142286

Please sign in to comment.