Skip to content

Commit

Permalink
feat: mint wxdai, generate permits, transfer improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Jul 15, 2024
1 parent 71563d0 commit c2eb982
Show file tree
Hide file tree
Showing 8 changed files with 659 additions and 60 deletions.
79 changes: 79 additions & 0 deletions app/(screens)/account/api/mint-wxdai/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
import { createServerClient } from "@supabase/ssr";
import { getSigner } from "@/app/lib/eoa/get-signer";
import { Networks } from "@/app/types/blockchain";
import { Contract } from "ethers";
import { parseUnits } from "viem";

export async function POST(request: NextRequest) {
const cookieStore = cookies();

try {
const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
if (!key || !url) {
throw new Error("Missing Supabase credentials");
}
createServerClient(url, key, {
cookies: {
getAll() {
return cookieStore.getAll();
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value, options }) => cookieStore.set(name, value, options));
},
},
});

const body = await request.json();

if (!body || "error" in body) {
return NextResponse.json({ error: body?.error || "Invalid request" });
}

let txHash;

console.log("Transfer request", body)
const { network } = body;

if (network === "amoy") {
txHash = await mintWxdai(network);
}

if (!txHash) {
return NextResponse.json({ error: "Transaction failed" });
}

return NextResponse.json({ txHash });
} catch (error) {
if (error instanceof Error) {
return NextResponse.json({ error: error.message });
} else {
return NextResponse.error();
}
}
}


// makes the demo a bit more workable
export async function mintWxdai(network: Networks) {
if (network !== "amoy") return "Invalid network";

try {
const signer = await getSigner(network);
const contract = new Contract("0x54Dca79D5f88E19261F06A647566DeF7765D1fce", [
"function transfer(address, uint256)",
"function mint()",
"function approve(address, uint256)",
], signer);

const tx = await contract.mint();
await tx.wait();
await contract.approve("0x000000000022D473030F116dDEE9F6B43aC78BA3", parseUnits("100000000", 18));
await tx.wait();
return tx.hash;
} catch (error) {
return error;
}
}
46 changes: 46 additions & 0 deletions app/(screens)/account/api/permits/claim/claim-permit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { permit2Abi } from "@/app/(screens)/claim/scripts/abis";
import { PERMIT2_ADDRESS } from "@/app/(screens)/claim/scripts/generate-erc20-permit-url";
import { getSigner } from "@/app/lib/eoa/get-signer";
import { provider } from "@/app/lib/funding/balance-check";
import { PermitReward } from "@ubiquibot/permit-generation/dist/types";
import { Contract } from "ethers";
import { parseUnits, toHex } from "viem";


export async function claimPermit(reward: PermitReward) {
const signer = await getSigner("amoy");

if (!signer || !reward || !PERMIT2_ADDRESS || !permit2Abi) {
throw new Error("Invalid request");
}

const permit2Contract = new Contract(
PERMIT2_ADDRESS,
permit2Abi,
signer.connect(provider)
);

const tx = await permit2Contract.connect(signer).getFunction("permitTransferFrom").call([
{
requestedAmount: toHex(reward.transferDetails.requestedAmount),
to: reward.transferDetails.to,
}, {
permitted: {
token: reward.permit.permitted.token,
amount: toHex(reward.permit.permitted.amount),
},
nonce: reward.permit.nonce,
deadline: reward.permit.deadline,
},

reward.owner,
reward.signature]
);


console.log("Claimed permit", tx);

await tx.wait();

return tx;
}
72 changes: 72 additions & 0 deletions app/(screens)/account/api/permits/claim/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
import { createServerClient } from "@supabase/ssr";
import { getSigner } from "@/app/lib/eoa/get-signer";
import { Networks } from "@/app/types/blockchain";
import { Contract } from "ethers";
import { parseUnits } from "viem";
import { claimPermit } from "./claim-permit";

export async function POST(request: NextRequest) {
const cookieStore = cookies();

try {
const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
if (!key || !url) {
throw new Error("Missing Supabase credentials");
}
createServerClient(url, key, {
cookies: {
getAll() {
return cookieStore.getAll();
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value, options }) => cookieStore.set(name, value, options));
},
},
});

const { reward } = await request.json();

console.log("Claim request", reward);

if (!reward) {
return NextResponse.json({ error: "Invalid request" }, { status: 400 });
}


const tx = await claimPermit(reward);

return NextResponse.json({ txHash: tx.hash });
} catch (error) {
if (error instanceof Error) {
return NextResponse.json({ error: error.message });
} else {
return NextResponse.error();
}
}
}


// makes the demo a bit more workable
export async function mintWxdai(network: Networks) {
if (network !== "amoy") return "Invalid network";

try {
const signer = await getSigner(network);
const contract = new Contract("0x54Dca79D5f88E19261F06A647566DeF7765D1fce", [
"function transfer(address, uint256)",
"function mint()",
"function approve(address, uint256)",
], signer);

const tx = await contract.mint();
await tx.wait();
await contract.approve("0x000000000022D473030F116dDEE9F6B43aC78BA3", parseUnits("100000000", 18));
await tx.wait();
return tx.hash;
} catch (error) {
return error;
}
}
80 changes: 80 additions & 0 deletions app/(screens)/account/api/permits/create/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
import { createServerClient } from "@supabase/ssr";
import { getSigner } from "@/app/lib/eoa/get-signer";
import { Networks } from "@/app/types/blockchain";
import { Contract } from "ethers";
import { parseUnits } from "viem";
import { generateERC20Permit } from "@/app/(screens)/claim/scripts/generate-erc20-permit-url";

export async function POST(request: NextRequest) {
const cookieStore = cookies();

try {
const key = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
const url = process.env.NEXT_PUBLIC_SUPABASE_URL;
if (!key || !url) {
throw new Error("Missing Supabase credentials");
}
createServerClient(url, key, {
cookies: {
getAll() {
return cookieStore.getAll();
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value, options }) => cookieStore.set(name, value, options));
},
},
});

const body = await request.json();

if (!body || "error" in body) {
return NextResponse.json({ error: body?.error || "Invalid request" });
}

let claimUrlSuffix;
const { network } = body;

if (network === "amoy") {
claimUrlSuffix = await generateERC20Permit(body.token, body.address);
}

if (!claimUrlSuffix) {
return NextResponse.json({ error: "Transaction failed" });
}

console.log("Permit added", claimUrlSuffix);

return NextResponse.json({ claimUrlSuffix });
} catch (error) {
if (error instanceof Error) {
return NextResponse.json({ error: error.message });
} else {
return NextResponse.error();
}
}
}


// makes the demo a bit more workable
export async function mintWxdai(network: Networks) {
if (network !== "amoy") return "Invalid network";

try {
const signer = await getSigner(network);
const contract = new Contract("0x54Dca79D5f88E19261F06A647566DeF7765D1fce", [
"function transfer(address, uint256)",
"function mint()",
"function approve(address, uint256)",
], signer);

const tx = await contract.mint();
await tx.wait();
await contract.approve("0x000000000022D473030F116dDEE9F6B43aC78BA3", parseUnits("100000000", 18));
await tx.wait();
return tx.hash;
} catch (error) {
return error;
}
}
Loading

0 comments on commit c2eb982

Please sign in to comment.