|
| 1 | +import { dydxToAdydx, Kiln, KILN_VALIDATORS } 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 pubkey from Fireblocks |
| 24 | +// |
| 25 | +const fireblocksPubkey = (await k.fireblocks.getPubkey(vault, 'DYDX_DYDX')).publicKey; |
| 26 | +if (!fireblocksPubkey) { |
| 27 | + console.log('Failed to get pubkey'); |
| 28 | + process.exit(0); |
| 29 | +} |
| 30 | + |
| 31 | +// |
| 32 | +// Craft the transaction |
| 33 | +// |
| 34 | +console.log('Crafting transaction...'); |
| 35 | +const txRequest = await k.client.POST('/dydx/transaction/stake', { |
| 36 | + body: { |
| 37 | + account_id: kilnAccountId, |
| 38 | + pubkey: fireblocksPubkey, |
| 39 | + validator: KILN_VALIDATORS.DYDX.mainnet.KILN, |
| 40 | + amount_adydx: dydxToAdydx('0.01').toString(), |
| 41 | + restake_rewards: false, |
| 42 | + }, |
| 43 | +}); |
| 44 | +if (txRequest.error) { |
| 45 | + console.log('Failed to craft transaction:', txRequest); |
| 46 | + process.exit(1); |
| 47 | +} else { |
| 48 | + console.log('Crafted transaction:', txRequest.data); |
| 49 | +} |
| 50 | +console.log('\n\n\n'); |
| 51 | + |
| 52 | +// |
| 53 | +// Sign the transaction |
| 54 | +// |
| 55 | +console.log('Signing transaction...'); |
| 56 | +const signRequest = await (async () => { |
| 57 | + try { |
| 58 | + return await k.fireblocks.signDydxTx(vault, txRequest.data.data); |
| 59 | + } catch (err) { |
| 60 | + console.log('Failed to sign transaction:', err); |
| 61 | + process.exit(1); |
| 62 | + } |
| 63 | +})(); |
| 64 | +console.log('Signed transaction:', signRequest); |
| 65 | +console.log('\n\n\n'); |
| 66 | + |
| 67 | +// |
| 68 | +// Broadcast the transaction |
| 69 | +// |
| 70 | +console.log('Broadcasting transaction...'); |
| 71 | +const broadcastedRequest = await k.client.POST('/dydx/transaction/broadcast', { |
| 72 | + body: { |
| 73 | + tx_serialized: signRequest.signed_tx.data.signed_tx_serialized, |
| 74 | + }, |
| 75 | +}); |
| 76 | +if (broadcastedRequest.error) { |
| 77 | + console.log('Failed to broadcast transaction:', broadcastedRequest); |
| 78 | + process.exit(1); |
| 79 | +} else { |
| 80 | + console.log('Broadcasted transaction:', broadcastedRequest.data); |
| 81 | +} |
0 commit comments