From c8782375f74f45d30d9dae786df4e2a24c30f647 Mon Sep 17 00:00:00 2001 From: Andrew Min Date: Thu, 7 Sep 2023 14:27:14 -0400 Subject: [PATCH] update examples --- examples/deployer/src/index.ts | 24 +++++++--- examples/rebalancer/src/provider.ts | 22 ++++++--- examples/sweeper/package.json | 1 + examples/sweeper/src/provider.ts | 30 ++++++++---- examples/trading-runner/src/provider.ts | 28 +++++++---- examples/with-ethers/src/index.ts | 24 +++++++--- examples/with-ethers/src/legacySepolia.ts | 24 +++++++--- examples/with-gnosis/src/index.ts | 48 +++++++++++-------- .../src/managedOptimistic.ts | 24 +++++++--- .../src/simpleSequential.ts | 24 +++++++--- examples/with-uniswap/package.json | 1 + examples/with-uniswap/src/provider.ts | 44 ++++++++++------- pnpm-lock.yaml | 6 +++ 13 files changed, 202 insertions(+), 98 deletions(-) diff --git a/examples/deployer/src/index.ts b/examples/deployer/src/index.ts index 882433a54..6c21494bf 100644 --- a/examples/deployer/src/index.ts +++ b/examples/deployer/src/index.ts @@ -6,6 +6,8 @@ dotenv.config({ path: path.resolve(process.cwd(), ".env.local") }); import { TurnkeySigner } from "@turnkey/ethers"; import { ethers } from "ethers"; +import { TurnkeyClient } from "@turnkey/http"; +import { ApiKeyStamper } from "@turnkey/api-key-stamper"; import { createNewEthereumPrivateKey } from "./createNewEthereumPrivateKey"; import compile from "./compile"; @@ -16,14 +18,22 @@ async function main() { return; } + const turnkeyClient = new TurnkeyClient( + { + baseUrl: process.env.BASE_URL!, + }, + new ApiKeyStamper({ + apiPublicKey: process.env.API_PUBLIC_KEY!, + apiPrivateKey: process.env.API_PRIVATE_KEY!, + }) + ); + // Initialize a Turnkey Signer - const turnkeySigner = new TurnkeySigner({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - baseUrl: process.env.BASE_URL!, - organizationId: process.env.ORGANIZATION_ID!, - privateKeyId: process.env.PRIVATE_KEY_ID!, - }); + const turnkeySigner = new TurnkeySigner( + turnkeyClient, + process.env.ORGANIZATION_ID!, + process.env.PRIVATE_KEY_ID!, + ); // Connect it with a Provider (https://docs.ethers.org/v5/api/providers/) const network = "goerli"; diff --git a/examples/rebalancer/src/provider.ts b/examples/rebalancer/src/provider.ts index e671f43c0..d98c3ca90 100644 --- a/examples/rebalancer/src/provider.ts +++ b/examples/rebalancer/src/provider.ts @@ -2,6 +2,8 @@ import * as path from "path"; import * as dotenv from "dotenv"; import { ethers } from "ethers"; import { TurnkeySigner } from "@turnkey/ethers"; +import { TurnkeyClient } from "@turnkey/http"; +import { ApiKeyStamper } from "@turnkey/api-key-stamper"; import { Environment } from "./utils"; const DEFAULT_INFURA_COMMUNITY_KEY = "84842078b09946638c03157f83405213"; @@ -34,14 +36,22 @@ export function getTurnkeySigner( provider: ethers.providers.Provider, privateKeyId: string ): TurnkeySigner { + const turnkeyClient = new TurnkeyClient( + { + baseUrl: process.env.BASE_URL!, + }, + new ApiKeyStamper({ + apiPublicKey: process.env.API_PUBLIC_KEY!, + apiPrivateKey: process.env.API_PRIVATE_KEY!, + }) + ); + // Initialize a Turnkey Signer - const turnkeySigner = new TurnkeySigner({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - baseUrl: process.env.BASE_URL!, - organizationId: process.env.ORGANIZATION_ID!, + const turnkeySigner = new TurnkeySigner( + turnkeyClient, + process.env.ORGANIZATION_ID!, privateKeyId, - }); + ); return turnkeySigner.connect(provider); } diff --git a/examples/sweeper/package.json b/examples/sweeper/package.json index 4c64db38f..7e856a318 100644 --- a/examples/sweeper/package.json +++ b/examples/sweeper/package.json @@ -10,6 +10,7 @@ "dependencies": { "@turnkey/ethers": "workspace:*", "@turnkey/http": "workspace:*", + "@turnkey/api-key-stamper": "workspace:*", "@uniswap/sdk-core": "^3.1.1", "dotenv": "^16.0.3", "ethers": "^5.7.2", diff --git a/examples/sweeper/src/provider.ts b/examples/sweeper/src/provider.ts index 16429c9c0..8d466ac75 100644 --- a/examples/sweeper/src/provider.ts +++ b/examples/sweeper/src/provider.ts @@ -2,6 +2,8 @@ import * as path from "path"; import * as dotenv from "dotenv"; import { ethers } from "ethers"; import { TurnkeySigner } from "@turnkey/ethers"; +import { TurnkeyClient } from "@turnkey/http"; +import { ApiKeyStamper } from "@turnkey/api-key-stamper"; import { Environment } from "./utils"; const DEFAULT_INFURA_COMMUNITY_KEY = "84842078b09946638c03157f83405213"; @@ -28,19 +30,27 @@ export function getProvider( return provider; } -// Initialize a Turnkey Signer -const turnkeySigner = new TurnkeySigner({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - baseUrl: process.env.BASE_URL!, - organizationId: process.env.ORGANIZATION_ID!, - privateKeyId: process.env.PRIVATE_KEY_ID!, -}); - // getTurnkeySigner returns a TurnkeySigner connected to the passed-in Provider // (https://docs.ethers.org/v5/api/providers/) export function getTurnkeySigner( - provider: ethers.providers.Provider + provider: ethers.providers.Provider, ): TurnkeySigner { + const turnkeyClient = new TurnkeyClient( + { + baseUrl: process.env.BASE_URL!, + }, + new ApiKeyStamper({ + apiPublicKey: process.env.API_PUBLIC_KEY!, + apiPrivateKey: process.env.API_PRIVATE_KEY!, + }) + ); + + // Initialize a Turnkey Signer + const turnkeySigner = new TurnkeySigner( + turnkeyClient, + process.env.ORGANIZATION_ID!, + process.env.PRIVATE_KEY_ID!, + ); + return turnkeySigner.connect(provider); } diff --git a/examples/trading-runner/src/provider.ts b/examples/trading-runner/src/provider.ts index 7157b4272..d98c3ca90 100644 --- a/examples/trading-runner/src/provider.ts +++ b/examples/trading-runner/src/provider.ts @@ -2,10 +2,12 @@ import * as path from "path"; import * as dotenv from "dotenv"; import { ethers } from "ethers"; import { TurnkeySigner } from "@turnkey/ethers"; +import { TurnkeyClient } from "@turnkey/http"; +import { ApiKeyStamper } from "@turnkey/api-key-stamper"; import { Environment } from "./utils"; const DEFAULT_INFURA_COMMUNITY_KEY = "84842078b09946638c03157f83405213"; -const DEFAULT_ENV = Environment.GOERLI; +const DEFAULT_ENV = Environment.SEPOLIA; // Load environment variables from `.env.local` dotenv.config({ path: path.resolve(process.cwd(), ".env.local") }); @@ -16,9 +18,9 @@ let provider = new ethers.providers.InfuraProvider( ); export function getProvider( - env = Environment.GOERLI + env = Environment.SEPOLIA ): ethers.providers.Provider { - if (env !== Environment.GOERLI) { + if (env !== Environment.SEPOLIA) { provider = new ethers.providers.InfuraProvider( env, process.env.INFURA_KEY || DEFAULT_INFURA_COMMUNITY_KEY @@ -34,14 +36,22 @@ export function getTurnkeySigner( provider: ethers.providers.Provider, privateKeyId: string ): TurnkeySigner { + const turnkeyClient = new TurnkeyClient( + { + baseUrl: process.env.BASE_URL!, + }, + new ApiKeyStamper({ + apiPublicKey: process.env.API_PUBLIC_KEY!, + apiPrivateKey: process.env.API_PRIVATE_KEY!, + }) + ); + // Initialize a Turnkey Signer - const turnkeySigner = new TurnkeySigner({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - baseUrl: process.env.BASE_URL!, - organizationId: process.env.ORGANIZATION_ID!, + const turnkeySigner = new TurnkeySigner( + turnkeyClient, + process.env.ORGANIZATION_ID!, privateKeyId, - }); + ); return turnkeySigner.connect(provider); } diff --git a/examples/with-ethers/src/index.ts b/examples/with-ethers/src/index.ts index a73a4aaec..de363ed07 100644 --- a/examples/with-ethers/src/index.ts +++ b/examples/with-ethers/src/index.ts @@ -6,6 +6,8 @@ dotenv.config({ path: path.resolve(process.cwd(), ".env.local") }); import { TurnkeySigner } from "@turnkey/ethers"; import { ethers } from "ethers"; +import { TurnkeyClient } from "@turnkey/http"; +import { ApiKeyStamper } from "@turnkey/api-key-stamper"; import { createNewEthereumPrivateKey } from "./createNewEthereumPrivateKey"; import WETH_TOKEN_ABI from "./weth-contract-abi.json"; @@ -18,14 +20,22 @@ async function main() { return; } + const turnkeyClient = new TurnkeyClient( + { + baseUrl: process.env.BASE_URL!, + }, + new ApiKeyStamper({ + apiPublicKey: process.env.API_PUBLIC_KEY!, + apiPrivateKey: process.env.API_PRIVATE_KEY!, + }) + ); + // Initialize a Turnkey Signer - const turnkeySigner = new TurnkeySigner({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - baseUrl: process.env.BASE_URL!, - organizationId: process.env.ORGANIZATION_ID!, - privateKeyId: process.env.PRIVATE_KEY_ID!, - }); + const turnkeySigner = new TurnkeySigner( + turnkeyClient, + process.env.ORGANIZATION_ID!, + process.env.PRIVATE_KEY_ID!, + ); // Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v5/api/providers/) const network = "goerli"; diff --git a/examples/with-ethers/src/legacySepolia.ts b/examples/with-ethers/src/legacySepolia.ts index df89a58d8..062cd042c 100644 --- a/examples/with-ethers/src/legacySepolia.ts +++ b/examples/with-ethers/src/legacySepolia.ts @@ -6,6 +6,8 @@ dotenv.config({ path: path.resolve(process.cwd(), ".env.local") }); import { TurnkeySigner } from "@turnkey/ethers"; import { ethers } from "ethers"; +import { TurnkeyClient } from "@turnkey/http"; +import { ApiKeyStamper } from "@turnkey/api-key-stamper"; import { createNewEthereumPrivateKey } from "./createNewEthereumPrivateKey"; import WETH_TOKEN_ABI from "./weth-contract-abi.json"; @@ -18,14 +20,22 @@ async function main() { return; } + const turnkeyClient = new TurnkeyClient( + { + baseUrl: process.env.BASE_URL!, + }, + new ApiKeyStamper({ + apiPublicKey: process.env.API_PUBLIC_KEY!, + apiPrivateKey: process.env.API_PRIVATE_KEY!, + }) + ); + // Initialize a Turnkey Signer - const turnkeySigner = new TurnkeySigner({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - baseUrl: process.env.BASE_URL!, - organizationId: process.env.ORGANIZATION_ID!, - privateKeyId: process.env.PRIVATE_KEY_ID!, - }); + const turnkeySigner = new TurnkeySigner( + turnkeyClient, + process.env.ORGANIZATION_ID!, + process.env.PRIVATE_KEY_ID!, + ); // Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v5/api/providers/) const network = "sepolia"; diff --git a/examples/with-gnosis/src/index.ts b/examples/with-gnosis/src/index.ts index 8d7636b4e..0d4615525 100644 --- a/examples/with-gnosis/src/index.ts +++ b/examples/with-gnosis/src/index.ts @@ -6,6 +6,8 @@ dotenv.config({ path: path.resolve(process.cwd(), ".env.local") }); import { TurnkeySigner } from "@turnkey/ethers"; import { ethers } from "ethers"; +import { TurnkeyClient } from "@turnkey/http"; +import { ApiKeyStamper } from "@turnkey/api-key-stamper"; import EthersAdapter from "@safe-global/safe-ethers-lib"; import Safe, { SafeFactory, @@ -28,30 +30,34 @@ async function main() { return; } + const turnkeyClient = new TurnkeyClient( + { + baseUrl: process.env.BASE_URL!, + }, + new ApiKeyStamper({ + apiPublicKey: process.env.API_PUBLIC_KEY!, + apiPrivateKey: process.env.API_PRIVATE_KEY!, + }) + ); + // Initialize a Turnkey Signer - const turnkeySigner1 = new TurnkeySigner({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - baseUrl: process.env.BASE_URL!, - organizationId: process.env.ORGANIZATION_ID!, - privateKeyId: process.env.PRIVATE_KEY_ID_1!, - }); + const turnkeySigner1 = new TurnkeySigner( + turnkeyClient, + process.env.ORGANIZATION_ID!, + process.env.PRIVATE_KEY_ID_1!, + ); - const turnkeySigner2 = new TurnkeySigner({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - baseUrl: process.env.BASE_URL!, - organizationId: process.env.ORGANIZATION_ID!, - privateKeyId: process.env.PRIVATE_KEY_ID_2!, - }); + const turnkeySigner2 = new TurnkeySigner( + turnkeyClient, + process.env.ORGANIZATION_ID!, + process.env.PRIVATE_KEY_ID_2!, + ); - const turnkeySigner3 = new TurnkeySigner({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - baseUrl: process.env.BASE_URL!, - organizationId: process.env.ORGANIZATION_ID!, - privateKeyId: process.env.PRIVATE_KEY_ID_3!, - }); + const turnkeySigner3 = new TurnkeySigner( + turnkeyClient, + process.env.ORGANIZATION_ID!, + process.env.PRIVATE_KEY_ID_3!, + ); // Bring your own provider (for the sake of this demo, we recommend using Sepolia + Infura) const network = "sepolia"; diff --git a/examples/with-nonce-manager/src/managedOptimistic.ts b/examples/with-nonce-manager/src/managedOptimistic.ts index 0b922f91d..f424d1d8c 100644 --- a/examples/with-nonce-manager/src/managedOptimistic.ts +++ b/examples/with-nonce-manager/src/managedOptimistic.ts @@ -7,6 +7,8 @@ dotenv.config({ path: path.resolve(process.cwd(), ".env.local") }); import { TurnkeySigner } from "@turnkey/ethers"; import { ethers } from "ethers"; +import { TurnkeyClient } from "@turnkey/http"; +import { ApiKeyStamper } from "@turnkey/api-key-stamper"; import { createNewEthereumPrivateKey } from "./createNewEthereumPrivateKey"; import { print, sleep, getUpdatedTransaction } from "./util"; import { @@ -139,14 +141,22 @@ async function main() { return; } + const turnkeyClient = new TurnkeyClient( + { + baseUrl: process.env.BASE_URL!, + }, + new ApiKeyStamper({ + apiPublicKey: process.env.API_PUBLIC_KEY!, + apiPrivateKey: process.env.API_PRIVATE_KEY!, + }) + ); + // Initialize a Turnkey Signer - const turnkeySigner = new TurnkeySigner({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - baseUrl: process.env.BASE_URL!, - organizationId: process.env.ORGANIZATION_ID!, - privateKeyId: process.env.PRIVATE_KEY_ID!, - }); + const turnkeySigner = new TurnkeySigner( + turnkeyClient, + process.env.ORGANIZATION_ID!, + process.env.PRIVATE_KEY_ID!, + ); // Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v5/api/providers/) const network = "goerli"; diff --git a/examples/with-nonce-manager/src/simpleSequential.ts b/examples/with-nonce-manager/src/simpleSequential.ts index d3a54b3b7..0970a3eb8 100644 --- a/examples/with-nonce-manager/src/simpleSequential.ts +++ b/examples/with-nonce-manager/src/simpleSequential.ts @@ -6,6 +6,8 @@ dotenv.config({ path: path.resolve(process.cwd(), ".env.local") }); import { TurnkeySigner } from "@turnkey/ethers"; import { ethers } from "ethers"; +import { TurnkeyClient } from "@turnkey/http"; +import { ApiKeyStamper } from "@turnkey/api-key-stamper"; import { createNewEthereumPrivateKey } from "./createNewEthereumPrivateKey"; import { print } from "./util"; @@ -16,14 +18,22 @@ async function main() { return; } + const turnkeyClient = new TurnkeyClient( + { + baseUrl: process.env.BASE_URL!, + }, + new ApiKeyStamper({ + apiPublicKey: process.env.API_PUBLIC_KEY!, + apiPrivateKey: process.env.API_PRIVATE_KEY!, + }) + ); + // Initialize a Turnkey Signer - const turnkeySigner = new TurnkeySigner({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - baseUrl: process.env.BASE_URL!, - organizationId: process.env.ORGANIZATION_ID!, - privateKeyId: process.env.PRIVATE_KEY_ID!, - }); + const turnkeySigner = new TurnkeySigner( + turnkeyClient, + process.env.ORGANIZATION_ID!, + process.env.PRIVATE_KEY_ID!, + ); // Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v5/api/providers/) const network = "goerli"; diff --git a/examples/with-uniswap/package.json b/examples/with-uniswap/package.json index 0b986b23e..665da33eb 100644 --- a/examples/with-uniswap/package.json +++ b/examples/with-uniswap/package.json @@ -10,6 +10,7 @@ "dependencies": { "@turnkey/ethers": "workspace:*", "@turnkey/http": "workspace:*", + "@turnkey/api-key-stamper": "workspace:*", "@uniswap/sdk-core": "^3.1.1", "@uniswap/v3-core": "^1.0.1", "@uniswap/v3-sdk": "^3.9.0", diff --git a/examples/with-uniswap/src/provider.ts b/examples/with-uniswap/src/provider.ts index 08cb8cfac..f3c0fedf3 100644 --- a/examples/with-uniswap/src/provider.ts +++ b/examples/with-uniswap/src/provider.ts @@ -1,18 +1,20 @@ -import * as dotenv from "dotenv"; import * as path from "path"; - -// Load environment variables from `.env.local` -dotenv.config({ path: path.resolve(process.cwd(), ".env.local") }); - +import * as dotenv from "dotenv"; import { ethers } from "ethers"; import { TurnkeySigner } from "@turnkey/ethers"; +import { TurnkeyClient } from "@turnkey/http"; +import { ApiKeyStamper } from "@turnkey/api-key-stamper"; import { Environment } from "./constants"; +const DEFAULT_INFURA_COMMUNITY_KEY = "84842078b09946638c03157f83405213"; const DEFAULT_ENV = Environment.GOERLI; +// Load environment variables from `.env.local` +dotenv.config({ path: path.resolve(process.cwd(), ".env.local") }); + let provider = new ethers.providers.InfuraProvider( DEFAULT_ENV, - process.env.INFURA_KEY! + process.env.INFURA_KEY || DEFAULT_INFURA_COMMUNITY_KEY ); export function getProvider( @@ -21,26 +23,34 @@ export function getProvider( if (env !== Environment.GOERLI) { provider = new ethers.providers.InfuraProvider( env, - process.env.INFURA_KEY! + process.env.INFURA_KEY || DEFAULT_INFURA_COMMUNITY_KEY ); } return provider; } -// Initialize a Turnkey Signer -const turnkeySigner = new TurnkeySigner({ - apiPublicKey: process.env.API_PUBLIC_KEY!, - apiPrivateKey: process.env.API_PRIVATE_KEY!, - baseUrl: process.env.BASE_URL!, - organizationId: process.env.ORGANIZATION_ID!, - privateKeyId: process.env.PRIVATE_KEY_ID!, -}); - // getTurnkeySigner returns a TurnkeySigner connected to the passed-in Provider // (https://docs.ethers.org/v5/api/providers/) export function getTurnkeySigner( - provider: ethers.providers.Provider + provider: ethers.providers.Provider, ): TurnkeySigner { + const turnkeyClient = new TurnkeyClient( + { + baseUrl: process.env.BASE_URL!, + }, + new ApiKeyStamper({ + apiPublicKey: process.env.API_PUBLIC_KEY!, + apiPrivateKey: process.env.API_PRIVATE_KEY!, + }) + ); + + // Initialize a Turnkey Signer + const turnkeySigner = new TurnkeySigner( + turnkeyClient, + process.env.ORGANIZATION_ID!, + process.env.PRIVATE_KEY_ID!, + ); + return turnkeySigner.connect(provider); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 90fc67733..cf5999278 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -83,6 +83,9 @@ importers: examples/sweeper: dependencies: + '@turnkey/api-key-stamper': + specifier: workspace:* + version: link:../../packages/api-key-stamper '@turnkey/ethers': specifier: workspace:* version: link:../../packages/ethers @@ -325,6 +328,9 @@ importers: examples/with-uniswap: dependencies: + '@turnkey/api-key-stamper': + specifier: workspace:* + version: link:../../packages/api-key-stamper '@turnkey/ethers': specifier: workspace:* version: link:../../packages/ethers