Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Sep 7, 2023
1 parent ebf87a9 commit c878237
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 98 deletions.
24 changes: 17 additions & 7 deletions examples/deployer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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";
Expand Down
22 changes: 16 additions & 6 deletions examples/rebalancer/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions examples/sweeper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 20 additions & 10 deletions examples/sweeper/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}
28 changes: 19 additions & 9 deletions examples/trading-runner/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") });
Expand All @@ -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
Expand All @@ -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);
}
24 changes: 17 additions & 7 deletions examples/with-ethers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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";
Expand Down
24 changes: 17 additions & 7 deletions examples/with-ethers/src/legacySepolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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";
Expand Down
48 changes: 27 additions & 21 deletions examples/with-gnosis/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand Down
24 changes: 17 additions & 7 deletions examples/with-nonce-manager/src/managedOptimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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";
Expand Down
Loading

0 comments on commit c878237

Please sign in to comment.