|
| 1 | +import { ksmToPlanck, Kiln } from '../src/kiln.ts'; |
| 2 | +import type { FireblocksIntegration } from '../src/fireblocks.ts'; |
| 3 | +import { loadEnv } from './env.ts'; |
| 4 | + |
| 5 | +const { kilnApiKey, kilnAccountId, kilnApiUrl, fireblocksApiKey, fireblocksApiSecret, fireblocksVaultId } = |
| 6 | + await loadEnv(); |
| 7 | + |
| 8 | +const k = new Kiln({ |
| 9 | + baseUrl: kilnApiUrl, |
| 10 | + apiToken: kilnApiKey, |
| 11 | +}); |
| 12 | + |
| 13 | +const vault: FireblocksIntegration = { |
| 14 | + config: { |
| 15 | + apiKey: fireblocksApiKey, |
| 16 | + secretKey: fireblocksApiSecret, |
| 17 | + basePath: 'https://api.fireblocks.io/v1', |
| 18 | + }, |
| 19 | + vaultId: fireblocksVaultId, |
| 20 | +}; |
| 21 | + |
| 22 | +// |
| 23 | +// Get the wallet address from Fireblocks |
| 24 | +// |
| 25 | +const fireblocksWallet = ( |
| 26 | + await k.fireblocks.getSdk(vault).vaults.getVaultAccountAssetAddressesPaginated({ |
| 27 | + vaultAccountId: vault.vaultId, |
| 28 | + assetId: 'KSM', |
| 29 | + limit: 1, |
| 30 | + }) |
| 31 | +).data.addresses?.[0].address; |
| 32 | + |
| 33 | +if (!fireblocksWallet) { |
| 34 | + console.log('Failed to get fireblocks wallet'); |
| 35 | + process.exit(0); |
| 36 | +} |
| 37 | + |
| 38 | +// |
| 39 | +// Craft the transaction |
| 40 | +// |
| 41 | +console.log('Crafting transaction...'); |
| 42 | +const txRequest = await k.client.POST('/ksm/transaction/bond-extra-pool', { |
| 43 | + body: { |
| 44 | + account_id: kilnAccountId, |
| 45 | + member_account: fireblocksWallet, |
| 46 | + amount_planck: ksmToPlanck('0.1').toString(), |
| 47 | + }, |
| 48 | +}); |
| 49 | +if (txRequest.error) { |
| 50 | + console.log('Failed to craft transaction:', txRequest); |
| 51 | + process.exit(1); |
| 52 | +} else { |
| 53 | + console.log('Crafted transaction:', txRequest.data); |
| 54 | +} |
| 55 | +console.log('\n\n\n'); |
| 56 | + |
| 57 | +// |
| 58 | +// Sign the transaction |
| 59 | +// |
| 60 | +console.log('Signing transaction...'); |
| 61 | +const signRequest = await (async () => { |
| 62 | + try { |
| 63 | + return await k.fireblocks.signKsmTx(vault, txRequest.data.data); |
| 64 | + } catch (err) { |
| 65 | + console.log('Failed to sign transaction:', err); |
| 66 | + process.exit(1); |
| 67 | + } |
| 68 | +})(); |
| 69 | +console.log('Signed transaction:', signRequest); |
| 70 | +console.log('\n\n\n'); |
| 71 | + |
| 72 | +// |
| 73 | +// Broadcast the transaction |
| 74 | +// |
| 75 | +console.log('Broadcasting transaction...'); |
| 76 | +const broadcastedRequest = await k.client.POST('/ksm/transaction/broadcast', { |
| 77 | + body: { |
| 78 | + tx_serialized: signRequest.signed_tx.data.signed_tx_serialized, |
| 79 | + }, |
| 80 | +}); |
| 81 | +if (broadcastedRequest.error) { |
| 82 | + console.log('Failed to broadcast transaction:', broadcastedRequest); |
| 83 | + process.exit(1); |
| 84 | +} else { |
| 85 | + console.log('Broadcasted transaction:', broadcastedRequest.data); |
| 86 | +} |
0 commit comments