mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
use transfer() approve() and balance() functions
This commit is contained in:
parent
580dce734f
commit
e42e031c1e
@ -3,14 +3,16 @@ import { AbiItem } from 'web3-utils/types'
|
|||||||
import { Contract } from 'web3-eth-contract'
|
import { Contract } from 'web3-eth-contract'
|
||||||
import BN from 'bn.js'
|
import BN from 'bn.js'
|
||||||
import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json'
|
import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json'
|
||||||
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 {
|
||||||
NftFactory,
|
NftFactory,
|
||||||
NftCreateData,
|
NftCreateData,
|
||||||
FixedRateExchange,
|
FixedRateExchange,
|
||||||
ZERO_ADDRESS
|
ZERO_ADDRESS,
|
||||||
|
approve,
|
||||||
|
transfer,
|
||||||
|
balance
|
||||||
} from '../../../../src'
|
} from '../../../../src'
|
||||||
import { FreCreationParams, Erc20CreateParams } from '../../../../src/@types'
|
import { FreCreationParams, Erc20CreateParams } from '../../../../src/@types'
|
||||||
|
|
||||||
@ -24,8 +26,6 @@ describe('Fixed Rate unit test', () => {
|
|||||||
let fixedRate: FixedRateExchange
|
let fixedRate: FixedRateExchange
|
||||||
let dtAddress: string
|
let dtAddress: string
|
||||||
let dtContract: Contract
|
let dtContract: Contract
|
||||||
let daiContract: Contract
|
|
||||||
let usdcContract: Contract
|
|
||||||
|
|
||||||
const nftData: NftCreateData = {
|
const nftData: NftCreateData = {
|
||||||
name: '72120Bundle',
|
name: '72120Bundle',
|
||||||
@ -63,16 +63,6 @@ describe('Fixed Rate unit test', () => {
|
|||||||
|
|
||||||
it('should deploy contracts', async () => {
|
it('should deploy contracts', async () => {
|
||||||
contracts = await deployContracts(web3, factoryOwner)
|
contracts = await deployContracts(web3, factoryOwner)
|
||||||
|
|
||||||
// initialize fixed rate
|
|
||||||
//
|
|
||||||
|
|
||||||
daiContract = new web3.eth.Contract(MockERC20.abi as AbiItem[], contracts.daiAddress)
|
|
||||||
|
|
||||||
usdcContract = new web3.eth.Contract(
|
|
||||||
MockERC20.abi as AbiItem[],
|
|
||||||
contracts.usdcAddress
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Test a Fixed Rate Exchange with DAI (18 Decimals)', () => {
|
describe('Test a Fixed Rate Exchange with DAI (18 Decimals)', () => {
|
||||||
@ -107,7 +97,7 @@ describe('Fixed Rate unit test', () => {
|
|||||||
|
|
||||||
dtContract = new web3.eth.Contract(ERC20Template.abi as AbiItem[], dtAddress)
|
dtContract = new web3.eth.Contract(ERC20Template.abi as AbiItem[], dtAddress)
|
||||||
// user1 has no dt1
|
// user1 has no dt1
|
||||||
expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0')
|
expect(await balance(web3, dtAddress, user1)).to.equal('0')
|
||||||
|
|
||||||
fixedRate = new FixedRateExchange(
|
fixedRate = new FixedRateExchange(
|
||||||
web3,
|
web3,
|
||||||
@ -212,20 +202,14 @@ describe('Fixed Rate unit test', () => {
|
|||||||
await dtContract.methods
|
await dtContract.methods
|
||||||
.mint(exchangeOwner, web3.utils.toWei('1000'))
|
.mint(exchangeOwner, web3.utils.toWei('1000'))
|
||||||
.send({ from: exchangeOwner })
|
.send({ from: exchangeOwner })
|
||||||
await dtContract.methods
|
await approve(web3, exchangeOwner, dtAddress, contracts.fixedRateAddress, '1000')
|
||||||
.approve(contracts.fixedRateAddress, web3.utils.toWei('1000'))
|
|
||||||
.send({ from: exchangeOwner })
|
|
||||||
// user1 gets 100 DAI so he can buy DTs
|
// user1 gets 100 DAI so he can buy DTs
|
||||||
await daiContract.methods
|
await transfer(web3, exchangeOwner, contracts.daiAddress, user1, '100')
|
||||||
.transfer(user1, web3.utils.toWei('100'))
|
await approve(web3, user1, contracts.daiAddress, contracts.fixedRateAddress, '100')
|
||||||
.send({ from: exchangeOwner })
|
|
||||||
await daiContract.methods
|
|
||||||
.approve(contracts.fixedRateAddress, web3.utils.toWei('100'))
|
|
||||||
.send({ from: user1 })
|
|
||||||
|
|
||||||
// user1 has no dts but has 100 DAI
|
// user1 has no dts but has 100 DAI
|
||||||
expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0')
|
expect(await balance(web3, dtAddress, user1)).to.equal('0')
|
||||||
const daiBalanceBefore = new BN(await daiContract.methods.balanceOf(user1).call())
|
const daiBalanceBefore = new BN(await balance(web3, contracts.daiAddress, user1))
|
||||||
|
|
||||||
// user1 buys 10 DT
|
// user1 buys 10 DT
|
||||||
const tx = await fixedRate.buyDT(user1, exchangeId, '10', '11')
|
const tx = await fixedRate.buyDT(user1, exchangeId, '10', '11')
|
||||||
@ -236,12 +220,10 @@ describe('Fixed Rate unit test', () => {
|
|||||||
expect(args.by).to.equal(user1)
|
expect(args.by).to.equal(user1)
|
||||||
expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10'))
|
expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10'))
|
||||||
expect(args.tokenOutAddress).to.equal(dtAddress)
|
expect(args.tokenOutAddress).to.equal(dtAddress)
|
||||||
expect(await dtContract.methods.balanceOf(user1).call()).to.equal(
|
expect(await balance(web3, dtAddress, user1)).to.equal(args.datatokenSwappedAmount)
|
||||||
args.datatokenSwappedAmount
|
|
||||||
)
|
|
||||||
expect(
|
expect(
|
||||||
daiBalanceBefore.sub(new BN(args.baseTokenSwappedAmount)).toString()
|
daiBalanceBefore.sub(new BN(args.baseTokenSwappedAmount)).toString()
|
||||||
).to.equal(await daiContract.methods.balanceOf(user1).call())
|
).to.equal(await balance(web3, contracts.daiAddress, user1))
|
||||||
// baseToken stays in the contract
|
// baseToken stays in the contract
|
||||||
expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('10')
|
expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('10')
|
||||||
// no dt in the contract
|
// no dt in the contract
|
||||||
@ -249,10 +231,8 @@ describe('Fixed Rate unit test', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('#sellDT - user1 should sell some dt', async () => {
|
it('#sellDT - user1 should sell some dt', async () => {
|
||||||
await dtContract.methods
|
await approve(web3, user1, dtAddress, contracts.fixedRateAddress, '100')
|
||||||
.approve(contracts.fixedRateAddress, web3.utils.toWei('10'))
|
const daiBalanceBefore = new BN(await balance(web3, contracts.daiAddress, user1))
|
||||||
.send({ from: user1 })
|
|
||||||
const daiBalanceBefore = new BN(await daiContract.methods.balanceOf(user1).call())
|
|
||||||
const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9')
|
const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9')
|
||||||
// console.log(tx.events.Swapped.returnValues)
|
// console.log(tx.events.Swapped.returnValues)
|
||||||
assert(tx.events.Swapped != null)
|
assert(tx.events.Swapped != null)
|
||||||
@ -261,10 +241,10 @@ describe('Fixed Rate unit test', () => {
|
|||||||
expect(args.by).to.equal(user1)
|
expect(args.by).to.equal(user1)
|
||||||
expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10'))
|
expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10'))
|
||||||
expect(args.tokenOutAddress).to.equal(contracts.daiAddress)
|
expect(args.tokenOutAddress).to.equal(contracts.daiAddress)
|
||||||
expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0')
|
expect(await balance(web3, dtAddress, user1)).to.equal('0')
|
||||||
expect(
|
expect(
|
||||||
daiBalanceBefore.add(new BN(args.baseTokenSwappedAmount)).toString()
|
daiBalanceBefore.add(new BN(args.baseTokenSwappedAmount)).toString()
|
||||||
).to.equal(await daiContract.methods.balanceOf(user1).call())
|
).to.equal(await balance(web3, contracts.daiAddress, user1))
|
||||||
// DTs stay in the contract
|
// DTs stay in the contract
|
||||||
expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('10')
|
expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('10')
|
||||||
// no BTs in the contract (except for the fees, but not accounted here)
|
// no BTs in the contract (except for the fees, but not accounted here)
|
||||||
@ -351,7 +331,7 @@ describe('Fixed Rate unit test', () => {
|
|||||||
expect(result.marketFeeCollector).to.equal(user2)
|
expect(result.marketFeeCollector).to.equal(user2)
|
||||||
|
|
||||||
const daiBalanceBeforeCollect = new BN(
|
const daiBalanceBeforeCollect = new BN(
|
||||||
await daiContract.methods.balanceOf(user2).call()
|
await balance(web3, contracts.daiAddress, user2)
|
||||||
)
|
)
|
||||||
|
|
||||||
// user4 calls collectMarketFee
|
// user4 calls collectMarketFee
|
||||||
@ -361,7 +341,7 @@ describe('Fixed Rate unit test', () => {
|
|||||||
// ocean fee still available
|
// ocean fee still available
|
||||||
expect(result.oceanFeeAvailable).to.equal('0.042')
|
expect(result.oceanFeeAvailable).to.equal('0.042')
|
||||||
// user2 is the marketFeeCollector
|
// user2 is the marketFeeCollector
|
||||||
expect(await daiContract.methods.balanceOf(user2).call()).to.equal(
|
expect(await balance(web3, contracts.daiAddress, user2)).to.equal(
|
||||||
daiBalanceBeforeCollect.add(new BN(web3.utils.toWei('0.021'))).toString()
|
daiBalanceBeforeCollect.add(new BN(web3.utils.toWei('0.021'))).toString()
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@ -414,7 +394,7 @@ describe('Fixed Rate unit test', () => {
|
|||||||
|
|
||||||
dtContract = new web3.eth.Contract(ERC20Template.abi as AbiItem[], dtAddress)
|
dtContract = new web3.eth.Contract(ERC20Template.abi as AbiItem[], dtAddress)
|
||||||
// user1 has no dt1
|
// user1 has no dt1
|
||||||
expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0')
|
expect(await balance(web3, dtAddress, user1)).to.equal('0')
|
||||||
|
|
||||||
fixedRate = new FixedRateExchange(
|
fixedRate = new FixedRateExchange(
|
||||||
web3,
|
web3,
|
||||||
@ -515,18 +495,14 @@ describe('Fixed Rate unit test', () => {
|
|||||||
await dtContract.methods
|
await dtContract.methods
|
||||||
.mint(exchangeOwner, web3.utils.toWei('1000'))
|
.mint(exchangeOwner, web3.utils.toWei('1000'))
|
||||||
.send({ from: exchangeOwner })
|
.send({ from: exchangeOwner })
|
||||||
await dtContract.methods
|
await approve(web3, exchangeOwner, dtAddress, contracts.fixedRateAddress, '1000')
|
||||||
.approve(contracts.fixedRateAddress, web3.utils.toWei('1000'))
|
|
||||||
.send({ from: exchangeOwner })
|
|
||||||
// user1 gets 100 USDC so he can buy DTs
|
// user1 gets 100 USDC so he can buy DTs
|
||||||
await usdcContract.methods.transfer(user1, 100 * 1e6).send({ from: exchangeOwner })
|
await transfer(web3, exchangeOwner, contracts.usdcAddress, user1, '100')
|
||||||
await usdcContract.methods
|
await approve(web3, user1, contracts.usdcAddress, contracts.fixedRateAddress, '100')
|
||||||
.approve(contracts.fixedRateAddress, 100 * 1e6)
|
|
||||||
.send({ from: user1 })
|
|
||||||
|
|
||||||
// user1 has no dts but has 100 USDC
|
// user1 has no dts but has 100 USDC
|
||||||
expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0')
|
expect(await balance(web3, dtAddress, user1)).to.equal('0')
|
||||||
const usdcBalanceBefore = new BN(await usdcContract.methods.balanceOf(user1).call())
|
const usdcBalanceBefore = new BN(await balance(web3, contracts.usdcAddress, user1))
|
||||||
|
|
||||||
// user1 buys 10 DT
|
// user1 buys 10 DT
|
||||||
const tx = await fixedRate.buyDT(user1, exchangeId, '10', '11')
|
const tx = await fixedRate.buyDT(user1, exchangeId, '10', '11')
|
||||||
@ -537,12 +513,10 @@ describe('Fixed Rate unit test', () => {
|
|||||||
expect(args.by).to.equal(user1)
|
expect(args.by).to.equal(user1)
|
||||||
expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10'))
|
expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10'))
|
||||||
expect(args.tokenOutAddress).to.equal(dtAddress)
|
expect(args.tokenOutAddress).to.equal(dtAddress)
|
||||||
expect(await dtContract.methods.balanceOf(user1).call()).to.equal(
|
expect(await balance(web3, dtAddress, user1)).to.equal(args.datatokenSwappedAmount)
|
||||||
args.datatokenSwappedAmount
|
|
||||||
)
|
|
||||||
expect(
|
expect(
|
||||||
usdcBalanceBefore.sub(new BN(args.baseTokenSwappedAmount)).toString()
|
usdcBalanceBefore.sub(new BN(args.baseTokenSwappedAmount)).toString()
|
||||||
).to.equal(await usdcContract.methods.balanceOf(user1).call())
|
).to.equal(await balance(web3, contracts.usdcAddress, user1))
|
||||||
// baseToken stays in the contract
|
// baseToken stays in the contract
|
||||||
expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('10')
|
expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('10')
|
||||||
// no dt in the contract
|
// no dt in the contract
|
||||||
@ -550,10 +524,8 @@ describe('Fixed Rate unit test', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('#sellDT - user1 should sell some dt', async () => {
|
it('#sellDT - user1 should sell some dt', async () => {
|
||||||
await dtContract.methods
|
await approve(web3, user1, dtAddress, contracts.fixedRateAddress, '10')
|
||||||
.approve(contracts.fixedRateAddress, web3.utils.toWei('10'))
|
const usdcBalanceBefore = new BN(await balance(web3, contracts.usdcAddress, user1))
|
||||||
.send({ from: user1 })
|
|
||||||
const usdcBalanceBefore = new BN(await usdcContract.methods.balanceOf(user1).call())
|
|
||||||
const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9')
|
const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9')
|
||||||
// console.log(tx.events.Swapped.returnValues)
|
// console.log(tx.events.Swapped.returnValues)
|
||||||
assert(tx.events.Swapped != null)
|
assert(tx.events.Swapped != null)
|
||||||
@ -562,10 +534,10 @@ describe('Fixed Rate unit test', () => {
|
|||||||
expect(args.by).to.equal(user1)
|
expect(args.by).to.equal(user1)
|
||||||
expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10'))
|
expect(args.datatokenSwappedAmount).to.equal(web3.utils.toWei('10'))
|
||||||
expect(args.tokenOutAddress).to.equal(contracts.usdcAddress)
|
expect(args.tokenOutAddress).to.equal(contracts.usdcAddress)
|
||||||
expect(await dtContract.methods.balanceOf(user1).call()).to.equal('0')
|
expect(await balance(web3, dtAddress, user1)).to.equal('0')
|
||||||
expect(
|
expect(
|
||||||
usdcBalanceBefore.add(new BN(args.baseTokenSwappedAmount)).toString()
|
usdcBalanceBefore.add(new BN(args.baseTokenSwappedAmount)).toString()
|
||||||
).to.equal(await usdcContract.methods.balanceOf(user1).call())
|
).to.equal(await balance(web3, contracts.usdcAddress, user1))
|
||||||
// DTs stay in the contract
|
// DTs stay in the contract
|
||||||
expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('10')
|
expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('10')
|
||||||
// no BTs in the contract (except for the fees, but not accounted here)
|
// no BTs in the contract (except for the fees, but not accounted here)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user