import { ethers, Signer, BigNumberish } from 'ethers'; import { Subgraph } from './subgraph'; import { UFOGees, UFOGees__factory } from './wrappers'; import { BigNumber as DecimalBigNumber } from 'bignumber.js'; import { NFTConfig } from './types'; import { BytesLike } from '@ethersproject/bytes'; export { Subgraph } from './subgraph'; const exponent = new DecimalBigNumber(10).pow(18); const zeroAddress = '0x0000000000000000000000000000000000000000'; export class ufogeesSDK { private signer: Signer; private ufogees: UFOGees; private subgraph: Subgraph; constructor(signer: Signer, nftConfig: NFTConfig, subgraphUrl: string) { this.signer = signer; this.ufogees = new UFOGees__factory(this.signer).attach(nftConfig.ufogeesNFT); this.subgraph = new Subgraph(subgraphUrl, nftConfig.production); } public async setMerkleRoot(newRootGroup: string) { await this.ufogees.setMerkleRoot(newRootGroup); } public async presaleNFT(amount: BigNumberish, proof: BytesLike[], price: string) { let overrides = { // To convert Ether to Wei: value: ethers.utils.parseEther(price), // ether in this case MUST be a string // Or you can use Wei directly if you have that: // value: someBigNumber // value: 1234 // Note that using JavaScript numbers requires they are less than Number.MAX_SAFE_INTEGER // value: "1234567890" // value: "0x1234" // Or, promises are also supported: // value: provider.getBalance(addr) }; await this.ufogees.presale(amount, proof, overrides); } public async approveNFT(address: string, tokenId: BigNumberish) { await this.ufogees.approve(address, tokenId); } public async publicSaleNFT(amount: BigNumberish, price: string) { let overrides = { value: ethers.utils.parseEther(price), // ether in this case MUST be a string }; await this.ufogees.publicsale(amount, overrides); } public async withdrawNFT() { await this.ufogees.withdraw(); } public async getMerkleRoot(): Promise { return await this.ufogees.merkleRootGroup(); } public async getNFTTotalSupply(): Promise { let totalSupply = await this.ufogees.totalSupply(); return totalSupply.toString(); } public async getNFTTokenURI(tokenId: BigNumberish): Promise { let url = await this.ufogees.tokenURI(tokenId); return url.toString(); } public async getNFTPresaleSupply(): Promise { let presaleSupply = await this.ufogees.presaleSupply(); return presaleSupply.toString(); } public async getNFTPublicPrice(): Promise { let publicPrice = await this.ufogees.pricePerToken(); return ethers.utils.formatEther(publicPrice); } public async getNFTPresalePrice(): Promise { let publicPrice = await this.ufogees.pricePerToken(); let presalePrice = publicPrice.mul(9).div(10); return ethers.utils.formatEther(presalePrice); } public async getPresaleState(): Promise { return await this.ufogees.presaleState(); } public async getPublicState(): Promise { return await this.ufogees.publicState(); } public async getPresaleStartTime(): Promise { let time = await this.ufogees.presaleStartTime(); return Number(time); } public async getUSDPerETH(): Promise { // const rpc_url = 'https://main-rpc.linkpool.io'; const rpc_url = 'https://ethereumnodelight.app.runonflux.io'; const provider = new ethers.providers.JsonRpcProvider(rpc_url); const aggregatorV3InterfaceABI = [ { inputs: [], name: 'decimals', outputs: [{ internalType: 'uint8', name: '', type: 'uint8' }], stateMutability: 'view', type: 'function', }, { inputs: [], name: 'description', outputs: [{ internalType: 'string', name: '', type: 'string' }], stateMutability: 'view', type: 'function', }, { inputs: [{ internalType: 'uint80', name: '_roundId', type: 'uint80' }], name: 'getRoundData', outputs: [ { internalType: 'uint80', name: 'roundId', type: 'uint80' }, { internalType: 'int256', name: 'answer', type: 'int256' }, { internalType: 'uint256', name: 'startedAt', type: 'uint256' }, { internalType: 'uint256', name: 'updatedAt', type: 'uint256' }, { internalType: 'uint80', name: 'answeredInRound', type: 'uint80' }, ], stateMutability: 'view', type: 'function', }, { inputs: [], name: 'latestRoundData', outputs: [ { internalType: 'uint80', name: 'roundId', type: 'uint80' }, { internalType: 'int256', name: 'answer', type: 'int256' }, { internalType: 'uint256', name: 'startedAt', type: 'uint256' }, { internalType: 'uint256', name: 'updatedAt', type: 'uint256' }, { internalType: 'uint80', name: 'answeredInRound', type: 'uint80' }, ], stateMutability: 'view', type: 'function', }, { inputs: [], name: 'version', outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], stateMutability: 'view', type: 'function', }, ]; const addr = '0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419'; const priceFeed = new ethers.Contract(addr, aggregatorV3InterfaceABI, provider); try { const roundData = Number((await priceFeed.latestRoundData()).answer); const decimals = Number(await priceFeed.decimals()); const usd = roundData / 10 ** decimals; return Number(usd.toFixed(2)); } catch (e) { return 0; } } }