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

bump to contracts alpha.20 (#1295)

* bump to contracts alpha.20

Co-authored-by: Bogdan Fazakas <bogdan.fazakas@gmail.com>
This commit is contained in:
Alex Coseru 2022-02-16 18:25:17 +02:00 committed by GitHub
parent 7ca5000a66
commit 7932a845d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 138 additions and 48 deletions

14
package-lock.json generated
View File

@ -9,7 +9,7 @@
"version": "1.0.0-next.19",
"license": "Apache-2.0",
"dependencies": {
"@oceanprotocol/contracts": "1.0.0-alpha.19",
"@oceanprotocol/contracts": "1.0.0-alpha.20",
"bignumber.js": "^9.0.2",
"cross-fetch": "^3.1.5",
"crypto-js": "^4.1.1",
@ -3033,9 +3033,9 @@
}
},
"node_modules/@oceanprotocol/contracts": {
"version": "1.0.0-alpha.19",
"resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.19.tgz",
"integrity": "sha512-3E4eMo2lMyWMjiGk5dvSZvs/QNq7GSAPPzfMbxVLCdjfyP7kvVVVgn0pxabQtexkK4NK7tpNdA+IbsJaM0BBdw==",
"version": "1.0.0-alpha.20",
"resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.20.tgz",
"integrity": "sha512-Z+zP7XzYjZfd0+URupld2LLZ91E07EWM4wlxpRGOhXEZ7oL3iQAlRJGq6SZN7C65LSUaMQuhj+n5C8aYOXUdwQ==",
"dependencies": {
"@openzeppelin/contracts": "^4.3.3",
"@openzeppelin/test-helpers": "^0.5.15",
@ -27682,9 +27682,9 @@
}
},
"@oceanprotocol/contracts": {
"version": "1.0.0-alpha.19",
"resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.19.tgz",
"integrity": "sha512-3E4eMo2lMyWMjiGk5dvSZvs/QNq7GSAPPzfMbxVLCdjfyP7kvVVVgn0pxabQtexkK4NK7tpNdA+IbsJaM0BBdw==",
"version": "1.0.0-alpha.20",
"resolved": "https://registry.npmjs.org/@oceanprotocol/contracts/-/contracts-1.0.0-alpha.20.tgz",
"integrity": "sha512-Z+zP7XzYjZfd0+URupld2LLZ91E07EWM4wlxpRGOhXEZ7oL3iQAlRJGq6SZN7C65LSUaMQuhj+n5C8aYOXUdwQ==",
"requires": {
"@openzeppelin/contracts": "^4.3.3",
"@openzeppelin/test-helpers": "^0.5.15",

View File

@ -57,7 +57,7 @@
"web3": "^1.7.0"
},
"dependencies": {
"@oceanprotocol/contracts": "1.0.0-alpha.19",
"@oceanprotocol/contracts": "1.0.0-alpha.20",
"bignumber.js": "^9.0.2",
"cross-fetch": "^3.1.5",
"crypto-js": "^4.1.1",

View File

@ -21,7 +21,7 @@ export interface FreOrderParams {
export interface PriceAndFees {
baseTokenAmount: string
baseTokenAmountBeforeFee: string
oceanFeeAmount: string
marketFeeAmount: string
consumeMarketFeeAmount: string
}

View File

@ -270,6 +270,25 @@ export class Pool {
return result
}
/**
* Get getMarketFee
* @param {String} poolAddress
* @return {String}
*/
async getMarketFee(poolAddress: string): Promise<string> {
const pool = setContractDefaults(
new this.web3.eth.Contract(this.poolAbi, poolAddress),
this.config
)
let result = null
try {
result = await pool.methods.getMarketFee().call()
} catch (e) {
LoggerInstance.error(`ERROR: Failed to get getMarketFee: ${e.message}`)
}
return this.web3.utils.fromWei(result).toString()
}
/**
* Get marketFeeCollector of this pool
* @param {String} poolAddress
@ -673,17 +692,19 @@ export class Pool {
}
/**
* Estimate gas cost for updateMarketFeeCollector
* Estimate gas cost for updatePublishMarketFee
* @param {String} address
* @param {String} poolAddress
* @param {String} newCollector new market fee collector address
* @param {String} newPublishMarketAddress new market address
* @param {String} newPublishMarketSwapFee new market swap fee
* @param {Contract} contractInstance optional contract instance
* @return {Promise<number>}
*/
public async estUpdateMarketFeeCollector(
public async estUpdatePublishMarketFee(
address: string,
poolAddress: string,
newCollector: string,
newPublishMarketAddress: string,
newPublishMarketSwapFee: string,
contractInstance?: Contract
): Promise<number> {
const poolContract =
@ -697,7 +718,7 @@ export class Pool {
let estGas
try {
estGas = await poolContract.methods
.updateMarketFeeCollector(newCollector)
.updatePublishMarketFee(newPublishMarketAddress, newPublishMarketSwapFee)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
@ -706,16 +727,18 @@ export class Pool {
}
/**
* updateMarketFeeCollector - updates marketFeeCollector - can be called only by the marketFeeCollector
* updatePublishMarketFee - sets a new newPublishMarketAddress and new newPublishMarketSwapFee- can be called only by the marketFeeCollector
* @param {String} address
* @param {String} poolAddress
* @param {String} newCollector new market fee collector address
* @param {String} newPublishMarketAddress new market fee collector address
* @param {String} newPublishMarketSwapFee fee recieved by the publisher market when a dt is swaped from a pool, percent
* @return {TransactionReceipt}
*/
async updateMarketFeeCollector(
async updatePublishMarketFee(
address: string,
poolAddress: string,
newCollector: string
newPublishMarketAddress: string,
newPublishMarketSwapFee: string
): Promise<TransactionReceipt> {
if ((await this.getMarketFeeCollector(poolAddress)) !== address) {
throw new Error(`Caller is not MarketFeeCollector`)
@ -725,20 +748,26 @@ export class Pool {
this.config
)
let result = null
const estGas = await this.estUpdateMarketFeeCollector(
const estGas = await this.estUpdatePublishMarketFee(
address,
poolAddress,
newCollector
newPublishMarketAddress,
this.web3.utils.toWei(newPublishMarketSwapFee)
)
try {
result = await pool.methods.updateMarketFeeCollector(newCollector).send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3, this.config)
})
result = await pool.methods
.updatePublishMarketFee(
newPublishMarketAddress,
this.web3.utils.toWei(newPublishMarketSwapFee)
)
.send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3, this.config)
})
} catch (e) {
LoggerInstance.error(`ERROR: Failed to swap exact amount in : ${e.message}`)
LoggerInstance.error(`ERROR: Failed to updatePublishMarketFee : ${e.message}`)
}
return result
}

View File

@ -120,6 +120,8 @@ export class FixedRateExchange {
* @param {String} account
* @param {String} dtAmount datatoken amount we want to buy
* @param {String} datatokenAddress datatokenAddress
* @param {String} consumeMarketAddress consumeMarketAddress
* @param {String} consumeMarketFee fee recieved by the consume market when a dt is bought from a fixed rate exchange, percent
* @param {Contract} contractInstance optional contract instance
* @return {Promise<number>}
*/
@ -128,6 +130,8 @@ export class FixedRateExchange {
datatokenAddress: string,
dtAmount: string,
maxBaseTokenAmount: string,
consumeMarketAddress: string,
consumeMarketFee: string,
contractInstance?: Contract
): Promise<number> {
const fixedRate = contractInstance || this.fixedRateContract
@ -135,7 +139,13 @@ export class FixedRateExchange {
let estGas
try {
estGas = await fixedRate.methods
.buyDT(datatokenAddress, dtAmount.toString(), maxBaseTokenAmount.toString())
.buyDT(
datatokenAddress,
dtAmount,
maxBaseTokenAmount,
consumeMarketAddress,
consumeMarketFee
)
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
@ -149,14 +159,19 @@ export class FixedRateExchange {
* @param {String} datatokenAmount Amount of datatokens
* @param {String} maxBaseTokenAmount max amount of baseToken we want to pay for datatokenAmount
* @param {String} address User address
* @param {String} consumeMarketAddress consumeMarketAddress
* @param {String} consumeMarketFee consumeMarketFee
* @return {Promise<TransactionReceipt>} transaction receipt
*/
public async buyDT(
address: string,
exchangeId: string,
datatokenAmount: string,
maxBaseTokenAmount: string
maxBaseTokenAmount: string,
consumeMarketAddress: string = '0x0000000000000000000000000000000000000000',
consumeMarketFee: string = '0'
): Promise<TransactionReceipt> {
const consumeMarketFeeFormatted = await this.web3.utils.toWei(consumeMarketFee)
const dtAmountFormatted = await this.amountToUnits(
(
await this.getExchange(exchangeId)
@ -174,11 +189,19 @@ export class FixedRateExchange {
address,
exchangeId,
dtAmountFormatted,
maxBtFormatted
maxBtFormatted,
consumeMarketAddress,
consumeMarketFeeFormatted
)
try {
const trxReceipt = await this.contract.methods
.buyDT(exchangeId, dtAmountFormatted, maxBtFormatted)
.buyDT(
exchangeId,
dtAmountFormatted,
maxBtFormatted,
consumeMarketAddress,
consumeMarketFeeFormatted
)
.send({
from: address,
gas: estGas + 1,
@ -196,6 +219,8 @@ export class FixedRateExchange {
* @param {String} account
* @param {String} dtAmount datatoken amount we want to sell
* @param {String} datatokenAddress datatokenAddress
* @param {String} consumeMarketAddress consumeMarketAddress
* @param {String} consumeMarketFee consumeMarketFee
* @param {Contract} contractInstance optional contract instance
* @return {Promise<number>}
*/
@ -204,6 +229,8 @@ export class FixedRateExchange {
datatokenAddress: string,
dtAmount: string,
maxBaseTokenAmount: string,
consumeMarketAddress: string,
consumeMarketFee: string,
contractInstance?: Contract
): Promise<number> {
const fixedRate = contractInstance || this.fixedRateContract
@ -211,7 +238,13 @@ export class FixedRateExchange {
let estGas
try {
estGas = await fixedRate.methods
.sellDT(datatokenAddress, dtAmount, maxBaseTokenAmount)
.sellDT(
datatokenAddress,
dtAmount,
maxBaseTokenAmount,
consumeMarketAddress,
consumeMarketFee
)
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
@ -225,14 +258,19 @@ export class FixedRateExchange {
* @param {String} datatokenAmount Amount of datatokens
* @param {String} minBaseTokenAmount min amount of baseToken we want to receive back
* @param {String} address User address
* @param {String} consumeMarketAddress consumeMarketAddress
* @param {String} consumeMarketFee consumeMarketFee
* @return {Promise<TransactionReceipt>} transaction receipt
*/
public async sellDT(
address: string,
exchangeId: string,
datatokenAmount: string,
minBaseTokenAmount: string
minBaseTokenAmount: string,
consumeMarketAddress: string = '0x0000000000000000000000000000000000000000',
consumeMarketFee: string = '0'
): Promise<TransactionReceipt> {
const consumeMarketFeeFormatted = await this.web3.utils.toWei(consumeMarketFee)
const dtAmountFormatted = await this.amountToUnits(
(
await this.getExchange(exchangeId)
@ -249,11 +287,19 @@ export class FixedRateExchange {
address,
exchangeId,
dtAmountFormatted,
minBtFormatted
minBtFormatted,
consumeMarketAddress,
consumeMarketFeeFormatted
)
try {
const trxReceipt = await this.contract.methods
.sellDT(exchangeId, dtAmountFormatted, minBtFormatted)
.sellDT(
exchangeId,
dtAmountFormatted,
minBtFormatted,
consumeMarketAddress,
consumeMarketFeeFormatted
)
.send({
from: address,
gas: estGas + 1,
@ -533,17 +579,20 @@ export class FixedRateExchange {
* calcBaseInGivenOutDT - 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
* @return {Promise<PriceAndFees>} how many base tokens are needed and fees
*/
public async calcBaseInGivenOutDT(
exchangeId: string,
datatokenAmount: string
datatokenAmount: string,
consumeMarketFee: string = '0'
): Promise<PriceAndFees> {
const fixedRateExchange = await this.getExchange(exchangeId)
const result = await this.contract.methods
.calcBaseInGivenOutDT(
exchangeId,
await this.amountToUnits(fixedRateExchange.datatoken, datatokenAmount)
await this.amountToUnits(fixedRateExchange.datatoken, datatokenAmount),
this.web3.utils.toWei(consumeMarketFee)
)
.call()
@ -552,10 +601,6 @@ export class FixedRateExchange {
fixedRateExchange.baseToken,
result.baseTokenAmount
),
baseTokenAmountBeforeFee: await this.unitsToAmount(
fixedRateExchange.baseToken,
result.baseTokenAmountBeforeFee
),
marketFeeAmount: await this.unitsToAmount(
fixedRateExchange.baseToken,
result.marketFeeAmount
@ -563,6 +608,10 @@ export class FixedRateExchange {
oceanFeeAmount: await this.unitsToAmount(
fixedRateExchange.baseToken,
result.oceanFeeAmount
),
consumeMarketFeeAmount: await this.unitsToAmount(
fixedRateExchange.baseToken,
result.consumeMarketFeeAmount
)
} as PriceAndFees
return priceAndFees
@ -572,11 +621,13 @@ export class FixedRateExchange {
* getBTOut - returns amount in baseToken that user will receive for datatokenAmount sold
* @param {String} exchangeId ExchangeId
* @param {Number} datatokenAmount Amount of datatokens
* @param {String} consumeMarketFee consumeMarketFee
* @return {Promise<string>} Amount of baseTokens user will receive
*/
public async getAmountBTOut(
exchangeId: string,
datatokenAmount: string
datatokenAmount: string,
consumeMarketFee: string = '0'
): Promise<string> {
const result = await this.contract.methods
.calcBaseOutGivenInDT(
@ -586,7 +637,8 @@ export class FixedRateExchange {
await this.getExchange(exchangeId)
).datatoken,
datatokenAmount
)
),
this.web3.utils.toWei(consumeMarketFee)
)
.call()
@ -594,7 +646,7 @@ export class FixedRateExchange {
(
await this.getExchange(exchangeId)
).baseToken,
result.baseTokenAmount
result[0]
)
}

View File

@ -560,10 +560,14 @@ describe('Pool unit test', () => {
it('#updateMarketFeeCollector- should update market fee collector', async () => {
// contracts.accounts[0] is the marketFeeCollector
assert((await pool.getMarketFeeCollector(poolAddress)) === contracts.accounts[0])
await pool.updateMarketFeeCollector(contracts.accounts[0], poolAddress, user3)
await pool.updatePublishMarketFee(
contracts.accounts[0],
poolAddress,
user3,
await pool.getMarketFee(poolAddress)
)
assert((await pool.getMarketFeeCollector(poolAddress)) === user3)
})
})
@ -1079,7 +1083,12 @@ describe('Pool unit test', () => {
// contracts.accounts[0] is the marketFeeCollector
assert((await pool.getMarketFeeCollector(poolAddress)) === contracts.accounts[0])
await pool.updateMarketFeeCollector(contracts.accounts[0], poolAddress, user3)
await pool.updatePublishMarketFee(
contracts.accounts[0],
poolAddress,
user3,
await pool.getMarketFee(poolAddress)
)
assert((await pool.getMarketFeeCollector(poolAddress)) === user3)
})