From 35e720803cc13440787030ee98e0c9aa9e7e5a0f Mon Sep 17 00:00:00 2001 From: Mihajlo Pavlovic Date: Wed, 18 Dec 2024 14:43:06 +0100 Subject: [PATCH] Implement bid suggestion for backwards compatiblity --- .../bid-suggestion-http-api-controller-v0.js | 37 +++---------------- .../blockchain/blockchain-module-manager.js | 4 ++ .../blockchain/implementation/web3-service.js | 8 ++++ 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/controllers/http-api/v0/bid-suggestion-http-api-controller-v0.js b/src/controllers/http-api/v0/bid-suggestion-http-api-controller-v0.js index 56c93816a..3dbb8145b 100644 --- a/src/controllers/http-api/v0/bid-suggestion-http-api-controller-v0.js +++ b/src/controllers/http-api/v0/bid-suggestion-http-api-controller-v0.js @@ -1,43 +1,18 @@ import BaseController from '../base-http-api-controller.js'; -import { LOW_BID_SUGGESTION } from '../../../constants/constants.js'; class BidSuggestionController extends BaseController { constructor(ctx) { super(ctx); - this.repositoryModuleManager = ctx.repositoryModuleManager; - this.shardingTableService = ctx.shardingTableService; + this.blockchainModuleManager = ctx.blockchainModuleManager; } - // TODO: We should use new on chain calculation async handleRequest(req, res) { - if ((await this.repositoryModuleManager.getPeersCount(req.query.blockchain)) === 0) { - const message = `Unable to get bid suggestion. Empty sharding table for blockchain id: ${req.query.blockchain}`; - this.logger.error(message); - this.returnResponse(res, 406, { - code: 406, - message, - }); - return; - } - - let { bidSuggestionRange } = req.query; try { - if (!bidSuggestionRange) { - bidSuggestionRange = LOW_BID_SUGGESTION; - } - // TODO: This isn't backwards compatible - // const bidSuggestion = await this.shardingTableService.getBidSuggestion( - // blockchain, - // epochsNumber, - // assertionSize, - // contentAssetStorageAddress, - // firstAssertionId, - // hashFunctionId, - // proximityScoreFunctionsPairId, - // bidSuggestionRange, - // ); - - const bidSuggestion = {}; + const { blockchain, epochsNumber, assertionSize } = req.body; + const bidSuggestion = + (await this.blockchainModuleManager.getStakeWeightedAverageAsk(blockchain)) * + epochsNumber * + assertionSize; this.returnResponse(res, 200, { bidSuggestion }); } catch (error) { this.logger.error(`Unable to get bid suggestion. Error: ${error}`); diff --git a/src/modules/blockchain/blockchain-module-manager.js b/src/modules/blockchain/blockchain-module-manager.js index 044c5421d..489927d66 100644 --- a/src/modules/blockchain/blockchain-module-manager.js +++ b/src/modules/blockchain/blockchain-module-manager.js @@ -267,6 +267,10 @@ class BlockchainModuleManager extends BaseModuleManager { async signMessage(blockchain, messageHash) { return this.callImplementationFunction(blockchain, 'signMessage', [messageHash]); } + + async getStakeWeightedAverageAsk(blockchain) { + return this.callImplementationFunction(blockchain, 'getStakeWeightedAverageAsk', []); + } } export default BlockchainModuleManager; diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index 164ceac57..5aac3deef 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -1145,6 +1145,14 @@ class Web3Service { const wallet = this.getRandomOperationalWallet(); return wallet.signMessage(ethers.utils.arrayify(messageHash)); } + + async getStakeWeightedAverageAsk() { + return this.callContractFunction( + this.contracts.ShardingTableStorage, + 'getStakeWeightedAverageAsk', + [], + ); + } } export default Web3Service;