Skip to content

Commit 3f21068

Browse files
committed
feat: add ton scripts
1 parent b7839cb commit 3f21068

File tree

6 files changed

+364
-0
lines changed

6 files changed

+364
-0
lines changed

package-lock.json

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@mysten/ledgerjs-hw-app-sui": "^0.4.1",
3737
"@mysten/sui": "^1.3.0",
3838
"@stellar/stellar-sdk": "^13.0.0",
39+
"@ton/ton": "^15.2.1",
3940
"axios": "^1.7.2",
4041
"csv-parser": "^3.0.0",
4142
"path": "^0.12.7",

ton/Readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
# Ton operational scripts
3+
4+
This folder contains operational scripts for the Ton AxelarGateway. It assumes an already deployed AxelarGateway.fc and an Executable.fc.
5+
6+
Required enviroment variables
7+
8+
```
9+
MNEMONIC=
10+
TONCENTER_API_KEY=
11+
TON_GATEWAY_ADDRESS=
12+
```
13+
14+
## Appoving a message
15+
16+
Run this with the hex encoded payload procuded by the Ton MultisigProver.
17+
18+
```bash
19+
node ton/approveMessages.js b5ee9c7241020c0100018a000208000000280102016180000000000000000000000000000000800000000000000000000000000000000000000000000000000000000035f72a40030101c00400e2d0b0005189ac84d3a1d675847700777891dde751fa107d64837bf6337d32df6790000000000000000000000000000000018b92f5624a0587f7ed939e1c0fe4a6e4688d3a945402e2e51785d1bb98a5efe47b1d85c89fdc1920681e1d30b98c0ed4ecb64132f32b7bfcc76c58450fe7f0000102d005044035d25b76a49eebc07a7419b922fc11bd7bba1970b579d2a380ddd6606c5a1ff80607080900883078343737653062373438626132303064613436383963633836323965306431323363623862346139346132386333333537303661343032376465653766326261352d30001c6176616c616e6368652d66756a69005430783831653633654138463634464564423938353845423645323137364234333146426431306431654302000a0b00404a1a80a7b0326b22310dced59d8b52efddf313e77f9b48f226b69b8efedbe24d0006746f6e477e6fee
20+
```
21+
22+
23+
## Executing a message
24+
25+
Run this to execute an approve message
26+
27+
```bash
28+
node ton/relayerExecute.js "0x477e0b748ba200da4689cc8629e0d123cb8b4a94a28c335706a4027dee7f2ba5-0" "avalanche-fuji" "0x81e63eA8F64FEdB9858EB6E2176B431FBd10d1eC" "48656c6c6f2066726f6d204176616c616e63686521" "0:4a1a80a7b0326b22310dced59d8b52efddf313e77f9b48f226b69b8efedbe24d" "ton" "0x35d25b76a49eebc07a7419b922fc11bd7bba1970b579d2a380ddd6606c5a1ff8"
29+
```

ton/approveMessages.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const { Command } = require('commander');
2+
const { Address, Cell, internal } = require('@ton/ton');
3+
const { getTonClient, loadWallet, waitForTransaction, GATEWAY_ADDRESS } = require('./common');
4+
5+
// Constants
6+
const APPROVE_MESSAGES_COST = '2';
7+
8+
function createApproveMessagesCell(encodedPayload) {
9+
return Cell.fromBoc(Buffer.from(encodedPayload, 'hex'))[0];
10+
}
11+
12+
async function run(encodedPayload) {
13+
try {
14+
const client = getTonClient();
15+
const { contract, key } = await loadWallet(client);
16+
17+
const gateway = Address.parse(GATEWAY_ADDRESS);
18+
const approveMessagesCell = createApproveMessagesCell(encodedPayload);
19+
20+
const message = internal({
21+
to: gateway,
22+
value: APPROVE_MESSAGES_COST,
23+
body: approveMessagesCell,
24+
});
25+
26+
const seqno = await contract.getSeqno();
27+
console.log('Current wallet seqno:', seqno);
28+
29+
console.log('Sending approve messages transaction...');
30+
const transfer = await contract.sendTransfer({
31+
secretKey: key.secretKey,
32+
messages: [message],
33+
seqno: seqno,
34+
amount: APPROVE_MESSAGES_COST,
35+
});
36+
37+
console.log('Approve messages transaction sent successfully!');
38+
39+
await waitForTransaction(contract, seqno);
40+
41+
} catch (error) {
42+
console.error('Error in approve messages:', error);
43+
throw error;
44+
}
45+
}
46+
47+
// Set up command line interface
48+
if (require.main === module) {
49+
const program = new Command();
50+
program
51+
.name('approveMessages')
52+
.description('Approve messages on TON gateway')
53+
.argument('<encodedPayload>', 'Encoded payload in hex format')
54+
.action(run);
55+
56+
program.parse();
57+
}

ton/common.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// ton/common.js
2+
const { TonClient, WalletContractV5R1 } = require('@ton/ton');
3+
const { mnemonicToWalletKey } = require('@ton/crypto');
4+
require('dotenv').config();
5+
6+
// Constants
7+
const TONCENTER_ENDPOINT = 'https://testnet.toncenter.com/api/v2/jsonRPC';
8+
const GATEWAY_ADDRESS = process.env.TON_GATEWAY_ADDRESS;
9+
10+
if (!GATEWAY_ADDRESS) {
11+
throw new Error('Please set TON_GATEWAY_ADDRESS in your .env file');
12+
}
13+
14+
// Helper function to initialize TON client
15+
function getTonClient() {
16+
if (!process.env.TONCENTER_API_KEY) {
17+
throw new Error('Please set TONCENTER_API_KEY environment variable. Get it from https://t.me/tontestnetapibot');
18+
}
19+
20+
return new TonClient({
21+
endpoint: TONCENTER_ENDPOINT,
22+
apiKey: process.env.TONCENTER_API_KEY,
23+
});
24+
}
25+
26+
// Helper function to load wallet
27+
async function loadWallet(client) {
28+
const mnemonic = process.env.MNEMONIC?.split(' ') || [];
29+
if (mnemonic.length !== 24) {
30+
throw new Error('Please set MNEMONIC environment variable with 24 words');
31+
}
32+
33+
const key = await mnemonicToWalletKey(mnemonic);
34+
const wallet = WalletContractV5R1.create({ publicKey: key.publicKey, workchain: 0 });
35+
return { contract: client.open(wallet), key, wallet };
36+
}
37+
38+
// Helper function to wait for transaction confirmation
39+
async function waitForTransaction(contract, seqno) {
40+
let currentSeqno = seqno;
41+
while (currentSeqno === seqno) {
42+
console.log('Waiting for transaction confirmation...');
43+
await new Promise(resolve => setTimeout(resolve, 1500));
44+
currentSeqno = await contract.getSeqno();
45+
}
46+
console.log('Transaction confirmed!');
47+
}
48+
49+
module.exports = {
50+
getTonClient,
51+
loadWallet,
52+
waitForTransaction,
53+
TONCENTER_ENDPOINT,
54+
GATEWAY_ADDRESS,
55+
};

0 commit comments

Comments
 (0)