1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

add abstract SmartContract classes

This commit is contained in:
Miquel A. Cabot 2022-06-06 17:20:13 +02:00
parent 7d21fc195b
commit b89a121997
3 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,46 @@
import Web3 from 'web3'
import { AbiItem } from 'web3-utils'
import { Config, ConfigHelper } from '../config'
import { amountToUnits, unitsToAmount } from '../utils'
export abstract class SmartContract {
public web3: Web3
public config: Config
public abi: AbiItem | AbiItem[]
abstract getDefaultAbi(): AbiItem | AbiItem[]
/**
* Instantiate the smart contract.
* @param {Web3} web3
* @param {string | number} network Network id or name
* @param {Config} config Configutation of the smart contract
* @param {AbiItem | AbiItem[]} abi ABI of the smart contract
*/
constructor(
web3: Web3,
network?: string | number,
config?: Config,
abi?: AbiItem | AbiItem[]
) {
this.web3 = web3
this.config = config || new ConfigHelper().getConfig(network || 'unknown')
this.abi = abi || (this.getDefaultAbi() as AbiItem[])
}
async amountToUnits(
token: string,
amount: string,
tokenDecimals?: number
): Promise<string> {
return amountToUnits(this.web3, token, amount, tokenDecimals)
}
async unitsToAmount(
token: string,
amount: string,
tokenDecimals?: number
): Promise<string> {
return unitsToAmount(this.web3, token, amount, tokenDecimals)
}
}

View File

@ -0,0 +1,34 @@
import Web3 from 'web3'
import { Contract } from 'web3-eth-contract'
import { AbiItem } from 'web3-utils'
import { Config } from '../config'
import { setContractDefaults } from '../utils'
import { SmartContract } from '.'
export abstract class SmartContractWithAddress extends SmartContract {
public address: string
public contract: Contract
/**
* Instantiate the smart contract.
* @param {string} address Address of the smart contract
* @param {Web3} web3
* @param {string | number} network Network id or name
* @param {Config} config Configutation of the smart contract
* @param {AbiItem | AbiItem[]} abi ABI of the smart contract
*/
constructor(
address: string,
web3: Web3,
network?: string | number,
config?: Config,
abi?: AbiItem | AbiItem[]
) {
super(web3, network, config, abi)
this.address = address
this.contract = setContractDefaults(
new this.web3.eth.Contract(this.getDefaultAbi(), this.address),
this.config
)
}
}

View File

@ -1,3 +1,5 @@
export * from './SmartContract'
export * from './SmartContractWithAddress'
export * from './factories/NFTFactory' export * from './factories/NFTFactory'
export * from './pools/Dispenser' export * from './pools/Dispenser'
export * from './pools/FixedRateExchange' export * from './pools/FixedRateExchange'