furqansiddiqui / erc20-php

Interact with Ethereum ERC20 Tokens
MIT License
163 stars 96 forks source link

how to call transfer method #1

Closed logan2013 closed 6 years ago

logan2013 commented 6 years ago

how can i call transfer method. thank you so much!

furqansiddiqui commented 6 years ago

I am still working on it, that's why there is no "release" yet. Give me a day or 2

furqansiddiqui commented 6 years ago

I have added transfer() and encodeTransferData() methods, use the string response from encodeTransferData() method as value to "data" param when sending Ethereum transactions

dformdotdk commented 6 years ago

Could you please provide an example for transfer token using your API?

So far I'm getting the encoded string response, but I don't know what to do with it? What function do I call to transfer token, and what parameters do I pass to it?

Thanks

This is the code I have now:

require('vendor/autoload.php');

use EthereumRPC\EthereumRPC;
use ERC20\ERC20;

$geth = new EthereumRPC('IPADDRESS', 8545);
$erc20 = new \ERC20\ERC20($geth);

// Declare the token smart contract address
$token = $erc20->token('0x00000000000000000000000000000000000000');

// Send token to address
$to = '0x00000000000000000000000000000000000000';
$amount = '1000';
$encodedResponse = $token->encodedTransferData($to, $amount);
anthonypaslange commented 6 years ago

@dyvel the author sent me some example code on how to send ERC20 tokens with this library.

$geth = new \EthereumRPC\EthereumRPC('127.0.0.1', 8545); $erc20 = new \ERC20\ERC20($geth);

// Grab Token Instance $token = $erc20->token("0x...CONTRACT-address"); // ERC20 Token Transfer Payee & Amount $data = $token->encodedTransferData("0x...PAYEE-address", "1.12345"); // Prepare ETH transaction $tx = $geth->personal()->transaction("0x...PAYER-address", "0x...CONTRACT-address") ->amount("0") // Amount in ETH to transfer ->data($data);

// Send Transaction $txId = $tx->send("PASSWORD");

(Sorry code formatting doesn't work on this one.)

dformdotdk commented 6 years ago

Thanks @mrpetfreak - tried to implent your code, but when I get to send traction, I'm getting this error:

PHP Fatal error:  Uncaught EthereumRPC\\Exception\\GethException: The method personal_sendTransaction does not exist/is not available in /home/DOMAIN/public_html/eth/vendor/furqansiddiqui/ethereum-rpc/src/EthereumRPC.php:159
Stack trace:
#0 /home/DOMAIN/public_html/eth/vendor/furqansiddiqui/ethereum-rpc/src/API/Personal.php(50): EthereumRPC\\EthereumRPC->jsonRPC('personal_sendTr...', NULL, Array)
#1 /home/DOMAIN/public_html/eth/vendor/furqansiddiqui/ethereum-rpc/src/API/Personal.php(139): EthereumRPC\\API\\Personal->accountsRPC('personal_sendTr...', Array)
#2 /home/DOMAIN/public_html/eth/vendor/furqansiddiqui/ethereum-rpc/src/API/Personal/RawTransaction.php(163): EthereumRPC\\API\\Personal->send(Object(EthereumRPC\\API\\Personal\\RawTransaction), 'PASSWORD')
#3 /home/DOMAIN/public_html/eth/test.php(31): EthereumRPC\\API\\Personal\\RawTransaction->send('PASSWORD')
#4 {main}
thrown in /home/DOMAIN/public_html/eth/vendor/furqansiddiqui/ethereum-rpc/src/EthereumRPC.php on line 159

I have a RawTransaction Object created

anthonypaslange commented 6 years ago

Can you send the code you used exactly in here? Also, do you have installed the newest version of erc20-php and its dependencies?

dformdotdk commented 6 years ago

Here's the code:

I installed erc20-php and its dependencies just a few days ago, so should be the latest.

require('vendor/autoload.php');

use EthereumRPC\EthereumRPC;
use ERC20\ERC20;

$geth = new EthereumRPC('SERVER_IP', 8545);
$erc20 = new \ERC20\ERC20($geth);

$contractAddress    = '0x0000000000000000000000000000000000000000'; // Smart contract address
$payeeAddress       = '0x0000000000000000000000000000000000000000'; // Wallet to account
$payerAddress       = '0x0000000000000000000000000000000000000000'; // Wallet from account 
$password           = 'PASSWORD'; // Smart contract pass
$amount             = '10';

// Declare the token smart contract address
$token = $erc20->token($contractAddress);

// ERC20 Token Transfer Payee & Amount
$encodedResponse    = $token->encodedTransferData($payeeAddress, $amount);

// Prepare ETH transaction
$tx = $geth->personal()->transaction($payerAddress, $contractAddress)
->amount("0") // Amount in ETH to transfer
->data($encodedResponse);

// Send Transaction
$txId = $tx->send($password);
anthonypaslange commented 6 years ago

Try replacing $erc20 = new \ERC20\ERC20($geth);

with: $erc20 = new ERC20($geth);

dformdotdk commented 6 years ago

I tried - getting the same error message as before.

dformdotdk commented 6 years ago

If it's any help, this is my RawTransaction Object:

EthereumRPC\API\Personal\RawTransaction Object
(
    [_api:EthereumRPC\API\Personal\RawTransaction:private] => EthereumRPC\API\Personal Object
        (
            [client:EthereumRPC\API\Personal:private] => EthereumRPC\EthereumRPC Object
                (
                    [host:EthereumRPC\EthereumRPC:private] => 1XX.XXX.XXX.XXX
                    [port:EthereumRPC\EthereumRPC:private] => 8545
                    [ssl:EthereumRPC\EthereumRPC:private] => 
                    [eth:EthereumRPC\EthereumRPC:private] => EthereumRPC\API\Eth Object
                        (
                            [client:EthereumRPC\API\Eth:private] => EthereumRPC\EthereumRPC Object
 *RECURSION*
                        )

                    [personal:EthereumRPC\EthereumRPC:private] => EthereumRPC\API\Personal Object
 *RECURSION*
                )

        )

    [from:EthereumRPC\API\Personal\RawTransaction:private] => 0xe4e57347XXXXXXXXXX79A0F028EC3f61F7D00d66
    [to:EthereumRPC\API\Personal\RawTransaction:private] => 0x42151DE4cbXXXXXXXXXX5aA2bE8d62Cb9bAd2F3b
    [value:EthereumRPC\API\Personal\RawTransaction:private] => 0
    [gas:EthereumRPC\API\Personal\RawTransaction:private] => 
    [gasPrice:EthereumRPC\API\Personal\RawTransaction:private] => 
    [data:EthereumRPC\API\Personal\RawTransaction:private] => 0xa9059cbb000000000000000000000000a51F121bc759BXXXXXXXXXX320d380e5B6c97f8600000000000000000000000000000000000000000000003635c9adc5dea00000
)
furqansiddiqui commented 6 years ago

PHP Fatal error: Uncaught EthereumRPC\Exception\GethException: The method personal_sendTransaction does not exist/is not available

This error indicates that you are not using correct/latest version of ethereum-rpc lib. Please share your composer setup, and try

composer require furqansiddiqui/ethereum-rpc

dformdotdk commented 6 years ago

Tried your command - getting this message:

composer require furqansiddiqui/ethereum-rpc
Using version dev-master for furqansiddiqui/ethereum-rpc
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files

Looking at my EthereumRPC.php file, I find public const VERSION = "1.16.1"; Same as with the one here on GitHub - so it seems like it's updated.

I'm very new to composer, so I'm unsure about what you mean by composer setup. If it's the composer.json file, this is what it contains:

{ "minimum-stability": "dev", "require": { "furqansiddiqui/erc20-php": "dev-master", "kesar/ethereum-php": "dev-master",
        "furqansiddiqui/ethereum-rpc": "dev-master" } }
dformdotdk commented 6 years ago

Do you have an idea about what could cause this?

furqansiddiqui commented 6 years ago

try removing "kesar/ethereum-php" from dependencies list in your composer.json, I am not sure which package is that or if it conflicts with my ethereum-rpc package or not.

Try to do clean run again, your composer log doesn't show any error. Can you please tell me what error you get after this, I will reply promptly

furqansiddiqui commented 6 years ago

also you may check this post on transferring ERC20 tokens using this lib

https://www.furqansiddiqui.com/transfer-erc20-token-from-one-account-to-another-using-php/

dformdotdk commented 6 years ago

Tried remove all files and build it again without kesar/ethereum-php. Getting the same error as before. See this screenshot of my folder structure: https://www.dropbox.com/s/xyt3v0ocldjlaig/folder-structure.png?dl=0 Server is running PHP 7.1.18

furqansiddiqui commented 6 years ago

Are you sure that geth server has personal functions available? make sure that "personal" is added in argument passed to rpcapi flag when starting geth server.

Example: geth --rpc --rpcapi eth,web3,personal

dformdotdk commented 6 years ago

Thank you for all your help. Slowly getting there I think :-)

I'm using this to start geth on the server now: geth --rpc --rpcaddr "0.0.0.0" --rpcapi eth,web3,personal --rpccorsdomain "*" --light --rinkeby

But now I'm getting this error below - looks like I'm getting closer. The contract exists on Rinkeby and I can get the token name, and balance of payer and payee, so I'm uncertain about what error I've made.

Fatal error: Uncaught EthereumRPC\Exception\GethException: unknown account in /home/DOMAIN/public_html/eth/vendor/furqansiddiqui/ethereum-rpc/src/EthereumRPC.php:159

If I do a personal.listAccounts in geth, a [] is returned - do I have to import my account in geth first?

furqansiddiqui commented 6 years ago

yes I think you need to import keystore files

dformdotdk commented 6 years ago

I created a new account and added funds to it, and got it working. :-) Thank you so much for all your help - much appreciated.

So when we declare the smart contract address, we never use the smart contract besides getting the token information.

The sender address needs to be added as an account on the geth server and has sufficient funds (eth for gas + token for sending to the receivers address)

furqansiddiqui commented 6 years ago

you're welcome

secondly, I don't understand what you mean in second part of your comment, but if you are talking about transferring tokens then actual payee address is placed in encodeTransferMethod of erc20-php lib while transaction is sent to contract address via ethereum-rpc lib. Please refer to these 2 links for further explanation:

https://www.furqansiddiqui.com/working-with-erc20-tokens-using-php/

https://www.furqansiddiqui.com/transfer-erc20-token-from-one-account-to-another-using-php/

dformdotdk commented 6 years ago

It was just an observation, but I see that was wrong. I wrongly assumed a transfer of a token between 2 accounts would not require the involvement of the smart contract.

aunakbar commented 2 years ago

Thanks for this Library :) Transfer code working fine on local with hOST: 127.0.0.1 Port: 8545 and using Ganache. But on live it gives an error

Fatal error: Uncaught EthereumRPC\Exception\ConnectionException: cURL error [0]: Could not resolve host: http in /web3/vendor/furqansiddiqui/ethereum-rpc/src/EthereumRPC.php:149 Stack trace: #0 /web3/vendor/furqansiddiqui/ethereum-rpc/src/Contracts/Contract.php(85): EthereumRPC\EthereumRPC->jsonRPC('eth_call', NULL, Array) #1 /web3/vendor/furqansiddiqui/erc20-php/src/ERC20_Token.php(98): EthereumRPC\Contracts\Contract->call('decimals') #2 /web3/vendor/furqansiddiqui/erc20-php/src/ERC20_Token.php(210): ERC20\ERC20_Token->decimals() #3 /web3/index.php(21): ERC20\ERC20_Token->encodedTransferData('0xba7ccaf64d10a...', '100') #4 {main} thrown in /web3/vendor/furqansiddiqui/ethereum-rpc/src/EthereumRPC.php on line 149

This is my code please guide.

require('vendor/autoload.php');

use EthereumRPC\EthereumRPC;
use ERC20\ERC20;

$geth = new EthereumRPC('http://rpc.slock.it/goerli',8545);
$erc20 = new \ERC20\ERC20($geth);

$contractAddress    = '00000000000000000000000000000000000000000'; // Smart contract address
$payerAddress       = "0x1489F5022108aCC0c4cEA36b5E9e66b877a3ebD1"; // Sender's Ethereum account
$payeeAddress       = "0xba7ccaf64d10af77e3374d3c1d90B59C88E714D2"; // Recipient's Ethereum account
$password           = '00000000000000000000000000000000000000000000000'; // Smart contract pass
$amount             = '1';

// Declare the token smart contract address
$token = $erc20->token($contractAddress);

// ERC20 Token Transfer Payee & Amount
$encodedResponse    = $token->encodedTransferData($payeeAddress, $amount);

// Prepare ETH transaction 
$tx = $geth->personal()->transaction($payerAddress, $payeeAddress)
->amount("1") // Amount in ETH to transfer
->data($encodedResponse);

// Send Transaction
echo $txId = $tx->send($password);