0x

0x:是否可以通過智能合約填寫訂單?

  • February 12, 2019

當我嘗試用契約中的資金填寫訂單時,我收到以下一般錯誤:

錯誤:事務:0x1d3161cd7f318c3b0b28fc34b2c6bca0cbf4efdb3b11fcf7ec3af7c9adfd821b 退出並出現錯誤(狀態 0)。給出的原因:TRANSFER_FAILED。請檢查交易: - 滿足 Solidityrequire語句設置的所有條件。- 不會觸發 Solidityrevert語句。

我有以下契約:

pragma solidity >=0.4.24;

Exchange exchange;
EthForERC20Token crowdsale;
WrapERC20Token wrapERC20Token;

constructor(
   address _exchangeAddress
) 
public 
{
   // The local 0x exchange intstance
   exchange = Exchange(_exchangeAddress);
   // Grand unlimited allownace to the exchange contract.
   wrapERC20Token = new WrapERC20Token(address(exchange)); 
}

function fillOrder (
   Exchange.Order memory _order, 
   uint256 _assetFillAmount,
   bytes memory _signature
) 
public 
payable
{
   exchange.fillOrder(_order,_assetFillAmount, _signature);
}

function getWrappedERC20Address() 
public 
view 
returns(address) {
   return address(wrapERC20Token);
}

我進行了測試,合約擁有足夠的 WrappedERC20Token,它只是一個 ERC20 代幣,可以選擇預先批准一個無限量的地址。

我的0x訂單創建如下:

const wrappedShareAddress = await contract.getWrappedShareAddress();
const maker = accounts[0];
const makerAssetData = zeroX.assetDataUtils.encodeERC20AssetData(etherToken);
const takerAssetData = zeroX.assetDataUtils.encodeERC20AssetData(wrappedShareAddress);
const contractWrappers = new zeroX.ContractWrappers(providerEngine, { networkId: 50 });


const order = {
           exchangeAddress: exchange,
           makerAddress: maker.toLowerCase(),
           takerAddress: NULL_ADDRESS,
           senderAddress: NULL_ADDRESS,
           feeRecipientAddress: NULL_ADDRESS,
           expirationTimeSeconds: (1581437362).toString(),
           salt: zeroX.generatePseudoRandomSalt().toString(),
           makerAssetAmount: new zeroX.BigNumber("5e17").toFixed(),
           takerAssetAmount: new zeroX.BigNumber("5e17").toFixed(),
           makerAssetData,
           takerAssetData,
           makerFee: new zeroX.BigNumber("0").toFixed(),
       takerFee: new zeroX.BigNumber("0").toFixed(),
       };
       // console.log(order);
       // Generate the order hash and sign it
       const orderHashHex = zeroX.orderHashUtils.getOrderHashHex(order);
   signature = await zeroX.signatureUtils.ecSignHashAsync(providerEngine, orderHashHex, maker);
       signedOrder = {...order, signature};
   const makerWETHApprovalTxHash = await contractWrappers.erc20Token.setUnlimitedProxyAllowanceAsync(
           etherToken,
           maker,
       );

       await toTxhash.mineTx(makerWETHApprovalTxHash);

       const makerWETHDepositTxHash = await contractWrappers.etherToken.depositAsync(
           etherToken,
           new zeroX.BigNumber("2e18"),
           maker,
       );

       await toTxhash.mineTx(makerWETHDepositTxHash);

然後我使用以下方法呼叫智能合約函式:

   const res = await contract.fillOrder(
       signedOrder,
       new zeroX.BigNumber("5e17").toFixed(),
       signature,
       {from: maker, value: new zeroX.BigNumber("1e18").toFixed()}
   );

您的合約需要批准 0x ERC20Proxy 才能移動您的 ERC20 代幣。

例如,看一下在建構子中執行此操作的Forwarder 合約。

引用自:https://ethereum.stackexchange.com/questions/66975