Skip to content

Commit

Permalink
initial erc20 extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsdls committed Jan 18, 2024
1 parent fccf278 commit d92cbc7
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 9 deletions.
8 changes: 8 additions & 0 deletions packages/thirdweb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -43,6 +48,9 @@
],
"wallets/*": [
"./types/wallets/*.d.ts"
],
"extensions/*": [
"./types/extensions/*.d.ts"
]
}
},
Expand Down
5 changes: 5 additions & 0 deletions packages/thirdweb/src/extensions/erc20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// read
export { balanceOf } from "./erc20/balanceOf.js";
export { decimals } from "./erc20/decimals.js";
// write
export { mintTo } from "./erc20/mintTo.js";
19 changes: 19 additions & 0 deletions packages/thirdweb/src/extensions/erc20/balanceOf.ts
Original file line number Diff line number Diff line change
@@ -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],
}),
);
}
13 changes: 13 additions & 0 deletions packages/thirdweb/src/extensions/erc20/decimals.ts
Original file line number Diff line number Diff line change
@@ -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)",
}),
);
}
33 changes: 33 additions & 0 deletions packages/thirdweb/src/extensions/erc20/mintTo.ts
Original file line number Diff line number Diff line change
@@ -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;
},
});
}
15 changes: 6 additions & 9 deletions packages/thirdweb/src/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ export type Transaction<abiFn extends AbiFunction> = {

export function transaction<
method extends string,
abi extends AbiFunction,
parsedAbi extends AbiFunction = abi extends AbiFunction
? abi
: method extends `function ${string}`
? ParseMethod<method>
: AbiFunction,
abi extends AbiFunction = method extends `function ${string}`
? ParseMethod<method>
: AbiFunction,
>(
client: ThirdwebClient,
options: TransactionOptions<method, abi> & GetContractOptions,
Expand All @@ -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) {}
Expand All @@ -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}`);
Expand All @@ -93,5 +90,5 @@ export function transaction<
}),
_encoded: null,
transactionHash: null,
} as Transaction<parsedAbi>;
} as Transaction<abi>;
}

0 comments on commit d92cbc7

Please sign in to comment.