import { ethers, BigNumberish, BaseContract, BigNumber } from 'ethers'; import { NFTConfig } from './types'; import { Provider } from '@ethersproject/abstract-provider'; import jsonABI from './ufogeesABI.json'; const contractAbi = JSON.stringify(jsonABI); export class ufogeesProviderSDK { private ufogees: BaseContract; constructor(provider: Provider, nftConfig: NFTConfig) { this.ufogees = new ethers.Contract(nftConfig.ufogeesNFT, contractAbi, provider); } public async getMerkleRoot(): Promise { return await this.ufogees.functions.merkleRootGroup(); } public async getNFTTotalSupply(): Promise { let totalSupply = await this.ufogees.functions.totalSupply(); return totalSupply.toString(); } public async getNFTTokenURI(tokenId: BigNumberish): Promise { let url = await this.ufogees.functions.tokenURI(tokenId); return url.toString(); } public async getNFTPresaleSupply(): Promise { let presaleSupply = await this.ufogees.functions.presaleSupply(); return presaleSupply.toString(); } public async getNFTPublicPrice(): Promise { let publicPrice = await this.ufogees.functions.pricePerToken(); return ethers.utils.formatEther(BigNumber.from(publicPrice.toString())); } public async getNFTPresalePrice(): Promise { let publicPrice = await this.ufogees.functions.pricePerToken(); let presalePrice = BigNumber.from(publicPrice.toString()).mul(9).div(10); return ethers.utils.formatEther(presalePrice); } public async getPresaleState(): Promise { return await this.ufogees.functions.presaleState(); } public async getPublicState(): Promise { return await this.ufogees.functions.publicState(); } public async getPresaleStartTime(): Promise { let time = await this.ufogees.functions.presaleStartTime(); return Number(time); } public async getUSDPerETH(): Promise { 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; } } }