💸Transactions

Explanation of transaction handling

Preface

Amity offers on-chain token transactions between either discord users or outside public addresses.

How this works

After fetching account information and preferred network, a transaction is built using the following command fields:

/send (value) (to) (*contract)

The transaction building and signing looks like this:

estimate = web3.eth.estimateGas({'to': to, 'from': address, 'value': int(web3.toWei(value, 'ether'))})
tx = {
    'nonce': web3.eth.get_transaction_count(address),
    'to': to, 
    'value': web3.toWei(value, 'ether'), 
    'gas': estimate, 
    'gasPrice': web3.eth.gasPrice
    }
signed_tx = web3.eth.account.sign_transaction(tx, decryptedPrivateKey) 

Contract/token transactions

If a contract is specified in the "*contract" field, the code looks slightly different as a transaction has to instead be built through a web3 contract object instead of a default tx dictionary.

First, the contract object has to be initialized through a Checksum address and ABI:

contract = web3.eth.contract(address=Web3.toChecksumAddress(contract), abi=abi)

Web3 requires that contracts are called in a Checksum format, so the toChecksum of the contract provided is plugged into the contract object. The ABI used is a generic ERC20 file pulled from a separate file. After this, the transaction can begin to be built in a similar fashion to a normal coin (non-token) transaction:

estimate = contract.functions.transfer(to, int(amount)).estimateGas({'from': address, 'chainId': chainId})
tx = contract.functions.transfer(
  to, web3.toWei(value, 'ether')
  ).buildTransaction({
    'chainId': chainId,
    'gas': estimate,
    'gasPrice': web3.eth.gasPrice,
    'nonce': web3.eth.get_transaction_count(address),
  })
signed_tx = web3.eth.account.sign_transaction(tx, decryptedPrivateKey)

Due to Amity utilizing contracts dynamically and by using generic ABI's, you can access and send any token just by referencing the contract address.

Last updated