diff --git a/gasless-v2-headless-example/bun.lockb b/gasless-v2-headless-example/bun.lockb index b66aae2d..df24e447 100755 Binary files a/gasless-v2-headless-example/bun.lockb and b/gasless-v2-headless-example/bun.lockb differ diff --git a/gasless-v2-headless-example/index.ts b/gasless-v2-headless-example/index.ts index 7c41a36f..2090d4e4 100644 --- a/gasless-v2-headless-example/index.ts +++ b/gasless-v2-headless-example/index.ts @@ -10,7 +10,7 @@ import { Hex, } from "viem"; import { privateKeyToAccount } from "viem/accounts"; -import { base } from "viem/chains"; +import { plasma } from "viem/chains"; import { wethAbi } from "./abi/weth-abi"; import { SignatureType, splitSignature } from "./utils/signature"; @@ -37,19 +37,19 @@ const headers = new Headers({ // setup wallet client const client = createWalletClient({ account: privateKeyToAccount(("0x" + PRIVATE_KEY) as `0x${string}`), - chain: base, + chain: plasma, transport: http(ALCHEMY_HTTP_TRANSPORT_URL), }).extend(publicActions); // extend wallet client with publicActions for public client // set up contracts -const usdc = getContract({ - address: "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913", +const usdt = getContract({ + address: "0xb8ce59fc3717ada4c02eadf9682a9e934f625ebb", abi: erc20Abi, client, }); -const weth = getContract({ - address: "0x4200000000000000000000000000000000000006", +const wxpl = getContract({ + address: "0x6100E367285b01F48D07953803A2d8dCA5D19873", abi: wethAbi, client, }); @@ -70,22 +70,22 @@ async function fetchJsonOrThrow(res: Response) { const main = async () => { // specify sell amount - // USDC supports gasless approvals because it is an ERC-20 that supports the Permit function - const sellAmount = parseUnits("0.1", await usdc.read.decimals()); + const sellAmount = 1_000_000_000_000_000_000; // 1. fetch price const priceParams = new URLSearchParams({ chainId: client.chain.id.toString(), - sellToken: usdc.address, - buyToken: weth.address, + sellToken: wxpl.address, + buyToken: usdt.address, sellAmount: sellAmount.toString(), }); - const priceUrl = "https://api.0x.org/gasless/price?" + priceParams.toString(); + const priceUrl = + "https://staging.api.0x.org/gasless/price?" + priceParams.toString(); const priceResponse = await fetch(priceUrl, { headers }); const price = await fetchJsonOrThrow(priceResponse); - console.log("Fetching price to swap 0.1 USDC for WETH with Gasless API\n"); + console.log("Fetching price to swap 1 WXPL for USDT with Gasless API\n"); console.log(priceUrl + "\n"); console.log("🏷 priceResponse: ", price, "\n"); @@ -94,11 +94,12 @@ const main = async () => { for (const [key, value] of priceParams.entries()) quoteParams.append(key, value); - const quoteUrl = "https://api.0x.org/gasless/quote?" + quoteParams.toString(); + const quoteUrl = + "https:/staging.api.0x.org/gasless/quote?" + quoteParams.toString(); const quoteResponse = await fetch(quoteUrl, { headers }); const quote = await fetchJsonOrThrow(quoteResponse); - console.log("Fetching quote to swap 0.1 USDC for WETH with Gasless API\n"); + console.log("Fetching quote to swap 1 WXPL for USDT with Gasless API\n"); console.log("💸 quoteResponse: ", quote, "\n"); // 3. Check if token approval is required and if gasless approval is available @@ -173,23 +174,9 @@ const main = async () => { async function standardApproval(): Promise { if (quote.issues.allowance !== null) { - try { - const { request } = await usdc.simulate.approve([ - quote.issues.allowance.spender, - maxUint256, - ]); - console.log("Approving Permit2 to spend USDC...", request); - // set approval - const hash = await usdc.write.approve(request.args); - console.log( - "Approved Permit2 to spend USDC.", - await client.waitForTransactionReceipt({ hash }) - ); - } catch (error) { - console.log("Error approving Permit2:", error); - } + console.log("WXPL needs to be approved for Permit2"); } else { - console.log("USDC already approved for Permit2"); + console.log("WXPL already approved for Permit2"); } } @@ -236,7 +223,7 @@ const main = async () => { requestBody.approval = approvalDataToSubmit; } - const response = await fetch("https://api.0x.org/gasless/submit", { + const response = await fetch("https:/staging.api.0x.org/gasless/submit", { method: "POST", headers: { "0x-api-key": ZERO_EX_API_KEY!, @@ -258,7 +245,7 @@ const main = async () => { // 5. Check trade status async function fetchStatus(tradeHash: string) { const url = - "https://api.0x.org/gasless/status/" + + "https://staging,api.0x.org/gasless/status/" + tradeHash + "?" + "chainId=" + diff --git a/gasless-v2-headless-example/package.json b/gasless-v2-headless-example/package.json index b673888e..ea633f30 100644 --- a/gasless-v2-headless-example/package.json +++ b/gasless-v2-headless-example/package.json @@ -12,6 +12,6 @@ "dependencies": { "dotenv": "^16.4.5", "qs": "^6.12.1", - "viem": "^2.16.2" + "viem": "^2.37.6" } } \ No newline at end of file