Skip to content

Commit

Permalink
Add trx example (#190)
Browse files Browse the repository at this point in the history
* add TRX example

* update with kilnAccountId

* use wallet address from fireblocks
  • Loading branch information
Ellzer authored Jan 9, 2025
1 parent 35e1b2c commit 5090c98
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions examples/trx.ts
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);
}

0 comments on commit 5090c98

Please sign in to comment.