Skip to content

Commit

Permalink
Merge pull request #50 from etherspot/ft/turnkey
Browse files Browse the repository at this point in the history
feat: adding example scripts for turnkey integration
  • Loading branch information
nikhilkumar1612 authored Nov 30, 2024
2 parents 26cbce1 + 0407983 commit 3568735
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
28 changes: 28 additions & 0 deletions examples/turnkey/create-wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { DEFAULT_ETHEREUM_ACCOUNTS, Turnkey } from '@turnkey/sdk-server';

const turnkey = new Turnkey({
apiBaseUrl: "https://api.turnkey.com",
apiPrivateKey: "", // turnkey api private key
apiPublicKey: "", // turnkey api public key
defaultOrganizationId: "", // default organization id
});

const apiClient = turnkey.apiClient();

async function main() {
const walletResponse = await apiClient.createWallet({
walletName: 'Example Wallet 1',
accounts: DEFAULT_ETHEREUM_ACCOUNTS,
});

const walletId = walletResponse.walletId;
const accountAddress = walletResponse.addresses[0];

console.log('\x1b[33m%s\x1b[0m', 'walletId: ', walletId);
console.log('\x1b[33m%s\x1b[0m', 'accountAddress: ', accountAddress);
return;
}

main()
.catch(console.error)
.finally(() => process.exit());
49 changes: 49 additions & 0 deletions examples/turnkey/get-counterfactual-address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { TurnkeyClient } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/sdk-server";
import { createAccount } from "@turnkey/viem";
import { EtherspotBundler, ModularSdk } from "../../src";
import { createWalletClient, http } from "viem";
import { polygonAmoy } from "viem/chains";

// example script to get etherspot smart wallet address using a turnkey client.
async function main() {
const turnkeyClient = new TurnkeyClient(
{ baseUrl: "https://api.turnkey.com" },
new ApiKeyStamper({
apiPrivateKey: "", // turnkey api private key
apiPublicKey: "" // turnkey api public key
})
);

const turnkeyAccount = await createAccount({
client: turnkeyClient,
organizationId: "", // turnkey organization id
signWith: "", // wallet address created using ./create-wallet.ts
});

const walletClient = createWalletClient({
transport: http("https://testnet-rpc.etherspot.io/v2/80002"),
account: turnkeyAccount,
chain: polygonAmoy
});

const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9';

const modularSdk = new ModularSdk(
walletClient,
{
chainId: 80002,
bundlerProvider: new EtherspotBundler(
80002,
bundlerApiKey,
)
}
);

const address = await modularSdk.getCounterFactualAddress();
console.log('\x1b[33m%s\x1b[0m', `EtherspotWallet address: ${address}`);
}

main()
.catch(console.error)
.finally(() => process.exit());
75 changes: 75 additions & 0 deletions examples/turnkey/transfer-funds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { TurnkeyClient } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/sdk-server";
import { createAccount } from "@turnkey/viem";
import { EtherspotBundler, ModularSdk } from "../../src";
import { createWalletClient, http, parseEther } from "viem";
import { polygonAmoy } from "viem/chains";

async function main() {
const turnkeyClient = new TurnkeyClient(
{ baseUrl: "https://api.turnkey.com" },
new ApiKeyStamper({
apiPrivateKey: "", // turnkey api private key
apiPublicKey: "" // turnkey api public key
})
);

const turnkeyAccount = await createAccount({
client: turnkeyClient,
organizationId: "", // turnkey organization id
signWith: "", // wallet address created using ./create-wallet.ts
});

const walletClient = createWalletClient({
transport: http("https://testnet-rpc.etherspot.io/v2/80002"),
account: turnkeyAccount,
chain: polygonAmoy
});

const bundlerApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjMxMDZiOGY2NTRhZTRhZTM4MGVjYjJiN2Q2NDMzMjM4IiwiaCI6Im11cm11cjEyOCJ9';

const modularSdk = new ModularSdk(
walletClient,
{
chainId: 80002,
bundlerProvider: new EtherspotBundler(
80002,
bundlerApiKey,
)
}
);

const recipient = '0x09FD4F6088f2025427AB1e89257A44747081Ed59'; // recipient wallet address
const value = '0.0000001'; // transfer value

await modularSdk.clearUserOpsFromBatch();


// add transactions to the batch
const transactionBatch = await modularSdk.addUserOpsToBatch({ to: recipient, value: parseEther(value) });
console.log('transactions: ', transactionBatch);

// get balance of the account address
const balance = await modularSdk.getNativeBalance();
console.log('balances: ', balance);

// estimate transactions added to the batch and get the fee data for the UserOp
const op = await modularSdk.estimate();
console.log(`Estimate UserOp:`, op);

// sign the UserOp and sending to the bundler...
const uoHash = await modularSdk.send(op);
console.log(`UserOpHash: ${uoHash}`);

// get transaction hash...
console.log('Waiting for transaction...');
let userOpsReceipt = null;
const timeout = Date.now() + 1200000; // 1 minute timeout
while ((userOpsReceipt == null) && (Date.now() < timeout)) {
userOpsReceipt = await modularSdk.getUserOpReceipt(uoHash);
}
console.log('\x1b[33m%s\x1b[0m', `Transaction Receipt: `, userOpsReceipt);
}
main()
.catch(console.error)
.finally(() => process.exit());

0 comments on commit 3568735

Please sign in to comment.