Skip to content

Commit

Permalink
Merge pull-request #422
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Dec 2, 2024
2 parents f8eb9d7 + a2d880e commit 6bc2c7a
Show file tree
Hide file tree
Showing 8 changed files with 866 additions and 202 deletions.
18 changes: 18 additions & 0 deletions examples/with-biconomy-aa/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# @turnkey/with-biconomy-aa

## 0.2.0

- Add support for Nexus (latest offering from Biconomy) with Turnkey and Viem.
- Use v2 of jiffyscan everywhere
- Several resources to learn more about Nexus here:
- https://github.com/bcnmy/nexus/wiki
- https://docs.biconomy.io/nexus-client
- https://docs.biconomy.io/overview#what-is-nexus
- https://docs.biconomy.io/quickstart#sending-your-first-transaction-with-nexus-sdk-%EF%B8%8F
- https://docs.biconomy.io/tutorials/gasless#set-up-nexus-client-with-paymaster

## 0.1.0

Initial release

- Add integration between Turnkey, Biconomy smart accounts, and both Viem and Ethers.
60 changes: 58 additions & 2 deletions examples/with-biconomy-aa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,67 @@ Sent 0.0000001 Ether to 0x08d2b0a37F869FF76BACB5Bab3278E26ab7067B7:
https://sepolia.etherscan.io/tx/0x2f2d996d6b262ebf0263b432ca3e6d621ba42d60b92344f31cf3ed94d09f49c4
User Ops can be found here:
https://jiffyscan.xyz/bundle/0x2f2d996d6b262ebf0263b432ca3e6d621ba42d60b92344f31cf3ed94d09f49c4?network=sepolia&pageNo=0&pageSize=10
https://v2.jiffyscan.xyz/tx/0x2f2d996d6b262ebf0263b432ca3e6d621ba42d60b92344f31cf3ed94d09f49c4?network=sepolia&pageNo=0&pageSize=10
```

#### Viem + Nexus

```bash
$ pnpm start-viem-nexus
```

This script will do the following:

1. instantiate a Turnkey Viem wallet client
2. instantiate a Viem public client (to be used to fetch onchain data)
3. connect the wallet client to Biconomy Nexus
4. send ETH (via type 2 EIP-1559 transaction)

Note: this script must be used specifically with Base's Sepolia testnet.

See the following for a sample output:

Network:
base-sepolia (chain ID 84532)

Signer address:
0xDC608F098255C89B36da905D9132A9Ee3DD266D9

Smart wallet address:
0xC00f5dCe7E266553cb3FBCd0BeDD11e290D551Fd

Balance:
0.009998258337028736 Ether

Transaction count:
1

Nonce:
106650284489225899116705470776299986053547585411730459514530106938958242906112

✔ Amount to send (wei). Default to 0.0000001 ETH … 100000000000
✔ Destination address (default to TKHQ warchest) … 0x08d2b0a37F869FF76BACB5Bab3278E26ab7067B7
Sent 0.0000001 Ether to 0x08d2b0a37F869FF76BACB5Bab3278E26ab7067B7:
https://sepolia.basescan.org/tx/0x453781ffcf1b4f5f98952347bcef14a0e7d6cbf7f9992b2a31163f67278625c6

User Ops can be found here:
https://v2.jiffyscan.xyz/tx/0x453781ffcf1b4f5f98952347bcef14a0e7d6cbf7f9992b2a31163f67278625c6?network=base-sepolia&pageNo=0&pageSize=10

#### Ethers

```bash
$ pnpm start-ethers
```

This script will do the following:

1. instantiate a Turnkey Ethers wallet client
2. instantiate a Ethers provider (to be used to fetch onchain data)
3. connect the wallet client to the Biconomy paymaster
4. send ETH (via type 2 EIP-1559 transaction)

See the following for a sample output:

```
Network:
sepolia (chain ID 11155111)
Expand All @@ -127,5 +183,5 @@ Sent 0.0000001 Ether to 0x08d2b0a37F869FF76BACB5Bab3278E26ab7067B7:
https://sepolia.etherscan.io/tx/0x0f0d5346ba726f7ccf80142ae295f28bf3873b0aeb7b29488b1e3dfb949d5ba6
User Ops can be found here:
https://jiffyscan.xyz/bundle/0x0f0d5346ba726f7ccf80142ae295f28bf3873b0aeb7b29488b1e3dfb949d5ba6?network=sepolia&pageNo=0&pageSize=10
https://v2.jiffyscan.xyz/tx/0x0f0d5346ba726f7ccf80142ae295f28bf3873b0aeb7b29488b1e3dfb949d5ba6?network=sepolia&pageNo=0&pageSize=10
```
5 changes: 4 additions & 1 deletion examples/with-biconomy-aa/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{
"name": "@turnkey/example-with-biconomy-aa",
"version": "0.1.0",
"version": "0.2.0",
"private": true,
"scripts": {
"start-ethers": "tsx src/ethers.ts",
"start-viem": "tsx src/viem.ts",
"start-nexus-viem": "tsx src/nexus-viem.ts",
"clean": "rimraf ./dist ./.cache",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@biconomy/account": "^4.5.6",
"@biconomy/sdk": "^0.0.10",
"@rhinestone/module-sdk": "^0.1.28",
"@turnkey/ethers": "workspace:*",
"@turnkey/sdk-server": "workspace:*",
"@turnkey/viem": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion examples/with-biconomy-aa/src/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async function main() {

print(
`User Ops can be found here:`,
`https://jiffyscan.xyz/bundle/${transactionHash}?network=${network}&pageNo=0&pageSize=10`
`https://v2.jiffyscan.xyz/tx/${transactionHash}?network=${network}&pageNo=0&pageSize=10`
);
}

Expand Down
166 changes: 166 additions & 0 deletions examples/with-biconomy-aa/src/nexus-viem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import * as path from "path";
import * as dotenv from "dotenv";
import prompts, { PromptType } from "prompts";
import {
createWalletClient,
createPublicClient,
http,
type Account,
formatEther,
} from "viem";
import { baseSepolia } from "viem/chains";
import { createNexusClient, createBicoPaymasterClient } from "@biconomy/sdk";

// Load environment variables from `.env.local`
dotenv.config({ path: path.resolve(process.cwd(), ".env.local") });

import { createAccount } from "@turnkey/viem";
import { Turnkey as TurnkeyServerSDK } from "@turnkey/sdk-server";
import { createNewWallet } from "./createNewWallet";
import { print } from "./util";

async function main() {
if (!process.env.SIGN_WITH) {
// If you don't specify a `SIGN_WITH`, we'll create a new wallet for you via calling the Turnkey API.
await createNewWallet();
return;
}

const turnkeyClient = new TurnkeyServerSDK({
apiBaseUrl: process.env.BASE_URL!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
apiPublicKey: process.env.API_PUBLIC_KEY!,
defaultOrganizationId: process.env.ORGANIZATION_ID!,
});

// Initialize a Turnkey-powered Viem Account
const turnkeyAccount = await createAccount({
client: turnkeyClient.apiClient(),
organizationId: process.env.ORGANIZATION_ID!,
signWith: process.env.SIGN_WITH!,
});

// Network must be base-sepolia at the moment
const network = "base-sepolia";
const bundlerUrl =
"https://bundler.biconomy.io/api/v3/84532/nJPK7B3ru.dd7f7861-190d-41bd-af80-6877f74b8f44";
const paymasterUrl =
"https://paymaster.biconomy.io/api/v2/84532/F7wyL1clz.75a64804-3e97-41fa-ba1e-33e98c2cc703";
const providerUrl = `https://${network}.infura.io/v3/${process.env
.INFURA_KEY!}`;

// Bring your own provider
const client = createWalletClient({
account: turnkeyAccount as Account,
chain: baseSepolia,
transport: http(providerUrl),
});

const publicClient = createPublicClient({
chain: baseSepolia,
transport: http(providerUrl),
});

const nexusClient = await createNexusClient({
signer: turnkeyAccount,
chain: baseSepolia,
transport: http(),
bundlerTransport: http(bundlerUrl),
paymaster: createBicoPaymasterClient({
paymasterUrl,
}),
});

const smartAccount = nexusClient.account;
const smartAccountAddress = nexusClient.account.address;

const chainId = client.chain.id;
const signerAddress = client.account.address; // signer

const transactionCount = await publicClient.getTransactionCount({
address: smartAccountAddress,
});
const nonce = await smartAccount.getNonce();
let balance =
(await publicClient.getBalance({ address: smartAccountAddress })) ?? 0;

print("Network:", `${network} (chain ID ${chainId})`);
print("Signer address:", signerAddress);
print("Smart wallet address:", smartAccountAddress);
print("Balance:", `${formatEther(balance)} Ether`);
print("Transaction count:", `${transactionCount}`);
print("Nonce:", `${nonce}`);

while (balance === 0n) {
console.log(
[
`\n💸 Your onchain balance is at 0! To continue this demo you'll need testnet funds! You can use:`,
`- Any online faucet (e.g. https://www.alchemy.com/faucets/)`,
`\nTo check your balance: https://${network}.etherscan.io/address/${smartAccountAddress}`,
`\n--------`,
].join("\n")
);

const { continue: _ } = await prompts([
{
type: "text" as PromptType,
name: "continue",
message: "Ready to continue? y/n",
initial: "y",
},
]);

balance = await publicClient.getBalance({
address: smartAccountAddress,
})!;
}

const { amount, destination } = await prompts([
{
type: "number" as PromptType,
name: "amount",
message: "Amount to send (wei). Default to 0.0000001 ETH",
initial: 100000000000,
},
{
type: "text" as PromptType,
name: "destination",
message: "Destination address (default to TKHQ warchest)",
initial: "0x08d2b0a37F869FF76BACB5Bab3278E26ab7067B7",
},
]);
const transactionRequest = {
to: destination,
value: amount,
type: 2,
};

const hash = await nexusClient.sendTransaction({
calls: [
{
to: destination,
value: amount,
},
],
});
const { transactionHash } = await nexusClient.waitForTransactionReceipt({
hash,
});

print(
`Sent ${formatEther(transactionRequest.value)} Ether to ${
transactionRequest.to
}:`,
`https://sepolia.basescan.org/tx/${transactionHash}`
);

print(
`User Ops can be found here:`,
`https://v2.jiffyscan.xyz/tx/${transactionHash}?network=${network}&pageNo=0&pageSize=10`
);
}

main().catch((error) => {
console.error(error);
process.exit(1);
});
2 changes: 1 addition & 1 deletion examples/with-biconomy-aa/src/viem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async function main() {

print(
`User Ops can be found here:`,
`https://jiffyscan.xyz/bundle/${transactionHash}?network=${network}&pageNo=0&pageSize=10`
`https://v2.jiffyscan.xyz/tx/${transactionHash}?network=${network}&pageNo=0&pageSize=10`
);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/with-eth-passkeys-galore/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export default function Home() {
const { transactionHash } = await userOpResponse.waitForTxHash();

setSignedTransaction(
`https://jiffyscan.xyz/bundle/${transactionHash}?network=sepolia&pageNo=0&pageSize=10`
`https://v2.jiffyscan.xyz/tx/${transactionHash}?network=sepolia&pageNo=0&pageSize=10`
);
};

Expand Down
Loading

0 comments on commit 6bc2c7a

Please sign in to comment.