diff --git a/src/base/model.ts b/src/base/model.ts index ab0d8878..02bce87d 100644 --- a/src/base/model.ts +++ b/src/base/model.ts @@ -42,7 +42,7 @@ export class Model { else this.web3Connection = new Web3Connection(web3Connection); if (this.web3Connection.started) - this.loadAbi(); + this.loadAbi(); // cannot call start because start is async, has to be called by user-land } /* eslint-enable complexity */ @@ -63,17 +63,20 @@ export class Model { get account(): Account { return this.connection.Account; } /** - * Permissive way of initializing the contract, used primarily for deploys and options.autoStart = true - * Prefer to use {@link loadContract} + * Initialize the underlying web3js contract */ loadAbi() { - this._contract = new Web3Contract(this.web3, this.abi, this.contractAddress); + this._contract = new Web3Contract(this.web3, this.abi, this._contractAddress); } /** - * Preferred way of initializing and loading a contract, use this function to customize contract loading, - * initializing any other dependencies the contract might have when extending from Model + * Deprecated - async capabilities on this function will affect autoStart: true option, use `start()` instead + * if you need async abilities. + * + * ~~Preferred~~ Alternative way of initializing and loading a contract, ~~use this function to customize contract loading, + * initializing any other dependencies the contract might have when extending from Model~~ * @throws Errors.MissingContractAddress + * @deprecated */ loadContract() { if (!this.contractAddress) @@ -90,18 +93,20 @@ export class Model { const connected = await this.web3Connection.connect(); if (connected) - this.loadContract(); + await this.start(); return connected; } /** * Alias for Web3Connection.start(); - * Will load contract if success + * Will load contract (via {@link loadAbi}) if success; use this function to customize contract loading, initializing any other + * dependencies the contract might have when extending from Model. + * @void */ async start() { await this.web3Connection.start(); - this.loadContract(); + this.loadAbi(); } /** @@ -137,7 +142,7 @@ export class Model { debug, customTransactionHandler: cb }: Partial = {}): Promise { - const from = (await this.web3.eth.givenProvider.request({method: 'eth_requestAccounts'}))[0]; + const from = (await this.web3.eth.givenProvider.request({method: 'eth_requestAccounts'}))?.[0]; return new Promise(async (resolve, reject) => { try { @@ -162,10 +167,10 @@ export class Model { */ async deploy(deployOptions: DeployOptions, account?: Account) { return this.contract.deploy(this.abi, deployOptions, account) - .then(tx => { + .then(async tx => { if (this.web3Connection.options.restartModelOnDeploy && tx.contractAddress) { this._contractAddress = tx.contractAddress; - this.loadContract(); + await this.start(); } return tx; }) diff --git a/src/base/web3-connection.ts b/src/base/web3-connection.ts index 041b6a8c..4407f1ab 100644 --- a/src/base/web3-connection.ts +++ b/src/base/web3-connection.ts @@ -14,12 +14,12 @@ export class Web3Connection { constructor(readonly options: Web3ConnectionOptions) { const {web3CustomProvider: provider = null, autoStart = true} = options; + if (options.restartModelOnDeploy === undefined) + this.options.restartModelOnDeploy = true; + if (autoStart || (provider && typeof provider !== "string" && provider?.connected)) { this.start(); } - - if (options.restartModelOnDeploy === undefined) - this.options.restartModelOnDeploy = true; } /* eslint-enable complexity */ diff --git a/src/interfaces/methods/erc721-standard.ts b/src/interfaces/methods/erc721-standard.ts index a24dcfbc..83c3791a 100644 --- a/src/interfaces/methods/erc721-standard.ts +++ b/src/interfaces/methods/erc721-standard.ts @@ -11,9 +11,9 @@ export interface ERC721StandardMethods { owner(): ContractCallMethod; ownerOf(tokenId: number): ContractCallMethod; safeTransferFrom(from: string, to: string, tokenId: number): ContractSendMethod; - safeTransferFrom(from: string, to: string, tokenId: number, _data: undefined): ContractSendMethod; + safeTransferFrom(from: string, to: string, tokenId: number, _data?: string): ContractSendMethod; setApprovalForAll(operator: string, approved: boolean): ContractSendMethod; - supportsInterface(interfaceId: undefined): ContractCallMethod; + supportsInterface(interfaceId?: string): ContractCallMethod; symbol(): ContractCallMethod; tokenByIndex(index: number): ContractCallMethod; tokenOfOwnerByIndex(owner: string, index: number): ContractCallMethod; diff --git a/src/models/cerc20.ts b/src/models/cerc20.ts index 9b33950d..9b288172 100644 --- a/src/models/cerc20.ts +++ b/src/models/cerc20.ts @@ -25,21 +25,14 @@ export class CERC20 extends Model implements Deployable { async start() { await super.start(); - await this.loadContract(); - } - - async loadContract() { - if (!this.contract) - super.loadContract(); - - if (!this.contractAddress) - throw new Error(Errors.MissingContractAddress); - if (!this.underlyingAddress) - throw new Error(Errors.MissingERC20UnderlyingToken); + if (this.contractAddress) { + if (!this.underlyingAddress) + throw new Error(Errors.MissingERC20UnderlyingToken); - this._erc20 = new ERC20(this.connection, this.underlyingAddress); - await this._erc20.loadContract(); + this._erc20 = new ERC20(this.connection, this.underlyingAddress); + await this._erc20.start(); + } } async deployJsonAbi(underlying_: string, initialExchangeRate_: number, decimals_: number) { diff --git a/src/models/erc20-distribution.ts b/src/models/erc20-distribution.ts index 2b7b4a6d..83a9c0c6 100644 --- a/src/models/erc20-distribution.ts +++ b/src/models/erc20-distribution.ts @@ -25,20 +25,16 @@ export class ERC20Distribution extends Model implement get pausable() { return this._pausable } get ownable() { return this._ownable } - async loadContract() { - if (!this.contract) - super.loadContract(); - - this._ownable = new Ownable(this); - this._pausable = new Pausable(this); - - this._erc20 = new ERC20(this.connection, await this.callTx(this.contract.methods.erc20())); - await this._erc20.loadContract(); - } - async start() { await super.start(); - await this.loadContract(); + + if (this.contract) { + this._ownable = new Ownable(this); + this._pausable = new Pausable(this); + + this._erc20 = new ERC20(this.connection, await this.callTx(this.contract.methods.erc20())); + await this._erc20.start(); + } } async deployJsonAbi() { diff --git a/src/models/erc20-token-lock.ts b/src/models/erc20-token-lock.ts index 7fb29633..0d5ccb24 100644 --- a/src/models/erc20-token-lock.ts +++ b/src/models/erc20-token-lock.ts @@ -91,18 +91,14 @@ export class Erc20TokenLock extends Model implements Depl async start() { await super.start(); - await this.loadContract(); - } - - async loadContract() { - if (!this.contract) - super.loadContract(); - this._ownable = new Ownable(this); - this._pausable = new Pausable(this); + if (this.contract) { + this._ownable = new Ownable(this); + this._pausable = new Pausable(this); - this._erc20 = new ERC20(this.connection, await this.getERC20TokenAddress()); - await this._erc20.loadContract(); + this._erc20 = new ERC20(this.connection, await this.getERC20TokenAddress()); + await this._erc20.start(); + } } deployJsonAbi(erc20ContractAddress: string) { diff --git a/src/models/erc20.ts b/src/models/erc20.ts index 9250dbe4..6a8ce529 100644 --- a/src/models/erc20.ts +++ b/src/models/erc20.ts @@ -21,16 +21,11 @@ export class ERC20 extends Model implements Deployable { async start() { await super.start(); - await this.loadContract(); - } - - async loadContract() { - if (!this.contract) - super.loadContract(); - this._ownable = new Ownable(this); - - this._decimals = await this.callTx(this.contract.methods.decimals()) || 18; + if (this.contractAddress) { + this._ownable = new Ownable(this); + this._decimals = await this.callTx(this.contract.methods.decimals()) || 18; + } } async name(): Promise { @@ -62,6 +57,7 @@ export class ERC20 extends Model implements Deployable { } /** + * use {@link transfer} * @deprecated */ async transferTokenAmount(toAddress: string, amount: string | number) { diff --git a/src/models/erc4626.ts b/src/models/erc4626.ts index d76698c6..40f4c48d 100644 --- a/src/models/erc4626.ts +++ b/src/models/erc4626.ts @@ -31,12 +31,15 @@ export class ERC4626 extends Model implements Deployable { return this.deploy(deployOptions, this.connection.Account); } - async loadContract() { - super.loadContract(); + async start() { + await super.start(); + + if (!this.contract) + return; this._decimals = await this.decimals(); this._asset = new ERC20(this.connection, await this.assetAddress()); - await this._asset.loadContract(); + await this._asset.start(); } async allowance(owner: string, spender: string) { diff --git a/src/models/erc721-collectibles.ts b/src/models/erc721-collectibles.ts index 54872438..c476f8a4 100644 --- a/src/models/erc721-collectibles.ts +++ b/src/models/erc721-collectibles.ts @@ -20,11 +20,11 @@ export class ERC721Collectibles extends Model impleme async loadContract() { if (!this.contract) - super.loadContract(); + return; const contractAddress = this._purchaseToken || await this.callTx(this.contract.methods._purchaseToken()); this._erc20 = new ERC20(this.connection, contractAddress); - await this._erc20.loadContract(); + await this._erc20.start(); } async start() { @@ -178,7 +178,6 @@ export class ERC721Collectibles extends Model impleme async setPricePerPack(newPrice: string | number) { newPrice = toSmartContractDecimals(newPrice, this.erc20.decimals); return this.sendTx(this.contract.methods.setPricePerPack(newPrice)); - // return this.sendTx(this.contract.methods.setPricePerPack(newPrice)); } async setPurchaseTokenAddress(purchaseToken: string) { @@ -246,7 +245,7 @@ export class ERC721Collectibles extends Model impleme } async getRegisteredIDs(_address: string) { - return (await this.callTx(this.contract.methods.getRegisteredIDs(_address))).map(id => +id); + return (await this.callTx(this.contract.methods.getRegisteredIDs(_address)))?.map(id => +id); } } diff --git a/src/models/erc721-standard.ts b/src/models/erc721-standard.ts index 4da7d783..75f0ab4d 100644 --- a/src/models/erc721-standard.ts +++ b/src/models/erc721-standard.ts @@ -5,6 +5,7 @@ import {Web3ConnectionOptions} from '@interfaces/web3-connection-options'; import ERC721Standard from '@abi/ERC721Standard.json'; import {AbiItem} from 'web3-utils'; import {Deployable} from '@interfaces/deployable'; +import {TransactionReceipt} from "@interfaces/web3-core"; export class Erc721Standard extends Model implements Deployable { @@ -24,11 +25,75 @@ export class Erc721Standard extends Model implements Depl return this.sendTx(this.contract.methods.setTokenURI(tokenId, uri)); } - async mint(to: string, tokenId: number, data?: string) { + async mint(to: string, tokenId: number, data = "0x0") { return this.sendTx(this.contract.methods.mint(to, tokenId, data)) } - deployJsonAbi(name: string, symbol: string) { + async totalSupply() { + return this.callTx(this.contract.methods.totalSupply()) + } + + async approve(address: string, tokenId: number): Promise { + return this.sendTx(this.contract.methods.approve(address, tokenId)); + } + + async getApproved(tokenId: number) { + return this.callTx(this.contract.methods.getApproved(tokenId)); + } + + async isApprovedForAll(owner:string, operator: string) { + return this.callTx(this.contract.methods.isApprovedForAll(owner, operator)); + } + + async setApprovalForAll(owner:string, approved: boolean) { + return this.sendTx(this.contract.methods.setApprovalForAll(owner, approved)); + } + + async balanceOf(address: string): Promise { + return this.sendTx(this.contract.methods.balanceOf(address)); + } + + async safeTransferFrom(from: string, to: string, tokenId: number, data = "0x0") { + return this.sendTx(this.contract.methods.safeTransferFrom(from, to, tokenId, data)) + } + + async transferFrom(from: string, to: string, tokenId: number) { + return this.sendTx(this.contract.methods.safeTransferFrom(from, to, tokenId)) + } + + async transferOwnership(newOwner: string) { + return this.sendTx(this.contract.methods.transferOwnership(newOwner)) + } + + async supportsInterface(interfaceId: string) { + return this.callTx(this.contract.methods.supportsInterface(interfaceId)) + } + + async symbol() { + return this.callTx(this.contract.methods.symbol()) + } + + async name() { + return this.callTx(this.contract.methods.name()) + } + + async owner() { + return this.callTx(this.contract.methods.owner()) + } + + async ownerOf(tokenId: number) { + return this.callTx(this.contract.methods.ownerOf(tokenId)) + } + + async tokenByIndex(index: number) { + return this.callTx(this.contract.methods.tokenByIndex(index)) + } + + async tokenOfOwnerByIndex(owner: string, index: number) { + return this.callTx(this.contract.methods.tokenOfOwnerByIndex(owner, index)) + } + + async deployJsonAbi(name: string, symbol: string) { const options = { data: ERC721Standard.bytecode, arguments: [name, symbol] diff --git a/src/models/loophole.ts b/src/models/loophole.ts index 49689129..3a59b090 100644 --- a/src/models/loophole.ts +++ b/src/models/loophole.ts @@ -35,38 +35,37 @@ export class Loophole extends Model implements Deployable, IsOw get swap() { return this._swap; } get ethUtils() { return this._ethUtils; } - /* eslint-disable complexity */ - async loadContract() { - if (!this.contract) - super.loadContract(); - + async loadDependencies() { if (!this.ethUtilsAddress) throw new Error(Errors.MissingEthUtilsAddressPleaseProvideOne); this._ethUtils = new ETHUtils(this.connection, this.ethUtilsAddress); this._ownable = new Ownable(this); - const lpTokenAddress = await this.lpToken() || this.lpTokenAddress; + const lpTokenAddress = await this.lpToken(); if (!lpTokenAddress) throw new Error(Errors.MissingLpTokenAddressPleaseDeployUsingOne); this._erc20 = new ERC20(this.connection, lpTokenAddress); - const swapRouterAddress = await this.swapRouter() || this.swapRouterAddress; + const swapRouterAddress = await this.swapRouter(); if (!swapRouterAddress) throw new Error(Errors.MissingSwapAddressPleaseDeployUsingOne); this._swap = new UniswapV3RouterBridge(this.connection, swapRouterAddress); - await this._swap.loadContract(); - await this._erc20.loadContract(); - await this._ethUtils.loadContract(); + await this._erc20.start(); + await this._swap.start(); + await this._ethUtils.start(); } - /* eslint-enable complexity */ async start() { await super.start(); - await this.loadContract(); + + if (!this.contractAddress) + return; + + await this.loadDependencies() } async deployJsonAbi(_swapRouter: string, @@ -77,7 +76,7 @@ export class Loophole extends Model implements Deployable, IsOw _exitPenaltyLP: number) { const erc20 = new ERC20(this.connection, _lpToken); - await erc20.loadContract(); + await erc20.start(); const lpTokensPerBlock = toSmartContractDecimals(_lpTokensPerBlock, erc20.decimals); const deployOptions = { @@ -101,6 +100,8 @@ export class Loophole extends Model implements Deployable, IsOw } async lpToken() { + if (this.lpTokenAddress) + return this.lpTokenAddress; return this.callTx(this.contract.methods.lpToken()); } @@ -121,6 +122,8 @@ export class Loophole extends Model implements Deployable, IsOw } async swapRouter() { + if (this.swapRouterAddress) + return this.swapRouterAddress; return this.callTx(this.contract.methods.swapRouter()); } diff --git a/src/models/network-factory.ts b/src/models/network-factory.ts index fae06452..5e2d24e8 100644 --- a/src/models/network-factory.ts +++ b/src/models/network-factory.ts @@ -74,15 +74,12 @@ export class NetworkFactory extends Model implements Depl async start() { await super.start(); - await this.loadContract(); - } - async loadContract() { if (!this.contract) - super.loadContract(); + return; this._erc20 = new ERC20(this.connection, await this.getSettlerTokenAddress()); - await this._erc20.loadContract(); + await this._erc20.start(); } deployJsonAbi(erc20ContractAddress: string): Promise { diff --git a/src/models/network-registry.ts b/src/models/network-registry.ts index 197c8139..0d7dbbea 100644 --- a/src/models/network-registry.ts +++ b/src/models/network-registry.ts @@ -38,7 +38,7 @@ export class Network_Registry extends Model implements async loadContract() { if (!this.contract) - await super.loadContract(); + return; const erc20Address = await this.erc20(); const bountyTokenAddress = await this.bountyTokenAddress(); @@ -49,8 +49,8 @@ export class Network_Registry extends Model implements this._DIVISOR = await this.getDivisor(); - await this._token.loadContract(); - await this._bountyToken.loadContract(); + await this._token.start(); + await this._bountyToken.start(); } async deployJsonAbi(_erc20: string, @@ -62,7 +62,6 @@ export class Network_Registry extends Model implements bountyToken = nativeZeroAddress) { const token = new ERC20(this.connection, _erc20); - await token.loadContract(); const deployOptions = { data: Network_RegistryJson.bytecode, diff --git a/src/models/network-v2.ts b/src/models/network-v2.ts index 57bfe57b..e8a8423e 100644 --- a/src/models/network-v2.ts +++ b/src/models/network-v2.ts @@ -66,12 +66,9 @@ export class Network_v2 extends Model implements Deployable { async start() { await super.start(); - await this.loadContract(); - } - async loadContract() { - if (!this.contract) - await super.loadContract(); + if (!this.contractAddress) + return; const nftAddress = await this.nftTokenAddress(); const transactionalTokenAddress = await this.networkTokenAddress(); @@ -79,20 +76,19 @@ export class Network_v2 extends Model implements Deployable { this._governed = new Governed(this); this._networkToken = new ERC20(this.connection, transactionalTokenAddress); - await this._networkToken.loadContract(); + await this._networkToken.start(); if (nftAddress !== nativeZeroAddress) { this._nftToken = new BountyToken(this.connection, nftAddress); - await this._nftToken.loadContract(); + await this._nftToken.start(); } if (registryAddress !== nativeZeroAddress) { this._registry = new NetworkRegistry(this.connection, registryAddress); - await this._registry.loadContract(); + await this._registry.start(); } this._DIVISOR = await this.getDivisor(); - } async deployJsonAbi(_oracleTokenAddress: string, @@ -382,13 +378,13 @@ export class Network_v2 extends Model implements Deployable { let _rewardAmount = 0 as string | number; const isFundingRequest = new BigNumber(fundingAmount).gt(0); const _transactional = new ERC20(this.connection, transactional); - await _transactional.loadContract(); + await _transactional.start(); const _tokenAmount = toSmartContractDecimals(isFundingRequest ? 0 : tokenAmount, _transactional.decimals); const _fundingAmount = toSmartContractDecimals(fundingAmount, _transactional.decimals); if (rewardAmount && rewardToken !== nativeZeroAddress) { const rewardERC20 = new ERC20(this.connection, rewardToken); - await rewardERC20.loadContract(); + await rewardERC20.start(); _rewardAmount = toSmartContractDecimals(rewardAmount, rewardERC20.decimals); } diff --git a/src/models/network.ts b/src/models/network.ts index 43b9beec..8da07303 100644 --- a/src/models/network.ts +++ b/src/models/network.ts @@ -69,25 +69,10 @@ export class Network extends Model implements Deployable { } } - // Arrays cant be queried without an argument - // async getAmountOfDisputers(): Promise { - // return (await this.sendTx(this.contract.methods.oraclersArray(), true)).length; - // } - - // Method does not exist - // async percentageNeededForApprove(): Promise { - // return parseInt(await this.sendTx(this.contract.methods.percentageNeededForApprove(), true), 10); - // } - async percentageNeededForDispute() { return +(await this.callTx(this.contract.methods.percentageNeededForDispute())); } - // Method does not exist - // async percentageNeededForMerge(): Promise { - // return parseInt(await this.sendTx(this.contract.methods.percentageNeededForMerge(), true), 10) - // } - async mergeCreatorFeeShare() { return +(await this.callTx(this.contract.methods.mergeCreatorFeeShare())); } @@ -245,15 +230,9 @@ export class Network extends Model implements Deployable { async start() { await super.start(); - await this.loadContract(); - } - async loadContract() { if (!this.contract) - super.loadContract(); - - if (!this.contractAddress) - throw new Error(Errors.MissingContractAddress); + return; const transactionAddress = await this.getTransactionTokenAddress(); const settlerAddress = await this.getSettlerTokenAddress(); @@ -261,11 +240,11 @@ export class Network extends Model implements Deployable { this._transactionToken = new ERC20(this.connection, transactionAddress); this._settlerToken = new ERC20(this.connection, settlerAddress); - await this._transactionToken.loadContract(); - await this._settlerToken.loadContract(); - + await this._transactionToken.start(); + await this._settlerToken.start(); } + deployJsonAbi(settlerAddress: string, transactionalAddress: string, governanceAddress: string) { const deployOptions = { diff --git a/src/models/real-fevr-marketplace.ts b/src/models/real-fevr-marketplace.ts index 7b4bde65..fdda5d10 100644 --- a/src/models/real-fevr-marketplace.ts +++ b/src/models/real-fevr-marketplace.ts @@ -19,8 +19,7 @@ export class RealFevrMarketplace extends Model imple super(web3Connection, RealFevrMarketplaceJson.abi as AbiItem[], contractAddress); } - private _isETHTransaction!: boolean; - get isETHTransaction() { return this._isETHTransaction; } + get isETHTransaction() { return this.tokenAddress === nativeZeroAddress; } private _decimals = 18; get decimals(): number { return this._decimals; } @@ -32,38 +31,32 @@ export class RealFevrMarketplace extends Model imple get opener() { return this._opener; } /* eslint-disable complexity */ - async loadContract() { - if (!this.contract) - await super.loadContract(); + async start() { + await super.start(); + + if (!this.contractAddress) + return; - const tokenAddress = await this.getERC20TokenAddress() || this.tokenAddress; + const tokenAddress = await this.getERC20TokenAddress(); if (!tokenAddress) throw new Error(Errors.MissingERC20AddressOnContract); - const collectiblesAddress = await this.getERC721TokenAddress() || this.collectiblesAddress; + const collectiblesAddress = await this.getERC721TokenAddress(); if (!collectiblesAddress) throw new Error(Errors.MissingERC721AddressOnContract); - this._isETHTransaction = tokenAddress === nativeZeroAddress; - - if (!this._isETHTransaction) { + if (tokenAddress !== nativeZeroAddress) { // Set Token Address Contract for easy access this._erc20 = new ERC20(this.connection, tokenAddress); - await this._erc20.loadContract(); - + await this._erc20.start(); this._decimals = this._erc20.decimals; } this._opener = new RealFevrOpener(this.connection, collectiblesAddress); - await this._opener.loadContract(); + await this._opener.start(); } /* eslint-enable complexity */ - async start() { - await super.start(); - await this.loadContract(); - } - /** * The marketplace can be deployed on a native-transactions mode; simply assign tokenAddress to * the null wallet '0x0000000000000000000000000000000000000000', and all transactions will be @@ -105,10 +98,14 @@ export class RealFevrMarketplace extends Model imple } async getERC20TokenAddress() { + if (this.tokenAddress) + return this.tokenAddress; return this.callTx(this.contract.methods.erc20Address()); } async getERC721TokenAddress() { + if (this.collectiblesAddress) + return this.collectiblesAddress; return this.callTx(this.contract.methods.erc721Address()); } diff --git a/src/models/real-fevr-opener.ts b/src/models/real-fevr-opener.ts index 91055655..d2d76509 100644 --- a/src/models/real-fevr-opener.ts +++ b/src/models/real-fevr-opener.ts @@ -25,28 +25,22 @@ export class RealFevrOpener extends Model implements Depl private _erc20!: ERC20; get erc20() { return this._erc20; } - /* eslint-disable complexity */ - async loadContract() { + + async start() { + await super.start(); + if (!this.contract) - await super.loadContract(); + return; - const purchaseToken = await this._purchaseToken() || this.purchaseTokenAddress; + const purchaseToken = await this._purchaseToken(); if (!purchaseToken) throw new Error(Errors.MissingERC20AddressOnContractPleaseSetPurchaseToken); - if (purchaseToken && purchaseToken !== nativeZeroAddress) { + if (purchaseToken !== nativeZeroAddress) { this._erc20 = new ERC20(this.connection, purchaseToken); - await this._erc20.loadContract(); - this._decimals = this._erc20.decimals; } } - /* eslint-enable complexity */ - - async start() { - await super.start(); - await this.loadContract(); - } async deployJsonAbi(name: string, symbol: string, _purchaseToken: string) { const deployOptions = { @@ -62,6 +56,8 @@ export class RealFevrOpener extends Model implements Depl } async _purchaseToken() { + if (this.purchaseTokenAddress) + return this.purchaseTokenAddress; return this.callTx(this.contract.methods._purchaseToken()); } diff --git a/src/models/staking-contract.ts b/src/models/staking-contract.ts index d784a86f..ee9a7f7e 100644 --- a/src/models/staking-contract.ts +++ b/src/models/staking-contract.ts @@ -37,7 +37,7 @@ export class StakingContract extends Model implements De /* eslint-disable complexity */ async loadContract() { if (!this.contract) - super.loadContract(); + return; this._ownable = new Ownable(this); this._pausable = new Pausable(this); @@ -45,7 +45,7 @@ export class StakingContract extends Model implements De const tokenAddress = this.stakeTokenAddress || await this.callTx(this.contract.methods.erc20()); this._erc20 = new ERC20(this.connection, tokenAddress); - await this._erc20.loadContract(); + await this._erc20.start(); const collectiblesAddress = this.collectiblesAddress || await this.callTx(this.contract.methods.erc721()); if (collectiblesAddress) { diff --git a/src/models/votable.ts b/src/models/votable.ts index 592a65d1..96455f48 100644 --- a/src/models/votable.ts +++ b/src/models/votable.ts @@ -23,11 +23,10 @@ export class Votable extends Model implements Deployable { async loadContract() { if (!this.contract) - super.loadContract(); + return; const contractAddress = this.erc20TokenAddress || await this.callTx(this.contract.methods.erc20()); this._erc20 = new ERC20(this.connection, contractAddress); - await this._erc20.loadContract(); } async start() { diff --git a/test/models/base-model.spec.ts b/test/models/base-model.spec.ts index a398a901..658fd155 100644 --- a/test/models/base-model.spec.ts +++ b/test/models/base-model.spec.ts @@ -3,8 +3,9 @@ import {Web3Connection} from '@base/web3-connection'; import {Model} from '@base/model'; import {expect} from 'chai'; import {Errors} from '@interfaces/error-enum'; -import {getPrivateKeyFromFile, shouldBeRejected} from '../utils/'; +import {getPrivateKeyFromFile, hasTxBlockNumber, shouldBeRejected} from '../utils/'; import erc20 from "../../build/contracts/ERC20.json"; +import {ERC20} from "../../src"; describe(`Model`, () => { let deployedAddress: string; @@ -38,7 +39,7 @@ describe(`Model`, () => { const model = new Model(web3Connection, erc20.abi as any); const tx = - await model.deploy({data: erc20.bytecode, arguments: ["name", "symbol"]}, web3Connection.Account); + await hasTxBlockNumber(model.deploy({data: erc20.bytecode, arguments: ["name", "symbol"]}, web3Connection.Account)); expect(model.contract.abi).to.exist; expect(tx.blockNumber).to.exist; @@ -54,5 +55,15 @@ describe(`Model`, () => { const AliceAddress = model.web3.eth.accounts.privateKeyToAccount(getPrivateKeyFromFile(1)).address; await shouldBeRejected(model.sendTx(model.contract.methods.transfer(AliceAddress, '10'))) }) + + it(`should await the start of a custom model after deploy`, async () => { + const model = new ERC20({...options, autoStart: true}); + const BobAddress = model.web3.eth.accounts.privateKeyToAccount(getPrivateKeyFromFile(0)).address; + const AliceAddress = model.web3.eth.accounts.privateKeyToAccount(getPrivateKeyFromFile(1)).address; + + await hasTxBlockNumber(model.deployJsonAbi("name", "symbol", "2000000000000000000", BobAddress)); + await hasTxBlockNumber(model.transfer(AliceAddress, 1)); + expect(await model.name()).to.be.eq(`name`) + }) }) }) diff --git a/test/models/erc1155-ownable.spec.ts b/test/models/erc1155-ownable.spec.ts index 6c11970a..c72bc2dc 100644 --- a/test/models/erc1155-ownable.spec.ts +++ b/test/models/erc1155-ownable.spec.ts @@ -38,7 +38,7 @@ describe(`ERC1155 Ownable`, () => { describe(`Methods`, () => { before(async () => { contract = new ERC1155Ownable(web3Connection, contractAddress!); - await contract.loadContract(); + await contract.start(); }); it(`Set a new URI for all tokens`, async () => { diff --git a/test/models/erc1155-standard.spec.ts b/test/models/erc1155-standard.spec.ts index d1ed3558..4977bb83 100644 --- a/test/models/erc1155-standard.spec.ts +++ b/test/models/erc1155-standard.spec.ts @@ -29,7 +29,7 @@ describe(`ERC1155 Standard`, () => { describe(`Methods`, () => { before(async () => { contract = new ERC1155Standard(web3Connection, contractAddress!); - await contract.loadContract(); + await contract.start(); }); it(`Set a new URI for all tokens`, async () => { diff --git a/test/models/erc20-token-lock.spec.ts b/test/models/erc20-token-lock.spec.ts index e2b680f4..a59106d3 100644 --- a/test/models/erc20-token-lock.spec.ts +++ b/test/models/erc20-token-lock.spec.ts @@ -35,7 +35,7 @@ describe(`ERC20TokenLock`, () => { before(async () => { tokenLock = new Erc20TokenLock(web3Connection, contractAddress); accountAddress = web3Connection.Account.address; - await tokenLock.loadContract(); + await tokenLock.start(); }); it(`Sets max amount to lock`, async () => { diff --git a/test/models/erc4626.spec.ts b/test/models/erc4626.spec.ts index befef282..da4c52e5 100644 --- a/test/models/erc4626.spec.ts +++ b/test/models/erc4626.spec.ts @@ -37,7 +37,7 @@ describe(`ERC4626`, () => { const erc20Receipt = await erc20Deployer(name, symbol, '0', web3Connection); erc20 = new ERC20(web3Connection, erc20Receipt.contractAddress); - await erc20.loadContract() + await erc20.start() }); it(`Deploys`, async () => { @@ -49,8 +49,8 @@ describe(`ERC4626`, () => { let erc4626: ERC4626; it(`Loads contracts`,async () => { - erc4626 = new ERC4626(web3Connection, erc4626Address);- - await erc4626.loadContract(); + erc4626 = new ERC4626(web3Connection, erc4626Address); + await erc4626.start(); }); it(`Asserts underlying ERC20`, () => { diff --git a/test/models/erc721-colectibles.spec.ts b/test/models/erc721-colectibles.spec.ts index 245792db..52d11e95 100644 --- a/test/models/erc721-colectibles.spec.ts +++ b/test/models/erc721-colectibles.spec.ts @@ -38,7 +38,7 @@ describe(`ERC271Collectibles`, () => { describe(`Methods`, () => { before(async () => { contract = new ERC721Collectibles(web3Connection, contractAddress!, purchaseTokenAddress); - await contract.loadContract(); + await contract.start(); }); it(`Asserts that created is limited`, async () => { diff --git a/test/models/network-factory.spec.ts b/test/models/network-factory.spec.ts index 6abd09f6..b10f835a 100644 --- a/test/models/network-factory.spec.ts +++ b/test/models/network-factory.spec.ts @@ -49,7 +49,7 @@ describe(`NetworkFactory`, () => { before(async () => { networkFactory = new NetworkFactory(web3Connection, networkFactoryContractAddress); - await networkFactory.loadContract(); + await networkFactory.start(); }) it(`Matches token address`, async () => { @@ -100,7 +100,7 @@ describe(`NetworkFactory`, () => { before(async () => { network = new Network(web3Connection, await networkFactory.getNetworkByAddress(accountAddress)); - await network.loadContract(); + await network.start(); }) it(`Asserts governor === accountAddress`, async () => { diff --git a/test/models/network-v2.spec.ts b/test/models/network-v2.spec.ts index 93889c6b..961e26bf 100644 --- a/test/models/network-v2.spec.ts +++ b/test/models/network-v2.spec.ts @@ -43,24 +43,24 @@ describe(`NetworkV2`, () => { rewardToken = new ERC20(web3Connection, rewardReceipt.contractAddress); bountyToken = new BountyToken(web3Connection, nftReceipt.contractAddress); - await networkToken.loadContract(); - await bountyTransactional.loadContract(); - await bountyToken.loadContract(); - await rewardToken.loadContract(); + await networkToken.start(); + await bountyTransactional.start(); + await bountyToken.start(); + await rewardToken.start(); - await bountyTransactional.transferTokenAmount(Alice.address, 10000); - await bountyTransactional.transferTokenAmount(Bob.address, 10000); + await bountyTransactional.transfer(Alice.address, 10000); + await bountyTransactional.transfer(Bob.address, 10000); }) describe(`No Registry`, () => { it(`Deploys Network_V2`, async () => { const _network = new Network_v2(web3Connection); - _network.loadAbi(); - const receipt = await _network.deployJsonAbi(networkToken.contractAddress!) - expect(receipt.contractAddress).to.exist; - network = new Network_v2(web3Connection, receipt.contractAddress); - await network.loadContract(); + await _network.start(); + const tx = await hasTxBlockNumber(_network.deployJsonAbi(networkToken.contractAddress!)) + expect(tx.contractAddress).to.exist; + network = new Network_v2(web3Connection, tx.contractAddress); + await network.start(); }); describe(`Owner`, () => { @@ -163,7 +163,7 @@ describe(`NetworkV2`, () => { const events = await network.getBountyCreatedEvents({fromBlock: receipt.blockNumber, filter: {creator: Admin.address}}); - expect(await bountyTransactional.getTokenAmount(network.contractAddress!)).to.eq((1000).toString()); + expect(await bountyTransactional.balanceOf(network.contractAddress!)).to.eq((1000).toString()); expect(events.length).to.be.eq(1); expect(events[0].returnValues.cid).to.be.eq('c1'); expect((await network.getBountiesOfAddress(Admin.address)).length).to.be.eq(1); @@ -411,9 +411,9 @@ describe(`NetworkV2`, () => { it(`Alice withdraws from bounty`, async () => { await hasTxBlockNumber(network.withdrawFundingReward(bountyId, 0)); - expect(await rewardToken.getTokenAmount(Alice.address)).to.be.eq((500).toString()); + expect(await rewardToken.balanceOf(Alice.address)).to.be.eq((500).toString()); await hasTxBlockNumber(network.withdrawFundingReward(bountyId, 1)); - expect(await rewardToken.getTokenAmount(Alice.address)).to.be.eq((1000).toString()); + expect(await rewardToken.balanceOf(Alice.address)).to.be.eq((1000).toString()); }); }); }); @@ -430,7 +430,7 @@ describe(`NetworkV2`, () => { const networkReceipt = await modelExtensionDeployer(web3Connection, Network_v2, [networkToken.contractAddress, registryReceipt.contractAddress]); network = new Network_v2(web3Connection, networkReceipt.contractAddress); - await network.loadContract(); + await network.start(); }); diff --git a/test/models/network.spec.ts b/test/models/network.spec.ts index aba1b082..68722ed8 100644 --- a/test/models/network.spec.ts +++ b/test/models/network.spec.ts @@ -55,7 +55,7 @@ describe(`Network`, () => { describe(`Methods`, () => { before(async () => { network = new Network(web3Connection, networkContractAddress); - await network.loadContract(); + await network.start(); accountAddress = web3Connection.Account.address; }); diff --git a/test/models/real-fevr-marketplace.spec.ts b/test/models/real-fevr-marketplace.spec.ts index 5ade5101..e5b0e814 100644 --- a/test/models/real-fevr-marketplace.spec.ts +++ b/test/models/real-fevr-marketplace.spec.ts @@ -52,7 +52,7 @@ describe('Marketplace RealFevr', async () => { describe('Methods', () => { before(async () => { marketplaceContract = new RealFevrMarketplace(web3Connection, contractAddress!, openerAddress, tokenAddress); - await marketplaceContract.loadContract(); + await marketplaceContract.start(); }); it('should create pack of NFTs', async () => { diff --git a/test/models/sablier.spec.ts b/test/models/sablier.spec.ts index 2fe5dc1c..9e7f7774 100644 --- a/test/models/sablier.spec.ts +++ b/test/models/sablier.spec.ts @@ -41,15 +41,15 @@ describe.skip(`Sablier`, () => { const ethUtilsTx = await modelExtensionDeployer(web3Connection, ETHUtils); ethUtils = new ETHUtils(web3Connection, ethUtilsTx.contractAddress); - await ethUtils.loadContract(); + await ethUtils.start(); const erc20Tx = await erc20Deployer(`My Sablier`, `$sablier`, toSmartContractDecimals(AMOUNT_1M), web3Connection); erc20 = new ERC20(web3Connection, erc20Tx.contractAddress); - await erc20.loadContract(); + await erc20.start(); const cercTx = await modelExtensionDeployer(web3Connection, CERC20, [erc20.contractAddress, INITIAL_EXCHANGE_RATE, 18]); cerc20 = new CERC20(web3Connection, cercTx.contractAddress, erc20.contractAddress!); - await cerc20.loadContract(); + await cerc20.start(); await web3Connection.switchToAccount(Admin.privateKey); // go back to the original account that created contracts await erc20.transferTokenAmount(Alice.address, SALARY); @@ -73,7 +73,7 @@ describe.skip(`Sablier`, () => { before(async () => { sablier = new Sablier(web3Connection, contractAddress); - await sablier.loadContract(); + await sablier.start(); startTime = await getChainDate(web3Connection); }); diff --git a/test/models/staking.spec.ts b/test/models/staking.spec.ts index 025a6bbe..b211b0af 100644 --- a/test/models/staking.spec.ts +++ b/test/models/staking.spec.ts @@ -53,7 +53,7 @@ describe(`StakingContract`, () => { describe(`Methods`, () => { before(async () => { contract = new StakingContract(web3Connection, contractAddress, stakeTokenAddress); - await contract.loadContract(); + await contract.start(); }); it(`Approves transfers`, async () => { diff --git a/test/models/votable.spec.ts b/test/models/votable.spec.ts index 9cf2797b..ed2c754f 100644 --- a/test/models/votable.spec.ts +++ b/test/models/votable.spec.ts @@ -34,7 +34,7 @@ describe(`Votable`, () => { let pollId!: number; before(async () => { contract = new Votable(web3Connection, contractAddress, tokenContractAddress); - await contract.loadContract(); + await contract.start(); }); it(`Creates a poll`, async () => {