web3p/web3.php

Lack of documentation make doing simple things almost impossible

Schiocco opened this issue · 2 comments

Hi

I want to thank you very much who made this PHP version of Web3. Unfortunately, there is no documentation and it is almost impossible to do anything, including very simple things like getting an encoded ABI string. While this is obvious for most libraries, in the case of Web3 the problem is much bigger because the libraries in other languages (e.g. JS) have completely different methods and structures, making it very hard to convert the code to another language, impossible in most cases.

I have now lost 20 hours and about $100 USD(yes, I must use mainnet because other testnets are not working the same as mainnet and this is confirmed by the fact that the exact same code works for mainnet but not on testnets) to make working the Uniswap function swapExactTokensForETH or swapExactTokensForTokens.

Online there is no PHP version help and JS version is totally different. E.g. it uses a method TransferFrom that does not exist in the PHP version. I know the error is the below:

You need to firstly approve DAI from your wallet to your contract address and then you can add this line after your approval call in the trade function:

With JS version is easy: IERC20(address(token)).approve(address(router), amountToApprove);
Unfortunately, it is impossible to understand how to do it via PHP version. I tried a lot of variants like below but none is working:

....
function __construct($rpcUrl, $tokenAddress, $ethAddress) {
        $this->ethAddress = $ethAddress;
        $this->rpcUrl = $rpcUrl;
        $this->web3 = new Web3($rpcUrl);
        $this->token = new Contract($this->web3->provider, $this->tokenabi);
        $this->token->at($tokenAddress);
        $this->altcoin = new Contract($this->web3->provider, $this->altcoinabi);
        $this->altcoin->at($ethAddress);
        $this->route = new Contract($this->web3->provider, $this->routeabi);
        $this->route->at($ethAddress);

        $this->ttt = new Contract($this->web3->provider, $this->altcoinabi);
        $this->ttt->at(bxc_eth_get_contract_address('usdc'));
        $this->xxx = new Contract($this->web3->provider, $this->routeabi);
        $this->xxx->at($this->routerAddress);
    }

    public function approve($amount) {
        // erc20.transferFrom(msg.sender, _contractAddress, _amountIn);
        $this->transfer(CONTRACT ADDRESS FROM, '0.1',  CONTRACT ADDRESS FROM);

        // erc20.approve(UNISWAP_ROUTER_ADDRESS, _amountIn);
        $data = $this->ttt->getData('approve', $this->routerAddress, bcmul(strval($amount), 1000000));
        return $this->tx($this->ethAddress, '', '0x' . $data);
    }

    public function swap($amount, $cryptocurrency_code_from, $cryptocurrency_code_to, $contract_address, $contract_address_from) {
        $data = $this->xxx->getData('swapExactTokensForETH', bcmul(strval($amount), 1000000), '0', [$contract_address_from, $this->wethAddress], $this->ethAddress, time() + 180);
        return $this->tx($this->routerAddress, '', '0x' . $data);
    }
...

I call this code with this function:

  $node = new Node(NODE URL, MY ETH ADDRESS, MY ETH ADDRESS);
    $node->setKey(KEY);
    $response_approve = $node->approve($amount);
   $node->swap($amount, $cryptocurrency_code_from, $cryptocurrency_code_to, CONTRACT ADDRESS TO, CONTRACT ADDRESS FROM);
    }

Mind that I'm able to use swapExactETHForTokens, it works, so all shared code parts are working.

It is very frustrating to have a PHP library and know that it can do what you need, but not be able to do anything due to a lack of documentation or any tutorial online. Does anyone know how to make it work? I can hire and pay as well.

Thank you!
Regards

I can give some help if you need.