Skip to content

Commit

Permalink
set custom gas price when creating NFT
Browse files Browse the repository at this point in the history
Polygon increased it's minimum gas price from 30 Gwei to 1Gwei to curb spam.

Other blockchains might change their default gas price as well so we should get that value from the provider.

see: https://medium.com/stakingbits/polygon-minimum-gas-fee-is-now-30-gwei-to-curb-spam-8bd4313c83a2

ethers-io/ethers.js#2828 (comment)

ethers-io/ethers.js#40 (comment)
  • Loading branch information
ademidun committed Jun 18, 2022
1 parent c9e9f3d commit c2a6be5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 26 deletions.
9 changes: 2 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ export let CONFIG_CHAINS: {[key: string]: Chain} = {};
const environmentName = process.env.REACT_ENVIRONMENT_NAME || 8008;
Object.values(ALL_CONFIG_CHAINS)
// comment the following line if you want to test with all chains without filtering any chains out
.filter(chain => environmentName === "prod" ? chain.IS_MAIN_NET : !chain.IS_MAIN_NET)
// .filter(chain => environmentName === "prod" ? chain.IS_MAIN_NET : !chain.IS_MAIN_NET)
.forEach(chain => {
CONFIG_CHAINS[chain.CHAIN_ID] = chain;
})

export const REACT_APP_MORALIS_SERVER_URL = process.env.REACT_APP_MORALIS_SERVER_URL;
export const REACT_APP_MORALIS_APP_ID = process.env.REACT_APP_MORALIS_APP_ID;

export const MORALIS_SUPPORTED_CHAINS = ["4", "80001", "97", "56", "137"];
});
42 changes: 28 additions & 14 deletions src/create-nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,42 @@ import {

import NFT from './artifacts/contracts/NFT.sol/NFT.json';
import { Chain } from './models/Chain';
import { getAccount } from './utils/network-helpers';

const activeChainId = "97";
const activeChain = new Chain({...CONFIG_CHAINS[activeChainId]});
const NFT_ADDRESS = activeChain.NFT_ADDRESS;

import { getAccount, getProvider } from './utils/network-helpers';
import { getMarketplaceUrls } from './models/Marketplace';
import { NFTMetadata } from './models/NFT';

export const createNFT = async () =>{
const chainId = "137";
const activeChain = new Chain({...CONFIG_CHAINS[chainId]});
const NFT_ADDRESS = activeChain.NFT_ADDRESS;

// TODO getAccount() is also calling getProvider() is this wasted duplicate effort?
const signer = getAccount(chainId) as Signer;
const provider = getProvider(chainId);


const signer = getAccount(activeChainId) as Signer;
const gasPrice = await provider.getGasPrice();
const url = "https://ipfs.moralis.io:2053/ipfs/QmXYfYAHxTwbY5sQJUNB2ftF5aHvxfkBUwgEKM5dSfVVLg";

let nftContract = new ethers.Contract(NFT_ADDRESS, NFT.abi, signer)
let mintTransactionPromise = await nftContract.createToken(url)
console.log({gasPrice});
let nftContract = new ethers.Contract(NFT_ADDRESS, NFT.abi, signer);
let mintTransactionPromise = await nftContract.createToken(url, {gasPrice})
let mintTransaction = await mintTransactionPromise.wait()
let event = mintTransaction.events[0]
let tokenId = event.args[2]

const nftExplorerUrl = `${activeChain.BLOCK_EXPLORER_URL}/token/${activeChain.NFT_ADDRESS}?a=${tokenId}`;

console.log("\x1b[32m%s\x1b[0m", `View in block explorer: ${nftExplorerUrl}`);
const blockExplorerUrl = `${activeChain.BLOCK_EXPLORER_URL}/token/${activeChain.NFT_ADDRESS}?a=${tokenId}`;

const nft: NFTMetadata = {
name: "",
description: "",
image: url,
tokenId,
chainId,
}

return { tokenId, nftExplorerUrl}
const marketplaceUrls = getMarketplaceUrls(chainId, nft);

console.log("\x1b[32m%s\x1b[0m", `View in block explorer: ${blockExplorerUrl}`);
console.log("\x1b[32m%s\x1b[0m", `View in marketplaces: ${marketplaceUrls}`);
return { nft, blockExplorerUrl, marketplaceUrls };
}
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ const app: Express = express();
const port = process.env.PORT || 8008;

app.get('/create-nft', async (req: Request, res: Response) => {
const nft = await createNFT();

res.send(nft);
try {
const nftResult = await createNFT();
res.send(nftResult);
} catch (error) {
res.send({error});
}
});

app.listen(port, () => {
Expand Down
4 changes: 2 additions & 2 deletions src/models/Marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const Marketplaces = [OpenSea, Rarible, LooksRare, NFTrade];

export const getMarketplaceUrls = (chainId: string, nft: NFTMetadata) => {
const activeChain = CONFIG_CHAINS[chainId];
Marketplaces.filter(marketplace => marketplace.supportedChains.includes(activeChain.CHAIN_ID)).map(marketplace => {
marketplace.getListingUrl(activeChain, nft)
return Marketplaces.filter(marketplace => marketplace.supportedChains.includes(activeChain.CHAIN_ID)).map(marketplace => {
return marketplace.getListingUrl(activeChain, nft)
})
}
17 changes: 17 additions & 0 deletions src/practice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { getProvider } from "./utils/network-helpers"

const chainId = "137"
const provider = getProvider(chainId);

provider.getGasPrice();

export const main = async () => {

const gasPrice = await provider.getGasPrice();
const feeData = await provider.getFeeData();

console.log("gasPrice", gasPrice);
console.log("feeData",feeData);
};

main();

0 comments on commit c2a6be5

Please sign in to comment.