Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofgazso committed Jul 12, 2023
0 parents commit 19d4107
Show file tree
Hide file tree
Showing 7 changed files with 1,371 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Pimlico

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Pimlico Tutorial 1

This repository contains the full code for [tutorial 1](https://docs.pimlico.io/tutorial/tutorial-1) in the Pimlico documentation.

To set up the repository, clone it, replace the `pimlicoApiKey` variable in `index.ts` with your Pimlico API key (use the [quick start guide](https://docs.pimlico.io/how-to/quick-start) to generate one), install the dependencies, and run `npm start`!

```bash
npm install
npm start
```

If everything works correctly, you should deploy a User Operation as per the flow of Tutorial 1.

Good luck!
137 changes: 137 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import {
SimpleAccountFactory__factory,
EntryPoint__factory,
SimpleAccount__factory,
EntryPoint,
UserOperationStruct
} from "@account-abstraction/contracts"
import { Provider, StaticJsonRpcProvider } from "@ethersproject/providers"
import { BigNumber, Wallet, constants, utils } from "ethers"
import { ERC20, ERC20__factory } from "@pimlico/erc20-paymaster/contracts"
import { getERC20Paymaster } from "@pimlico/erc20-paymaster"

const apiKey = "YOUR_PIMLICO_API_KEY" // REPLACE THIS
// You can get an API key by signing up at https://dashboard.pimlico.io

// GENERATE THE INITCODE
const SIMPLE_ACCOUNT_FACTORY_ADDRESS = "0x9406Cc6185a346906296840746125a0E44976454"
const lineaProvider = new StaticJsonRpcProvider("https://rpc.goerli.linea.build/")
const owner = Wallet.createRandom()
console.log("Generated wallet with private key:", owner.privateKey)

const simpleAccountFactory = SimpleAccountFactory__factory.connect(
SIMPLE_ACCOUNT_FACTORY_ADDRESS,
lineaProvider,
)
const initCode = utils.hexConcat([
SIMPLE_ACCOUNT_FACTORY_ADDRESS,
simpleAccountFactory.interface.encodeFunctionData("createAccount", [owner.address, 0]),
])

console.log("Generated initCode:", initCode)

// CALCULATE THE SENDER ADDRESS
const ENTRY_POINT_ADDRESS = "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789"

const entryPoint = EntryPoint__factory.connect(
ENTRY_POINT_ADDRESS,
lineaProvider,
)

const senderAddress = await entryPoint.callStatic.getSenderAddress(initCode)
.then(() => {
throw new Error("Expected getSenderAddress() to revert");
})
.catch((e) => {
const data = e.message.match(/0x6ca7b806([a-fA-F\d]*)/)?.[1];
if (!data) {
return Promise.reject(new Error("Failed to parse revert data"));
}
const addr = utils.getAddress(`0x${data.slice(24, 64)}`);
return Promise.resolve(addr);
})

console.log("Calculated sender address:", senderAddress)

// GENERATE THE CALLDATA
const to = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045" // vitalik
const value = 0
const data = "0x68656c6c6f" // "hello" encoded to utf-8 bytes

const simpleAccount = SimpleAccount__factory.connect(
senderAddress,
lineaProvider,
)

const callData = simpleAccount.interface.encodeFunctionData("execute", [to, value, data])

console.log("Generated callData:", callData)

// FILL OUT REMAINING USER OPERATION VALUES
const gasPrice = await lineaProvider.getGasPrice()

const userOperation = {
sender: senderAddress,
nonce: utils.hexlify(0),
initCode,
callData,
callGasLimit: utils.hexlify(100_000), // hardcode it for now at a high value
verificationGasLimit: utils.hexlify(400_000), // hardcode it for now at a high value
preVerificationGas: utils.hexlify(50_000), // hardcode it for now at a high value
maxFeePerGas: utils.hexlify(gasPrice),
maxPriorityFeePerGas: utils.hexlify(gasPrice),
paymasterAndData: "0x",
signature: "0x"
}

// REQUEST PIMLICO VERIFYING PAYMASTER SPONSORSHIP
const chain = "linea-testnet" // find the list of chain names on the Pimlico verifying paymaster reference page

const pimlicoEndpoint = `https://api.pimlico.io/v1/${chain}/rpc?apikey=${apiKey}`

const pimlicoProvider = new StaticJsonRpcProvider(pimlicoEndpoint)

const sponsorUserOperationResult = await pimlicoProvider.send("pm_sponsorUserOperation", [
userOperation,
{
entryPoint: ENTRY_POINT_ADDRESS,
},
])

const paymasterAndData = sponsorUserOperationResult.paymasterAndData

userOperation.paymasterAndData = paymasterAndData

console.log("Pimlico paymasterAndData:", paymasterAndData)

// SIGN THE USER OPERATION
const signature = await owner.signMessage(
utils.arrayify(await entryPoint.getUserOpHash(userOperation)),
)

userOperation.signature = signature

console.log("UserOperation signature:", signature)

// SUBMIT THE USER OPERATION TO BE BUNDLED
const userOperationHash = await pimlicoProvider.send("eth_sendUserOperation", [
userOperation,
ENTRY_POINT_ADDRESS
])

console.log("UserOperation hash:", userOperationHash)

// let's also wait for the userOperation to be included, by continually querying for the receipts
console.log("Querying for receipts...")
let receipt = null
while (receipt === null) {
await new Promise((resolve) => setTimeout(resolve, 1000))
receipt = await pimlicoProvider.send("eth_getUserOperationReceipt", [
userOperationHash,
]);
console.log(receipt === null ? "Still waiting..." : receipt)
}

const txHash = receipt.receipt.transactionHash

console.log(`UserOperation included: https://goerli.lineascan.build/tx/${txHash}`)
Loading

0 comments on commit 19d4107

Please sign in to comment.