From a48233f2e185636ea03f086addff91955652840c Mon Sep 17 00:00:00 2001 From: Arno Simon Date: Thu, 9 Jan 2025 10:14:24 +0100 Subject: [PATCH] add near example --- examples/.env.example | 1 + examples/env.ts | 7 ++++- examples/near.ts | 73 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 examples/near.ts diff --git a/examples/.env.example b/examples/.env.example index d208346..cb0a954 100644 --- a/examples/.env.example +++ b/examples/.env.example @@ -3,3 +3,4 @@ KILN_API_KEY="" KILN_ACCOUNT_ID="" FIREBLOCKS_API_KEY="" FIREBLOCKS_VAULT_ID="" +FIREBLOCKS_SECRET_FILENAME="" diff --git a/examples/env.ts b/examples/env.ts index d7381b8..beacdb7 100644 --- a/examples/env.ts +++ b/examples/env.ts @@ -24,7 +24,12 @@ export const loadEnv = async () => { process.exit(1); } - const fireblocksApiSecret = await Bun.file('fireblocks_secret_prod.key').text(); + if (!Bun.env.FIREBLOCKS_SECRET_FILENAME) { + console.log('FIREBLOCKS_SECRET_FILENAME is required'); + process.exit(1); + } + + const fireblocksApiSecret = await Bun.file(Bun.env.FIREBLOCKS_SECRET_FILENAME).text(); return { kilnApiUrl: Bun.env.KILN_API_URL, diff --git a/examples/near.ts b/examples/near.ts new file mode 100644 index 0000000..4249a19 --- /dev/null +++ b/examples/near.ts @@ -0,0 +1,73 @@ +import { Kiln, KILN_VALIDATORS } from '../src/kiln.ts'; +import type { FireblocksIntegration } from '../src/fireblocks.ts'; +import { loadEnv } from './env.ts'; +import { parseUnits } from "viem"; + +const { kilnApiKey, kilnAccountId, kilnApiUrl, fireblocksApiKey, fireblocksApiSecret, fireblocksVaultId } = + await loadEnv(); + +const k = new Kiln({ + baseUrl: kilnApiUrl, + apiToken: kilnApiKey, +}); + +const vault: FireblocksIntegration = { + config: { + apiKey: fireblocksApiKey, + secretKey: fireblocksApiSecret, + basePath: 'https://api.fireblocks.io/v1', + }, + vaultId: fireblocksVaultId, +}; + +// +// Craft the transaction +// +console.log('Crafting transaction...'); +const txRequest = await k.client.POST('/near/transaction/stake', { + body: { + account_id: kilnAccountId, + wallet: 'c36b1a5da2e60d1fd5d3a6b46f7399eb26571457f3272f3c978bc9527ad2335f', + pool_id: KILN_VALIDATORS.NEAR.testnet.KILN, + amount_yocto: parseUnits('0.1', 24).toString(), + }, +}); +if (txRequest.error) { + console.log('Failed to craft transaction:', txRequest); + process.exit(1); +} else { + console.log('Crafted transaction:', txRequest.data); +} +console.log('\n\n\n'); + +// +// Sign the transaction +// +console.log('Signing transaction...'); +const signRequest = await (async () => { + try { + // @ts-ignore + return await k.fireblocks.signNearTx(vault, txRequest.data.data, "NEAR_TEST"); + } catch (err) { + console.log('Failed to sign transaction:', err); + process.exit(1); + } +})(); +console.log('Signed transaction:', signRequest); +console.log('\n\n\n'); + +// +// Broadcast the transaction +// +console.log('Broadcasting transaction...'); +const broadcastedRequest = await k.client.POST('/near/transaction/broadcast', { + body: { + signed_tx_serialized: signRequest.signed_tx.data.signed_tx_serialized, + }, +}); +if (broadcastedRequest.error) { + console.log('Failed to broadcast transaction:', broadcastedRequest); + process.exit(1); +} else { + console.log('Broadcasted transaction:', broadcastedRequest.data); +}