Skip to content

Commit

Permalink
feat: Add example code for interacting with a contract
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsdls committed Jan 18, 2024
1 parent d92cbc7 commit 8defe93
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions packages/thirdweb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,65 @@ npm install thirdweb@alpha

### Backend (Node, Bun, Deno, etc)

#### With Extensions

```ts
import { createClient } from "thirdweb";
import { privateKeyWallet } from "thirdweb/wallets/private-key";
import { balanceOf, mintTo } from "thirdweb/extensions/erc20";

// Step 1: create a client
const client = createClient({
// create a secret key at https://thirdweb.com/dashboard
secretKey: process.env.SECRET_KEY as string,
});

// Step 2: define a contract to interact with
const contract = client.contract({
// the contract address
address: "0xBCfaB342b73E08858Ce927b1a3e3903Ddd203980",
// the chainId of the chain the contract is deployed on
chainId: 5,
});

// Step 3: read contract state
const balance = await balanceOf(contract, {
address: "0x0890C23024089675D072E984f28A93bb391a35Ab",
});

console.log("beginning balance", balance);

// Step 4: initialize a wallet
const wallet = privateKeyWallet(client);

await wallet.connect({ pkey: process.env.PRIVATE_KEY as string });

// Step 5: create a transaction
const tx = mintTo(contract, {
to: "0x0890C23024089675D072E984f28A93bb391a35Ab",
amount: 100,
});

// Step 6: execute the transaction with the wallet
const receipt = await wallet.sendTransaction(tx);

console.log("tx hash", receipt.transactionHash);

// Step 7: wait for the receipt to be mined
const txReceipt = await receipt.wait();

console.log(txReceipt);

// Step 8: read contract state
const newBalance = await balanceOf(contract, {
address: "0x0890C23024089675D072E984f28A93bb391a35Ab",
});

console.log("ending balance", newBalance);
```

#### Without Extensions

```ts
import { createClient } from "thirdweb";
import { privateKeyWallet } from "thirdweb/wallets/private-key";
Expand Down

0 comments on commit 8defe93

Please sign in to comment.