From d2442c8ffb626e14b92c0a8f6d9918bfdd936ffb Mon Sep 17 00:00:00 2001 From: SwatX18 <44673462+SwatX18@users.noreply.github.com> Date: Tue, 5 Jul 2022 19:51:15 +0200 Subject: [PATCH 1/3] Work for upgradable contract --- public/index.html | 9 ++++++--- src/constants.js | 2 +- src/contract.js | 19 ++++++++++++------- src/index.js | 5 +---- src/mint/index.js | 2 +- src/mint/web3.js | 5 ++++- src/tx.js | 2 +- src/wallet.js | 4 ++-- 8 files changed, 28 insertions(+), 20 deletions(-) diff --git a/public/index.html b/public/index.html index 5ac2329..fab5605 100644 --- a/public/index.html +++ b/public/index.html @@ -4,9 +4,12 @@ diff --git a/src/constants.js b/src/constants.js index 023eab7..30a95ca 100644 --- a/src/constants.js +++ b/src/constants.js @@ -118,4 +118,4 @@ export const getBaseURL = () => { return "http://localhost:3000" } return "https://nftcomponents.vercel.app" -} +} \ No newline at end of file diff --git a/src/contract.js b/src/contract.js index fda3884..9adaa8e 100644 --- a/src/contract.js +++ b/src/contract.js @@ -7,16 +7,21 @@ export let NFTContract; export const initContract = async (contract, shouldSwitchNetwork=true) => { const host = normalizeURL(window.location.href); const allowedURLs = contract?.allowedURLs?.map(u => normalizeURL(u)); + if (allowedURLs && !allowedURLs?.some(v => v.includes(host))) { return undefined; } + let currentNetwork = await getCurrentNetwork(); + if (shouldSwitchNetwork && !contract.allowedNetworks.includes(currentNetwork)) { await switchNetwork(contract.allowedNetworks[0]) currentNetwork = await getCurrentNetwork(); } + const address = contract.address[contract.allowedNetworks[0]]; - const abi = contract.abi ?? await fetchABI(address, currentNetwork); + const abi = contract.abi; + return new web3.eth.Contract(abi, address); } @@ -33,7 +38,8 @@ const initContractGlobalObject = async () => { address: { [chainID]: window.CONTRACT_ADDRESS, }, - abi: await fetchABI(window.CONTRACT_ADDRESS, chainID), + implementationAddress: window.IMPLEMENTATION_ADDRESS, + abi: await fetchABI(window.IMPLEMENTATION_ADDRESS, chainID), allowedNetworks: [chainID] } } @@ -45,10 +51,9 @@ export const fetchABI = async (address, chainID) => { if (cachedABI) return cachedABI - const abi = await fetch(`https://metadata.buildship.xyz/api/info/${address}?network_id=${chainID}`) - .then(r => r.json()) - .then(r => r.abi) - .catch(e => null) + const networkName = window.IS_TESTNET || window.NETWORK_ID == 4 ? "-rinkeby" : ""; + const abi = await fetch(`http://api${networkName}.etherscan.io/api?module=contract&action=getabi&address=${address}&format=raw&apikey=WBB6MKCCU58DU1ZX2446B915PTURZ4QE78`) + .then(res => res.json()) if (!abi) { console.log("No ABI returned from https://metadata.buildship.xyz") @@ -104,4 +109,4 @@ export const setContracts = async (shouldSwitchNetwork=true) => { } NFTContract = await initContract(window.CONTRACT.nft, false); console.log("NFTContract", NFTContract) -} +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 646f807..073a835 100644 --- a/src/index.js +++ b/src/index.js @@ -48,7 +48,4 @@ document.addEventListener("DOMContentLoaded", () => { sendAnonymousAnalytics() }); -export { showAlert, showMintModal, renderAppContainer }; - - - +export { showAlert, showMintModal, renderAppContainer }; \ No newline at end of file diff --git a/src/mint/index.js b/src/mint/index.js index f89beaf..dcef20b 100644 --- a/src/mint/index.js +++ b/src/mint/index.js @@ -17,4 +17,4 @@ export const init = async () => { updateMintButton(); } -init(); +init(); \ No newline at end of file diff --git a/src/mint/web3.js b/src/mint/web3.js index a469458..bd4da2b 100644 --- a/src/mint/web3.js +++ b/src/mint/web3.js @@ -85,7 +85,7 @@ export const getMintedNumber = async () => { if (!NFTContract) return undefined if (NFTContract.methods.totalSupply) - return await NFTContract.methods.totalSupply().call() + return await NFTContract.methods.totalSupply().call() - 1 // temporary solution, works only for buildship.xyz contracts // totalSupply was removed to save gas when minting // but number minted still accessible in the contract as a private variable @@ -113,6 +113,9 @@ export const getDefaultMaxTokensPerMint = () => { } export const getMaxTokensPerMint = async () => { + if(NFTContract?.methods?.maxMintAmountPerTx) { + return Number(await NFTContract.methods.maxMintAmountPerTx().call()) + } if (NFTContract?.methods?.maxPerMint) { return Number(await NFTContract.methods.maxPerMint().call()) } diff --git a/src/tx.js b/src/tx.js index 5bdb630..7f57367 100644 --- a/src/tx.js +++ b/src/tx.js @@ -34,4 +34,4 @@ const estimateMaxGasFee = async (tx) => { const estimateMaxPriorityFeePerGas = async () => { const chainID = await web3.eth.getChainId(); return [1, 4].includes(chainID) ? 2e9 : undefined; -} +} \ No newline at end of file diff --git a/src/wallet.js b/src/wallet.js index 25421f5..4e0a171 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -267,7 +267,7 @@ export const connectWallet = async () => { try { await initWeb3(true); } catch (e) { - if (!e.includes("Modal closed by user")) { + if (typeof e === "string" && !e.includes("Modal closed by user")) { alert(`Error in initWeb3 in connectWallet: ${e.toString()}`) console.error(e) } @@ -287,7 +287,7 @@ export const updateWalletStatus = async () => { try { await initWeb3(); } catch (e) { - if (!e.includes("Modal closed by user")) { + if (typeof e === "string" && !e.includes("Modal closed by user")) { alert(`Error in initWeb3: ${e.toString()}`) console.error(e) } From c87b6e6284de2f7dbb96f8f5f3100bd749b05be7 Mon Sep 17 00:00:00 2001 From: SwatX18 <44673462+SwatX18@users.noreply.github.com> Date: Tue, 5 Jul 2022 20:01:45 +0200 Subject: [PATCH 2/3] Update wallet.js --- src/wallet.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/wallet.js b/src/wallet.js index 4e0a171..d80dc19 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -296,7 +296,8 @@ export const updateWalletStatus = async () => { const connected = await isWalletConnected(); const button = getConnectButton(); if (button && connected) { - button.textContent = window?.DEFAULTS?.labels?.walletConnected ?? "Wallet connected"; + const walletAddress = await getWalletAddress(); + button.textContent = window?.DEFAULTS?.labels?.walletConnected ?? "Connected: " + truncate(walletAddress, 10); } } @@ -309,4 +310,18 @@ export const updateConnectButton = () => { await updateMintedCounter() } }); -} \ No newline at end of file +} + +function truncate(fullStr, strLen, separator) { + if (fullStr === undefined) return; + if (fullStr.length <= strLen) return fullStr; + + separator = separator || '...'; + + var sepLen = separator.length, + charsToShow = strLen - sepLen, + frontChars = Math.ceil(charsToShow/1.5), + backChars = Math.floor(charsToShow/2.5); + + return fullStr.substr(0, frontChars) + separator + fullStr.substr(fullStr.length - backChars); +}; \ No newline at end of file From 7f9864627c1f239ac479c94a220e9757b62734ac Mon Sep 17 00:00:00 2001 From: SwatX18 <44673462+SwatX18@users.noreply.github.com> Date: Tue, 5 Jul 2022 20:15:24 +0200 Subject: [PATCH 3/3] Fixed a bug in obtaining the wallet address --- public/index.html | 1 + src/wallet.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/public/index.html b/public/index.html index fab5605..95a0d9a 100644 --- a/public/index.html +++ b/public/index.html @@ -33,6 +33,7 @@
+