testnet
GitHub

IOkrw.mint

mint(address recipient, uint256 amount) external returns (bool)

Mints a specified amount of OKRW tokens and credits them to the recipient address. This function can only be successfully called by the authorized Minter address, which is configured at the chain level in the x/okrw module parameters. Calls from any other address will revert with a custom error.

Parameters

Name Type Required Description
recipient address The address to receive the newly minted OKRW. Cannot be the zero address.
amount uint256 The amount of OKRW to mint, in its smallest unit (wei, 10^18). Must be greater than zero.

Returns

Type: bool

Returns true on successful execution. The function will revert on failure rather than returning false.

Errors

Code Name Description
UnauthorizedMinter Reverts if the `msg.sender` of the call is not the authorized minter. Returns the `caller` and the `authorizedMinter` addresses.
InvalidAddress Reverts if the provided `recipient` address is invalid (e.g., the zero address) or if the `caller` address cannot be converted to a Cosmos address. Returns the invalid `addr`.
InvalidAmount Reverts if the `amount` is zero. Note: The current implementation returns a generic revert reason for this case.

Examples

Basic Minting from a Contract

This example shows a contract that calls the `mint` function. For this to succeed, the address of `MinterContract` itself must be set as the authorized minter in the `x/okrw` module parameters.

// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.18;

import "./IOkrw.sol";

contract MinterContract {
    address constant OKRW_PRECOMPILE = 0x1000000000000000000000000000000000000001;
    IOkrw okrw = IOkrw(OKRW_PRECOMPILE);

    // This function will only succeed if this contract's address
    // is the authorized minter on the Maroo network.
    function mintOkrw(address to, uint256 amount) external {
        bool success = okrw.mint(to, amount);
        require(success, "OKRW minting failed");
    }
}

Handling Custom Errors in Solidity

This example demonstrates how to use a `try/catch` block to gracefully handle a potential `UnauthorizedMinter` error from the precompile, allowing the contract to react to the failure instead of reverting entirely.

// ...
function safeMintOkrw(address to, uint256 amount) external {
    try okrw.mint(to, amount) returns (bool success) {
        require(success, "Mint returned false");
        // Emit a local event or perform other logic
    } catch (bytes memory lowLevelData) {
        // Catch custom errors
        if (lowLevelData.length == 68 && lowLevelData[0:4] == IOkrw.UnauthorizedMinter.selector) {
            (address caller, address authorizedMinter) = abi.decode(lowLevelData[4:], (address, address));
            // Handle unauthorized error, e.g., emit an event
            revert("Unauthorized minter caught");
        } else {
            revert("Unknown custom error");
        }
    }
}

Calling and Handling Errors with Ethers.js

This client-side example shows how to call the `mint` function and parse the custom error data if the transaction reverts. This is useful for providing clear feedback to users in a dApp interface.

const { ethers } = require("ethers");

const okrwPrecompileAddress = "0x1000000000000000000000000000000000000001";
const okrwAbi = [
    "function mint(address recipient, uint256 amount) external returns (bool)",
    "error UnauthorizedMinter(address caller, address authorizedMinter)"
];

// Assume 'unauthorizedSigner' is an ethers.Signer for an account that is NOT the minter
const okrwContract = new ethers.Contract(okrwPrecompileAddress, okrwAbi, unauthorizedSigner);

async function attemptMint(recipient, amount) {
    try {
        const tx = await okrwContract.mint(recipient, ethers.parseEther(amount));
        await tx.wait();
        console.log("Minting successful!");
    } catch (error) {
        if (error.data) {
            const decodedError = okrwContract.interface.parseError(error.data);
            console.error(`Minting failed with error: ${decodedError.name}`);
            console.error(`Args: ${decodedError.args}`);
        } else {
            console.error("An unknown error occurred:", error.message);
        }
    }
}

attemptMint("0xRecipientAddress...", "1000");
ESC
Type to search