ufomarketplace-sdk
Version:
SDK to interact with set ufo marketplace contracts
82 lines (74 loc) • 3.47 kB
text/typescript
import { ethers, BaseContract, BigNumber } from 'ethers';
import { UFOConfig } from './types';
import { Provider } from '@ethersproject/abstract-provider';
import { NETWORK_ID } from './constants/constants';
import ufoABI from './abis/ufoABI.json';
import ufoLPABI from './abis/ufoLPABI.json';
import plasmaABI from './abis/ufoLPABI.json';
const ufoAbi = JSON.stringify(ufoABI);
const ufoLPAbi = JSON.stringify(ufoLPABI);
const plasmaAbi = JSON.stringify(plasmaABI);
export class ufoProviderSDK {
private ufo: BaseContract = null;
private ufoLP: BaseContract = null;
private plasma: BaseContract = null;
private provider: Provider = null;
constructor(provider: Provider, ufoConfig: UFOConfig, netID: number) {
this.provider = provider;
switch (netID) {
case NETWORK_ID.ETHEREUM:
case NETWORK_ID.GOERLI:
default:
this.ufo = new ethers.Contract(ufoConfig.ufoTokenOnEth, ufoAbi, provider);
this.ufoLP = new ethers.Contract(ufoConfig.ufoLPTokenOnEth, ufoLPAbi, provider);
if (ufoConfig.plasmaTokenOnEth != undefined) {
this.plasma = new ethers.Contract(ufoConfig.plasmaTokenOnEth, plasmaAbi, provider);
}
break;
case NETWORK_ID.POLYGON:
case NETWORK_ID.MUMBAI:
this.ufo = new ethers.Contract(ufoConfig.ufoTokenOnMatic, ufoAbi, provider);
this.ufoLP = new ethers.Contract(ufoConfig.ufoLPTokenOnMatic, ufoLPAbi, provider);
this.plasma = new ethers.Contract(ufoConfig.plasmaTokenOnMatic, plasmaAbi, provider);
break;
}
}
public async getUFOTotalSupply(): Promise<string> {
let totalSupply = await this.ufo.functions.totalSupply();
let decimals = await this.ufo.functions.decimals();
let count = BigNumber.from(totalSupply.toString()).div(BigNumber.from('10').pow(decimals));
return count.toString();
}
public async getUFOLPTotalSupply(): Promise<string> {
let totalSupply = await this.ufoLP.functions.totalSupply();
let decimals = await this.ufoLP.functions.decimals();
let count = BigNumber.from(totalSupply.toString()).div(BigNumber.from('10').pow(decimals));
return count.toString();
}
public async getUFOCount(address: string): Promise<string> {
let balance = await this.ufo.functions.balanceOf(address);
let decimals = await this.ufo.functions.decimals();
let count = BigNumber.from(balance.toString()).div(BigNumber.from('10').pow(decimals));
return count.toString();
}
public async getUFOLPCount(address: string): Promise<string> {
let balance = await this.ufoLP.functions.balanceOf(address);
let decimals = await this.ufoLP.functions.decimals();
let count = BigNumber.from(balance.toString()).div(BigNumber.from('10').pow(decimals));
return count.toString();
}
public async getPlasmaCount(address: string): Promise<string> {
if (this.plasma == null) {
return '0';
}
let balance = await this.plasma.functions.balanceOf(address);
let decimals = await this.plasma.functions.decimals();
let count = BigNumber.from(balance.toString()).div(BigNumber.from('10').pow(decimals));
return count.toString();
}
public async getNativeTokenBalance(address: string): Promise<string> {
let balance = await this.provider.getBalance(address);
let actual = ethers.utils.formatEther(balance.toString());
return actual.toString();
}
}