Skip to content

Instantly share code, notes, and snippets.

@satanworker
Created April 7, 2024 09:33
Show Gist options
  • Select an option

  • Save satanworker/0f953f616085fcc559d90440d9cc5952 to your computer and use it in GitHub Desktop.

Select an option

Save satanworker/0f953f616085fcc559d90440d9cc5952 to your computer and use it in GitHub Desktop.

Revisions

  1. satanworker created this gist Apr 7, 2024.
    106 changes: 106 additions & 0 deletions Transfer and swap no bounce.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    # Transfer

    # Transfer swap
    async transferSwap(userId: number, accountIndex: number, params: { swapTxParams: SwapTxParams }) {
    const { swapTxParams } = params;
    const [connectedWallet, secretKey] = await Promise.all([
    await this.walletService.getTONWallet({
    userId,
    accountIndex
    }),
    await this.walletService.getTonWalletSecretKey({
    userId,
    accountIndex
    })
    ])

    const seqno = await connectedWallet.methods.seqno().call() || 0;
    const res = await connectedWallet.methods.transfer({
    secretKey,
    toAddress: swapTxParams.to,
    amount: swapTxParams.gasAmount.add(
    TonWeb.utils.toNano(JettonsService.defaultTONAmount)
    ),
    payload: swapTxParams.payload,
    seqno: seqno,
    sendMode: 3,
    }).send();
    }

    #transfer


    async transfer(userId: number, accountIndex: number, params: TransferParams): Promise<SendRequestAnswer> {
    const executingFunction = async () => {
    console.log(`try TON transfer with params:`, userId, accountIndex, params)
    const { toAddress, amount, jettonMasterAddress, queryId, comment } = params;
    const [connectedWallet, secretKey] = await Promise.all([
    this.walletService.getTONWallet({
    userId,
    accountIndex
    }),
    this.walletService.getTonWalletSecretKey({
    userId,
    accountIndex
    })
    ])
    const seqno = await connectedWallet.methods.seqno().call() || 0;
    await this.walletService.deployWallet({ userId, accountIndex })

    let response: { '@type': string } | undefined = undefined

    if (jettonMasterAddress && (TonRawAddressZero != jettonMasterAddress)) {
    const fromJettonMinter = new TonWeb.token.jetton.JettonMinter(connectedWallet.provider, { address: jettonMasterAddress });
    const fromJettonAddress = await fromJettonMinter.getJettonWalletAddress(new TonWeb.utils.Address((await connectedWallet.getAddress()).toString(true, true, false)));
    console.log("fromAddress jettonMinter", fromJettonAddress.toString(true, true, false)) // адрес отправителя жетонов

    const toJettonMinter = new TonWeb.token.jetton.JettonMinter(connectedWallet.provider, { address: jettonMasterAddress });
    const toJettonAddress = await toJettonMinter.getJettonWalletAddress(new TonWeb.utils.Address(toAddress));
    console.log("toAddress jettonMinter", toJettonAddress.toString(true, true, false)) // адрес получателя жетонов

    const jettonWallet = new JettonWallet(connectedWallet.provider, {
    address: fromJettonAddress.toString(true, true, false)
    });

    response = await connectedWallet.methods.transfer({
    secretKey,
    toAddress: new TonWeb.Address(fromJettonAddress),
    amount: TonWeb.utils.toNano('0.25'),
    seqno: seqno,
    payload: await jettonWallet.createTransferBody({
    jettonAmount: amount,
    toAddress: new TonWeb.Address(toAddress),
    forwardAmount: TonWeb.utils.toNano('0.01'),
    forwardPayload: comment ?? '',
    queryId,
    responseAddress: new TonWeb.Address((await connectedWallet.getAddress()).toString(true, true, false))
    }),
    sendMode: 3,
    }).send()

    console.log("transfer response ", response)
    } else {
    response = await connectedWallet.methods.transfer({
    secretKey,
    toAddress: new TonWeb.Address(toAddress).toString(true, true, false),
    amount: amount,
    payload: `${queryId}`,
    seqno: seqno,
    sendMode: 3,
    }).send();

    console.log("transfer response ", response)
    }
    return {
    isSent: response?.["@type"] == 'ok'
    }
    }
    return await this.functionWrapper(
    executingFunction,
    (result: SendRequestAnswer) => {
    return result.isSent
    },
    4,
    ) as SendRequestAnswer

    }