Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support transfer with auth #1341

Merged
merged 24 commits into from
Jan 6, 2025
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: swap test scripts
dohaki committed Dec 20, 2024
commit 432c160dc7e1d2268a2b12e485b3d80a37545d33
86 changes: 83 additions & 3 deletions scripts/tests/_swap-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { CHAIN_IDs, TOKEN_SYMBOLS_MAP } from "@across-protocol/constants";
import { ethers } from "ethers";
import { ethers, Wallet } from "ethers";
import dotenv from "dotenv";
import axios from "axios";
import { getProvider } from "../../api/_utils";
dotenv.config();

export const { SWAP_API_BASE_URL = "http://localhost:3000" } = process.env;
@@ -65,7 +67,7 @@ export const MIN_OUTPUT_CASES = [
{
labels: ["B2A", "MIN_OUTPUT", "USDC - APE"],
params: {
amount: ethers.utils.parseUnits("1", 18).toString(),
amount: ethers.utils.parseUnits("3", 18).toString(),
tradeType: "minOutput",
inputToken: TOKEN_SYMBOLS_MAP.USDC.addresses[originChainId],
originChainId,
@@ -101,6 +103,30 @@ export const MIN_OUTPUT_CASES = [
depositor,
},
},
{
labels: ["A2B", "MIN_OUTPUT", "USDC - WETH"],
params: {
amount: ethers.utils.parseUnits("0.001", 18).toString(),
tradeType: "minOutput",
inputToken: TOKEN_SYMBOLS_MAP.USDC.addresses[originChainId],
originChainId,
outputToken: TOKEN_SYMBOLS_MAP.WETH.addresses[destinationChainId],
destinationChainId,
depositor,
},
},
{
labels: ["A2B", "MIN_OUTPUT", "WETH - USDC"],
params: {
amount: ethers.utils.parseUnits("1", 6).toString(),
tradeType: "minOutput",
inputToken: TOKEN_SYMBOLS_MAP.WETH.addresses[originChainId],
originChainId,
outputToken: TOKEN_SYMBOLS_MAP.USDC.addresses[destinationChainId],
destinationChainId,
depositor,
},
},
// A2A
{
labels: ["A2A", "MIN_OUTPUT", "bridged USDC - APE"],
@@ -110,7 +136,7 @@ export const MIN_OUTPUT_CASES = [
inputToken:
TOKEN_SYMBOLS_MAP["USDC.e"].addresses[originChainId] ||
TOKEN_SYMBOLS_MAP.USDbC.addresses[originChainId],
originChainId: CHAIN_IDs.BASE,
originChainId,
outputToken: anyDestinationOutputTokens[destinationChainId], // APE Coin
destinationChainId,
depositor,
@@ -143,3 +169,57 @@ export function filterTestCases(
});
return filteredTestCases;
}

export async function swap() {
const filterString = process.argv[2];
const testCases = [...MIN_OUTPUT_CASES, ...EXACT_OUTPUT_CASES];
const filteredTestCases = filterTestCases(testCases, filterString);
for (const testCase of filteredTestCases) {
console.log("\nTest case:", testCase.labels.join(" "));
console.log("Params:", testCase.params);
const response = await axios.get(
`${SWAP_API_BASE_URL}/api/swap/allowance`,
{
params: testCase.params,
}
);
console.log(response.data);

if (process.env.DEV_WALLET_PK) {
const wallet = new Wallet(process.env.DEV_WALLET_PK!).connect(
getProvider(testCase.params.originChainId)
);

if (response.data.approvalTxns) {
console.log("Approval needed...");
let step = 1;
for (const approvalTxn of response.data.approvalTxns) {
const stepLabel = `(${step}/${response.data.approvalTxns.length})`;
const tx = await wallet.sendTransaction({
to: approvalTxn.to,
data: approvalTxn.data,
});
console.log(`${stepLabel} Approval tx hash:`, tx.hash);
await tx.wait();
console.log(`${stepLabel} Approval tx mined`);
step++;
}
}

try {
const tx = await wallet.sendTransaction({
to: response.data.swapTx.to,
data: response.data.swapTx.data,
value: response.data.swapTx.value,
gasLimit: response.data.swapTx.gas,
gasPrice: response.data.swapTx.gasPrice,
});
console.log("Tx hash: ", tx.hash);
await tx.wait();
console.log("Tx mined");
} catch (e) {
console.error("Tx reverted", e);
}
}
}
}
72 changes: 5 additions & 67 deletions scripts/tests/swap-allowance.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,11 @@
import axios from "axios";
import { Wallet } from "ethers";
import dotenv from "dotenv";
import { getProvider } from "../../api/_utils";
import {
filterTestCases,
MIN_OUTPUT_CASES,
EXACT_OUTPUT_CASES,
SWAP_API_BASE_URL,
} from "./_swap-utils";
dotenv.config();
import { swap } from "./_swap-utils";

/**
* Manual test script for the swap API. Should be converted to a proper test suite.
*/
async function swap() {
const filterString = process.argv[2];
const testCases = [...MIN_OUTPUT_CASES, ...EXACT_OUTPUT_CASES];
const filteredTestCases = filterTestCases(testCases, filterString);
for (const testCase of filteredTestCases) {
console.log("\nTest case:", testCase.labels.join(" "));
console.log("Params:", testCase.params);
const response = await axios.get(
`${SWAP_API_BASE_URL}/api/swap/allowance`,
{
params: testCase.params,
}
);
console.log(response.data);

if (process.env.DEV_WALLET_PK) {
const wallet = new Wallet(process.env.DEV_WALLET_PK!).connect(
getProvider(testCase.params.originChainId)
);

if (response.data.approvalTxns) {
console.log("Approval needed...");
let step = 1;
for (const approvalTxn of response.data.approvalTxns) {
const stepLabel = `(${step}/${response.data.approvalTxns.length})`;
const tx = await wallet.sendTransaction({
to: approvalTxn.to,
data: approvalTxn.data,
});
console.log(`${stepLabel} Approval tx hash:`, tx.hash);
await tx.wait();
console.log(`${stepLabel} Approval tx mined`);
step++;
}
}

try {
const tx = await wallet.sendTransaction({
to: response.data.swapTx.to,
data: response.data.swapTx.data,
value: response.data.swapTx.value,
gasLimit: response.data.swapTx.gas,
gasPrice: response.data.swapTx.gasPrice,
});
console.log("Tx hash: ", tx.hash);
await tx.wait();
console.log("Tx mined");
} catch (e) {
console.error("Tx reverted", e);
}
}
}
async function swapWithAllowance() {
console.log("Swapping with allowance...");
await swap();
}

swap()
swapWithAllowance()
.then(() => console.log("Done"))
.catch((e) => {
console.error(e);
36 changes: 11 additions & 25 deletions scripts/tests/swap-permit.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
import axios from "axios";
import dotenv from "dotenv";
import {
filterTestCases,
MIN_OUTPUT_CASES,
EXACT_OUTPUT_CASES,
SWAP_API_BASE_URL,
} from "./_swap-utils";
dotenv.config();
import { swap } from "./_swap-utils";

/**
* Manual test script for the swap API. Should be converted to a proper test suite.
*/
async function swap() {
const filterString = process.argv[2];
const testCases = [...MIN_OUTPUT_CASES, ...EXACT_OUTPUT_CASES];
const filteredTestCases = filterTestCases(testCases, filterString);
for (const testCase of filteredTestCases) {
console.log("\nTest case:", testCase.labels.join(" "));
const response = await axios.get(`${SWAP_API_BASE_URL}/api/swap/permit`, {
params: testCase.params,
});
console.log(response.data);
}
async function swapWithPermit() {
console.log("Swapping with permit...");
await swap();
}

swap()
swapWithPermit()
.then(() => console.log("Done"))
.catch(console.error);
.catch((e) => {
console.error(e);
if (e.response?.data) {
console.log("Tx for debug sim:", e.response.data.transaction);
}
});