|
| 1 | +import "dotenv/config"; |
| 2 | +import {getFireblocksInformation, getFireblocksSignature} from "./fireblocks"; |
| 3 | +import { |
| 4 | + BondExtraResponse, |
| 5 | + DeriveTransactionResponse, |
| 6 | + fetchBondExtraTransaction, |
| 7 | + fetchUnsignedTransaction, |
| 8 | + prepareTransaction, |
| 9 | + StakeIntentResponse |
| 10 | +} from "./api"; |
| 11 | +import {compileAndSend} from "./api"; |
| 12 | +import {CompileAndSendResponse} from "./api"; |
| 13 | + |
| 14 | + |
| 15 | +function checkRequiredEnvVars() { |
| 16 | + const requiredVars = [ |
| 17 | + 'BLOCKDAEMON_STAKE_API_KEY', 'POLKADOT_NETWORK', 'FIREBLOCKS_BASE_PATH', |
| 18 | + 'FIREBLOCKS_API_KEY', 'FIREBLOCKS_SECRET_KEY', 'FIREBLOCKS_VAULT_ACCOUNT_ID', 'BLOCKDAEMON_API_KEY' |
| 19 | + ]; |
| 20 | + |
| 21 | + requiredVars.forEach(varName => { |
| 22 | + if (!process.env[varName]) { |
| 23 | + throw new Error(`${varName} environment variable not set`); |
| 24 | + } |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +// Determine the configuration for mainnet or testnet |
| 29 | +function getConfigForNetwork() { |
| 30 | + const network = process.env.POLKADOT_NETWORK?.toLowerCase(); |
| 31 | + const isTestnet = network === 'westend'; |
| 32 | + |
| 33 | + return { |
| 34 | + assetID: isTestnet ? "WND" : "DOT", |
| 35 | + stakeApiUrl: isTestnet |
| 36 | + ? 'https://svc.blockdaemon.com/boss/v1/polkadot/westend/bond-extra' |
| 37 | + : 'https://svc.blockdaemon.com/boss/v1/polkadot/mainnet/bond-extra', |
| 38 | + compileAndSendUrl: isTestnet |
| 39 | + ? 'https://svc.blockdaemon.com/tx/v1/polkadot-westend/compile_and_send' |
| 40 | + : 'https://svc.blockdaemon.com/tx/v1/polkadot-mainnet/compile_and_send', |
| 41 | + deriveApiUrl: isTestnet |
| 42 | + ? 'https://svc.blockdaemon.com/tx/v1/polkadot-westend/derive_signing_payload' |
| 43 | + : 'https://svc.blockdaemon.com/tx/v1/polkadot-mainnet/derive_signing_payload' |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +async function main() { |
| 48 | + checkRequiredEnvVars() |
| 49 | + const config = getConfigForNetwork() |
| 50 | + |
| 51 | + const {address: sender, fireblocks} = await getFireblocksInformation(config.assetID); |
| 52 | + const amount = "1000000000000" //This is 1 DOT & the minimum |
| 53 | + |
| 54 | + //Call the Staking API to get your unsigned transaction |
| 55 | + const apiResponse: BondExtraResponse = await fetchBondExtraTransaction(sender, amount); |
| 56 | + console.log("Blockdaemon Staking API Response::", apiResponse); |
| 57 | + |
| 58 | + //Call our tx lifecycle API to derive the payload to send to fireblocks |
| 59 | + const deriveTransactionResponse: DeriveTransactionResponse = await prepareTransaction(apiResponse.polkadot.unsigned_transaction, apiResponse.polkadot.customer_address, config.deriveApiUrl) |
| 60 | + |
| 61 | + //Call and sign the transaction in fireblocks |
| 62 | + const {signature, pubKey} = await getFireblocksSignature( |
| 63 | + fireblocks, |
| 64 | + process.env.FIREBLOCKS_VAULT_ACCOUNT_ID!, |
| 65 | + "WND", |
| 66 | + deriveTransactionResponse.signing_payload |
| 67 | + ); |
| 68 | + |
| 69 | + //Take the signature, pubkey and unsigned transaction and broadcast to the network |
| 70 | + await compileAndSend(deriveTransactionResponse.unsigned_tx, signature, pubKey, config.compileAndSendUrl); |
| 71 | +} |
| 72 | + |
| 73 | +main().catch(console.error); |
0 commit comments