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:
parent
7d21fc195b
commit
b89a121997
46
src/contracts/SmartContract.ts
Normal file
46
src/contracts/SmartContract.ts
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
34
src/contracts/SmartContractWithAddress.ts
Normal file
34
src/contracts/SmartContractWithAddress.ts
Normal 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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -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'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user