1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00
ocean.js/src/contracts/SmartContract.ts
2022-06-09 09:32:31 +02:00

51 lines
1.3 KiB
TypeScript

import Web3 from 'web3'
import { AbiItem } from 'web3-utils'
import { Config, ConfigHelper } from '../config'
import { amountToUnits, getFairGasPrice, 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)
}
async getFairGasPrice(): Promise<string> {
return getFairGasPrice(this.web3, this.config)
}
}