Contract Functions

Key Functions in the Smart Contract

1. transfer: Transfers tokens from the caller's account to the recipient.

function transfer(address recipient, uint256 amount) external returns (bool) {
    require(balances[msg.sender] >= amount, "Insufficient balance");
    balances[msg.sender] -= amount;
    balances[recipient] += amount;
    emit Transfer(msg.sender, recipient, amount);
    return true;
}
  • Purpose: To allow a user to send tokens from their balance to another address.

  • Inputs:

    • recipient: The address receiving the tokens.

    • amount: The number of tokens to transfer.

  • Outputs: Returns true if the transfer is successful.

  • Logic: The function first checks if the sender has enough tokens, then deducts the specified amount from the sender’s balance and adds it to the recipient's balance. It also emits a Transfer event for transparency.

2. mint: Allows the contract owner to mint new tokens and increase the total supply.

function mint(uint256 amount) external onlyOwner {
    balances[owner] += amount;
    emit Transfer(address(0), owner, amount); 
    // Mint event treated as a "transfer" from zero address
}
  • Purpose: To create (mint) new tokens and add them to the owner's balance, increasing the total token supply.

  • Inputs:

    • amount: The number of tokens to be created and assigned to the owner's balance.

  • Outputs: No return value, but an event Transfer is emitted to reflect the minting process (from address 0x0 to the owner).

  • Logic: This function can only be called by the owner (enforced by the onlyOwner modifier). It adds the specified amount of tokens to the owner's balance and emits a Transfer event, treating it as if tokens were sent from the zero address to the owner.

3. burn: Allows a user to burn tokens from their balance, reducing the total supply.

function burn(uint256 amount) external {
    require(balances[msg.sender] >= amount, "Insufficient balance to burn");
    balances[msg.sender] -= amount;
    emit Transfer(msg.sender, address(0), amount); 
    // Burn event treated as a "transfer" to zero address
}
  • Purpose: To allow users to burn (destroy) tokens from their balance, reducing the total supply.

  • Inputs:

    • amount: The number of tokens to burn.

  • Outputs: No return value, but emits a Transfer event indicating that the tokens were transferred to the zero address (indicating they are burned).

  • Logic: The function checks that the caller has sufficient tokens to burn. If the condition is met, the specified amount is deducted from the user's balance, and the tokens are effectively destroyed. A Transfer event is emitted to indicate the burn process, with the destination address being 0x0.

4. balanceOf: Returns the balance of a specified address.

function balanceOf(address account) external view returns (uint256) {
    return balances[account];
}
  • Purpose: To provide a way for anyone to check the token balance of a specific address.

  • Inputs:

    • account: The address whose balance is being queried.

  • Outputs: Returns the current token balance of the specified account.

  • Logic: This is a simple read-only function (view) that does not modify the contract’s state. It looks up the balance of the given address in the balances mapping and returns the value.

5. transferFrom: Allows an approved spender to transfer tokens on behalf of the token owner.

function transferFrom(address sender, address recipient, uint256 amount) external returns (bool) {
    require(allowances[sender][msg.sender] >= amount, "Allowance exceeded");
    require(balances[sender] >= amount, "Insufficient balance");
    balances[sender] -= amount;
    balances[recipient] += amount;
    allowances[sender][msg.sender] -= amount;
    emit Transfer(sender, recipient, amount);
    return true;
}
  • Purpose: To allow a third-party (spender) to transfer tokens on behalf of the token owner, given that they’ve been granted approval.

  • Inputs:

    • sender: The address of the token owner who is authorizing the transfer.

    • recipient: The address receiving the tokens.

    • amount: The number of tokens to transfer.

  • Outputs: Returns true if the transfer is successful.

  • Logic: The function first checks that the sender has approved enough tokens (allowances) for the caller (spender) to transfer on their behalf. It also ensures the sender has sufficient balance. Upon success, it transfers the tokens and updates the allowance. A Transfer event is emitted for logging purposes.

6. approve: Allows a token owner to approve a spender to transfer tokens on their behalf.

function approve(address spender, uint256 amount) external returns (bool) {
    allowances[msg.sender][spender] = amount;
    emit Approval(msg.sender, spender, amount);
    return true;
}
  • Purpose: To grant approval to a third-party (spender) to transfer tokens on behalf of the token owner.

  • Inputs:

    • spender: The address of the entity authorized to transfer tokens.

    • amount: The number of tokens they are allowed to transfer.

  • Outputs: Returns true if the approval is successful.

  • Logic: The function updates the allowances mapping, allowing the specified spender to transfer up to amount tokens from the caller's account. The Approval event is emitted to notify off-chain services of the approval.


Summary of Key Functions

Each function serves a specific role in token management, ensuring that users can securely transfer, mint, burn, and authorize token transactions, all while maintaining transparency via events.

Last updated