ufomarketplace-sdk
Version:
SDK to interact with set ufo marketplace contracts
169 lines • 7.48 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ufoInfoSDK = void 0;
const ethers_1 = require("ethers");
const constants_1 = require("./constants/constants");
const ufoABI_json_1 = __importDefault(require("./abis/ufoABI.json"));
const ufoLPABI_json_1 = __importDefault(require("./abis/ufoLPABI.json"));
const ufoLPABI_json_2 = __importDefault(require("./abis/ufoLPABI.json"));
const ethereum_multicall_1 = require("ethereum-multicall");
const abis_1 = require("./abis");
const ufoAbi = JSON.stringify(ufoABI_json_1.default);
const ufoLPAbi = JSON.stringify(ufoLPABI_json_1.default);
const plasmaAbi = JSON.stringify(ufoLPABI_json_2.default);
class ufoInfoSDK {
constructor(ufoConfig, netType) {
this.ethProvider = null;
this.maticProvider = null;
this.ufoConfig = null;
this.ufoConfig = ufoConfig;
if (netType == 'mainnet') {
this.maticProvider = ethers_1.ethers.providers.getDefaultProvider(constants_1.RPCS[constants_1.NETWORK_ID.POLYGON]);
this.ethProvider = ethers_1.ethers.providers.getDefaultProvider(constants_1.RPCS[constants_1.NETWORK_ID.ETHEREUM]);
}
else if (netType == 'testnet') {
this.maticProvider = ethers_1.ethers.providers.getDefaultProvider(constants_1.RPCS[constants_1.NETWORK_ID.MUMBAI]);
this.ethProvider = ethers_1.ethers.providers.getDefaultProvider(constants_1.RPCS[constants_1.NETWORK_ID.GOERLI]);
}
else {
console.error('UFO INFO SDK', 'provided wrong netType, netType will be "mainnet" or "testnet"');
}
}
getMultiCallParams(netType, userAddr) {
let ufoAddr = null;
let ufoLpAddr = null;
let plasmaAddr = null;
if (netType == 'eth') {
ufoAddr = this.ufoConfig.ufoTokenOnEth;
ufoLpAddr = this.ufoConfig.ufoLPTokenOnEth;
plasmaAddr = this.ufoConfig.plasmaTokenOnEth;
}
else if (netType == 'polygon') {
ufoAddr = this.ufoConfig.ufoTokenOnMatic;
ufoLpAddr = this.ufoConfig.ufoLPTokenOnMatic;
plasmaAddr = this.ufoConfig.plasmaTokenOnMatic;
}
let contractsCall = [
{
reference: 'ufo',
contractAddress: ufoAddr,
abi: abis_1.BALANCE_ABI,
calls: [
{
reference: 'balance',
methodName: 'balanceOf',
methodParameters: [userAddr],
},
],
},
{
reference: 'ufo_lp',
contractAddress: ufoLpAddr,
abi: abis_1.BALANCE_ABI,
calls: [
{
reference: 'balance',
methodName: 'balanceOf',
methodParameters: [userAddr],
},
],
},
];
if (plasmaAddr != undefined) {
contractsCall.push({
reference: 'plasma',
contractAddress: plasmaAddr,
abi: abis_1.BALANCE_ABI,
calls: [
{
reference: 'balance',
methodName: 'balanceOf',
methodParameters: [userAddr],
},
],
});
}
return contractsCall;
}
getInfoOfEth(address) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.getBalanceInfo(address, 'eth');
});
}
getInfoOfMatic(address) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.getBalanceInfo(address, 'polygon');
});
}
getBalanceInfo(address, blockchainType) {
return __awaiter(this, void 0, void 0, function* () {
let provider = blockchainType == 'eth' ? this.ethProvider : this.maticProvider;
const multicall = new ethereum_multicall_1.Multicall({
ethersProvider: provider,
tryAggregate: true,
});
const multiParam = this.getMultiCallParams(blockchainType, address);
let res = yield multicall.call(multiParam);
let ufoBalance, ufoLpBalance, plasma;
try {
ufoBalance = ethers_1.BigNumber.from(res.results.ufo.callsReturnContext[0].returnValues[0].hex.toString()).div(ethers_1.BigNumber.from('10').pow(18));
ufoLpBalance = ethers_1.BigNumber.from(res.results.ufo_lp.callsReturnContext[0].returnValues[0].hex.toString()).div(ethers_1.BigNumber.from('10').pow(18));
plasma =
res.results.plasma != undefined
? ethers_1.BigNumber.from(res.results.plasma.callsReturnContext[0].returnValues[0].hex.toString()).div(ethers_1.BigNumber.from('10').pow(18))
: 0;
}
catch (e) {
console.log(e);
}
let balance = yield provider.getBalance(address);
let actual = ethers_1.ethers.utils.formatEther(balance.toString());
return {
ufo: ufoBalance.toString(),
ufoLP: ufoLpBalance.toString(),
plasma: plasma.toString(),
nativeCrypto: actual.toString(),
};
});
}
getNFTContractInfo(nftAddr, nftType = 'polygon') {
return __awaiter(this, void 0, void 0, function* () {
let nftContract = null;
if (nftType == 'polygon') {
nftContract = new ethers_1.ethers.Contract(nftAddr, abis_1.TOTAL_SUPPLY_ABI, this.maticProvider);
}
else {
nftContract = new ethers_1.ethers.Contract(nftAddr, abis_1.TOTAL_SUPPLY_ABI, this.ethProvider);
}
let totalSupply = 0;
try {
totalSupply = yield nftContract.totalSupply();
}
catch (e) {
if (e.reason == null) {
console.log('Error : network or smart contract address is not correct');
}
else {
console.log(e);
}
}
return {
totalSupply,
};
});
}
}
exports.ufoInfoSDK = ufoInfoSDK;
//# sourceMappingURL=ufoInfoSDK.js.map