-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from 0xClouds/viem
Viem Example
- Loading branch information
Showing
8 changed files
with
1,980 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); |
Oops, something went wrong.