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

use balance() and transfer() functions

This commit is contained in:
Miquel A. Cabot 2022-05-26 13:28:53 +02:00
parent 03f3d3b7d5
commit b44f1f802a

View File

@ -1,7 +1,5 @@
import { assert, expect } from 'chai' import { assert, expect } from 'chai'
import { AbiItem } from 'web3-utils/types'
import { Contract } from 'web3-eth-contract' import { Contract } from 'web3-eth-contract'
import MockERC20 from '@oceanprotocol/contracts/artifacts/contracts/utils/mock/MockERC20Decimals.sol/MockERC20Decimals.json'
import { deployContracts, Addresses } from '../../../TestContractHandler' import { deployContracts, Addresses } from '../../../TestContractHandler'
import { web3 } from '../../../config' import { web3 } from '../../../config'
import { import {
@ -13,7 +11,8 @@ import {
Pool, Pool,
unitsToAmount, unitsToAmount,
ZERO_ADDRESS, ZERO_ADDRESS,
balance balance,
transfer
} from '../../../../src' } from '../../../../src'
import { import {
PoolCreationParams, PoolCreationParams,
@ -32,8 +31,6 @@ describe('Pool unit test', () => {
let pool: Pool let pool: Pool
let poolAddress: string let poolAddress: string
let erc20Token: string let erc20Token: string
let daiContract: Contract
let usdcContract: Contract
let ercParams: Erc20CreateParams let ercParams: Erc20CreateParams
const nftData: NftCreateData = { const nftData: NftCreateData = {
@ -73,12 +70,6 @@ describe('Pool unit test', () => {
pool = new Pool(web3, 8996) pool = new Pool(web3, 8996)
assert(pool != null) assert(pool != null)
daiContract = new web3.eth.Contract(MockERC20.abi as AbiItem[], contracts.daiAddress)
usdcContract = new web3.eth.Contract(
MockERC20.abi as AbiItem[],
contracts.usdcAddress
)
await approve( await approve(
web3, web3,
factoryOwner, factoryOwner,
@ -155,7 +146,7 @@ describe('Pool unit test', () => {
}) })
it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => { it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => {
expect(await daiContract.methods.balanceOf(user1).call()).to.equal( expect(await balance(web3, contracts.daiAddress, user1)).to.equal(
web3.utils.toWei(await pool.sharesBalance(user1, contracts.daiAddress)) web3.utils.toWei(await pool.sharesBalance(user1, contracts.daiAddress))
) )
}) })
@ -234,12 +225,8 @@ describe('Pool unit test', () => {
}) })
it('#swapExactAmountIn - should swap', async () => { it('#swapExactAmountIn - should swap', async () => {
await daiContract.methods await transfer(web3, factoryOwner, contracts.daiAddress, user1, '1000')
.transfer(user1, web3.utils.toWei('1000')) expect(await balance(web3, contracts.daiAddress, user1)).to.equal('1000')
.send({ from: factoryOwner })
expect(await daiContract.methods.balanceOf(user1).call()).to.equal(
web3.utils.toWei('1000')
)
expect(await balance(web3, erc20Token, user1)).to.equal('0') expect(await balance(web3, erc20Token, user1)).to.equal('0')
await approve(web3, user1, contracts.daiAddress, poolAddress, '10') await approve(web3, user1, contracts.daiAddress, poolAddress, '10')
@ -266,9 +253,7 @@ describe('Pool unit test', () => {
it('#swapExactAmountOut - should swap', async () => { it('#swapExactAmountOut - should swap', async () => {
await approve(web3, user1, contracts.daiAddress, poolAddress, '100') await approve(web3, user1, contracts.daiAddress, poolAddress, '100')
expect(await daiContract.methods.balanceOf(user1).call()).to.equal( expect(await balance(web3, contracts.daiAddress, user1)).to.equal('990')
web3.utils.toWei('990')
)
const tokenInOutMarket: TokenInOutMarket = { const tokenInOutMarket: TokenInOutMarket = {
tokenIn: contracts.daiAddress, tokenIn: contracts.daiAddress,
tokenOut: erc20Token, tokenOut: erc20Token,
@ -438,7 +423,7 @@ describe('Pool unit test', () => {
// factoryOwner is the marketFeeCollector // factoryOwner is the marketFeeCollector
assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner) assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner)
// user2 has no DAI (we are going to send DAI fee to him) // user2 has no DAI (we are going to send DAI fee to him)
assert((await daiContract.methods.balanceOf(user2).call()) === '0') assert((await balance(web3, contracts.daiAddress, user2)) === '0')
// only marketFeeCollector can call this, set user2 as receiver // only marketFeeCollector can call this, set user2 as receiver
await pool.collectMarketFee(factoryOwner, poolAddress) await pool.collectMarketFee(factoryOwner, poolAddress)
// DAI fees have been collected // DAI fees have been collected
@ -471,9 +456,11 @@ describe('Pool unit test', () => {
assert((await pool.getCommunityFees(poolAddress, contracts.daiAddress)) > '0') assert((await pool.getCommunityFees(poolAddress, contracts.daiAddress)) > '0')
// opf collector has no DAI // opf collector has no DAI
assert( assert(
(await daiContract.methods (await balance(
.balanceOf(contracts.opfCommunityFeeCollectorAddress) web3,
.call()) === '0' contracts.daiAddress,
contracts.opfCommunityFeeCollectorAddress
)) === '0'
) )
// anyone can call callectOPF // anyone can call callectOPF
await pool.collectOPC(factoryOwner, poolAddress) await pool.collectOPC(factoryOwner, poolAddress)
@ -481,9 +468,11 @@ describe('Pool unit test', () => {
assert((await pool.getCommunityFees(poolAddress, contracts.daiAddress)) === '0') assert((await pool.getCommunityFees(poolAddress, contracts.daiAddress)) === '0')
// OPF collector got DAI // OPF collector got DAI
assert( assert(
(await daiContract.methods (await balance(
.balanceOf(contracts.opfCommunityFeeCollectorAddress) web3,
.call()) > '0' contracts.daiAddress,
contracts.opfCommunityFeeCollectorAddress
)) > '0'
) )
// Spot price hasn't changed after fee collection // Spot price hasn't changed after fee collection
assert( assert(
@ -522,7 +511,7 @@ describe('Pool unit test', () => {
marketFeeCollector: factoryOwner, marketFeeCollector: factoryOwner,
poolTemplateAddress: contracts.poolTemplateAddress, poolTemplateAddress: contracts.poolTemplateAddress,
rate: '1', rate: '1',
baseTokenDecimals: await usdcContract.methods.decimals().call(), baseTokenDecimals: 6,
vestingAmount: '10000', vestingAmount: '10000',
vestedBlocks: 2500000, vestedBlocks: 2500000,
initialBaseTokenLiquidity: await unitsToAmount( initialBaseTokenLiquidity: await unitsToAmount(
@ -622,7 +611,7 @@ describe('Pool unit test', () => {
}) })
it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => { it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => {
expect(await usdcContract.methods.balanceOf(user1).call()).to.equal( expect(await balance(web3, contracts.usdcAddress, user1)).to.equal(
await pool.sharesBalance(user1, contracts.usdcAddress) await pool.sharesBalance(user1, contracts.usdcAddress)
) )
}) })
@ -702,10 +691,8 @@ describe('Pool unit test', () => {
it('#swapExactAmountIn - should swap', async () => { it('#swapExactAmountIn - should swap', async () => {
const transferAmount = await amountToUnits(web3, contracts.usdcAddress, '1000') // 1000 USDC const transferAmount = await amountToUnits(web3, contracts.usdcAddress, '1000') // 1000 USDC
await usdcContract.methods await transfer(web3, factoryOwner, contracts.usdcAddress, user1, transferAmount)
.transfer(user1, transferAmount) expect(await balance(web3, contracts.usdcAddress, user1)).to.equal(
.send({ from: factoryOwner })
expect(await usdcContract.methods.balanceOf(user1).call()).to.equal(
transferAmount.toString() transferAmount.toString()
) )
@ -733,7 +720,7 @@ describe('Pool unit test', () => {
}) })
it('#swapExactAmountOut - should swap', async () => { it('#swapExactAmountOut - should swap', async () => {
expect(await usdcContract.methods.balanceOf(user1).call()).to.equal( expect(await balance(web3, contracts.usdcAddress, user1)).to.equal(
(await amountToUnits(web3, contracts.usdcAddress, '990')).toString() (await amountToUnits(web3, contracts.usdcAddress, '990')).toString()
) )
await approve(web3, user1, contracts.usdcAddress, poolAddress, '100') await approve(web3, user1, contracts.usdcAddress, poolAddress, '100')
@ -889,7 +876,7 @@ describe('Pool unit test', () => {
// factoryOwner is the marketFeeCollector // factoryOwner is the marketFeeCollector
assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner) assert((await pool.getMarketFeeCollector(poolAddress)) === factoryOwner)
// user2 has no USDC (we are going to send USDC fee to him) // user2 has no USDC (we are going to send USDC fee to him)
assert((await usdcContract.methods.balanceOf(user2).call()) === '0') assert((await balance(web3, contracts.usdcAddress, user2)) === '0')
// only marketFeeCollector can call this, set user2 as receiver // only marketFeeCollector can call this, set user2 as receiver
await pool.collectMarketFee(factoryOwner, poolAddress) await pool.collectMarketFee(factoryOwner, poolAddress)
// USDC fees have been collected // USDC fees have been collected
@ -932,9 +919,11 @@ describe('Pool unit test', () => {
assert((await pool.getCommunityFees(poolAddress, contracts.usdcAddress)) > '0') assert((await pool.getCommunityFees(poolAddress, contracts.usdcAddress)) > '0')
// opf collector has no USDC // opf collector has no USDC
assert( assert(
(await usdcContract.methods (await balance(
.balanceOf(contracts.opfCommunityFeeCollectorAddress) web3,
.call()) === '0' contracts.usdcAddress,
contracts.opfCommunityFeeCollectorAddress
)) === '0'
) )
// anyone can call callectOPF // anyone can call callectOPF
await pool.collectOPC(factoryOwner, poolAddress) await pool.collectOPC(factoryOwner, poolAddress)
@ -942,9 +931,11 @@ describe('Pool unit test', () => {
assert((await pool.getCommunityFees(poolAddress, contracts.usdcAddress)) === '0') assert((await pool.getCommunityFees(poolAddress, contracts.usdcAddress)) === '0')
// OPF collector got USDC // OPF collector got USDC
assert( assert(
(await usdcContract.methods (await balance(
.balanceOf(contracts.opfCommunityFeeCollectorAddress) web3,
.call()) > '0' contracts.usdcAddress,
contracts.opfCommunityFeeCollectorAddress
)) > '0'
) )
// Spot price hasn't changed after fee collection // Spot price hasn't changed after fee collection
assert( assert(