-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add TRX example * update with kilnAccountId * use wallet address from fireblocks
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { atomToUatom, Kiln, KILN_VALIDATORS, trxToSun } from '../src/kiln.ts'; | ||
import type { FireblocksIntegration } from '../src/fireblocks.ts'; | ||
import { loadEnv } from './env.ts'; | ||
|
||
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, | ||
}; | ||
|
||
// | ||
// Get the wallet address from Fireblocks | ||
// | ||
const fireblocksWallet = ( | ||
await k.fireblocks.getSdk(vault).vaults.getVaultAccountAssetAddressesPaginated({ | ||
vaultAccountId: vault.vaultId, | ||
assetId: 'TRX', | ||
limit: 1, | ||
}) | ||
).data.addresses?.[0].address; | ||
|
||
if (!fireblocksWallet) { | ||
console.log('Failed to get fireblocks wallet'); | ||
process.exit(0); | ||
} | ||
|
||
// | ||
// Craft the transaction | ||
// | ||
console.log('Crafting transaction...'); | ||
const txRequest = await k.client.POST('/trx/transaction/stake', { | ||
body: { | ||
account_id: kilnAccountId, | ||
owner_address: fireblocksWallet, | ||
amount_sun: Number(trxToSun('1')), | ||
resource: 'BANDWIDTH', | ||
}, | ||
}); | ||
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 { | ||
return await k.fireblocks.signTrxTx(vault, txRequest.data.data); | ||
} 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('/trx/transaction/broadcast', { | ||
body: { | ||
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); | ||
} |