|
| 1 | +import { Turnkey } from "@turnkey/sdk-server"; |
| 2 | +import * as dotenv from "dotenv"; |
| 3 | +import * as path from "path"; |
| 4 | + |
| 5 | +// Load environment variables from `.env.local` |
| 6 | +dotenv.config({ path: path.resolve(process.cwd(), ".env.local") }); |
| 7 | + |
| 8 | +async function main() { |
| 9 | + const turnkeyClient = new Turnkey({ |
| 10 | + apiBaseUrl: "https://api.turnkey.com", |
| 11 | + apiPrivateKey: process.env.TURNKEY_API_PRIVATE_KEY!, |
| 12 | + apiPublicKey: process.env.TURNKEY_API_PUBLIC_KEY!, |
| 13 | + defaultOrganizationId: process.env.TURNKEY_ORGANIZATION_ID!, |
| 14 | + }).apiClient(); |
| 15 | + |
| 16 | + // The id of the non-root user that you'll be using to sign the Yield related transactions |
| 17 | + const userId = process.env.NONROOT_USER_ID!; |
| 18 | + |
| 19 | + //approval policy |
| 20 | + const approvalPolicy = { |
| 21 | + policyName: |
| 22 | + "Allow API key user to call the approve function on the USDC_ADDRESS", |
| 23 | + effect: "EFFECT_ALLOW" as const, |
| 24 | + consensus: `approvers.any(user, user.id == '${userId}')`, |
| 25 | + condition: `eth.tx.to == '${process.env.USDC_ADDRESS}' && eth.tx.data[0..10] == '0x095ea7b3'`, |
| 26 | + notes: "", |
| 27 | + }; |
| 28 | + |
| 29 | + const { policyId: approvalPolicyId } = |
| 30 | + await turnkeyClient.createPolicy(approvalPolicy); |
| 31 | + |
| 32 | + console.log( |
| 33 | + [ |
| 34 | + `Created approval policy:`, |
| 35 | + `- Name: ${approvalPolicy.policyName}`, |
| 36 | + `- Policy ID: ${approvalPolicyId}`, |
| 37 | + `- Effect: ${approvalPolicy.effect}`, |
| 38 | + `- Consensus: ${approvalPolicy.consensus}`, |
| 39 | + `- Condition: ${approvalPolicy.condition}`, |
| 40 | + ``, |
| 41 | + ].join("\n"), |
| 42 | + ); |
| 43 | + |
| 44 | + //deposit policy |
| 45 | + const depositPolicy = { |
| 46 | + policyName: |
| 47 | + "Allow API key user to call the deposit function on the gtUSDCf_VAULT_ADDRESS", |
| 48 | + effect: "EFFECT_ALLOW" as const, |
| 49 | + consensus: `approvers.any(user, user.id == '${userId}')`, |
| 50 | + condition: `eth.tx.to == '${process.env.gtUSDCf_VAULT_ADDRESS}' && eth.tx.data[0..10] == '0x6e553f65'`, |
| 51 | + notes: "", |
| 52 | + }; |
| 53 | + |
| 54 | + const { policyId: depositPolicyId } = |
| 55 | + await turnkeyClient.createPolicy(depositPolicy); |
| 56 | + |
| 57 | + console.log( |
| 58 | + [ |
| 59 | + `Created deposit policy:`, |
| 60 | + `- Name: ${depositPolicy.policyName}`, |
| 61 | + `- Policy ID: ${depositPolicyId}`, |
| 62 | + `- Effect: ${depositPolicy.effect}`, |
| 63 | + `- Consensus: ${depositPolicy.consensus}`, |
| 64 | + `- Condition: ${depositPolicy.condition}`, |
| 65 | + ``, |
| 66 | + ].join("\n"), |
| 67 | + ); |
| 68 | + |
| 69 | + //withdraw policy |
| 70 | + const withdrawPolicy = { |
| 71 | + policyName: |
| 72 | + "Allow API key user to call the withdraw function on the gtUSDCf_VAULT_ADDRESS", |
| 73 | + effect: "EFFECT_ALLOW" as const, |
| 74 | + consensus: `approvers.any(user, user.id == '${userId}')`, |
| 75 | + condition: `eth.tx.to == '${process.env.gtUSDCf_VAULT_ADDRESS}' && eth.tx.data[0..10] == '0xba087652'`, |
| 76 | + notes: "", |
| 77 | + }; |
| 78 | + |
| 79 | + const { policyId: withdrawPolicyId } = |
| 80 | + await turnkeyClient.createPolicy(withdrawPolicy); |
| 81 | + |
| 82 | + console.log( |
| 83 | + [ |
| 84 | + `Created withdraw policy:`, |
| 85 | + `- Name: ${withdrawPolicy.policyName}`, |
| 86 | + `- Policy ID: ${withdrawPolicyId}`, |
| 87 | + `- Effect: ${withdrawPolicy.effect}`, |
| 88 | + `- Consensus: ${withdrawPolicy.consensus}`, |
| 89 | + `- Condition: ${withdrawPolicy.condition}`, |
| 90 | + ``, |
| 91 | + ].join("\n"), |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +main().catch((error) => { |
| 96 | + console.error(error); |
| 97 | + process.exit(1); |
| 98 | +}); |
0 commit comments