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

inherit Dispenser from SmartContract

This commit is contained in:
Miquel A. Cabot 2022-06-06 17:36:21 +02:00
parent c09a48fa72
commit fc588cfdb1

View File

@ -1,48 +1,14 @@
import Web3 from 'web3'
import { AbiItem } from 'web3-utils' import { AbiItem } from 'web3-utils'
import { Contract } from 'web3-eth-contract'
import { TransactionReceipt } from 'web3-eth' import { TransactionReceipt } from 'web3-eth'
import Decimal from 'decimal.js' import Decimal from 'decimal.js'
import defaultDispenserAbi from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json' import DispenserAbi from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json'
import { import { LoggerInstance, getFairGasPrice, estimateGas } from '../../utils/'
LoggerInstance as logger, import { Datatoken, SmartContractWithAddress } from '..'
getFairGasPrice,
setContractDefaults,
estimateGas
} from '../../utils/'
import { Datatoken } from '..'
import { Config, ConfigHelper } from '../../config'
import { DispenserToken } from '../../@types' import { DispenserToken } from '../../@types'
export class Dispenser { export class Dispenser extends SmartContractWithAddress {
public web3: Web3 = null getDefaultAbi(): AbiItem | AbiItem[] {
public dispenserAddress: string return DispenserAbi.abi as AbiItem[]
public config: Config
public dispenserAbi: AbiItem | AbiItem[]
public dispenserContract: Contract
/**
* Instantiate Dispenser
* @param {any} web3
* @param {String} dispenserAddress
* @param {any} dispenserABI
*/
constructor(
web3: Web3,
network?: string | number,
dispenserAddress: string = null,
dispenserAbi: AbiItem | AbiItem[] = null,
config?: Config
) {
this.web3 = web3
this.dispenserAddress = dispenserAddress
this.dispenserAbi = dispenserAbi || (defaultDispenserAbi.abi as AbiItem[])
this.config = config || new ConfigHelper().getConfig(network || 'unknown')
if (web3)
this.dispenserContract = setContractDefaults(
new this.web3.eth.Contract(this.dispenserAbi, this.dispenserAddress),
this.config
)
} }
/** /**
@ -52,15 +18,13 @@ export class Dispenser {
*/ */
public async status(dtAdress: string): Promise<DispenserToken> { public async status(dtAdress: string): Promise<DispenserToken> {
try { try {
const result: DispenserToken = await this.dispenserContract.methods const result: DispenserToken = await this.contract.methods.status(dtAdress).call()
.status(dtAdress)
.call()
result.maxTokens = this.web3.utils.fromWei(result.maxTokens) result.maxTokens = this.web3.utils.fromWei(result.maxTokens)
result.maxBalance = this.web3.utils.fromWei(result.maxBalance) result.maxBalance = this.web3.utils.fromWei(result.maxBalance)
result.balance = this.web3.utils.fromWei(result.balance) result.balance = this.web3.utils.fromWei(result.balance)
return result return result
} catch (e) { } catch (e) {
logger.warn(`No dispenser available for datatoken: ${dtAdress}`) LoggerInstance.warn(`No dispenser available for datatoken: ${dtAdress}`)
} }
return null return null
} }
@ -83,7 +47,7 @@ export class Dispenser {
): Promise<any> { ): Promise<any> {
return estimateGas( return estimateGas(
address, address,
this.dispenserContract.methods.create, this.contract.methods.create,
dtAddress, dtAddress,
this.web3.utils.toWei(maxTokens), this.web3.utils.toWei(maxTokens),
this.web3.utils.toWei(maxBalance), this.web3.utils.toWei(maxBalance),
@ -110,7 +74,7 @@ export class Dispenser {
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const estGas = await estimateGas( const estGas = await estimateGas(
address, address,
this.dispenserContract.methods.create, this.contract.methods.create,
dtAddress, dtAddress,
this.web3.utils.toWei(maxTokens), this.web3.utils.toWei(maxTokens),
this.web3.utils.toWei(maxBalance), this.web3.utils.toWei(maxBalance),
@ -119,7 +83,7 @@ export class Dispenser {
) )
// Call createFixedRate contract method // Call createFixedRate contract method
const trxReceipt = await this.dispenserContract.methods const trxReceipt = await this.contract.methods
.create( .create(
dtAddress, dtAddress,
this.web3.utils.toWei(maxTokens), this.web3.utils.toWei(maxTokens),
@ -151,7 +115,7 @@ export class Dispenser {
): Promise<any> { ): Promise<any> {
return estimateGas( return estimateGas(
address, address,
this.dispenserContract.methods.activate, this.contract.methods.activate,
dtAddress, dtAddress,
this.web3.utils.toWei(maxTokens), this.web3.utils.toWei(maxTokens),
this.web3.utils.toWei(maxBalance) this.web3.utils.toWei(maxBalance)
@ -175,13 +139,13 @@ export class Dispenser {
try { try {
const estGas = await estimateGas( const estGas = await estimateGas(
address, address,
this.dispenserContract.methods.activate, this.contract.methods.activate,
dtAddress, dtAddress,
this.web3.utils.toWei(maxTokens), this.web3.utils.toWei(maxTokens),
this.web3.utils.toWei(maxBalance) this.web3.utils.toWei(maxBalance)
) )
const trxReceipt = await this.dispenserContract.methods const trxReceipt = await this.contract.methods
.activate( .activate(
dtAddress, dtAddress,
this.web3.utils.toWei(maxTokens), this.web3.utils.toWei(maxTokens),
@ -194,7 +158,7 @@ export class Dispenser {
}) })
return trxReceipt return trxReceipt
} catch (e) { } catch (e) {
logger.error(`ERROR: Failed to activate dispenser: ${e.message}`) LoggerInstance.error(`ERROR: Failed to activate dispenser: ${e.message}`)
} }
return null return null
} }
@ -206,7 +170,7 @@ export class Dispenser {
* @return {Promise<any>} * @return {Promise<any>}
*/ */
public async estGasDeactivate(dtAddress: string, address: string): Promise<any> { public async estGasDeactivate(dtAddress: string, address: string): Promise<any> {
return estimateGas(address, this.dispenserContract.methods.deactivate, dtAddress) return estimateGas(address, this.contract.methods.deactivate, dtAddress)
} }
/** /**
@ -222,18 +186,18 @@ export class Dispenser {
try { try {
const estGas = await estimateGas( const estGas = await estimateGas(
address, address,
this.dispenserContract.methods.deactivate, this.contract.methods.deactivate,
dtAddress dtAddress
) )
const trxReceipt = await this.dispenserContract.methods.deactivate(dtAddress).send({ const trxReceipt = await this.contract.methods.deactivate(dtAddress).send({
from: address, from: address,
gas: estGas + 1, gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3, this.config) gasPrice: await getFairGasPrice(this.web3, this.config)
}) })
return trxReceipt return trxReceipt
} catch (e) { } catch (e) {
logger.error(`ERROR: Failed to activate dispenser: ${e.message}`) LoggerInstance.error(`ERROR: Failed to activate dispenser: ${e.message}`)
} }
return null return null
} }
@ -252,7 +216,7 @@ export class Dispenser {
): Promise<any> { ): Promise<any> {
return estimateGas( return estimateGas(
address, address,
this.dispenserContract.methods.setAllowedSwapper, this.contract.methods.setAllowedSwapper,
dtAddress, dtAddress,
newAllowedSwapper newAllowedSwapper
) )
@ -273,12 +237,12 @@ export class Dispenser {
try { try {
const estGas = await estimateGas( const estGas = await estimateGas(
address, address,
this.dispenserContract.methods.setAllowedSwapper, this.contract.methods.setAllowedSwapper,
dtAddress, dtAddress,
newAllowedSwapper newAllowedSwapper
) )
const trxReceipt = await this.dispenserContract.methods const trxReceipt = await this.contract.methods
.setAllowedSwapper(dtAddress, newAllowedSwapper) .setAllowedSwapper(dtAddress, newAllowedSwapper)
.send({ .send({
from: address, from: address,
@ -287,7 +251,7 @@ export class Dispenser {
}) })
return trxReceipt return trxReceipt
} catch (e) { } catch (e) {
logger.error(`ERROR: Failed to activate dispenser: ${e.message}`) LoggerInstance.error(`ERROR: Failed to activate dispenser: ${e.message}`)
} }
return null return null
} }
@ -307,7 +271,7 @@ export class Dispenser {
): Promise<any> { ): Promise<any> {
return estimateGas( return estimateGas(
address, address,
this.dispenserContract.methods.dispense, this.contract.methods.dispense,
dtAddress, dtAddress,
this.web3.utils.toWei(amount), this.web3.utils.toWei(amount),
destination destination
@ -332,14 +296,14 @@ export class Dispenser {
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const estGas = await estimateGas( const estGas = await estimateGas(
address, address,
this.dispenserContract.methods.dispense, this.contract.methods.dispense,
dtAddress, dtAddress,
this.web3.utils.toWei(amount), this.web3.utils.toWei(amount),
destination destination
) )
try { try {
const trxReceipt = await this.dispenserContract.methods const trxReceipt = await this.contract.methods
.dispense(dtAddress, this.web3.utils.toWei(amount), destination) .dispense(dtAddress, this.web3.utils.toWei(amount), destination)
.send({ .send({
from: address, from: address,
@ -348,7 +312,7 @@ export class Dispenser {
}) })
return trxReceipt return trxReceipt
} catch (e) { } catch (e) {
logger.error(`ERROR: Failed to dispense tokens: ${e.message}`) LoggerInstance.error(`ERROR: Failed to dispense tokens: ${e.message}`)
} }
return null return null
} }
@ -361,7 +325,7 @@ export class Dispenser {
* @return {Promise<any>} * @return {Promise<any>}
*/ */
public async estGasOwnerWithdraw(dtAddress: string, address: string): Promise<any> { public async estGasOwnerWithdraw(dtAddress: string, address: string): Promise<any> {
return estimateGas(address, this.dispenserContract.methods.ownerWithdraw, dtAddress) return estimateGas(address, this.contract.methods.ownerWithdraw, dtAddress)
} }
/** /**
@ -376,21 +340,19 @@ export class Dispenser {
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const estGas = await estimateGas( const estGas = await estimateGas(
address, address,
this.dispenserContract.methods.ownerWithdraw, this.contract.methods.ownerWithdraw,
dtAddress dtAddress
) )
try { try {
const trxReceipt = await this.dispenserContract.methods const trxReceipt = await this.contract.methods.ownerWithdraw(dtAddress).send({
.ownerWithdraw(dtAddress)
.send({
from: address, from: address,
gas: estGas + 1, gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3, this.config) gasPrice: await getFairGasPrice(this.web3, this.config)
}) })
return trxReceipt return trxReceipt
} catch (e) { } catch (e) {
logger.error(`ERROR: Failed to withdraw tokens: ${e.message}`) LoggerInstance.error(`ERROR: Failed to withdraw tokens: ${e.message}`)
} }
return null return null
} }