Skip to content

Commit 009b41f

Browse files
Ellzermenefrego15
authored andcommitted
Add trx example (#190)
* add TRX example * update with kilnAccountId * use wallet address from fireblocks
1 parent 3c37a68 commit 009b41f

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

examples/trx.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { atomToUatom, Kiln, KILN_VALIDATORS, trxToSun } 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: 'TRX',
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('/trx/transaction/stake', {
43+
body: {
44+
account_id: kilnAccountId,
45+
owner_address: fireblocksWallet,
46+
amount_sun: Number(trxToSun('1')),
47+
resource: 'BANDWIDTH',
48+
},
49+
});
50+
if (txRequest.error) {
51+
console.log('Failed to craft transaction:', txRequest);
52+
process.exit(1);
53+
} else {
54+
console.log('Crafted transaction:', txRequest.data);
55+
}
56+
console.log('\n\n\n');
57+
58+
//
59+
// Sign the transaction
60+
//
61+
console.log('Signing transaction...');
62+
const signRequest = await (async () => {
63+
try {
64+
return await k.fireblocks.signTrxTx(vault, txRequest.data.data);
65+
} catch (err) {
66+
console.log('Failed to sign transaction:', err);
67+
process.exit(1);
68+
}
69+
})();
70+
console.log('Signed transaction:', signRequest);
71+
console.log('\n\n\n');
72+
73+
//
74+
// Broadcast the transaction
75+
//
76+
console.log('Broadcasting transaction...');
77+
const broadcastedRequest = await k.client.POST('/trx/transaction/broadcast', {
78+
body: {
79+
tx_serialized: signRequest.signed_tx.data.signed_tx_serialized,
80+
},
81+
});
82+
if (broadcastedRequest.error) {
83+
console.log('Failed to broadcast transaction:', broadcastedRequest);
84+
process.exit(1);
85+
} else {
86+
console.log('Broadcasted transaction:', broadcastedRequest.data);
87+
}

0 commit comments

Comments
 (0)