Triggers the buy flow to purchase a token. Will only work during a public sale. This is equivalent to what web3 developers refer to as "mint".

πŸ“˜

If a user has not connected their wallet, buy() will automatically call connect()

NameTypeRequiredDescription
amountNumberY# of tokens to mint/buy
tokenIdNumberNA specific token to buy/mint. For 721 contracts this should be 0. For 1155 contracts this should be the tokenId. TokenIds can be found in the dashboard in the "Tokens" tab.
waitBooleanNBlocks the return of this function until the transaction has completed on network by calling waitForTransaction(). Note that the transaction may take a while to resolve depending on network traffic.

Response

{
    "hash": "string"
}

Example Usage

const amount = 1;
const waitForTransaction = true;

// ERC721
const transaction = await contract.buy(amount, 0, waitForTransaction);

// ERC1155
const tokenId = 0;
const transaction = await contract.buy(amount, tokenId, waitForTransaction);

// Optional (if waitForTransaction is false)
const status = await contract.getTransactionStatus(transaction);

Returns a transaction with the network hash which can be used to poll for completion or in the waitForTransaction function.

On Polygon networks, an approve transaction is also triggered to give the contract access to the user's WETH.

Possible Errors

All errors are prefixed with HM (buy) -

  • Insufficient tokens - The contract does not have enough remaining supply
  • Insufficient funds - The wallet does not have enough funds
  • Errors in the connect() function

Feedback

Something not quite right, unclear or can't find what you are looking for? Please let us know at support.moonpay.com and we will get back to you as soon as we can.