generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mint wxdai, generate permits, transfer improvement
- Loading branch information
Showing
8 changed files
with
659 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.