Skip to content

Instantly share code, notes, and snippets.

@sench93
Created May 19, 2023 13:34
Show Gist options
  • Save sench93/c0a332f32e4221323cd0fb017d456e63 to your computer and use it in GitHub Desktop.
Save sench93/c0a332f32e4221323cd0fb017d456e63 to your computer and use it in GitHub Desktop.
πŸ’‘ Smart Description: This code sends a specified amount of cryptocurrency to a specified address, with an optional message. If the "wait" parameter is true, it waits for the transaction to be confirmed and returns the transaction hash.

Async Ton Transfer Function.

Preview:
async function sendTon(mnemonic: string, nano: string, to: string, message: string = '', waitForResult: boolean): Promise<any> {

    const key = await mnemonicToKeyPair(mnemonic.split(" ")); //creates keyPair from mnemonic
    const tonweb = new TonWeb(new TonWeb.HttpProvider('https://toncenter.com/api/v2/jsonRPC', {apiKey: 'API_KEY'})); //creates Tonweb  with your api key
    const WalletClass = tonweb.wallet.all["v4R2"]; //loads WalletClass
    const wallet = new WalletClass(tonweb.provider, {publicKey: key.publicKey}); //creates wallet from key public key
    const seqno = await wallet.methods.seqno().call() || 0; //waits for current seqno
    let knownTxHash: any;// creates empty transaction
    let address = await wallet.getAddress() // gets your address
    let readableAddress = address.toString(true, true, true) // gets human readble address
    getLastTx(readableAddress, 1).then(value => {
        knownTxHash = value // sets the last transaction value to  previously creaed  transaction object
    }) // get current Last transaction

    await wallet.methods.transfer({
        secretKey: key.secretKey,
        toAddress: to,
        amount: nano,
        seqno: seqno,
        payload: message,
        sendMode: 3,
    }).send(); // transfers TON Coin  sendMode is 3 for normal mode, for emptying wallet use 128

    if (waitForResult) { // if we want to wait for result we  enter this block
        let currentSeqno = seqno;
        while (currentSeqno == seqno) {
            await sleep(500);
            currentSeqno = await wallet.methods.seqno().call() || 0;
        } // this block waits for seqno change


        let txHash = knownTxHash; // when seqno changes we query  for last transaction    
        while (txHash.txId == knownTxHash.txId) {
            getLastTx(readableAddress, 1).then(value => {
                txHash = value
            })
            await sleep(500)
        } // when transactions changes we takes the transaction and return


        return txHash;
    } else {
        return {
            txId: "sent" // if we don't want to wait  for transaction we just send empty object  or anything you want. 
        }
    }
}
Associated Context
Type Code Snippet ( .ts )
Associated Tags solidity blockchain smartcontracts Promise Transfer Method autodesk-viewer getLastTx Function Mnemonic HttpProvider SendMode WalletClass angular Nano ethereum Sleep Function TonWeb SDK Framework:TonWeb.
πŸ’‘ Smart Description This code sends a specified amount of cryptocurrency to a specified address, with an optional message. If the "wait" parameter is true, it waits for the transaction to be confirmed and returns the transaction hash.
πŸ”Ž Suggested Searches Javascript function to send TON with mnemonic and nano
How to send TON using TonWeb and HttpProvider
WalletClass for TON transfer with TonWeb
Javascript code to transfer TON with wait option
TonWeb HttpProvider transfer function with mnemonic and wait option
Related Links https://www.npmjs.com/package/ton-client-js
https://www.npmjs.com/package/@tonclient/lib-node
https://www.npmjs.com/package/tonweb
https://www.npmjs.com/package/@tonclient/lib-web
https://docs.ton.dev/
https://toncenter.com/api/v2/jsonRPC
https://github.com/tonlabs/ever-sdk-js
https://www.npmjs.com/package/@tonclient/core
https://www.npmjs.com/package/@tonclient/lib-react-native
Related People Arsen Sench, sench@outlook.com
Sensitive Information πŸ”΄ [HIGH] SECRET: b89b6✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻✻d838d
Shareable Link https://user-b0da9628-298e-4cce-acfd-a969441a210d-fhcmbheklq-uk.a.run.app/?p=6bd64eace1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment