Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Binary file modified gasless-v2-headless-example/bun.lockb
Binary file not shown.
51 changes: 19 additions & 32 deletions gasless-v2-headless-example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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,
});
Expand All @@ -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");

Expand All @@ -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
Expand Down Expand Up @@ -173,23 +174,9 @@ const main = async () => {

async function standardApproval(): Promise<any> {
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");
}
}

Expand Down Expand Up @@ -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!,
Expand All @@ -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=" +
Expand Down
2 changes: 1 addition & 1 deletion gasless-v2-headless-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"dependencies": {
"dotenv": "^16.4.5",
"qs": "^6.12.1",
"viem": "^2.16.2"
"viem": "^2.37.6"
}
}