📖
Metapass API
  • Welcome!
  • Quick Start
    • Graph Node Requests
    • Smart Contract Requests
    • Miscellaneous
  • Reference
    • Graph Node API
      • TKETSFactory
      • Event
      • Ticket
      • TicketToken
    • Smart Contract Specifications
      • EventFactory
        • ABI
      • Ticket
        • ABI
Powered by GitBook
On this page
  • Prerequisites
  • Checking ownership of a ticket
  • Stamping a ticket
  1. Quick Start

Smart Contract Requests

PreviousGraph Node RequestsNextMiscellaneous

Last updated 3 years ago

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 , 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

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

This requires a smart contract interaction, and therefore will cost gas. You will need to first.

ethers.js
web3-react
obtain an intstance of ticket contract