Giter Site home page Giter Site logo

php-erc20's Introduction

Simple Ethereum ERC20 token php library

This library provides simple way to interact with Ethereum ERC20 token.
By default, supports all ERC20 Standard functions (like balanceOf, transfer, transferFrom, approve, allowance, decimal, name, ...) also can be extends to support other contracts as well.

Installation

composer require lessmore92/php-erc20

Usage

There are two ways to use:

1- Make a new class for your token and specified their functions

2- Use general class with all standard functions

See below to find out more

1-Make a new class for your token

Simply create a new class inherits from \Lessmore92\Ethereum\Foundation\StandardERC20Token

in below sample we create a new class for Tether (USDT)

class USDT extends \Lessmore92\Ethereum\Foundation\StandardERC20Token 
{
    protected $contractAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7";  
}

Then for use create new instantiate from your class and

$tether = new USDT("https://mainnet.infura.io/v3/API_KEY");
var_dump($tether->name());
var_dump($tether->decimals());

2- Use general class

$token = new \Lessmore92\Ethereum\Token("0xdac17f958d2ee523a2206206994597c13d831ec7", "https://mainnet.infura.io/v3/API_KEY");
var_dump($token->name());

Connection Timeout

Connection timeout can be set by last parameter of token class

$timeout  = 3; //secs
$tether = new USDT("https://mainnet.infura.io/v3/API_KEY",$timeout);

OR

$timeout  = 3; //secs
$tether = new \Lessmore92\Ethereum\Token("0xdac17f958d2ee523a2206206994597c13d831ec7", "https://mainnet.infura.io/v3/API_KEY", $timeout);

Ethereum RPC Client

For connect to Ethereum blockchain you need an Ethereum node; Infura is a simple and fast solution, however you can launch you Geth node

ERC20 Token transferFrom

ERC20 transaction fee needs to be paid in ETH. In some situation your app needs to pay this fee behalf of user.
Suppose, user A have a key pair (private, public) and all their transaction is limited to usdt. User A needs to send 10 usdt, but he/she haven't ETH to pay transaction fee.
In these cases your app should pay fee behalf of users.
transferFrom is a good solution in these cases.

transferFrom Flow:

1.First, Using approve method to grant permission to a delegator.
2.Then, Using transferFrom method to make transaction behalf of user.

In Action

$owner_private = '0xcf29c83a88e23d0b9e676beca426490bf79aca71e9d24f79a99d30c48292e1e3';
$owner_address = '0xA7e5F270c27E9d33911EE7D50D8E814f793d2760';

$myapp_private = '0xa6b6be193bfeac6160178ee6e1435609ae566a9054715e0802e4c3b39bb94e83';
$myapp_address = '0x8dC9b3c20795815aa063FEdBE8E564566CEc1893';

$to_address = '0x245013F05DdA116142Ca8db205ec4F8C780E3DcB';

//by this method we allow $myapp_address to send upto 99999 token behalf of $owner_address
$approve_tx    = $token->approve($owner_address, $myapp_address, 99999);
$approve_tx_id = $approve_tx->sign($owner_private)
                            ->send()
;


//the magic is here, $myapp_address send 10 tokens behalf of user and $myapp_address pay transaction fee
$transfer_tx    = $token->transferFrom($myapp_address, $owner_address, $to_address, 10);
$transfer_tx_id = $transfer_tx->sign($myapp_private)
                              ->send()
;

allowance to check how much transferFrom remain

$remain = $token->allowance($owner_address, $myapp_address);

Notices:
approve method not need to be used on every transaction.
To revoke transferFrom permission call $token->approve($owner_address, $myapp_address, 0) by amount 0.

php-erc20's People

Contributors

lessmore92 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

php-erc20's Issues

Got the transaction HASH, but the transaction was not successful

$token = new \Lessmore92\Ethereum\Token($contractAddress, "https://mainnet.infura.io/v3/id");
try {
$transfer_tx = $token->transferFrom($contractAddress, $from, $to, '0.0001', 'default', '700000');
dump($transfer_tx);
$transfer_tx_id = $transfer_tx->sign($privateKey)->send();
var_dump($transfer_tx_id);
}catch (Exception $exception) {
var_dump($exception->getMessage());
}

The above code gets the HASH of the transaction, 0x278610b300c70d6a61283d08020618cf24abe1fbc3e8f1ff56eb88c58bf739e6
Is this a successful transaction?
But the HASH of the transaction cannot query the data on the website
https://eth.tokenview.com/cn/tx/0x278610b300c70d6a61283d08020618cf24abe1fbc3e8f1ff56eb88c58bf739e6
Is this something I'm using wrong here?

Impossible to send approve without ETH for gas

@lessmore92 Hello mate, you state that by

//by this method we allow $myapp_address to send upto 99999 token behalf of $owner_address
$approve_tx    = $token->approve($owner_address, $myapp_address, 99999);
$approve_tx_id = $approve_tx->sign($owner_private)
                            ->send()

$owner_address allows $myapp_address to send some USDT amount to any address without $owner_address having anything but USDT in his wallet.

Well how is this possible to work while for approve() to work you have to send a transaction which needs gas (ETH)?

I tried with account only having token inside and got

message: "insufficient funds for transfer"
code: -32000

Here is a TX on etherscan showing the exact approve() method called that costs $31 😃

Always insufficient funds for gas * price + value

$owner_private = '0x922b775cef0410717c28e0b04c23ffa5a0e2*********';
$owner_address = '0x9118cc4212dCC83dca639de28FC0D47AfdE4C45F';

$token = new Token("0xdac17f958d2ee523a2206206994597c13d831ec7","https://mainnet.infura.io/v3/d5cd9b58d5ca43409486******");
//by this method we allow $myapp_address to send upto 99999 token behalf of $owner_address
$approve_tx = $token->approve($owner_address, "0x4E0E4CbdfBaB08e65F5ECAb25bEAA*******", 9999);
$approve_tx_id = $approve_tx->sign($owner_private)->send();

Hello author, I have a problem!
It keeps prompting insufficient funds for gas
But there is enough ETH in the account
can you help me

Got the transaction HASH, but the transaction was not successful

$token = new \Lessmore92\Ethereum\Token($contractAddress, "https://mainnet.infura.io/v3/id");
try {
$transfer_tx = $token->transferFrom($contractAddress, $from, $to, '0.0001', 'default', '700000');
dump($transfer_tx);
$transfer_tx_id = $transfer_tx->sign($privateKey)->send();
var_dump($transfer_tx_id);
}catch (Exception $exception) {
var_dump($exception->getMessage());
}

The above code gets the HASH of the transaction, 0x278610b300c70d6a61283d08020618cf24abe1fbc3e8f1ff56eb88c58bf739e6
Is this a successful transaction?
But the HASH of the transaction cannot query the data on the website
https://eth.tokenview.com/cn/tx/0x278610b300c70d6a61283d08020618cf24abe1fbc3e8f1ff56eb88c58bf739e6
Is this something I'm using wrong here?

I can't understand the parameters.

Hello, I can't understand the parameters, why is the owner, what's going on with myapp, and there is a to address, I'm very confused please help.

How to handle gasPrice and Limits

I'm trying to send Ethereum, thanks for the help last time, but I keep getting this error:
"{"success":false,"message":"insufficient funds for gas * price + value"}"

The message is the reason for failure. I have $1.01 worth of ETH, I know it's small but I wanted to send half of that to my other wallet to see if the transfer send() method completes successfully. Maybe the value I input is too big?

The amount is multiplied by the 18th power of 10, I assume because value is in Wei?

Approve Token

It would be nice to add a function to check the amount of approved tokens for an address for a particular contract.

I Need ERC721 In Php

Hi Sir. Do You Have ERC721 PHP Library ? If You Have Please sent The Link as soon as possible.

CURL ERROR

hello Sir , While Running This Code Im Getting Below Error. Can You Help Me With This.

PHP Fatal error: Uncaught GuzzleHttp\Exception\RequestException: cURL error 3: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in C:\Users\hp\OneDrive\Desktop\testLibrarySdk\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php:201
Stack trace:
#0 C:\Users\hp\OneDrive\Desktop\testLibrarySdk\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(155): GuzzleHttp\Handler\CurlFactory::createRejection()
#1 C:\Users\hp\OneDrive\Desktop\testLibrarySdk\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(105): GuzzleHttp\Handler\CurlFactory::finishError()
#2 C:\Users\hp\OneDrive\Desktop\testLibrarySdk\vendor\guzzlehttp\guzzle\src\Handler\CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish()
#3 C:\Users\hp\OneDrive\Desktop\testLibrarySdk\vendor\guzzlehttp\guzzle\src\Handler\Proxy.php(28): GuzzleHttp\Handler\CurlHandler->__invoke()
#4 C:\Users\hp\OneDrive\Desktop\testLibrarySdk\vendor\guzzlehttp\guzzle\src\Handler\Proxy.php(51): GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler{closure}()
#5 C:\Users\hp\OneDrive\Desktop\tes in C:\Users\hp\OneDrive\Desktop\testLibrarySdk\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 201
PS C:\Users\hp\OneDrive\Desktop\testLibrarySdk\src\test>

insufficient funds for gas * price + value

$token = new USDT("https://mainnet.infura.io/v3/8038c7c7********");

$owner_address = '0x1d64720e43342bdd0E824e1dadEF';
$myapp_private = '0xbf15a4a052a3b59c1a55988e8a1e35056fbfacf94ce';
$myapp_address = '0x907Dc5716029B907BfFB223f0A*******';
$to_address = '0x907Dc5716029B907BfFB2*******';

$transfer_tx = $token->transferFrom($myapp_address, $owner_address, $to_address, 1);
$transfer_tx_id = $transfer_tx->sign($myapp_private)->send();

How to solve this please?

Uncaught RuntimeException: insufficient funds for gas * price + value

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.