|
| 1 | +import type { Abi, Account, Address, Hash } from "viem"; |
| 2 | +import { isAddress, parseAbi, parseEther } from "viem"; |
| 3 | + |
| 4 | +import FCFWrapperAbi from "../abi/FCFWrapper.json" with { type: "json" }; |
| 5 | + |
| 6 | +const fcfWrapperAbi = FCFWrapperAbi as Abi; |
| 7 | +const erc20Abi = parseAbi([ |
| 8 | + "function allowance(address owner, address spender) view returns (uint256)", |
| 9 | + "function approve(address spender, uint256 amount) returns (bool)", |
| 10 | +]); |
| 11 | + |
| 12 | +export type TransactionReceipt = { |
| 13 | + readonly status: "success" | "reverted"; |
| 14 | +}; |
| 15 | + |
| 16 | +export type PublicClientLike = { |
| 17 | + readContract(args: { |
| 18 | + readonly address: Address; |
| 19 | + readonly abi: Abi; |
| 20 | + readonly functionName: string; |
| 21 | + readonly args?: readonly unknown[]; |
| 22 | + }): Promise<unknown>; |
| 23 | + waitForTransactionReceipt(args: { readonly hash: Hash }): Promise<TransactionReceipt>; |
| 24 | +}; |
| 25 | + |
| 26 | +export type WalletClientLike = { |
| 27 | + writeContract(args: { |
| 28 | + readonly address: Address; |
| 29 | + readonly abi: Abi; |
| 30 | + readonly functionName: string; |
| 31 | + readonly args: readonly unknown[]; |
| 32 | + readonly account: Account; |
| 33 | + }): Promise<Hash>; |
| 34 | +}; |
| 35 | + |
| 36 | +export type B20OperationParams = { |
| 37 | + readonly amount: string; |
| 38 | + readonly wrapperAddress: Address; |
| 39 | + readonly account: Account; |
| 40 | + readonly publicClient: PublicClientLike; |
| 41 | + readonly walletClient: WalletClientLike; |
| 42 | +}; |
| 43 | + |
| 44 | +export type ApprovalResult = |
| 45 | + | { readonly approved: false } |
| 46 | + | { readonly approved: true; readonly hash: Hash; readonly receipt: TransactionReceipt }; |
| 47 | + |
| 48 | +export type B20WrapResult = { |
| 49 | + readonly amount: bigint; |
| 50 | + readonly fcfToken: Address; |
| 51 | + readonly wrappedB20: Address; |
| 52 | + readonly approval: ApprovalResult; |
| 53 | + readonly hash: Hash; |
| 54 | + readonly receipt: TransactionReceipt; |
| 55 | +}; |
| 56 | + |
| 57 | +export type B20UnwrapResult = { |
| 58 | + readonly amount: bigint; |
| 59 | + readonly wrappedB20: Address; |
| 60 | + readonly approval: ApprovalResult; |
| 61 | + readonly hash: Hash; |
| 62 | + readonly receipt: TransactionReceipt; |
| 63 | +}; |
| 64 | + |
| 65 | +export async function wrapB20(params: B20OperationParams): Promise<B20WrapResult> { |
| 66 | + const amount = parsePositiveAmount(params.amount); |
| 67 | + const fcfToken = await readWrapperAddress(params.publicClient, params.wrapperAddress, "fcfToken"); |
| 68 | + const wrappedB20 = await readWrapperAddress(params.publicClient, params.wrapperAddress, "wrappedB20"); |
| 69 | + const approval = await approveIfNeeded({ |
| 70 | + ...params, |
| 71 | + token: fcfToken, |
| 72 | + spender: params.wrapperAddress, |
| 73 | + amount, |
| 74 | + }); |
| 75 | + const hash = await params.walletClient.writeContract({ |
| 76 | + address: params.wrapperAddress, |
| 77 | + abi: fcfWrapperAbi, |
| 78 | + functionName: "deposit", |
| 79 | + args: [amount], |
| 80 | + account: params.account, |
| 81 | + }); |
| 82 | + const receipt = await params.publicClient.waitForTransactionReceipt({ hash }); |
| 83 | + |
| 84 | + return { amount, fcfToken, wrappedB20, approval, hash, receipt }; |
| 85 | +} |
| 86 | + |
| 87 | +export async function unwrapB20(params: B20OperationParams): Promise<B20UnwrapResult> { |
| 88 | + const amount = parsePositiveAmount(params.amount); |
| 89 | + const wrappedB20 = await readWrapperAddress(params.publicClient, params.wrapperAddress, "wrappedB20"); |
| 90 | + const approval = await approveIfNeeded({ |
| 91 | + ...params, |
| 92 | + token: wrappedB20, |
| 93 | + spender: params.wrapperAddress, |
| 94 | + amount, |
| 95 | + }); |
| 96 | + const hash = await params.walletClient.writeContract({ |
| 97 | + address: params.wrapperAddress, |
| 98 | + abi: fcfWrapperAbi, |
| 99 | + functionName: "withdraw", |
| 100 | + args: [amount], |
| 101 | + account: params.account, |
| 102 | + }); |
| 103 | + const receipt = await params.publicClient.waitForTransactionReceipt({ hash }); |
| 104 | + |
| 105 | + return { amount, wrappedB20, approval, hash, receipt }; |
| 106 | +} |
| 107 | + |
| 108 | +function parsePositiveAmount(amount: string): bigint { |
| 109 | + let parsed: bigint; |
| 110 | + try { |
| 111 | + parsed = parseEther(amount); |
| 112 | + } catch { |
| 113 | + throw new Error(`invalid amount: ${amount}`); |
| 114 | + } |
| 115 | + if (parsed <= 0n) throw new Error("amount must be greater than zero"); |
| 116 | + return parsed; |
| 117 | +} |
| 118 | + |
| 119 | +async function readWrapperAddress( |
| 120 | + publicClient: PublicClientLike, |
| 121 | + wrapperAddress: Address, |
| 122 | + functionName: "fcfToken" | "wrappedB20", |
| 123 | +): Promise<Address> { |
| 124 | + const value = await publicClient.readContract({ |
| 125 | + address: wrapperAddress, |
| 126 | + abi: fcfWrapperAbi, |
| 127 | + functionName, |
| 128 | + }); |
| 129 | + if (typeof value !== "string" || !isAddress(value)) throw new Error(`invalid ${functionName} address`); |
| 130 | + return value; |
| 131 | +} |
| 132 | + |
| 133 | +async function approveIfNeeded(params: Omit<B20OperationParams, "amount"> & { |
| 134 | + readonly token: Address; |
| 135 | + readonly spender: Address; |
| 136 | + readonly amount: bigint; |
| 137 | +}): Promise<ApprovalResult> { |
| 138 | + const allowance = await params.publicClient.readContract({ |
| 139 | + address: params.token, |
| 140 | + abi: erc20Abi, |
| 141 | + functionName: "allowance", |
| 142 | + args: [params.account.address, params.spender], |
| 143 | + }); |
| 144 | + if (typeof allowance !== "bigint") throw new Error("invalid ERC-20 allowance result"); |
| 145 | + if (allowance >= params.amount) return { approved: false }; |
| 146 | + |
| 147 | + const hash = await params.walletClient.writeContract({ |
| 148 | + address: params.token, |
| 149 | + abi: erc20Abi, |
| 150 | + functionName: "approve", |
| 151 | + args: [params.spender, params.amount], |
| 152 | + account: params.account, |
| 153 | + }); |
| 154 | + const receipt = await params.publicClient.waitForTransactionReceipt({ hash }); |
| 155 | + return { approved: true, hash, receipt }; |
| 156 | +} |
0 commit comments