tron-php icon indicating copy to clipboard operation
tron-php copied to clipboard

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

Open tianheng2017 opened this issue 3 years ago • 1 comments

        $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
)

tianheng2017 avatar Mar 15 '22 06:03 tianheng2017

参考#9 #13

Fenguoz avatar Mar 15 '22 09:03 Fenguoz