Smart Contract Requests
Prerequisites
For this section, you will need:
A user's Theta Network wallet address, see the previous page on how to obtain this
A web3 library installed, such as ethers.js, and connected to the Theta Mainnet RPC endpoint
The smart contract address(es) of your event
Checking ownership of a ticket
import { useWeb3React } from '@web3-react/core'
const { library } = useWeb3React()
async function getTicketContractBalance(ticketAddress: string, library: any) {
return library.getBalance(ticketAddress)
}
Stamping a ticket
This requires a smart contract interaction, and therefore will cost gas. You will need to obtain an intstance of ticket contract first.
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
})
}
}
Last updated