首页 > 其他分享 >etherjs调用合约过程

etherjs调用合约过程

时间:2022-11-05 22:24:33浏览次数:87  
标签:调用 name uint256 合约 bytes32 etherjs address type internalType

前言:调用合约成功,虽然是一小步,但总算有进展了,慢慢往前。

 

 

// library是provider
const { account, library, active } = useWeb3React();


// 取消挂单
  async function cancelOrder(orderHash: string) {
    var privateKey = "";
    let wallet = new ethers.Wallet(privateKey, library);
var contractAddr = "0xf1730217Bd65f86D2F008f1821D8Ca9A26d64619"; var jsonStr = '[{"inputs":[{"internalType":"address","name":"perpetualV1","type":"address"},{"internalType":"uint256","name":"chainId","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"maker","type":"address"},{"indexed":false,"internalType":"bytes32","name":"orderHash","type":"bytes32"}],"name":"LogOrderApproved","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"maker","type":"address"},{"indexed":false,"internalType":"bytes32","name":"orderHash","type":"bytes32"}],"name":"LogOrderCanceled","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes32","name":"orderHash","type":"bytes32"},{"indexed":false,"internalType":"bytes32","name":"flags","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"triggerPrice","type":"uint256"},{"components":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"price","type":"uint256"},{"internalType":"uint256","name":"fee","type":"uint256"},{"internalType":"bool","name":"isNegativeFee","type":"bool"}],"indexed":false,"internalType":"structP1Orders.Fill","name":"fill","type":"tuple"}],"name":"LogOrderFilled","type":"event"},{"constant":true,"inputs":[],"name":"_EIP712_DOMAIN_HASH_","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"_FILLED_AMOUNT_","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_PERPETUAL_V1_","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"_STATUS_","outputs":[{"internalType":"enumP1Orders.OrderStatus","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"maker","type":"address"},{"internalType":"address","name":"taker","type":"address"},{"internalType":"uint256","name":"price","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"},{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"trade","outputs":[{"components":[{"internalType":"uint256","name":"marginAmount","type":"uint256"},{"internalType":"uint256","name":"positionAmount","type":"uint256"},{"internalType":"bool","name":"isBuy","type":"bool"},{"internalType":"bytes32","name":"traderFlags","type":"bytes32"}],"internalType":"structP1Types.TradeResult","name":"","type":"tuple"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"components":[{"internalType":"bytes32","name":"flags","type":"bytes32"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"limitPrice","type":"uint256"},{"internalType":"uint256","name":"triggerPrice","type":"uint256"},{"internalType":"uint256","name":"limitFee","type":"uint256"},{"internalType":"address","name":"maker","type":"address"},{"internalType":"address","name":"taker","type":"address"},{"internalType":"uint256","name":"expiration","type":"uint256"}],"internalType":"structP1Orders.Order","name":"order","type":"tuple"}],"name":"approveOrder","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"components":[{"internalType":"bytes32","name":"flags","type":"bytes32"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"limitPrice","type":"uint256"},{"internalType":"uint256","name":"triggerPrice","type":"uint256"},{"internalType":"uint256","name":"limitFee","type":"uint256"},{"internalType":"address","name":"maker","type":"address"},{"internalType":"address","name":"taker","type":"address"},{"internalType":"uint256","name":"expiration","type":"uint256"}],"internalType":"structP1Orders.Order","name":"order","type":"tuple"}],"name":"cancelOrder","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"bytes32[]","name":"orderHashes","type":"bytes32[]"}],"name":"getOrdersStatus","outputs":[{"components":[{"internalType":"enumP1Orders.OrderStatus","name":"status","type":"uint8"},{"internalType":"uint256","name":"filledAmount","type":"uint256"}],"internalType":"structP1Orders.OrderQueryOutput[]","name":"","type":"tuple[]"}],"payable":false,"stateMutability":"view","type":"function"}]'; var jsonAbi = JSON.parse(jsonStr); // 取到合约 let contract = new ethers.Contract(contractAddr, jsonAbi, wallet); console.log(contract); contract.cancelOrder(); // await axios.post( // `http://${SERVER_HOST}:${SERVER_PORT}/orderbook/v1/cancelOrder`, // { ordersHash: [orderHash] } // ); // dispatch(removeOrder(orderHash)); }

 

参考:

https://blog.csdn.net/sanqima/article/details/121424571

 

标签:调用,name,uint256,合约,bytes32,etherjs,address,type,internalType
From: https://www.cnblogs.com/zccst/p/16861489.html

相关文章