2

I need to send USDT and other ERC-20 tokens via Geth on Ethereum. I'm using PHP and Web3.php library to work with Ethereum. So far, it worked well for everything. Now, I need to start sending tokens based on contracts.
In the official documents of Ethereum, it said about data parametere:

The compiled code of a contract OR the hash of the invoked method signature and encoded parameters

I thought I could use sendTransaction() to send ERC-20 tokens directly, so I used the given USDT encoded ABI in the data parameter, but it did not work. Then I came back to Web3.php project, and saw there are different codes for contracts like this:

use Web3\Contract;

$contract = new Contract('http://localhost:8545', $abi);

// deploy contract
$contract->bytecode($bytecode)->new($params, $callback);

// call contract function
$contract->at($contractAddress)->call($functionName, $params, $callback);

// change function state
$contract->at($contractAddress)->send($functionName, $params, $callback);

// estimate deploy contract gas
$contract->bytecode($bytecode)->estimateGas($params, $callback);

// estimate function gas
$contract->at($contractAddress)->estimateGas($functionName, $params, $callback);

// get constructor data
$constructorData = $contract->bytecode($bytecode)->getData($params);

// get function data
$functionData = $contract->at($contractAddress)->getData($functionName, $params);

Unfortunately, as I am new in contracts, I do not know which one of the codes above should be used for my project (Sending USDT). All of them? or just some of them?
And unfortunately, the provided document for the methods inside this section did not have details about used parameters! This made me more confused.
May you help me please?

1
  • Where is the from? Or it needs to sign by private key of from?
    – Louis
    Apr 5, 2020 at 3:00

1 Answer 1

1

The steps to send an ERC-20 token are:

  1. Get the deployed contract instance.
  2. Get the amount to send, the receiver, and the sender.
  3. Invoke the ERC-20 contract's transfer function.

Code for the above steps are:

Step 1:
    use Web3\Contract;
    $contractAddress = 'ERC-20 contract deployed address'
    $contract = new Contract('http://localhost:8545', $abi);
    $contractInstance = $contract->at($contractAddress);

Step 2:

    $amount = 100, $receiver = 'Receiver addresss'

Step 3:

    $contractInstance->call(transfer, [$amount, $receiver], $callback);

This will get you going.

4
  • Thanks @balajipachai, but where is $from? Also, shouldn't I put transfer in quotations? Mar 29, 2020 at 9:43
  • Give it a try, I have given you directions, go ahead and find the way. Mar 29, 2020 at 10:03
  • I have 4 USDTs in one account and want to send 1 to another address. But I always receive this error: insufficient funds for gas * price + value. I've set amount to 1 Mar 30, 2020 at 9:10
  • ethereum.stackexchange.com/questions/9043/… this would help you to understand the above error and fix it Mar 31, 2020 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.