Skip to content

Commit 3c37a68

Browse files
nooxxmenefrego15
authored andcommitted
add near example (#189)
1 parent 9889abf commit 3c37a68

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

examples/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ KILN_API_KEY=""
33
KILN_ACCOUNT_ID=""
44
FIREBLOCKS_API_KEY=""
55
FIREBLOCKS_VAULT_ID=""
6+
FIREBLOCKS_SECRET_FILENAME=""

examples/env.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ export const loadEnv = async () => {
2424
process.exit(1);
2525
}
2626

27-
const fireblocksApiSecret = await Bun.file('fireblocks_secret_prod.key').text();
27+
if (!Bun.env.FIREBLOCKS_SECRET_FILENAME) {
28+
console.log('FIREBLOCKS_SECRET_FILENAME is required');
29+
process.exit(1);
30+
}
31+
32+
const fireblocksApiSecret = await Bun.file(Bun.env.FIREBLOCKS_SECRET_FILENAME).text();
2833

2934
return {
3035
kilnApiUrl: Bun.env.KILN_API_URL,

examples/near.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { Kiln, KILN_VALIDATORS } from '../src/kiln.ts';
2+
import type { FireblocksIntegration } from '../src/fireblocks.ts';
3+
import { loadEnv } from './env.ts';
4+
import { parseUnits } from "viem";
5+
6+
const { kilnApiKey, kilnAccountId, kilnApiUrl, fireblocksApiKey, fireblocksApiSecret, fireblocksVaultId } =
7+
await loadEnv();
8+
9+
const k = new Kiln({
10+
baseUrl: kilnApiUrl,
11+
apiToken: kilnApiKey,
12+
});
13+
14+
const vault: FireblocksIntegration = {
15+
config: {
16+
apiKey: fireblocksApiKey,
17+
secretKey: fireblocksApiSecret,
18+
basePath: 'https://api.fireblocks.io/v1',
19+
},
20+
vaultId: fireblocksVaultId,
21+
};
22+
23+
//
24+
// Craft the transaction
25+
//
26+
console.log('Crafting transaction...');
27+
const txRequest = await k.client.POST('/near/transaction/stake', {
28+
body: {
29+
account_id: kilnAccountId,
30+
wallet: 'c36b1a5da2e60d1fd5d3a6b46f7399eb26571457f3272f3c978bc9527ad2335f',
31+
pool_id: KILN_VALIDATORS.NEAR.testnet.KILN,
32+
amount_yocto: parseUnits('0.1', 24).toString(),
33+
},
34+
});
35+
if (txRequest.error) {
36+
console.log('Failed to craft transaction:', txRequest);
37+
process.exit(1);
38+
} else {
39+
console.log('Crafted transaction:', txRequest.data);
40+
}
41+
console.log('\n\n\n');
42+
43+
//
44+
// Sign the transaction
45+
//
46+
console.log('Signing transaction...');
47+
const signRequest = await (async () => {
48+
try {
49+
// @ts-ignore
50+
return await k.fireblocks.signNearTx(vault, txRequest.data.data, "NEAR_TEST");
51+
} catch (err) {
52+
console.log('Failed to sign transaction:', err);
53+
process.exit(1);
54+
}
55+
})();
56+
console.log('Signed transaction:', signRequest);
57+
console.log('\n\n\n');
58+
59+
//
60+
// Broadcast the transaction
61+
//
62+
console.log('Broadcasting transaction...');
63+
const broadcastedRequest = await k.client.POST('/near/transaction/broadcast', {
64+
body: {
65+
signed_tx_serialized: signRequest.signed_tx.data.signed_tx_serialized,
66+
},
67+
});
68+
if (broadcastedRequest.error) {
69+
console.log('Failed to broadcast transaction:', broadcastedRequest);
70+
process.exit(1);
71+
} else {
72+
console.log('Broadcasted transaction:', broadcastedRequest.data);
73+
}

0 commit comments

Comments
 (0)