import { BigNumber, ethers } from 'ethers'
const { library, account } = useWeb3React()
const ticketAddress = "..."
const ticketId = "..."
const TicketABI = "..." // you can get the ABI from the reference document
const TICKET_CONTRACT = getContract(ticketAddress, TicketABI, library, account)
const GAS_MARGIN = BigNumber.from(1000)
function calculateGasMargin(value: BigNumber, margin: BigNumber) {
const offset = value.mul(margin).div(BigNumber.from(10000))
return value.add(offset)
}
const handleStamp = () => {
if (ticketAddress && ticketId !== undefined && TICKET_CONTRACT) {
let estimate = TICKET_CONTRACT.estimateGas.stampTicket;
let method = TICKET_CONTRACT.stampTicket;
const ticketIdBN = BigNumber.from(ticketId)
let args = [
ticketIdBN
];
let value = ethers.constants.Zero
return estimate(...args, { value }).then((estimatedGasLimit: any) => {
return method(...args, {
value,
gasLimit: calculateGasMargin(estimatedGasLimit, GAS_MARGIN)
})
}).then((tx: any) => {
return true
}).catch((e: Error) => {
console.log(e.message)
return false
})
}
}