Skip to content

Commit 3d1cc69

Browse files
authored
ton example (#151)
* add ton example * TON stake/unstake ton-whales * ton example * remove ts ignore
1 parent 66a50c7 commit 3d1cc69

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

examples/ton.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { Kiln, tonToNanoton } from "../src/kiln";
2+
import fs from "node:fs";
3+
import 'dotenv/config'
4+
import type { FireblocksIntegration } from "../src/fireblocks.ts";
5+
6+
7+
const apiSecret = fs.readFileSync(`${__dirname}/fireblocks_secret_prod.key`, 'utf8');
8+
9+
const k = new Kiln({
10+
baseUrl: process.env.KILN_API_URL as string,
11+
apiToken: process.env.KILN_API_KEY as string,
12+
});
13+
14+
const vault: FireblocksIntegration = {
15+
provider: 'fireblocks',
16+
fireblocksApiKey: process.env.FIREBLOCKS_API_KEY as string,
17+
fireblocksSecretKey: apiSecret,
18+
vaultId: 17
19+
};
20+
21+
try {
22+
console.log('crafting...');
23+
const tx = await k.client.POST(
24+
'/v1/ton/transaction/stake-ton-whales-pool',
25+
{
26+
body: {
27+
account_id: process.env.KILN_ACCOUNT_ID as string,
28+
wallet: 'UQAd57R6nYTCpgo1OxSmpbFRsIO6HIIfO2SW6WcfCe5qIo08',
29+
pool_address: 'EQBXDSbE9s03Waq62YuGdtqe-bcjsN6K9fi64eUy9M8H_Yhf',
30+
vesting_contract_address: 'EQBdL-upJbGStg4MF8acfEfilqd34cfoHe_k2E-yecki3yS6',
31+
amount_nanoton: tonToNanoton('2').toString(),
32+
}
33+
}
34+
);
35+
// const tx = await k.client.POST(
36+
// '/v1/ton/transaction/unstake-ton-whales-pool',
37+
// {
38+
// body: {
39+
// wallet: 'UQAd57R6nYTCpgo1OxSmpbFRsIO6HIIfO2SW6WcfCe5qIo08',
40+
// pool_address: 'EQBXDSbE9s03Waq62YuGdtqe-bcjsN6K9fi64eUy9M8H_Yhf',
41+
// vesting_contract_address: 'EQBdL-upJbGStg4MF8acfEfilqd34cfoHe_k2E-yecki3yS6',
42+
// amount_nanoton: tonToNanoton('0.5').toString(),
43+
// }
44+
// }
45+
// );
46+
console.log('signing...');
47+
if(!tx.data?.data) throw new Error('No data in response');
48+
const signResponse = await k.fireblocks.signTonTx(vault, tx.data.data, "TON");
49+
console.log('broadcasting...');
50+
if(!signResponse.signed_tx?.data?.signed_tx_serialized) throw new Error('No signed_tx in response');
51+
const broadcastedTx = await k.client.POST("/v1/ton/transaction/broadcast", {
52+
body: {
53+
tx_serialized: signResponse.signed_tx.data.signed_tx_serialized,
54+
}
55+
});
56+
console.log(broadcastedTx);
57+
58+
} catch (err) {
59+
console.log(err);
60+
}

src/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ export const nanotonToTon = (nanoton: bigint): string => {
5656
return formatUnits(nanoton, 9);
5757
};
5858

59+
/**
60+
* Convert TON to nanoTON
61+
*/
62+
export const tonToNanoton = (ton: string): bigint => {
63+
return parseUnits(ton, 9);
64+
};
65+
5966
/**
6067
* Convert uZETA to ZETA
6168
*/

0 commit comments

Comments
 (0)