buyAuthorized()
Triggers the buy flow to purchase a token using an access list or authorisation from your server. This function allows for private sales, access list sales, or any other means of gating the minting of a token from the public.
Parameters
Name | Type | Required | Description |
---|---|---|---|
amount | Number | Y | # of tokens to mint/buy |
tokenId | Number | Y | A 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. |
wait | Boolean | N | Blocks 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. |
ethPrice | Number | N | Total price for all tokens to be purchased, in ETH. |
maxPerAddress | Number | Y | Required if you provide a signature. Must be greater than 0 regardless of contract type. On 1155 contracts, this must be a value of 0. |
expires | Number | N | An expiration date on the signature. |
signature | String | N | A signature in the r,s,v format. See detailed documentation in Authorizing Access List Purchases |
Response
{
"hash": "string"
}
Example Implemenation (HyperMint Access List)
When using a HyperMint access list, the signature and pricing is handled automatically and the function can be used like this.
const amount = 1;
const waitForTransaction = true;
const tokenId = 0; // Always 0 for ERC721
const transaction = await contract.buyAuthorised(amount, tokenId, waitForTransaction);
// Optional (if waitForTransaction is false)
const status = await contract.getTransactionStatus(transaction);
Example Implementation (Self Hosted Access List)
If you are managing the access list yourself, you will need to provide the additional parameters provided in this implementation.
const amount = 1;
const waitForTransaction = true;
const {totalPrice: number, expires: string, signature: string } = // A call to your server
const tokenId = 0; // Always 0 for ERC721
const transaction = await contract.buyAuthorised(amount, tokenId, waitForTransaction, totalPrice, maxPerAddress, expires, signature);
// Optional (if waitForTransaction is false)
const status = await contract.getTransactionStatus(transaction);
buyAuthorised() accepts the total price to charge for all tokens, not the price per token
Returns a transaction with a network hash. That network hash 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.
See Authorising Presales for more information about how to obtain the r, s, v
signature properties.
Possible Errors
All errors are prefixed with HM (buyAuthorised)
:
Insufficient tokens
- The contract does not have enough remaining supplyInsufficient 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.
Updated 8 months ago