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

general renaming in FixedRateExchange

This commit is contained in:
Miquel A. Cabot 2022-06-14 10:52:51 +02:00
parent 76bbf49d93
commit 83b8d1dce3
3 changed files with 93 additions and 85 deletions

View File

@ -33,7 +33,7 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} consumeMarketFee consumeMarketFee in fraction
* @return {Promise<TransactionReceipt>} transaction receipt
*/
public async buyDT<G extends boolean = false>(
public async buyDatatokens<G extends boolean = false>(
address: string,
exchangeId: string,
datatokenAmount: string,
@ -97,7 +97,7 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} consumeMarketFee consumeMarketFee in fraction
* @return {Promise<TransactionReceipt>} transaction receipt
*/
public async sellDT<G extends boolean = false>(
public async sellDatatokens<G extends boolean = false>(
address: string,
exchangeId: string,
datatokenAmount: string,
@ -299,7 +299,7 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} exchangeId ExchangeId
* @return {Promise<string>} dt supply formatted
*/
public async getDTSupply(exchangeId: string): Promise<string> {
public async getDatatokenSupply(exchangeId: string): Promise<string> {
const dtSupply = await this.contract.methods.getDTSupply(exchangeId).call()
const exchange = await this.getExchange(exchangeId)
return await this.unitsToAmount(exchange.datatoken, dtSupply, +exchange.dtDecimals)
@ -310,7 +310,7 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} exchangeId ExchangeId
* @return {Promise<string>} dt supply formatted
*/
public async getBTSupply(exchangeId: string): Promise<string> {
public async getBasetokenSupply(exchangeId: string): Promise<string> {
const btSupply = await this.contract.methods.getBTSupply(exchangeId).call()
const exchange = await this.getExchange(exchangeId)
return await this.unitsToAmount(exchange.baseToken, btSupply, +exchange.btDecimals)
@ -326,19 +326,19 @@ export class FixedRateExchange extends SmartContractWithAddress {
}
/**
* calcBaseInGivenOutDT - Calculates how many base tokens are needed to get specified amount of datatokens
* calcBaseInGivenDatatokensOut - Calculates how many base tokens are needed to get specified amount of datatokens
* @param {String} exchangeId ExchangeId
* @param {string} datatokenAmount Amount of datatokens user wants to buy
* @param {String} consumeMarketFee consumeMarketFee in fraction
* @return {Promise<PriceAndFees>} how many base tokens are needed and fees
*/
public async calcBaseInGivenOutDT(
public async calcBaseInGivenDatatokensOut(
exchangeId: string,
datatokenAmount: string,
consumeMarketFee: string = '0'
): Promise<PriceAndFees> {
const fixedRateExchange = await this.getExchange(exchangeId)
const result = await this.contract.methods
const outDT = await this.contract.methods
.calcBaseInGivenOutDT(
exchangeId,
await this.amountToUnits(
@ -353,22 +353,22 @@ export class FixedRateExchange extends SmartContractWithAddress {
const priceAndFees = {
baseTokenAmount: await this.unitsToAmount(
fixedRateExchange.baseToken,
result.baseTokenAmount,
outDT.baseTokenAmount,
+fixedRateExchange.btDecimals
),
marketFeeAmount: await this.unitsToAmount(
fixedRateExchange.baseToken,
result.marketFeeAmount,
outDT.marketFeeAmount,
+fixedRateExchange.btDecimals
),
oceanFeeAmount: await this.unitsToAmount(
fixedRateExchange.baseToken,
result.oceanFeeAmount,
outDT.oceanFeeAmount,
+fixedRateExchange.btDecimals
),
consumeMarketFeeAmount: await this.unitsToAmount(
fixedRateExchange.baseToken,
result.consumeMarketFeeAmount,
outDT.consumeMarketFeeAmount,
+fixedRateExchange.btDecimals
)
} as PriceAndFees
@ -382,7 +382,7 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} consumeMarketFee consumeMarketFee in fraction
* @return {Promise<string>} Amount of baseTokens user will receive
*/
public async getAmountBTOut(
public async getAmountBasetokensOut(
exchangeId: string,
datatokenAmount: string,
consumeMarketFee: string = '0'
@ -409,34 +409,34 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @return {Promise<FixedPricedExchange>} Exchange details
*/
public async getExchange(exchangeId: string): Promise<FixedPriceExchange> {
const result: FixedPriceExchange = await this.contract.methods
const exchange: FixedPriceExchange = await this.contract.methods
.getExchange(exchangeId)
.call()
result.dtDecimals = result.dtDecimals.toString()
result.btDecimals = result.btDecimals.toString()
result.dtBalance = await this.unitsToAmount(
result.datatoken,
result.dtBalance,
+result.dtDecimals
exchange.dtDecimals = exchange.dtDecimals.toString()
exchange.btDecimals = exchange.btDecimals.toString()
exchange.dtBalance = await this.unitsToAmount(
exchange.datatoken,
exchange.dtBalance,
+exchange.dtDecimals
)
result.btBalance = await this.unitsToAmount(
result.baseToken,
result.btBalance,
+result.btDecimals
exchange.btBalance = await this.unitsToAmount(
exchange.baseToken,
exchange.btBalance,
+exchange.btDecimals
)
result.dtSupply = await this.unitsToAmount(
result.datatoken,
result.dtSupply,
+result.dtDecimals
exchange.dtSupply = await this.unitsToAmount(
exchange.datatoken,
exchange.dtSupply,
+exchange.dtDecimals
)
result.btSupply = await this.unitsToAmount(
result.baseToken,
result.btSupply,
+result.btDecimals
exchange.btSupply = await this.unitsToAmount(
exchange.baseToken,
exchange.btSupply,
+exchange.btDecimals
)
result.fixedRate = this.web3.utils.fromWei(result.fixedRate)
result.exchangeId = exchangeId
return result
exchange.fixedRate = this.web3.utils.fromWei(exchange.fixedRate)
exchange.exchangeId = exchangeId
return exchange
}
/**
@ -445,24 +445,24 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @return {Promise<FixedPricedExchange>} Exchange details
*/
public async getFeesInfo(exchangeId: string): Promise<FeesInfo> {
const result: FeesInfo = await this.contract.methods.getFeesInfo(exchangeId).call()
result.opcFee = this.web3.utils.fromWei(result.opcFee.toString())
result.marketFee = this.web3.utils.fromWei(result.marketFee.toString())
const feesInfo: FeesInfo = await this.contract.methods.getFeesInfo(exchangeId).call()
feesInfo.opcFee = this.web3.utils.fromWei(feesInfo.opcFee.toString())
feesInfo.marketFee = this.web3.utils.fromWei(feesInfo.marketFee.toString())
const exchange = await this.getExchange(exchangeId)
result.marketFeeAvailable = await this.unitsToAmount(
feesInfo.marketFeeAvailable = await this.unitsToAmount(
exchange.baseToken,
result.marketFeeAvailable,
feesInfo.marketFeeAvailable,
+exchange.btDecimals
)
result.oceanFeeAvailable = await this.unitsToAmount(
feesInfo.oceanFeeAvailable = await this.unitsToAmount(
exchange.baseToken,
result.oceanFeeAvailable,
feesInfo.oceanFeeAvailable,
+exchange.btDecimals
)
result.exchangeId = exchangeId
return result
feesInfo.exchangeId = exchangeId
return feesInfo
}
/**
@ -480,8 +480,8 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @return {Promise<Boolean>} Result
*/
public async isActive(exchangeId: string): Promise<boolean> {
const result = await this.contract.methods.isActive(exchangeId).call()
return result
const active = await this.contract.methods.isActive(exchangeId).call()
return active
}
/**
@ -558,7 +558,7 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} amount amount to be collected
* @return {Promise<TransactionReceipt>} transaction receipt
*/
public async collectBT<G extends boolean = false>(
public async collectBasetokens<G extends boolean = false>(
address: string,
exchangeId: string,
amount: string,
@ -599,7 +599,7 @@ export class FixedRateExchange extends SmartContractWithAddress {
* @param {String} amount amount to be collected
* @return {Promise<TransactionReceipt>} transaction receipt
*/
public async collectDT<G extends boolean = false>(
public async collectDatatokens<G extends boolean = false>(
address: string,
exchangeId: string,
amount: string,

View File

@ -616,7 +616,7 @@ describe('Marketplace flow tests', async () => {
/// ```Typescript
const fixedRate = new FixedRateExchange(freAddress, web3)
const oceanAmount = await (
await fixedRate.calcBaseInGivenOutDT(freId, '1')
await fixedRate.calcBaseInGivenDatatokensOut(freId, '1')
).baseTokenAmount
/// ```
/// Now that the market has fetched those values it can display the asset on the front end. In our case we will just console log the results:
@ -659,7 +659,7 @@ describe('Marketplace flow tests', async () => {
/// ```
/// Now we can make the contract call
/// ```Typescript
await fixedRate.buyDT(consumerAccount, freId, '1', '2')
await fixedRate.buyDatatokens(consumerAccount, freId, '1', '2')
consumerOCEANBalance = await balance(web3, addresses.Ocean, consumerAccount)
console.log(`Consumer OCEAN balance after swap: ${consumerOCEANBalance}`)

View File

@ -168,31 +168,31 @@ describe('Fixed Rate unit test', () => {
expect(await fixedRate.getRate(exchangeId)).to.equal('1')
})
it('#getDTSupply - should get the dt supply in the exchange', async () => {
it('#getDatatokenSupply - should get the dt supply in the exchange', async () => {
// exchange owner hasn't approved any DT for sell
expect(await fixedRate.getDTSupply(exchangeId)).to.equal('0')
expect(await fixedRate.getDatatokenSupply(exchangeId)).to.equal('0')
})
it('#getBTSupply - should get the bt supply in the exchange', async () => {
it('#getBasetokenSupply - should get the bt supply in the exchange', async () => {
// no baseToken at the beginning
expect(await fixedRate.getBTSupply(exchangeId)).to.equal('0')
expect(await fixedRate.getBasetokenSupply(exchangeId)).to.equal('0')
})
it('#calcBaseInGivenOutDT - should get bt amount in for a specific dt amount', async () => {
it('#calcBaseInGivenDatatokensOut - should get bt amount in for a specific dt amount', async () => {
// 100.2 DAI for 100 DT (0.1% market fee and 0.1% ocean fee)
expect(
await (
await fixedRate.calcBaseInGivenOutDT(exchangeId, '100')
await fixedRate.calcBaseInGivenDatatokensOut(exchangeId, '100')
).baseTokenAmount
).to.equal('100.3')
})
it('#getAmountBTOut - should get bt amount out for a specific dt amount', async () => {
it('#getAmountBasetokensOut - should get bt amount out for a specific dt amount', async () => {
// 99.8 DAI for 100 DT (0.1% market fee and 0.1% ocean fee)
expect(await fixedRate.getAmountBTOut(exchangeId, '100')).to.equal('99.7')
expect(await fixedRate.getAmountBasetokensOut(exchangeId, '100')).to.equal('99.7')
})
it('#buyDT - user1 should buy some dt', async () => {
it('#buyDatatokens - user1 should buy some dt', async () => {
// total supply is ZERO right now so dt owner mints 1000 DT and approves the fixed rate contract
await dtContract.methods
.mint(exchangeOwner, web3.utils.toWei('1000'))
@ -209,7 +209,7 @@ describe('Fixed Rate unit test', () => {
)
// user1 buys 10 DT
const tx = await fixedRate.buyDT(user1, exchangeId, '10', '11')
const tx = await fixedRate.buyDatatokens(user1, exchangeId, '10', '11')
// console.log(tx.events.Swapped.returnValues)
assert(tx.events.Swapped != null)
const args = tx.events.Swapped.returnValues
@ -235,12 +235,12 @@ describe('Fixed Rate unit test', () => {
expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('0')
})
it('#sellDT - user1 should sell some dt', async () => {
it('#sellDatatokens - user1 should sell some dt', async () => {
await approve(web3, user1, dtAddress, contracts.fixedRateAddress, '100')
const daiBalanceBefore = new BigNumber(
await balance(web3, contracts.daiAddress, user1)
)
const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9')
const tx = await fixedRate.sellDatatokens(user1, exchangeId, '10', '9')
// console.log(tx.events.Swapped.returnValues)
assert(tx.events.Swapped != null)
const args = tx.events.Swapped.returnValues
@ -263,7 +263,7 @@ describe('Fixed Rate unit test', () => {
// no BTs in the contract (except for the fees, but not accounted here)
expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0')
// DT supply is back at 1000 (exchange Owner allowance + dt balance in the fixed rate)
expect(await fixedRate.getDTSupply(exchangeId)).to.equal('1000')
expect(await fixedRate.getDatatokenSupply(exchangeId)).to.equal('1000')
})
it('#getExchange - should return exchange details', async () => {
@ -307,26 +307,30 @@ describe('Fixed Rate unit test', () => {
expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ZERO_ADDRESS)
})
it('#collectBT- should collect BT in the contract, if exchangeOwner', async () => {
it('#collectBasetokens- should collect BT in the contract, if exchangeOwner', async () => {
// there are no bt in the contract
expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0')
// user1 buys 1 DT
await fixedRate.buyDT(user1, exchangeId, '1', '2')
await fixedRate.buyDatatokens(user1, exchangeId, '1', '2')
// 1 DAI in the contract
const fixedRateDetails = await fixedRate.getExchange(exchangeId)
expect(fixedRateDetails.btBalance).to.equal('1')
// owner collects BTs
await fixedRate.collectBT(exchangeOwner, exchangeId, fixedRateDetails.btBalance)
await fixedRate.collectBasetokens(
exchangeOwner,
exchangeId,
fixedRateDetails.btBalance
)
// btBalance is zero
expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0')
})
it('#collectDT- should collect DT in the contract, if exchangeOwner', async () => {
it('#collectDatatokens- should collect DT in the contract, if exchangeOwner', async () => {
const result = await fixedRate.getExchange(exchangeId)
// 9 dts left
expect(result.dtBalance).to.equal('9')
// owner collects DTs
await fixedRate.collectDT(exchangeOwner, exchangeId, result.dtBalance)
await fixedRate.collectDatatokens(exchangeOwner, exchangeId, result.dtBalance)
// no more dts in the contract
const result2 = await fixedRate.getExchange(exchangeId)
expect(result2.dtBalance).to.equal('0')
@ -473,31 +477,31 @@ describe('Fixed Rate unit test', () => {
expect(await fixedRate.getRate(exchangeId)).to.equal('1')
})
it('#getDTSupply - should get the dt supply in the exchange', async () => {
it('#getDatatokenSupply - should get the dt supply in the exchange', async () => {
// exchange owner hasn't approved any DT for sell
expect(await fixedRate.getDTSupply(exchangeId)).to.equal('0')
expect(await fixedRate.getDatatokenSupply(exchangeId)).to.equal('0')
})
it('#getBTSupply - should get the bt supply in the exchange', async () => {
it('#getBasetokenSupply - should get the bt supply in the exchange', async () => {
// no baseToken at the beginning
expect(await fixedRate.getBTSupply(exchangeId)).to.equal('0')
expect(await fixedRate.getBasetokenSupply(exchangeId)).to.equal('0')
})
it('#calcBaseInGivenOutDT - should get bt amount in for a specific dt amount', async () => {
it('#calcBaseInGivenDatatokensOut - should get bt amount in for a specific dt amount', async () => {
// 100.2 USDC for 100 DT (0.1% market fee and 0.1% ocean fee)
expect(
await (
await fixedRate.calcBaseInGivenOutDT(exchangeId, '100')
await fixedRate.calcBaseInGivenDatatokensOut(exchangeId, '100')
).baseTokenAmount
).to.equal('100.3')
})
it('#getAmountBTOut - should get bt amount out for a specific dt amount', async () => {
it('#getAmountBasetokensOut - should get bt amount out for a specific dt amount', async () => {
// 99.8 USDC for 100 DT (0.1% market fee and 0.1% ocean fee)
expect(await fixedRate.getAmountBTOut(exchangeId, '100')).to.equal('99.7')
expect(await fixedRate.getAmountBasetokensOut(exchangeId, '100')).to.equal('99.7')
})
it('#buyDT - user1 should buy some dt', async () => {
it('#buyDatatokens - user1 should buy some dt', async () => {
// total supply is ZERO right now so dt owner mints 1000 DT and approves the fixed rate contract
await dtContract.methods
.mint(exchangeOwner, web3.utils.toWei('1000'))
@ -514,7 +518,7 @@ describe('Fixed Rate unit test', () => {
)
// user1 buys 10 DT
const tx = await fixedRate.buyDT(user1, exchangeId, '10', '11')
const tx = await fixedRate.buyDatatokens(user1, exchangeId, '10', '11')
// console.log(tx.events.Swapped.returnValues)
assert(tx.events.Swapped != null)
const args = tx.events.Swapped.returnValues
@ -544,12 +548,12 @@ describe('Fixed Rate unit test', () => {
expect((await fixedRate.getExchange(exchangeId)).dtBalance).to.equal('0')
})
it('#sellDT - user1 should sell some dt', async () => {
it('#sellDatatokens - user1 should sell some dt', async () => {
await approve(web3, user1, dtAddress, contracts.fixedRateAddress, '10')
const usdcBalanceBefore = new BigNumber(
await balance(web3, contracts.usdcAddress, user1)
)
const tx = await fixedRate.sellDT(user1, exchangeId, '10', '9')
const tx = await fixedRate.sellDatatokens(user1, exchangeId, '10', '9')
// console.log(tx.events.Swapped.returnValues)
assert(tx.events.Swapped != null)
const args = tx.events.Swapped.returnValues
@ -576,7 +580,7 @@ describe('Fixed Rate unit test', () => {
// no BTs in the contract (except for the fees, but not accounted here)
expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0')
// DT supply is back at 1000 (exchange Owner allowance + dt balance in the fixed rate)
expect(await fixedRate.getDTSupply(exchangeId)).to.equal('1000')
expect(await fixedRate.getDatatokenSupply(exchangeId)).to.equal('1000')
})
it('#getExchange - should return exchange details', async () => {
@ -620,26 +624,30 @@ describe('Fixed Rate unit test', () => {
expect(await fixedRate.getAllowedSwapper(exchangeId)).to.equal(ZERO_ADDRESS)
})
it('#collectBT- should collect BT in the contract, if exchangeOwner', async () => {
it('#collectBasetokens- should collect BT in the contract, if exchangeOwner', async () => {
// there are no bt in the contract
expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0')
// user1 buys 1 DT
await fixedRate.buyDT(user1, exchangeId, '1', '2')
await fixedRate.buyDatatokens(user1, exchangeId, '1', '2')
// 1 DAI in the contract
const exchangeDetails = await fixedRate.getExchange(exchangeId)
expect(exchangeDetails.btBalance).to.equal('1')
// owner collects BTs
await fixedRate.collectBT(exchangeOwner, exchangeId, exchangeDetails.btBalance)
await fixedRate.collectBasetokens(
exchangeOwner,
exchangeId,
exchangeDetails.btBalance
)
// btBalance is zero
expect((await fixedRate.getExchange(exchangeId)).btBalance).to.equal('0')
})
it('#collectDT- should collect DT in the contract, if exchangeOwner', async () => {
it('#collectDatatokens- should collect DT in the contract, if exchangeOwner', async () => {
const result = await fixedRate.getExchange(exchangeId)
// 9 dts left
expect(result.dtBalance).to.equal('9')
// owner collects DTs
await fixedRate.collectDT(exchangeOwner, exchangeId, result.dtBalance)
await fixedRate.collectDatatokens(exchangeOwner, exchangeId, result.dtBalance)
// no more dts in the contract
const result2 = await fixedRate.getExchange(exchangeId)
expect(result2.dtBalance).to.equal('0')