1
0
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:
alexcos20 2020-08-28 01:47:25 -07:00
parent 205a763d97
commit 22ab44713d
2 changed files with 231 additions and 54 deletions

View File

@ -1,11 +1,14 @@
import defaultFixedRateExchangeABI from '@oceanprotocol/contracts/artifacts/FixedRateExchange.json' import defaultFixedRateExchangeABI from '@oceanprotocol/contracts/artifacts/FixedRateExchange.json'
import BigNumber from 'bignumber.js'
export interface FixedPricedExchange { export interface FixedPricedExchange {
exchangeID?: string
exchangeOwner: string exchangeOwner: string
dataToken: string dataToken: string
baseToken: string baseToken: string
fixedRate: number fixedRate: string
active: boolean active: boolean
supply: string
} }
export class OceanFixedRateExchange { export class OceanFixedRateExchange {
@ -90,18 +93,36 @@ export class OceanFixedRateExchange {
* @param {String} address User address * @param {String} address User address
* @return {Promise<any>} transaction receipt * @return {Promise<any>} transaction receipt
*/ */
public async swap( public async buyDT(
exchangeId: string, exchangeId: string,
dataTokenAmount: string, dataTokenAmount: string,
address: string address: string
): Promise<any> { ): Promise<any> {
const estGas = await this.contract.methods 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))) .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount)))
.estimateGas(function (err, estGas) { .estimateGas(function (err, estGas) {
if (err) console.log('FixedPriceExchange: ' + err) if (err) console.log('FixedPriceExchange: ' + err)
return estGas return estGas
}) })
*/
try {
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount))) .swap(exchangeId, this.web3.utils.toWei(String(dataTokenAmount)))
.send({ .send({
@ -109,6 +130,10 @@ export class OceanFixedRateExchange {
gas: estGas + 1 gas: estGas + 1
}) })
return trxReceipt return trxReceipt
} catch (e) {
console.error(e)
return null
}
} }
/** /**
@ -134,12 +159,20 @@ export class OceanFixedRateExchange {
newRate: number, newRate: number,
address: string address: string
): Promise<any> { ): Promise<any> {
const estGas = await this.contract.methods let estGas
try {
estGas = await this.contract.methods
.setRate(exchangeId, this.web3.utils.toWei(String(newRate))) .setRate(exchangeId, this.web3.utils.toWei(String(newRate)))
.estimateGas(function (err, estGas) { .estimateGas(function (err, estGas) {
if (err) console.log('FixedPriceExchange: ' + err) if (err) {
// console.log('FixedPriceExchange: ' + err)
return 200000
}
return estGas return estGas
}) })
} catch (e) {
estGas = 200000
}
const trxReceipt = await this.contract.methods const trxReceipt = await this.contract.methods
.setRate(exchangeId, this.web3.utils.toWei(String(newRate))) .setRate(exchangeId, this.web3.utils.toWei(String(newRate)))
.send({ .send({
@ -156,12 +189,20 @@ export class OceanFixedRateExchange {
* @return {Promise<any>} transaction receipt * @return {Promise<any>} transaction receipt
*/ */
public async activate(exchangeId: string, address: string): Promise<any> { public async activate(exchangeId: string, address: string): Promise<any> {
const estGas = await this.contract.methods let estGas
try {
estGas = await this.contract.methods
.activate(exchangeId) .activate(exchangeId)
.estimateGas(function (err, estGas) { .estimateGas(function (err, estGas) {
if (err) console.log('FixedPriceExchange: ' + err) if (err) {
// console.log('FixedPriceExchange: ' + err)
estGas = 200000
}
return estGas return estGas
}) })
} catch (e) {
estGas = 200000
}
const trxReceipt = await this.contract.methods.activate(exchangeId).send({ const trxReceipt = await this.contract.methods.activate(exchangeId).send({
from: address, from: address,
gas: estGas + 1 gas: estGas + 1
@ -176,12 +217,20 @@ export class OceanFixedRateExchange {
* @return {Promise<any>} transaction receipt * @return {Promise<any>} transaction receipt
*/ */
public async deactivate(exchangeId: string, address: string): Promise<any> { public async deactivate(exchangeId: string, address: string): Promise<any> {
const estGas = await this.contract.methods let estGas
try {
estGas = await this.contract.methods
.deactivate(exchangeId) .deactivate(exchangeId)
.estimateGas(function (err, estGas) { .estimateGas(function (err, estGas) {
if (err) console.log('FixedPriceExchange: ' + err) if (err) {
// console.log('FixedPriceExchange: ' + err)
estGas = 200000
}
return estGas return estGas
}) })
} catch (e) {
estGas = 200000
}
const trxReceipt = await this.contract.methods.deactivate(exchangeId).send({ const trxReceipt = await this.contract.methods.deactivate(exchangeId).send({
from: address, from: address,
gas: estGas + 1 gas: estGas + 1
@ -199,6 +248,32 @@ export class OceanFixedRateExchange {
return this.web3.utils.fromWei(weiRate) 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 * Get exchange details
* @param {String} exchangeId ExchangeId * @param {String} exchangeId ExchangeId
@ -230,4 +305,44 @@ export class OceanFixedRateExchange {
const result = await this.contract.methods.isActive(exchangeId).call() const result = await this.contract.methods.isActive(exchangeId).call()
return result 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
}
} }

View File

@ -32,10 +32,12 @@ describe('FixedRateExchange flow', () => {
let owner let owner
let contracts let contracts
let consoleDebug: false const consoleDebug = false
let greatPool let greatPool
const tokenAmount = '1000' const tokenAmount = '1000000000000000000000000000000000'
const transferAmount = '200' const fixedPriceRate = '0.5'
const updatedPriceRate = '0.5'
const swapAmount = '1'
const blob = 'http://localhost:8030/api/v1/services/consume' const blob = 'http://localhost:8030/api/v1/services/consume'
describe('#test', () => { describe('#test', () => {
before(async () => { before(async () => {
@ -79,7 +81,8 @@ describe('FixedRateExchange flow', () => {
it('should create datatokens smart contract', async () => { it('should create datatokens smart contract', async () => {
tokenAddress = await datatoken.create(blob, alice) tokenAddress = await datatoken.create(blob, alice)
assert(tokenAddress !== null) 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 () => { it('Create a dummy OceanToken', async () => {
// Bob creates a Datatoken // Bob creates a Datatoken
@ -90,7 +93,8 @@ describe('FixedRateExchange flow', () => {
web3 web3
) )
oceanTokenAddress = await oceandatatoken.create(blob, bob) 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 () => { it('should initialize FixedExchangeRate class', async () => {
@ -104,33 +108,40 @@ describe('FixedRateExchange flow', () => {
}) })
it('Alice mints 1000 tokens', async () => { 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 () => { 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 () => { it('Alice should have 1000 tokens', async () => {
const balance = await datatoken.balance(tokenAddress, alice) 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 () => { it('Bob should have 1000 ocean tokens', async () => {
const balance = await oceandatatoken.balance(oceanTokenAddress, bob) const balance = await oceandatatoken.balance(oceanTokenAddress, bob)
console.log(balance) if (consoleDebug) console.log("Bob's ocean balance:" + 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)
}) })
it('Alice allows Exchange to spend 1000 data tokens', async () => { 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 () => { it('Bob allows Exchange to spend 1000 ocean tokens', async () => {
await oceandatatoken.approve( const txid = await oceandatatoken.approve(
oceanTokenAddress, oceanTokenAddress,
FixedRateExchangeAddress, FixedRateExchangeAddress,
tokenAmount, tokenAmount,
bob bob
) )
if (consoleDebug) console.log(txid)
}) })
it('Alice should aproved speding datatokens', async () => { it('Alice should aproved speding datatokens', async () => {
const balance = await datatoken.allowance( const balance = await datatoken.allowance(
@ -138,7 +149,7 @@ describe('FixedRateExchange flow', () => {
alice, alice,
FixedRateExchangeAddress FixedRateExchangeAddress
) )
console.log(balance) if (consoleDebug) console.log('Alice datatoken allowance:' + balance)
}) })
it('Bob should aproved speding oceantokens', async () => { it('Bob should aproved speding oceantokens', async () => {
const balance = await oceandatatoken.allowance( const balance = await oceandatatoken.allowance(
@ -146,16 +157,67 @@ describe('FixedRateExchange flow', () => {
bob, bob,
FixedRateExchangeAddress 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 () => { it('Bob should get the exchange details', async () => {
const exchangeDetails = await FixedRateClass.getExchange(aliceExchangeId) 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 () => { it('Bob should swap 1 DataToken', async () => {
const swapResult = await FixedRateClass.swap(aliceExchangeId, '1', bob) const swapResult = await FixedRateClass.buyDT(aliceExchangeId, swapAmount, bob)
console.log(swapResult) 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 () => { it('Alice creates a new OceanPool pool', async () => {
/// new pool with total DT = 45 , dt weight=90% with swap fee 2% /// new pool with total DT = 45 , dt weight=90% with swap fee 2%