From 80426fd2310dbdc06982faa9041f4a76d066bf9d Mon Sep 17 00:00:00 2001 From: Bogdan Fazakas Date: Fri, 29 Oct 2021 16:16:55 +0300 Subject: [PATCH] added more tests and isDispensable method --- src/pools/dispenser/Dispenser.ts | 33 ++++++++++++++++++++++++++++++- test/unit/pools/Dispenser.test.ts | 26 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/pools/dispenser/Dispenser.ts b/src/pools/dispenser/Dispenser.ts index 094519f6..e09a7786 100644 --- a/src/pools/dispenser/Dispenser.ts +++ b/src/pools/dispenser/Dispenser.ts @@ -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 { - 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} + */ + public async isDispensable( + dtAddress: string, + dataToken: Datatoken, + address: string, + amount: string = '1' + ): Promise { + 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 + } } diff --git a/test/unit/pools/Dispenser.test.ts b/test/unit/pools/Dispenser.test.ts index 0d3ac239..ae7f4f00 100644 --- a/test/unit/pools/Dispenser.test.ts +++ b/test/unit/pools/Dispenser.test.ts @@ -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') + }) })