1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00
ocean.js/test/unit/veOcean.test.ts
Alex Coseru 9bf71ba4f0
Features/ethers (#1696)
* add ethers

* global updates

* ve updates

* ve tests

* contract updates

* first test passing

* downgrade to ethers 5.7.2

* add log

* add tx wait

* full NftFactory.test

* add wait

* add Router tests

* fix dispenser

* Nft test

* add dispenser tests

* add fre tests part 1

* WIP datatoken unit tests

* add DFRewards test

* increase gas estimate

* increase gas estimate

* Work datatoken unit tests

* datatoken test more tests

* finished datatoken tests

* fix nft get data

* fix nft transfer tests

* Provider int tests

* Updating CodeExamples.md

* update provider & fix publish flow int test

* wip publish edit consume integration test

* more work on  publish edit consume integration test

* fix edit  publish edit consume integration test

* add 3 int tests

* Updating ComputeExamples.md

* fix signature and download

* fix compute flow integration test

* udapte handleComputeOrder helper

* update datatoken instance

* update datatoken global variable

* mint ocean tokens to consumer as well and added logs

* update compute exammples

* Updating ComputeExamples.md

* wip code examples readme

* update code examples readme

* Updating CodeExamples.md

* run all tests

* update dep in readme

* update readme

* code examples update metadata flags

* update dt instance in code examples md

* set metadata updates

* Updating CodeExamples.md

* update code examples readme structure

* Updating CodeExamples.md

* update readmes table links

* Updating CodeExamples.md

* Updating ComputeExamples.md

* clean-up

* Updating CodeExamples.md

* added missing unit tests for usdc fixed rate exchange tests

* more cleanup and jsdoc updates

* more  jsdoc updates

* donw with jsdoc updates

* handle provider errors

* add missing error handling file

* adds most of the provider errors

* update get compute env return type

* Release 3.0.0-next.0

* adding Typedoc to ethers branch

* update provider signature message

* fix lint

* Release 3.0.0-next.1

* fix lint

* adding Typedoc to ethers branch

* Updating CI to build and commit the documentation

* Updating documentation

* Updating script permissions

* fix todos add missing logic to send tx

* npm package cleanups

* Release 3.0.0-next.2

* update log messages for errors

* Release 3.0.0-next.3

* fix gasFee issue on sendTx

* Release 3.0.0-next.4

* add consume params typings (#1731)

* fix gas fee estimate

* remove comments

* add some delays before resolving datasets

* adds delay to Publish flow tests

* Release 3.0.0-next.5

---------

Co-authored-by: Bogdan Fazakas <bogdan.fazakas@gmail.com>
Co-authored-by: GitHub Actions Bot <>
Co-authored-by: Jamie Hewitt <jamie.hewitt15@gmail.com>
Co-authored-by: Jamie Hewitt <jamie@oceanprotocol.com>
2023-05-29 11:20:38 +03:00

263 lines
8.6 KiB
TypeScript

import { assert } from 'chai'
import { getTestConfig, provider, getAddresses } from '../config'
import {
Config,
approve,
VeOcean,
VeFeeDistributor,
sendTx,
NftFactory,
VeAllocate,
VeFeeEstimate,
getEventFromTx,
amountToUnits
} from '../../src'
import { ethers, Signer } from 'ethers'
describe('veOcean tests', () => {
let config: Config
let addresses: any
let nftFactory
let veOcean: VeOcean
let veFeeDistributor: VeFeeDistributor
let veFeeEstimate: VeFeeEstimate
let veAllocate: VeAllocate
let ownerAccount
let Alice
let Bob
let nft1, nft2, nft3
let chainId
before(async () => {
ownerAccount = (await provider.getSigner(0)) as Signer
Alice = (await provider.getSigner(1)) as Signer
Bob = (await provider.getSigner(2)) as Signer
config = await getTestConfig(ownerAccount as Signer)
addresses = await getAddresses()
})
it('initialize accounts', async () => {
chainId = (await Alice.provider.getNetwork()).chainId
const minAbi = [
{
constant: false,
inputs: [
{ name: 'to', type: 'address' },
{ name: 'value', type: 'uint256' }
],
name: 'mint',
outputs: [{ name: '', type: 'bool' }],
payable: false,
stateMutability: 'nonpayable',
type: 'function'
}
]
const tokenContract = new ethers.Contract(addresses.Ocean, minAbi, ownerAccount)
const estGas = await tokenContract.estimateGas.mint(
await Alice.getAddress(),
amountToUnits(null, null, '1000', 18)
)
await sendTx(
estGas,
ownerAccount,
1,
tokenContract.mint,
await Alice.getAddress(),
amountToUnits(null, null, '1000', 18)
)
await sendTx(
estGas,
ownerAccount,
1,
tokenContract.mint,
await Bob.getAddress(),
amountToUnits(null, null, '1000', 18)
)
veOcean = new VeOcean(addresses.veOCEAN, Alice)
veFeeDistributor = new VeFeeDistributor(addresses.veFeeDistributor, Alice)
veAllocate = new VeAllocate(addresses.veAllocate, Alice)
veFeeEstimate = new VeFeeEstimate(addresses.veFeeEstimate, Alice)
nftFactory = new NftFactory(addresses.ERC721Factory, Alice)
})
it('Alice should lock 100 Ocean', async () => {
// since we can only lock once, we test if tx fails or not
// so if there is already a lock, skip it
const currentBalance = await veOcean.getLockedAmount(await Alice.getAddress())
const currentLock = await veOcean.lockEnd(await Alice.getAddress())
const amount = '100'
await approve(
Alice,
config,
await Alice.getAddress(),
addresses.Ocean,
addresses.veOCEAN,
amount
)
const timestamp = Math.floor(Date.now() / 1000)
const unlockTime = timestamp + 7 * 86400
if (parseInt(currentBalance) > 0 || currentLock > 0) {
// we already have some locked tokens, so our transaction should fail
try {
await veOcean.lockTokens(amount, unlockTime)
assert(false, 'This should fail!')
} catch (e) {
// do nothing
}
} else {
const response = await veOcean.lockTokens(amount, unlockTime)
const tx = await response.wait()
// check events
const depositEvent = getEventFromTx(tx, 'Deposit')
const supplyEvent = getEventFromTx(tx, 'Supply')
assert(
depositEvent.args[0].toLowerCase() === (await Alice.getAddress()).toLowerCase()
)
assert(
depositEvent.args[1].toString() ===
(await amountToUnits(null, null, amount, 18)).toString()
)
assert(depositEvent.args[2] > 0) // we cannot compare it to the actual untiLock, because contract will round it to weeks
assert(depositEvent.args[1] > supplyEvent.args[0]) // supply has increased
}
})
it('Alice should increase the lock time', async () => {
const currentLock = await veOcean.lockEnd(await Alice.getAddress())
const newLock = parseInt(String(currentLock)) + 7 * 86400 + 20
const tx = await veOcean.increaseUnlockTime(newLock)
await tx.wait()
const newCurrentLock = await veOcean.lockEnd(await Alice.getAddress())
assert(newCurrentLock > currentLock, 'Lock time should change"')
})
it('Alice should increase the locked amount', async () => {
const currentBalance = await veOcean.getLockedAmount(await Alice.getAddress())
const currentLock = await veOcean.lockEnd(await Alice.getAddress())
const amount = '200'
let tx = await approve(
Alice,
config,
await Alice.getAddress(),
addresses.Ocean,
addresses.veOCEAN,
amount
)
tx = await veOcean.increaseAmount(amount)
await tx.wait()
const newCurrentBalance = await veOcean.getLockedAmount(await Alice.getAddress())
const newCurrentLock = await veOcean.lockEnd(await Alice.getAddress())
assert(newCurrentLock === currentLock, 'Lock time should not change')
assert(
parseFloat(newCurrentBalance) > parseFloat(currentBalance),
'Amount error:' + newCurrentBalance + ' shoud be > than ' + currentBalance
)
})
it('Alice should publish 3 NFTs', async () => {
// publish 3 nfts
nft1 = await nftFactory.createNFT({
name: 'testNft1',
symbol: 'TSTF1',
templateIndex: 1,
tokenURI: '',
transferable: 1,
owner: await Alice.getAddress()
})
nft2 = await nftFactory.createNFT({
name: 'testNft2',
symbol: 'TSTF2',
templateIndex: 1,
tokenURI: '',
transferable: 1,
owner: await Alice.getAddress()
})
nft3 = await nftFactory.createNFT({
name: 'testNft3',
symbol: 'TSTF3',
templateIndex: 1,
tokenURI: '',
transferable: 1,
owner: await Alice.getAddress()
})
})
it('Alice should allocate 10% to NFT1', async () => {
const totalAllocation = await veAllocate.getTotalAllocation(await Alice.getAddress())
const response = await veAllocate.setAllocation('1000', nft1, chainId)
const tx = await response.wait()
const allocationSetEvent = getEventFromTx(tx, 'AllocationSet')
assert(
allocationSetEvent.args[0].toLowerCase() ===
(await Alice.getAddress()).toLowerCase(),
'Incorect address'
)
assert(allocationSetEvent.args[1] === nft1, 'Incorect NFT address')
assert(parseInt(allocationSetEvent.args[2]) === parseInt(chainId), 'Incorect chainId')
assert(parseInt(allocationSetEvent.args[3]) === 1000, 'Incorect ammount')
const newTotalAllocation = await veAllocate.getTotalAllocation(
await Alice.getAddress()
)
const expectedAllocation = parseInt(String(totalAllocation)) + 1000
assert(
parseInt(String(newTotalAllocation)) === parseInt(String(expectedAllocation)),
'NewAllocation (' + newTotalAllocation + ') should be ' + expectedAllocation
)
const nftAllocation = await veAllocate.getVeAllocation(
await Alice.getAddress(),
nft1,
chainId
)
assert(
parseInt(String(nftAllocation)) === parseInt('1000'),
nftAllocation + ' should be 1000'
)
})
it('Alice should allocate 10% to NFT2 and 20% to NFT3', async () => {
const totalAllocation = await veAllocate.getTotalAllocation(await Alice.getAddress())
const response = await veAllocate.setBatchAllocation(
['1000', '2000'],
[nft2, nft3],
[chainId, chainId]
)
const tx = await response.wait()
const allocationSetEvent = getEventFromTx(tx, 'AllocationSetMultiple')
assert(
allocationSetEvent.args[0].toLowerCase() ===
(await Alice.getAddress()).toLowerCase()
)
assert(allocationSetEvent.args[1][0] === nft2)
assert(allocationSetEvent.args[1][1] === nft3)
assert(parseInt(allocationSetEvent.args[2][0]) === parseInt(chainId))
assert(parseInt(allocationSetEvent.args[3][0]) === 1000)
assert(parseInt(allocationSetEvent.args[3][1]) === 2000)
const newTotalAllocation = await veAllocate.getTotalAllocation(
await Alice.getAddress()
)
const expectedAllocation = parseInt(String(totalAllocation)) + 3000
assert(
parseInt(String(newTotalAllocation)) === parseInt(String(expectedAllocation)),
'NewAllocation (' + newTotalAllocation + ') should be ' + expectedAllocation
)
let nftAllocation = await veAllocate.getVeAllocation(
await Alice.getAddress(),
nft2,
chainId
)
assert(
parseInt(String(nftAllocation)) === parseInt('1000'),
nftAllocation + ' should be 1000'
)
nftAllocation = await veAllocate.getVeAllocation(
await Alice.getAddress(),
nft3,
chainId
)
assert(
parseInt(String(nftAllocation)) === parseInt('2000'),
nftAllocation + ' should be 2000'
)
})
})