Replies: 5 comments 1 reply
-
Can you do a Also, you can use |
Beta Was this translation helpful? Give feedback.
-
Hi Richard, thanks for the response. I'd adjusted the BigNumber value but I still don't get why I need to put Here's I share my code and I'm really open with any improvement on it! /* Contract address */
const contractAddress = "0xB1f9Dee562D326527302125096cB793A6893409a";
/* Contract ABI */
const contractAbi = [{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"approved","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"operator","type":"address"},{"indexed":false,"internalType":"bool","name":"approved","type":"bool"}],"name":"ApprovalForAll","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"MAX_NFT_PURCHASE","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"MAX_NFT_SUPPLY","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"NFT_PROVENANCE","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"approve","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"baseURI","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"flipSaleState","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"getApproved","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"getNFTPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"operator","type":"address"}],"name":"isApprovedForAll","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"isSaleActive","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"numberOfTokens","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"ownerOf","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"safeTransferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"bytes","name":"_data","type":"bytes"}],"name":"safeTransferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"operator","type":"address"},{"internalType":"bool","name":"approved","type":"bool"}],"name":"setApprovalForAll","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"baseURI","type":"string"}],"name":"setBaseURI","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_newPrice","type":"uint256"}],"name":"setPrice","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"_hash","type":"string"}],"name":"setProvenanceHash","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"index","type":"uint256"}],"name":"tokenByIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint256","name":"index","type":"uint256"}],"name":"tokenOfOwnerByIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"tokenURI","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"transferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"withdraw","outputs":[],"stateMutability":"payable","type":"function"}]
;
/* Max token supply based on contract */
const TOTAL_TOKENS = 100;
/* Token price based on contract */
let tokenPrice = ethers.utils.parseEther('0.05'); // 0.05 ether
let provider
let signer
let accounts
let contract
const connectButton = document.querySelector('#connectButton')
const showAccount = document.querySelector('#showAccount')
const currentSupply = document.querySelector('#currentSupply')
const quantity = document.querySelector('#quantity')
const purchaseButton = document.querySelector('#purchaseButton')
/* Purchase (mint) button default is disabled when is not connected to the provider */
document.getElementById('purchaseButton').disabled = true;
/* Functions which are executed when reload the page */
window.onload=function(){
let minusButton = document.getElementById("minus"),
plusButton = document.getElementById("plus"),
totalQty = document.getElementById("quantity"),
totalPrc = document.getElementById("totalPrice"),
quantity = 1, // Default quantity value
min = 1, // min. quantity
max = 10, // max. quantity
price = 0.05; // Abstraction of price per unit
minusButton.onclick = function(){
if (quantity>min){
quantity = quantity-1;
totalQty.innerText = quantity ;
totalPrc.innerText = (quantity * price).toFixed(2);
}
if(quantity == min) {
totalQty.style.color= "red";
setTimeout(function(){totalQty.style.color= "black"},750)
}
else {
totalQty.style.color="black";
}
}
plusButton.onclick = function(){
if(quantity<max){
quantity = quantity+1;
totalQty.innerText = quantity ;
totalPrc.innerText = (quantity * price).toFixed(2);
}
if(quantity == max){
totalQty.style.color= "red";
setTimeout(function(){totalQty.style.color= "black"},750)
}
else {
totalQty.style.color= "black";
}
}
/* Check Metamask */
if (typeof window.ethereum == 'undefined') {
console.log('Metamask is not detected.')
connectButton.innerHTML = 'Install Wallet'
connectButton.style.background = 'Red'
installWallet();
}
}
function installWallet() {
connectButton.onclick = function() {
window.open('https://metamask.io/download','_blank').focus();
}
}
/* Functions */
function connectWallet() {
// If Metamask as a provider is detected
if (typeof window.ethereum !== 'undefined'){
provider = new ethers.providers.Web3Provider(window.ethereum)
signer = provider.getSigner()
provider.send('eth_requestAccounts')
.then((result) => {
accounts = result
document.dispatchEvent(new Event('walletConnected'))
})
.catch((err) => {
console.log(err)
//error message - later
})
// If Metamask is not detected (redundant - need to be adjusted later)
} else {
console.log('Metamask is not detected.')
}
}
function onWalletConnected() {
// Create contract instance
contract = new ethers.Contract(contractAddress, contractAbi, provider)
// Enable the purchase (mint) button
document.getElementById('purchaseButton').disabled = false;
connectedButton();
updateAccountAddress();
purchaseButton.addEventListener('click', purchase)
document.dispatchEvent(new Event('fetchTotalSupply'))
contract.on('Transfer',(from, to, tokenId) => {
console.log(from, to, tokenId.toNumber());
document.dispatchEvent(new Event('fetchTotalSupply'))
})
}
/* Showing connet wallet button style changes when connected to Metamask */
function connectedButton() {
connectButton.innerHTML = 'Connected'
connectButton.style.background = 'Green'
}
/* Showing wallet address (account) on minter box */
async function updateAccountAddress() {
accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const account = accounts[0];
showAccount.innerHTML = account;
}
/* Retrieve current total supply from contract */
function onFetchTotalSupply() {
contract.totalSupply()
.then((result) => {
totalSupply = result
document.dispatchEvent(new Event('updateTotalSupply'))
})
.catch((err) => {
console.log(err)
//error message - later
})
}
/* Update recent total supply on UI */
function onUpdateTotalSupply() {
const remaining = TOTAL_TOKENS - totalSupply
if (remaining <= 0) {
currentSupply.innerHTML = 'SOLD OUT!' // Need to be adjusted later
purchaseButton.remove()
} else {
currentSupply.innerHTML = remaining
}
}
/* Purchase (mint) function */
function purchase() {
console.log(tokenPrice.mul(quantity))
contract.connect(signer).mint({value: tokenPrice.mul(quantity)}) // !!!
.then((_result) => {
console.log('Purchase transaction sent successfully.')
})
.catch((err) => {
console.log(err)
//error message - later
})
}
connectButton.addEventListener('click', connectWallet)
document.addEventListener('walletConnected', onWalletConnected)
document.addEventListener('fetchTotalSupply', onFetchTotalSupply)
document.addEventListener('updateTotalSupply', onUpdateTotalSupply) As always, thanks in advance! |
Beta Was this translation helpful? Give feedback.
-
I just want more debugging information from you. To make sure you are passing in the value you think you are passing in, and that hoisting or something similar isn’t getting in the way. |
Beta Was this translation helpful? Give feedback.
-
I see your problem. You are passing in an overrides object to the mint method. It takes a single parameter, which you are omitting or are conflating with that object. You probably want to call |
Beta Was this translation helpful? Give feedback.
-
(moving to discussions) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm a newbie and recently creating my first dApp. The project is a "minter box" that require the buyer as the minter to pay some ether (beside of gas fee) to generate a tokenId.
Recently, I've found no problem from connecting to metamask, showing the connected wallet address and updating recent total supply (by fetching to the contract) using ethers js. But the problem came when I execute purchase(mint) function by clicking the purchase button.
Here's a snippet of my code. (Environment: ether-5.4.4, Vanilla JS)
Here's the error:
I still don't get it with the error message. Please guide me. Thanks in advance! :)
Beta Was this translation helpful? Give feedback.
All reactions