-
Notifications
You must be signed in to change notification settings - Fork 1
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 #50 from etherspot/ft/turnkey
feat: adding example scripts for turnkey integration
- Loading branch information
Showing
3 changed files
with
152 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,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()); |
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,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()); |
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,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()); |