mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
added more tests and isDispensable method
This commit is contained in:
parent
5072bdbb5a
commit
80426fd231
@ -2,8 +2,10 @@ import Web3 from 'web3'
|
||||
import { AbiItem } from 'web3-utils'
|
||||
import { Contract } from 'web3-eth-contract'
|
||||
import { TransactionReceipt } from 'web3-eth'
|
||||
import Decimal from 'decimal.js'
|
||||
import defaultDispenserABI from '@oceanprotocol/contracts/pools/dispenser/Dispenser.json'
|
||||
import { LoggerInstance as logger, getFairGasPrice } from '../../utils/'
|
||||
import { Datatoken } from '.../../../src/datatokens/'
|
||||
|
||||
export interface DispenserToken {
|
||||
active: boolean
|
||||
@ -119,7 +121,7 @@ export class Dispenser {
|
||||
maxBalance: string,
|
||||
allowedSwapper: string
|
||||
): Promise<TransactionReceipt> {
|
||||
const estGas = await this.estGasCreateDispenser(
|
||||
const estGas = await this.estGasCreate(
|
||||
dtAddress,
|
||||
address,
|
||||
maxTokens,
|
||||
@ -409,4 +411,33 @@ export class Dispenser {
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if tokens can be dispensed
|
||||
* @param {String} dtAddress
|
||||
* @param {String} address User address that will receive datatokens
|
||||
* @param {String} amount amount of datatokens required.
|
||||
* @return {Promise<Boolean>}
|
||||
*/
|
||||
public async isDispensable(
|
||||
dtAddress: string,
|
||||
dataToken: Datatoken,
|
||||
address: string,
|
||||
amount: string = '1'
|
||||
): Promise<Boolean> {
|
||||
const status = await this.status(dtAddress)
|
||||
if (!status) return false
|
||||
// check active
|
||||
if (status.active === false) return false
|
||||
// check maxBalance
|
||||
const userBalance = new Decimal(await dataToken.balance(dataToken, address))
|
||||
if (userBalance.greaterThanOrEqualTo(status.maxBalance)) return false
|
||||
// check maxAmount
|
||||
if (new Decimal(String(amount)).greaterThan(status.maxTokens)) return false
|
||||
// check dispenser balance
|
||||
const contractBalance = new Decimal(status.balance)
|
||||
if (contractBalance.greaterThanOrEqualTo(amount) || status.isTrueMinter === true)
|
||||
return true
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -126,4 +126,30 @@ describe('Dispenser flow', () => {
|
||||
const status = await DispenserClass.status(dtAddress)
|
||||
assert(status.active === false, 'Dispenser is still active')
|
||||
})
|
||||
|
||||
it('user2 sets user3 as an AllowedSwapper for the dispenser', async () => {
|
||||
const tx = await DispenserClass.setAllowedSwapper(dtAddress, user2, user3)
|
||||
assert(tx, 'Cannot deactivate dispenser')
|
||||
const status = await DispenserClass.status(dtAddress)
|
||||
assert(status.allowedSwapper === user3, 'Dispenser is still active')
|
||||
})
|
||||
|
||||
it('User3 requests datatokens', async () => {
|
||||
const check = await DispenserClass.isDispensable(dtAddress, datatoken, user3, '1')
|
||||
assert(check === true, 'isDispensable should return true')
|
||||
const tx = await DispenserClass.dispense(dtAddress, user3, '1', user3)
|
||||
assert(tx, 'user3 failed to get 1DT')
|
||||
})
|
||||
|
||||
it('tries to withdraw all datatokens', async () => {
|
||||
const tx = await DispenserClass.ownerWithdraw(dtAddress, user3)
|
||||
assert(tx === null, 'Request should fail')
|
||||
})
|
||||
|
||||
it('user2 withdraws all datatokens', async () => {
|
||||
const tx = await DispenserClass.ownerWithdraw(dtAddress, user2)
|
||||
assert(tx, 'user2 failed to withdraw all her tokens')
|
||||
const status = await DispenserClass.status(dtAddress)
|
||||
assert(status.balance === '0', 'Balance > 0')
|
||||
})
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user