In JavaScript, you can use the ton
package. I have adapted an example from a community-led TON tutorial. Please use at your own risk.
What it does is:
- Generate multiple mnemonics
- Find the addresses of the generated mnemonics
- Fund the addresses of these mnemonics with a pre-funded wallet
- Send a transaction out of these mnemonnics to automatically deploy the contract
import { getHttpEndpoint } from "@orbs-network/ton-access";
import { mnemonicToWalletKey, mnemonicNew } from "ton-crypto";
import { TonClient, WalletContractV4, internal } from "ton";
async function main() {
// open wallet v4 (set your correct wallet version here)
const mnemonic = "unfold sugar water ..."; // insert a mnemonic for a wallet that has funds
const key = await mnemonicToWalletKey(mnemonic.split(" "));
const fundingWallet = WalletContractV4.create({ publicKey: key.publicKey, workchain: 0 });
for(let i = 0; i < 5; i++) {
let m = await mnemonicNew();
console.log(m);
await initializeWallet(fundingWallet, m);
}
}
async function initializeWallet(fundingWallet: WalletContractV4, mnemonic: string[]) {
// open wallet v4 (notice the correct wallet version here)
const key = await mnemonicToWalletKey(mnemonic);
const generatedWallet = WalletContractV4.create({ publicKey: key.publicKey, workchain: 0 });
// initialize ton rpc client on testnet
const endpoint = await getHttpEndpoint({ network: "testnet" });
const client = new TonClient({ endpoint });
// send 0.1 TON from funding wallet to new wallet
let walletContract = client.open(generatedWallet);
let seqno = await walletContract.getSeqno();
await walletContract.sendTransfer({
secretKey: key.secretKey,
seqno: seqno,
messages: [
internal({
to: generatedWallet.address,
value: "0.09", // 0.001 TON
bounce: false,
})
]
});
// send 0.9 back TON to funding wallet
walletContract = client.open(generatedWallet);
seqno = await walletContract.getSeqno();
await walletContract.sendTransfer({
secretKey: key.secretKey,
seqno: seqno,
messages: [
internal({
to: fundingWallet.address,
value: "0.09", // 0.001 TON
bounce: false
})
]
});
await waitForTransaction(seqno, walletContract);
}
async function waitForTransaction(seqno: number, walletContract: any) {
// wait until confirmed
let currentSeqno = seqno;
while (currentSeqno == seqno) {
console.log("waiting for transaction to confirm...");
await sleep(1500);
currentSeqno = await walletContract.getSeqno();
}
console.log("transaction confirmed!");
}
function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
main();
Won't work.
let walletContract = client.open(generatedWallet);
let seqno = await walletContract.getSeqno();
await walletContract.sendTransfer({
secretKey: key.secretKey,
seqno: seqno,
messages: [
internal({
to: generatedWallet.address,
value: "0.09", // 0.001 TON
bounce: false,
})
]
});
// This will try to send frome generatedWallet to generatedWallet, you need to open fundingwallet instead and use its key.
You can use the TonWeb javascript SDK.
const tonweb = new TonWeb();
const wallet = tonweb.wallet.create({publicKey});
const address = await wallet.getAddress();
const nonBounceableAddress = address.toString(true, true, false);
const seqno = await wallet.methods.seqno().call();
await wallet.deploy(secretKey).send(); // deploy wallet to blockchain
const fee = await wallet.methods.transfer({
secretKey,
toAddress: 'EQDjVXa_oltdBP64Nc__p397xLCvGm2IcZ1ba7anSW0NAkeP',
amount: TonWeb.utils.toNano(0.01), // 0.01 TON
seqno: seqno,
payload: 'Hello',
sendMode: 3,
}).estimateFee();
const Cell = TonWeb.boc.Cell;
const cell = new Cell();
cell.bits.writeUint(0, 32);
cell.bits.writeAddress(address);
cell.bits.writeGrams(1);
console.log(cell.print()); // print cell data like Fift
const bocBytes = cell.toBoc();
const history = await tonweb.getTransactions(address);
const balance = await tonweb.getBalance(address);
tonweb.sendBoc(bocBytes);