From d92cbc754d3d9574563e67f871c650c7a66d6127 Mon Sep 17 00:00:00 2001 From: Jonas Daniels Date: Thu, 18 Jan 2024 17:19:18 +1300 Subject: [PATCH] initial erc20 extensions --- packages/thirdweb/package.json | 8 +++++ packages/thirdweb/src/extensions/erc20.ts | 5 +++ .../src/extensions/erc20/balanceOf.ts | 19 +++++++++++ .../thirdweb/src/extensions/erc20/decimals.ts | 13 ++++++++ .../thirdweb/src/extensions/erc20/mintTo.ts | 33 +++++++++++++++++++ packages/thirdweb/src/transaction/index.ts | 15 ++++----- 6 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 packages/thirdweb/src/extensions/erc20.ts create mode 100644 packages/thirdweb/src/extensions/erc20/balanceOf.ts create mode 100644 packages/thirdweb/src/extensions/erc20/decimals.ts create mode 100644 packages/thirdweb/src/extensions/erc20/mintTo.ts diff --git a/packages/thirdweb/package.json b/packages/thirdweb/package.json index 0eb1bec2d63..9d6acfd19a2 100644 --- a/packages/thirdweb/package.json +++ b/packages/thirdweb/package.json @@ -34,6 +34,11 @@ "import": "./esm/wallets/*.js", "default": "./cjs/wallets/*.js" }, + "./extensions/*": { + "types": "./types/extensions/*.d.ts", + "import": "./esm/extensions/*.js", + "default": "./cjs/extensions/*.js" + }, "./package.json": "./package.json" }, "typesVersions": { @@ -43,6 +48,9 @@ ], "wallets/*": [ "./types/wallets/*.d.ts" + ], + "extensions/*": [ + "./types/extensions/*.d.ts" ] } }, diff --git a/packages/thirdweb/src/extensions/erc20.ts b/packages/thirdweb/src/extensions/erc20.ts new file mode 100644 index 00000000000..fc5bf0835c7 --- /dev/null +++ b/packages/thirdweb/src/extensions/erc20.ts @@ -0,0 +1,5 @@ +// read +export { balanceOf } from "./erc20/balanceOf.js"; +export { decimals } from "./erc20/decimals.js"; +// write +export { mintTo } from "./erc20/mintTo.js"; diff --git a/packages/thirdweb/src/extensions/erc20/balanceOf.ts b/packages/thirdweb/src/extensions/erc20/balanceOf.ts new file mode 100644 index 00000000000..981209a569d --- /dev/null +++ b/packages/thirdweb/src/extensions/erc20/balanceOf.ts @@ -0,0 +1,19 @@ +import { transaction } from "../../transaction/index.js"; +import type { ThirdwebContract } from "../../contract/index.js"; +import { read } from "../../transaction/actions/read.js"; + +type BalanceOfParams = { address: string }; + +export async function balanceOf( + contract: ThirdwebContract, + options: BalanceOfParams, +) { + return read( + transaction(contract, { + address: contract.address, + chainId: contract.chainId, + method: "function balanceOf(address) view returns (uint256)", + params: [options.address], + }), + ); +} diff --git a/packages/thirdweb/src/extensions/erc20/decimals.ts b/packages/thirdweb/src/extensions/erc20/decimals.ts new file mode 100644 index 00000000000..3ad211a51c4 --- /dev/null +++ b/packages/thirdweb/src/extensions/erc20/decimals.ts @@ -0,0 +1,13 @@ +import { transaction } from "../../transaction/index.js"; +import type { ThirdwebContract } from "../../contract/index.js"; +import { read } from "../../transaction/actions/read.js"; + +export async function decimals(contract: ThirdwebContract) { + return read( + transaction(contract, { + address: contract.address, + chainId: contract.chainId, + method: "function decimals() view returns (uint8)", + }), + ); +} diff --git a/packages/thirdweb/src/extensions/erc20/mintTo.ts b/packages/thirdweb/src/extensions/erc20/mintTo.ts new file mode 100644 index 00000000000..9ee626554de --- /dev/null +++ b/packages/thirdweb/src/extensions/erc20/mintTo.ts @@ -0,0 +1,33 @@ +import { transaction } from "../../transaction/index.js"; +import type { ThirdwebContract } from "../../contract/index.js"; + +type MintToParams = { to: string } & ( + | { + amount: number; + } + | { + amountGwei: bigint; + } +); + +export function mintTo(contract: ThirdwebContract, options: MintToParams) { + return transaction(contract, { + address: contract.address, + chainId: contract.chainId, + method: "function mintTo(address to, uint256 amount)", + params: async () => { + let amount: bigint; + if ("amount" in options) { + // if we need to parse the amount from ether to gwei then we pull in the decimals extension + const { decimals } = await import("./decimals.js"); + // if this fails we fall back to `18` decimals + const d = await decimals(contract).catch(() => 18); + // turn ether into gwei + amount = BigInt(options.amount) * 10n ** BigInt(d); + } else { + amount = options.amountGwei; + } + return [options.to, amount] as const; + }, + }); +} diff --git a/packages/thirdweb/src/transaction/index.ts b/packages/thirdweb/src/transaction/index.ts index 7457467aaa8..fd7e05c52bf 100644 --- a/packages/thirdweb/src/transaction/index.ts +++ b/packages/thirdweb/src/transaction/index.ts @@ -46,12 +46,9 @@ export type Transaction = { export function transaction< method extends string, - abi extends AbiFunction, - parsedAbi extends AbiFunction = abi extends AbiFunction - ? abi - : method extends `function ${string}` - ? ParseMethod - : AbiFunction, + abi extends AbiFunction = method extends `function ${string}` + ? ParseMethod + : AbiFunction, >( client: ThirdwebClient, options: TransactionOptions & GetContractOptions, @@ -70,7 +67,7 @@ export function transaction< try { const abiItem = parseAbiItem(options.method as string); if (abiItem.type === "function") { - return abiItem as parsedAbi; + return abiItem as abi; } throw new Error(`could not find function with name ${options.method}`); } catch (e) {} @@ -84,7 +81,7 @@ export function transaction< } // if the item is a function we can compare the name return item.name === options.method; - }) as parsedAbi | undefined; + }) as abi | undefined; if (!abiFunction) { throw new Error(`could not find function with name ${options.method}`); @@ -93,5 +90,5 @@ export function transaction< }), _encoded: null, transactionHash: null, - } as Transaction; + } as Transaction; }