Skip to content

Smart Contracts

Smart contracts are programs that run on the blockchain. They are a set of code (functions) and data (state) located at a specific address on the blockchain.

Smart contracts are also considered accounts, which we call contract accounts. They have balances and can be the subject of transactions. They are deployed on the network to run as programs. Individual users can interact with smart contracts by submitting transactions to execute a specific function within the smart contract.

Smart contracts can define rules like traditional contracts and automatically enforce them through code. By default, you cannot delete smart contracts, and interactions with them are irreversible.

ABI

The Application Binary Interface (ABI) is the interface specification for calling Ethereum contracts. Different contracts have their own ABIs, and you need to obtain the ABI of a contract before calling it.

The USDT contract address and its ABI on Ethereum are as follows:

json
[
  {
    "constant": true,
    "inputs": [],
    "name": "name",
    "outputs": [{ "name": "", "type": "string" }],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "_totalSupply",
    "outputs": [{ "name": "", "type": "uint256" }],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  ...
  { "anonymous": false, "inputs": [], "name": "Unpause", "type": "event" }
]
[
  {
    "constant": true,
    "inputs": [],
    "name": "name",
    "outputs": [{ "name": "", "type": "string" }],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  {
    "constant": true,
    "inputs": [],
    "name": "_totalSupply",
    "outputs": [{ "name": "", "type": "uint256" }],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  },
  ...
  { "anonymous": false, "inputs": [], "name": "Unpause", "type": "event" }
]

Contract initialization

You can use various libraries to interact with smart contracts, such as web3.js.

The initialization of a contract is as follows:

js
import Web3 from 'web3'

const web3 = new Web3(provider);

const abi = [{"constant":true,"inputs":[],...}];

const contractAddress = '0xdAC17F958D2ee523a2206206994597C13D831ec7';//usdt contract address

const contract = new web3.eth.Contract(abi, contractAddress);
import Web3 from 'web3'

const web3 = new Web3(provider);

const abi = [{"constant":true,"inputs":[],...}];

const contractAddress = '0xdAC17F958D2ee523a2206206994597C13D831ec7';//usdt contract address

const contract = new web3.eth.Contract(abi, contractAddress);

Note: The provider must correspond to the selected wallet.

Calling Contract Methods

Data reading methods.myMethod.call

js
contract.methods.myMethod(...args).call(); //returns promise

await usdtContract.methods.name().call(); // => Tether USD

await usdtContract.methods.totalSupply().call(); // => 51998545629548753

await usdtContract.methods.balances("your address").call(); // your balances
contract.methods.myMethod(...args).call(); //returns promise

await usdtContract.methods.name().call(); // => Tether USD

await usdtContract.methods.totalSupply().call(); // => 51998545629548753

await usdtContract.methods.balances("your address").call(); // your balances

Estimated Gas Fee

js
contract.methods.myMethod(...args).estimateGas({
  from: "your address",
});
contract.methods.myMethod(...args).estimateGas({
  from: "your address",
});

Data Writing

js
contract.methods.myMethod(...args).send({
  from: "your address",
});
contract.methods.myMethod(...args).send({
  from: "your address",
});

And so on for other contract methods.