注意: 此页面尚未翻译成中文。以下内容为英文原版。
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
How to interact with and configure a smart contract on the Quai Ledger.
注意: 此页面尚未翻译成中文。以下内容为英文原版。
// JSON ABI
const abi = [
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
...
]
// use the JSON ABI to create a contract
const contract = new quais.Contract(address, abi, provider)
// simplified Solidity signature ABI
const abi = [
'function decimals() view returns (uint8)',
'function symbol() view returns (string)',
'function balanceOf(address addr) view returns (uint)',
]
// use the simplified ABI to create a contract
const contract = new quais.Contract(address, abi, provider)
// contract ABI (only includes the methods we care about)
const abi = [
'function decimals() view returns (uint8)',
'function symbol() view returns (string)',
'function balanceOf(address addr) view returns (uint)',
]
// create contract with only a Provider
const contract = new quais.Contract(address, abi, provider)
// get the token symbol
symbol = await contract.symbol() // "ABC"
// get the token decimals
decimals = await contract.decimals() // 18n
// get the balance of an address
balance = await contract.balanceOf('0x1234567890123456789012345678901234567890') // 10000000000000000000n
// format the balance
formatUnits(balance, decimals) // "10.0"
// contract ABI (only includes the methods we care about)
const abi = ['function transferTo(address to, uint amount)']
// create wallet and pass it to the contract
const wallet = new quais.Wallet(privateKey, provider)
const contract = new quais.Contract(address, abi, wallet)
// transfer some tokens
const tx = await contract.transferTo('0x1234567890123456789012345678901234567890', parseUnits('1.0', 18))
await tx.wait()
// contract ABI (only includes the events we care about)
const abi = ['event Transfer(address indexed from, address indexed to, uint amount)']
// create contract with only a Provider
const contract = new quais.Contract(address, abi, provider)
// use an event listener to listen for any transfer event
contract.on('Transfer', (from, to, amount, event) => {
const tokenAmount = formatUnits(amount, 18)
console.log('Transfer from', from, 'to', to, 'amount', tokenAmount)
// stop listening for events
event.removeListener()
})
// use a filter to listen for transfer events from a specific address
const filter = contract.filters.Transfer('0x1234567890123456789012345678901234567890')
contract.on(filter, (from, to, amount, event) => {
const tokenAmount = formatUnits(amount, 18)
console.log('Transfer from', from, 'to', to, 'amount', tokenAmount)
})
// contract ABI
const abi = ['event Transfer(address indexed from, address indexed to, uint amount)']
// create contract with only a Provider
const contract = new quais.Contract(address, abi, provider)
// query the last 50 blocks for any transfer events
const filter = contract.filters.Transfer
const events = await contract.queryFilter(filter, -50) // returns an array of events
// query all transfer events from a specific address
const filter = contract.filters.Transfer('0x1234567890123456789012345678901234567890')
const events = await contract.queryFilter(filter) // returns an array of events
