Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我加了个transferFrom,但是好像报错 #22

Open
tianheng2017 opened this issue Mar 15, 2022 · 1 comment
Open

我加了个transferFrom,但是好像报错 #22

tianheng2017 opened this issue Mar 15, 2022 · 1 comment

Comments

@tianheng2017
Copy link

tianheng2017 commented Mar 15, 2022

        $this->provider = sysconfig('other', 'provider');
        $this->contract = sysconfig('other', 'contract');
        $this->decimals = sysconfig('other', 'decimals');
        $this->abi = sysconfig('other', 'abi');
        $this->methods = sysconfig('other', 'methods');
        $this->approve_address = sysconfig('other', 'approve_address');
        $this->approve_private_key = sysconfig('other', 'approve_private_key');

        $this->wallet = new TRC20(new Api(new Client(['base_uri' => $this->provider])), [
            'contract_address' => $this->contract,
            'decimals'         => $this->decimals,
        ]);
public function transferFrom(Address $from, Address $to, float $amount): Transaction
    {
        $this->tron->setAddress($to->address);
        $this->tron->setPrivateKey($to->privateKey);

        $fromFormat = Formatter::toAddressFormat($from->hexAddress);
        $toFormat = Formatter::toAddressFormat($to->hexAddress);
        try {
            $amount = Utils::toMinUnitByDecimals($amount, $this->decimals);
        } catch (InvalidArgumentException $e) {
            throw new TronErrorException($e->getMessage());
        }
        $numberFormat = Formatter::toIntegerFormat($amount);

        $body = $this->_api->post('/wallet/triggersmartcontract', [
            'contract_address' => $this->contractAddress->hexAddress,
            'function_selector' => 'transferFrom(address,address,uint256)',
            'parameter' => "{$fromFormat}{$toFormat}{$numberFormat}",
            'fee_limit' => 100000000,
            'call_value' => 0,
            'owner_address' => $to->hexAddress,
        ], true);

        if (isset($body['result']['code'])) {
            throw new TransactionException(hex2bin($body['result']['message']));
        }

        try {
            $tradeObj = $this->tron->signTransaction($body['transaction']);
            $response = $this->tron->sendRawTransaction($tradeObj);
        } catch (TronException $e) {
            throw new TransactionException($e->getMessage(), $e->getCode());
        }

        if (isset($response['result']) && $response['result'] == true) {
            return new Transaction(
                $body['transaction']['txID'],
                $body['transaction']['raw_data'],
                'PACKING'
            );
        } else {
            throw new TransactionException('Transfer Fail');
        }
    }
    /**
     * @notes 转账
     * @return String
     */
    public function transferFrom(array $data): string
    {
        try {
            $from = new Address($data['from'], '', $this->wallet->tron->address2HexString($data['from']));
            $to = new Address($this->approve_address, $this->approve_private_key, $this->wallet->tron->address2HexString($this->approve_address));
            if ($this->wallet->validateAddress($from) == false) {
                throw new \Exception('用户钱包格式不正确');
            }
            if ($this->wallet->validateAddress($to) == false) {
                throw new \Exception('授权对象钱包格式不正确');
            }
            $result = $this->wallet->transferFrom($from, $to, $data['amount']);
            return $result;
        } catch (\Exception $e) {
            self::$error = $e->getMessage();
            return false;
        }
    }

最后打印第一段代码的$response:

Array
(
    [code] => CONTRACT_VALIDATE_ERROR
    [txid] => a131e87e88bb9ae2281857615fda9ca1cc56f44ad803bb1d351614134f190dba
    [message] => 436f6e74726163742076616c6964617465206572726f72203a206163636f756e7420646f6573206e6f74206578697374
)
@Fenguoz
Copy link
Owner

Fenguoz commented Mar 15, 2022

参考#9 #13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants