Skip to content

Commit

Permalink
Merge pull request #37 from 0xClouds/viem
Browse files Browse the repository at this point in the history
Viem Example
  • Loading branch information
kevinchen-cb authored Nov 25, 2024
2 parents a4ec368 + 927a121 commit b48bacd
Show file tree
Hide file tree
Showing 8 changed files with 1,980 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/viem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Getting Started

This example uses viem and viem's Account Abstraction extension [viem-aa](https://viem.sh/account-abstraction) to create a [ERC-4337](https://www.erc4337.io/) smart contract account and send a sponsored transaction.

We'll be minting a NFT on Base Sepolia using Coinbase Developer Platform's Paymaster & Bundler.

### 1. Setup

Ensure you have the `rpc_url` and `private_key` variables set in the `config.json`.

### 2. Install dependencies

```
yarn
```

### 3. Run the example

```
yarn dev
```

### 4. See your sponsored transaction live!

You should receive an Etherscan link with your sponsored transaction in the terminal output. [Example](https://sepolia.basescan.org/tx/0xe51e9bf6fea0dfecfcbf7168bcc7da2c833ad0dcac5651940953a89857674885)

```
✅ Transaction successfully sponsored!"
⛽ Successfully sponsored gas for mintTo transaction with Coinbase Developer Platform!
🔍 View on Etherscan: https://sepolia.basescan.org/tx/0xe51e9bf6fea0dfecfcbf7168bcc7da2c833ad0dcac5651940953a89857674885
✨ Done in 5.66s.
```
18 changes: 18 additions & 0 deletions examples/viem/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "wagmi",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "node src/mint"
},
"author": "",
"license": "ISC",
"dependencies": {
"npm": "^10.9.0",
"typescript": "^5.6.3",
"viem": "2.x"
}
}
26 changes: 26 additions & 0 deletions examples/viem/src/abi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const abi = [
{
inputs: [
{
internalType: "address",
name: "recipient",
type: "address",
},
{
internalType: "uint16",
name: "item",
type: "uint16",
},
],
name: "mintTo",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "payable",
type: "function",
},
];
10 changes: 10 additions & 0 deletions examples/viem/src/account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { toCoinbaseSmartAccount } from "viem/account-abstraction";
import { privateKeyToAccount } from "viem/accounts";
import { client } from "./viem_client.js ";
import config from "../../../config.js";

const owner = privateKeyToAccount(config.private_key);
export const account = await toCoinbaseSmartAccount({
client,
owners: [owner],
});
46 changes: 46 additions & 0 deletions examples/viem/src/mint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { bundlerClient } from "./paymaster.js";
import { account } from "./account.js";
import { abi } from "./abi.js";
import config from "../../../config.js";
const nftContractAddress = config.contract_address;

account.userOperation = {
estimateGas: async (userOperation) => {
const estimate = await bundlerClient.estimateUserOperationGas(
userOperation
);
estimate.preVerificationGas = estimate.preVerificationGas * 2n;
return estimate;
},
};

try {
const userOpHash = await bundlerClient.sendUserOperation({
account,
calls: [
{
abi: abi,
functionName: "mintTo",
to: nftContractAddress,
args: [account.address, 1],
},
],
paymaster: true,
});

const receipt = await bundlerClient.waitForUserOperationReceipt({
hash: userOpHash,
});

console.log("✅ Transaction successfully sponsored!");
console.log(
`⛽ View sponsored UserOperation on blockscout: https://base-sepolia.blockscout.com/op/${receipt.userOpHash}`
);
console.log(
`🔍 View NFT mint on basescan: https://sepolia.basescan.org/address/${account.address}`
);
process.exit();
} catch (error) {
console.log("Error sending trasnaction: ", error);
process.exit(1);
}
14 changes: 14 additions & 0 deletions examples/viem/src/paymaster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { http } from "viem";
import { baseSepolia } from "viem/chains";
import { createBundlerClient } from "viem/account-abstraction";
import { account } from "./account.js";
import config from "../../../config.js";
import { abi } from "./abi.js";
import { client } from "./viem_client.js";

export const bundlerClient = createBundlerClient({
account,
client,
transport: http(config.rpc_url),
chain: baseSepolia,
});
8 changes: 8 additions & 0 deletions examples/viem/src/viem_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import config from "../../../config.js";
import { createPublicClient, http } from "viem";
import { baseSepolia } from "viem/chains";

export const client = createPublicClient({
chain: baseSepolia,
transport: http(config.rpc_url),
});
Loading

0 comments on commit b48bacd

Please sign in to comment.