mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
add more tests
This commit is contained in:
parent
205a763d97
commit
22ab44713d
@ -1,11 +1,14 @@
|
||||
import defaultFixedRateExchangeABI from '@oceanprotocol/contracts/artifacts/FixedRateExchange.json'
|
||||
import BigNumber from 'bignumber.js'
|
||||
|
||||
export interface FixedPricedExchange {
|
||||
exchangeID?: string
|
||||
exchangeOwner: string
|
||||
dataToken: string
|
||||
baseToken: string
|
||||
fixedRate: number
|
||||
fixedRate: string
|
||||
active: boolean
|
||||
supply: string
|
||||
}
|
||||
|
||||
export class OceanFixedRateExchange {
|
||||
@ -90,25 +93,47 @@ export class OceanFixedRateExchange {
|
||||
* @param {String} address User address
|
||||
* @return {Promise<any>} transaction receipt
|
||||
*/
|
||||
public async swap(
|
||||
public async buyDT(
|
||||
exchangeId: string,
|
||||
dataTokenAmount: string,
|
||||
address: string
|
||||
): Promise<any> {
|
||||
const estGas = await this.contract.methods
|
||||
.swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount)))
|
||||
.estimateGas(function (err, estGas) {
|
||||
if (err) console.log('FixedPriceExchange: ' + err)
|
||||
return estGas
|
||||
})
|
||||
|
||||
const trxReceipt = await this.contract.methods
|
||||
.swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount)))
|
||||
.send({
|
||||
from: address,
|
||||
gas: estGas + 1
|
||||
})
|
||||
return trxReceipt
|
||||
let estGas
|
||||
try {
|
||||
estGas = await this.contract.methods
|
||||
.swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount)))
|
||||
.estimateGas(function (err, g) {
|
||||
if (err) {
|
||||
// console.log('FixedPriceExchange: ' + err)
|
||||
return 200000
|
||||
} else {
|
||||
return g
|
||||
}
|
||||
})
|
||||
} catch (e) {
|
||||
// console.log('FixedPriceExchange: ' + e)
|
||||
estGas = 200000
|
||||
}
|
||||
// console.log('estGas: ' + estGas)
|
||||
/* const estGas = await this.contract.methods
|
||||
.swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount)))
|
||||
.estimateGas(function (err, estGas) {
|
||||
if (err) console.log('FixedPriceExchange: ' + err)
|
||||
return estGas
|
||||
})
|
||||
*/
|
||||
try {
|
||||
const trxReceipt = await this.contract.methods
|
||||
.swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount)))
|
||||
.send({
|
||||
from: address,
|
||||
gas: estGas + 1
|
||||
})
|
||||
return trxReceipt
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,12 +159,20 @@ export class OceanFixedRateExchange {
|
||||
newRate: number,
|
||||
address: string
|
||||
): Promise<any> {
|
||||
const estGas = await this.contract.methods
|
||||
.setRate(exchangeId, this.web3.utils.toWei(String(newRate)))
|
||||
.estimateGas(function (err, estGas) {
|
||||
if (err) console.log('FixedPriceExchange: ' + err)
|
||||
return estGas
|
||||
})
|
||||
let estGas
|
||||
try {
|
||||
estGas = await this.contract.methods
|
||||
.setRate(exchangeId, this.web3.utils.toWei(String(newRate)))
|
||||
.estimateGas(function (err, estGas) {
|
||||
if (err) {
|
||||
// console.log('FixedPriceExchange: ' + err)
|
||||
return 200000
|
||||
}
|
||||
return estGas
|
||||
})
|
||||
} catch (e) {
|
||||
estGas = 200000
|
||||
}
|
||||
const trxReceipt = await this.contract.methods
|
||||
.setRate(exchangeId, this.web3.utils.toWei(String(newRate)))
|
||||
.send({
|
||||
@ -156,12 +189,20 @@ export class OceanFixedRateExchange {
|
||||
* @return {Promise<any>} transaction receipt
|
||||
*/
|
||||
public async activate(exchangeId: string, address: string): Promise<any> {
|
||||
const estGas = await this.contract.methods
|
||||
.activate(exchangeId)
|
||||
.estimateGas(function (err, estGas) {
|
||||
if (err) console.log('FixedPriceExchange: ' + err)
|
||||
return estGas
|
||||
})
|
||||
let estGas
|
||||
try {
|
||||
estGas = await this.contract.methods
|
||||
.activate(exchangeId)
|
||||
.estimateGas(function (err, estGas) {
|
||||
if (err) {
|
||||
// console.log('FixedPriceExchange: ' + err)
|
||||
estGas = 200000
|
||||
}
|
||||
return estGas
|
||||
})
|
||||
} catch (e) {
|
||||
estGas = 200000
|
||||
}
|
||||
const trxReceipt = await this.contract.methods.activate(exchangeId).send({
|
||||
from: address,
|
||||
gas: estGas + 1
|
||||
@ -176,12 +217,20 @@ export class OceanFixedRateExchange {
|
||||
* @return {Promise<any>} transaction receipt
|
||||
*/
|
||||
public async deactivate(exchangeId: string, address: string): Promise<any> {
|
||||
const estGas = await this.contract.methods
|
||||
.deactivate(exchangeId)
|
||||
.estimateGas(function (err, estGas) {
|
||||
if (err) console.log('FixedPriceExchange: ' + err)
|
||||
return estGas
|
||||
})
|
||||
let estGas
|
||||
try {
|
||||
estGas = await this.contract.methods
|
||||
.deactivate(exchangeId)
|
||||
.estimateGas(function (err, estGas) {
|
||||
if (err) {
|
||||
// console.log('FixedPriceExchange: ' + err)
|
||||
estGas = 200000
|
||||
}
|
||||
return estGas
|
||||
})
|
||||
} catch (e) {
|
||||
estGas = 200000
|
||||
}
|
||||
const trxReceipt = await this.contract.methods.deactivate(exchangeId).send({
|
||||
from: address,
|
||||
gas: estGas + 1
|
||||
@ -199,6 +248,32 @@ export class OceanFixedRateExchange {
|
||||
return this.web3.utils.fromWei(weiRate)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Supply
|
||||
* @param {String} exchangeId ExchangeId
|
||||
* @return {Promise<string>} Rate (converted from wei)
|
||||
*/
|
||||
public async getSupply(exchangeId: string): Promise<string> {
|
||||
const weiRate = await this.contract.methods.getSupply(exchangeId).call()
|
||||
return this.web3.utils.fromWei(weiRate)
|
||||
}
|
||||
|
||||
/**
|
||||
* getOceanNeeded
|
||||
* @param {String} exchangeId ExchangeId
|
||||
* @param {Number} dataTokenAmount Amount of Data Tokens
|
||||
* @return {Promise<string>} Ocean amount needed
|
||||
*/
|
||||
public async getOceanNeeded(
|
||||
exchangeId: string,
|
||||
dataTokenAmount: string
|
||||
): Promise<string> {
|
||||
const weiRate = await this.contract.methods
|
||||
.CalcInGivenOut(exchangeId, this.web3.utils.toWei(dataTokenAmount))
|
||||
.call()
|
||||
return this.web3.utils.fromWei(weiRate)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get exchange details
|
||||
* @param {String} exchangeId ExchangeId
|
||||
@ -230,4 +305,44 @@ export class OceanFixedRateExchange {
|
||||
const result = await this.contract.methods.isActive(exchangeId).call()
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates how many basetokens are needed to get specifyed amount of datatokens
|
||||
* @param {String} exchangeId ExchangeId
|
||||
* @param {String} dataTokenAmount dataTokenAmount
|
||||
* @return {Promise<Boolean>} Result
|
||||
*/
|
||||
public async CalcInGivenOut(
|
||||
exchangeId: string,
|
||||
dataTokenAmount: string
|
||||
): Promise<boolean> {
|
||||
const result = await this.contract.methods
|
||||
.CalcInGivenOut(exchangeId, this.web3.utils.toWei(dataTokenAmount))
|
||||
.call()
|
||||
return this.web3.utils.fromWei(result)
|
||||
}
|
||||
|
||||
public async searchforDT(
|
||||
dataTokenAddress: string,
|
||||
minSupply: string
|
||||
): Promise<FixedPricedExchange[]> {
|
||||
const result: FixedPricedExchange[] = []
|
||||
const events = await this.contract.getPastEvents('ExchangeCreated', {
|
||||
filter: { datatoken: dataTokenAddress },
|
||||
fromBlock: 0,
|
||||
toBlock: 'latest'
|
||||
})
|
||||
for (let i = 0; i < events.length; i++) {
|
||||
const constituents = await this.getExchange(events[i].returnValues[0])
|
||||
constituents.exchangeID = events[i].returnValues[0]
|
||||
if (constituents.active === true) {
|
||||
const supply = new BigNumber(this.web3.utils.fromWei(constituents.supply))
|
||||
const required = new BigNumber(minSupply)
|
||||
if (supply >= required) {
|
||||
result.push(constituents)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
@ -32,10 +32,12 @@ describe('FixedRateExchange flow', () => {
|
||||
let owner
|
||||
let contracts
|
||||
|
||||
let consoleDebug: false
|
||||
const consoleDebug = false
|
||||
let greatPool
|
||||
const tokenAmount = '1000'
|
||||
const transferAmount = '200'
|
||||
const tokenAmount = '1000000000000000000000000000000000'
|
||||
const fixedPriceRate = '0.5'
|
||||
const updatedPriceRate = '0.5'
|
||||
const swapAmount = '1'
|
||||
const blob = 'http://localhost:8030/api/v1/services/consume'
|
||||
describe('#test', () => {
|
||||
before(async () => {
|
||||
@ -79,7 +81,8 @@ describe('FixedRateExchange flow', () => {
|
||||
it('should create datatokens smart contract', async () => {
|
||||
tokenAddress = await datatoken.create(blob, alice)
|
||||
assert(tokenAddress !== null)
|
||||
console.log('data Token address:' + tokenAddress)
|
||||
if (consoleDebug) console.log("Alice's address:" + alice)
|
||||
if (consoleDebug) console.log('data Token address:' + tokenAddress)
|
||||
})
|
||||
it('Create a dummy OceanToken', async () => {
|
||||
// Bob creates a Datatoken
|
||||
@ -90,7 +93,8 @@ describe('FixedRateExchange flow', () => {
|
||||
web3
|
||||
)
|
||||
oceanTokenAddress = await oceandatatoken.create(blob, bob)
|
||||
console.log('oceanTokenAddress:' + oceanTokenAddress)
|
||||
if (consoleDebug) console.log("Bob's address:" + bob)
|
||||
if (consoleDebug) console.log('oceanTokenAddress:' + oceanTokenAddress)
|
||||
})
|
||||
|
||||
it('should initialize FixedExchangeRate class', async () => {
|
||||
@ -104,33 +108,40 @@ describe('FixedRateExchange flow', () => {
|
||||
})
|
||||
|
||||
it('Alice mints 1000 tokens', async () => {
|
||||
await datatoken.mint(tokenAddress, alice, tokenAmount)
|
||||
const txid = await datatoken.mint(tokenAddress, alice, tokenAmount)
|
||||
if (consoleDebug) console.log(txid)
|
||||
assert(txid !== null)
|
||||
})
|
||||
it('Bob mints 1000 Ocean tokens', async () => {
|
||||
await oceandatatoken.mint(oceanTokenAddress, bob, tokenAmount)
|
||||
const txid = await oceandatatoken.mint(oceanTokenAddress, bob, tokenAmount)
|
||||
if (consoleDebug) console.log(txid)
|
||||
assert(txid !== null)
|
||||
})
|
||||
it('Alice should have 1000 tokens', async () => {
|
||||
const balance = await datatoken.balance(tokenAddress, alice)
|
||||
console.log(balance)
|
||||
if (consoleDebug) console.log("Alice's datatoke balance:" + balance)
|
||||
})
|
||||
it('Bob should have 1000 ocean tokens', async () => {
|
||||
const balance = await oceandatatoken.balance(oceanTokenAddress, bob)
|
||||
console.log(balance)
|
||||
})
|
||||
it('Alice creates a new FixedRate Exchange with a rate of 0.5', async () => {
|
||||
aliceExchangeId = await FixedRateClass.create(tokenAddress, '0.1', alice)
|
||||
console.log(aliceExchangeId)
|
||||
if (consoleDebug) console.log("Bob's ocean balance:" + balance)
|
||||
})
|
||||
it('Alice allows Exchange to spend 1000 data tokens', async () => {
|
||||
await datatoken.approve(tokenAddress, FixedRateExchangeAddress, tokenAmount, alice)
|
||||
const txid = await datatoken.approve(
|
||||
tokenAddress,
|
||||
FixedRateExchangeAddress,
|
||||
tokenAmount,
|
||||
alice
|
||||
)
|
||||
if (consoleDebug) console.log(txid)
|
||||
})
|
||||
it('Bob allows Exchange to spend 1000 ocean tokens', async () => {
|
||||
await oceandatatoken.approve(
|
||||
const txid = await oceandatatoken.approve(
|
||||
oceanTokenAddress,
|
||||
FixedRateExchangeAddress,
|
||||
tokenAmount,
|
||||
bob
|
||||
)
|
||||
if (consoleDebug) console.log(txid)
|
||||
})
|
||||
it('Alice should aproved speding datatokens', async () => {
|
||||
const balance = await datatoken.allowance(
|
||||
@ -138,7 +149,7 @@ describe('FixedRateExchange flow', () => {
|
||||
alice,
|
||||
FixedRateExchangeAddress
|
||||
)
|
||||
console.log(balance)
|
||||
if (consoleDebug) console.log('Alice datatoken allowance:' + balance)
|
||||
})
|
||||
it('Bob should aproved speding oceantokens', async () => {
|
||||
const balance = await oceandatatoken.allowance(
|
||||
@ -146,16 +157,67 @@ describe('FixedRateExchange flow', () => {
|
||||
bob,
|
||||
FixedRateExchangeAddress
|
||||
)
|
||||
console.log(balance)
|
||||
if (consoleDebug) console.log('Bob ocean allowance:' + balance)
|
||||
})
|
||||
it('Alice creates a new FixedRate Exchange with a rate of 0.5', async () => {
|
||||
aliceExchangeId = await FixedRateClass.create(tokenAddress, fixedPriceRate, alice)
|
||||
if (consoleDebug) console.log('aliceExchangeId:' + aliceExchangeId)
|
||||
})
|
||||
it('Bob should find the exchange', async () => {
|
||||
const exchangeDetails = await FixedRateClass.searchforDT(tokenAddress, '0')
|
||||
assert(exchangeDetails[0].exchangeID === aliceExchangeId)
|
||||
})
|
||||
it('Bob should get the exchange details', async () => {
|
||||
const exchangeDetails = await FixedRateClass.getExchange(aliceExchangeId)
|
||||
console.log(exchangeDetails)
|
||||
if (consoleDebug) console.log(exchangeDetails)
|
||||
})
|
||||
|
||||
it('Bob should get the amount of Ocean needed', async () => {
|
||||
const OceansNeeded = await FixedRateClass.CalcInGivenOut(
|
||||
aliceExchangeId,
|
||||
swapAmount
|
||||
)
|
||||
if (consoleDebug) console.log('Oceans needed:' + OceansNeeded)
|
||||
assert(OceansNeeded !== null)
|
||||
})
|
||||
it('Bob should swap 1 DataToken', async () => {
|
||||
const swapResult = await FixedRateClass.swap(aliceExchangeId, '1', bob)
|
||||
console.log(swapResult)
|
||||
const swapResult = await FixedRateClass.buyDT(aliceExchangeId, swapAmount, bob)
|
||||
if (consoleDebug) console.log(swapResult)
|
||||
assert(swapResult !== null)
|
||||
})
|
||||
it('Alice datatoken balance after swap', async () => {
|
||||
const balance = await datatoken.balance(tokenAddress, alice)
|
||||
if (consoleDebug) console.log('Alice datatoken balance:' + balance)
|
||||
})
|
||||
it('Alice ocean balance after swap', async () => {
|
||||
const balance = await oceandatatoken.balance(oceanTokenAddress, alice)
|
||||
if (consoleDebug) console.log('Alice ocean balance:' + balance)
|
||||
})
|
||||
it('Bob datatoken balance after swap', async () => {
|
||||
const balance = await datatoken.balance(tokenAddress, bob)
|
||||
if (consoleDebug) console.log('Bob datatoken balance:' + balance)
|
||||
})
|
||||
it('Bob ocean balance after swap', async () => {
|
||||
const balance = await oceandatatoken.balance(oceanTokenAddress, bob)
|
||||
if (consoleDebug) console.log('Bob ocean balance:' + balance)
|
||||
})
|
||||
it('Alice should update the rate', async () => {
|
||||
const tx = await FixedRateClass.setRate(aliceExchangeId, updatedPriceRate, alice)
|
||||
assert(tx !== null)
|
||||
})
|
||||
it('Alice should be able to deactivate the exchange', async () => {
|
||||
const tx = await FixedRateClass.deactivate(aliceExchangeId, alice)
|
||||
assert(tx !== null)
|
||||
const exchangeDetails = await FixedRateClass.getExchange(aliceExchangeId)
|
||||
assert(exchangeDetails.active === false)
|
||||
})
|
||||
it('Alice should be able to activate the exchange', async () => {
|
||||
const tx = await FixedRateClass.activate(aliceExchangeId, alice)
|
||||
assert(tx !== null)
|
||||
const exchangeDetails = await FixedRateClass.getExchange(aliceExchangeId)
|
||||
assert(exchangeDetails.active === true)
|
||||
})
|
||||
|
||||
/*
|
||||
it('Alice creates a new OceanPool pool', async () => {
|
||||
/// new pool with total DT = 45 , dt weight=90% with swap fee 2%
|
||||
|
Loading…
x
Reference in New Issue
Block a user