Skip to content

jetton transfer  #28

Description

@mahdi4187

image

<?php

namespace App\Services;

use Exception;
use Http\Client\Common\HttpMethodsClient;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Olifanton\Mnemonic\TonMnemonic;
use Olifanton\Ton\Transports\Toncenter\ClientOptions;
use Olifanton\Ton\Transports\Toncenter\ToncenterHttpV2Client;
use Olifanton\Ton\Transports\Toncenter\ToncenterTransport;
use Olifanton\Interop\Address;
use Olifanton\Interop\Boc\SnakeString;
use Olifanton\Interop\Units;
use Olifanton\Ton\AddressState;
use Olifanton\Ton\Contracts\Jetton\JettonMinter;
use Olifanton\Ton\Contracts\Jetton\JettonWallet;
use Olifanton\Ton\Contracts\Jetton\JettonWalletOptions;
use Olifanton\Ton\Contracts\Jetton\TransferJettonOptions;
use Olifanton\Ton\Contracts\Wallets\Transfer;
use Olifanton\Ton\Contracts\Wallets\TransferOptions;
use Olifanton\Ton\Contracts\Wallets\V4\WalletV4Options;
use Olifanton\Ton\Contracts\Wallets\V4\WalletV4R2;
use Olifanton\Ton\SendMode;

class TONService
{
    private $isMainnet = true;
    private $httpClient, $toncenter, $transport, $kp, $wallet, $walletAddress;
    public function __construct()
    {
        $this->isMainnet = config('services.ton.is_mainnet');
        $this->httpClient = new HttpMethodsClient(
            Psr18ClientDiscovery::find(),
            Psr17FactoryDiscovery::findRequestFactory(),
            Psr17FactoryDiscovery::findStreamFactory(),
        );
        $this->toncenter = new ToncenterHttpV2Client(
            $this->httpClient,
            new ClientOptions(
                $this->isMainnet ? 'https://toncenter.com/api/v2' : 'https://test.toncenter.com/api/v2',
                $this->isMainnet ? config('services.ton.toncenter_api_key_mainnet') : config('services.ton.toncenter_api_key_testnet'),
            ),
        );
        $this->transport = new ToncenterTransport($this->toncenter);

        $words = explode(" ", trim(config("services.ton.wallet_words")));
        $this->kp = TonMnemonic::mnemonicToKeyPair($words);

        $this->wallet = new WalletV4R2(
            new WalletV4Options(
                publicKey: $this->kp->publicKey,
            ),
        );
        $this->walletAddress = $this->wallet->getAddress();
    }
    //for example: jetton usdt address = EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs
    public function jettonTransfer($address, $comment, $amount, $jettonAddress)
    {
        try {
            $recipient = new Address($address);

            $jettonRoot = JettonMinter::fromAddress(
                $this->transport,
                new Address($jettonAddress),
            );
            $jettonWalletAddress = $jettonRoot->getJettonWalletAddress($this->transport, $this->walletAddress);
            $jettonWallet = new JettonWallet(new JettonWalletOptions(
                address: $jettonWalletAddress,
            ));

            $state = $this->transport->getState($jettonWalletAddress);

            if ($state !== AddressState::ACTIVE) {
                throw new Exception("Your jetton wallet is not initialized, you cannot perform a transfer of funds that do not exist");
            }

            $extMessage = $this->wallet->createTransferMessage([
                new Transfer(
                    dest: $jettonWalletAddress,
                    amount: Units::toNano("0.1"),
                    payload: $jettonWallet->createTransferBody(
                        new TransferJettonOptions(
                            jettonAmount: Units::toNano($amount, Units::USDt),
                            toAddress: $recipient,
                            responseAddress: $this->walletAddress,
                            forwardPayload: SnakeString::fromString($comment)->cell(true),
                            forwardAmount: Units::toNano("0.0000001"),
                        ),
                    ),
                    sendMode: SendMode::IGNORE_ERRORS->combine(SendMode::PAY_GAS_SEPARATELY, SendMode::CARRY_ALL_REMAINING_INCOMING_VALUE),
                )
            ], new TransferOptions(
                seqno: (int)$this->wallet->seqno($this->transport),
            ));
            $this->transport->sendMessage($extMessage, $this->kp->secretKey);
        } catch (Exception $e) {
            dd($e->getMessage(), $e);
        }
    }
}

Hi i send USDT but in transaction list get UKWN

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions