UNPKG

@skalenetwork/ima-js

Version:

Simple TS/JS library to interact with SKALE IMA

95 lines (94 loc) 4.47 kB
/** * @license * SKALE ima-js * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <https://www.gnu.org/licenses/>. */ import { Logger } from 'tslog'; import * as transactions from './transactions'; import TimeoutException from './exceptions/TimeoutException'; import * as constants from './constants'; import * as helper from './helper'; const log = new Logger(); export class BaseChain { provider; chainId; abi; constructor(provider, abi, chainId) { this.provider = provider; this.abi = abi; if (chainId !== undefined) this.chainId = chainId; } async getERC20Balance(tokenContract, address) { return await tokenContract.balanceOf(address); } async getERC721OwnerOf(tokenContract, tokenId) { try { if (typeof tokenId === 'string') tokenId = Number(tokenId); return await tokenContract.ownerOf(tokenId); } catch (err) { return constants.ZERO_ADDRESS; // todo: replace with IMA-ERC721 exception: no such token } } async getERC1155Balance(tokenContract, address, tokenId) { return await tokenContract.balanceOf(address, tokenId); } async setTokenURI(tokenContract, tokenId, tokenURI, opts) { const txData = await tokenContract.setTokenURI.populateTransaction(tokenId, tokenURI); return await transactions.send(this.provider, txData, opts, 'TokenContract::setTokenURI'); } async waitETHBalanceChange(address, initial, sleepInterval = constants.DEFAULT_SLEEP, iterations = constants.DEFAULT_ITERATIONS) { for (let i = 1; i <= iterations; i++) { const res = await this.ethBalance(address); if (initial !== res) { return; } log.info(`🔎 ${i}/${iterations} Waiting for ETH balance change - address: ` + `${address}, sleep: ${sleepInterval}ms, initial: ${initial}, current: ${res}`); await helper.sleep(sleepInterval); } throw new TimeoutException('waitETHBalanceChange timeout'); } async waitForChange(tokenContract, getFunc, address, initial, tokenId, sleepInterval = constants.DEFAULT_SLEEP, iterations = constants.DEFAULT_ITERATIONS) { const logData = 'token: ' + await tokenContract.getAddress() + ', address: ' + (address ?? ''); for (let i = 1; i <= iterations; i++) { let res; if (tokenId === undefined) res = await getFunc(tokenContract, address); if (address === undefined) res = await getFunc(tokenContract, tokenId); if (tokenId !== undefined && address !== undefined) { res = await getFunc(tokenContract, address, tokenId); } if (initial !== res) { return; } log.info(`🔎 ${i}/${iterations} Waiting for change - ${logData}, sleep ${sleepInterval}ms`); await helper.sleep(sleepInterval); } throw new TimeoutException('waitForTokenClone timeout - ' + logData); } async waitERC20BalanceChange(tokenContract, address, initialBalance, sleepInterval = constants.DEFAULT_SLEEP) { await this.waitForChange(tokenContract, this.getERC20Balance.bind(this), address, initialBalance, undefined, sleepInterval); } async waitERC721OwnerChange(tokenContract, tokenId, initialOwner, sleepInterval = constants.DEFAULT_SLEEP) { await this.waitForChange(tokenContract, this.getERC721OwnerOf.bind(this), undefined, initialOwner, tokenId, sleepInterval); } async waitERC1155BalanceChange(tokenContract, address, tokenId, initialBalance, sleepInterval = constants.DEFAULT_SLEEP) { await this.waitForChange(tokenContract, this.getERC1155Balance.bind(this), address, initialBalance, tokenId, sleepInterval); } }