Skip to content

Commit

Permalink
Fix v0 bid suggestion to work with new contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihajlo-Pavlovic committed Dec 21, 2024
1 parent ea6d443 commit d30af8b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export const UINT40_MAX_BN = BigNumber.from(2).pow(40).sub(1);

export const UINT32_MAX_BN = BigNumber.from(2).pow(32).sub(1);

export const ONE_ETHER = BigNumber.from(1e18);

export const HASH_RING_SIZE = ethers.constants.MaxUint256;

export const STAKE_UINT256_MULTIPLIER_BN = UINT256_MAX_BN.div(500000000);
Expand Down Expand Up @@ -271,6 +273,7 @@ export const ABIs = {
ParanetsRegistry: require('dkg-evm-module/abi/ParanetsRegistry.json'),
ParanetKnowledgeAssetsRegistry: require('dkg-evm-module/abi/ParanetKnowledgeAssetsRegistry.json'),
Ask: require('dkg-evm-module/abi/Ask.json'),
Chronos: require('dkg-evm-module/abi/Chronos.json'),
};

export const CONTRACT_FUNCTION_PRIORITY = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { BigNumber } from 'ethers';
import BaseController from '../base-http-api-controller.js';
import { ONE_ETHER } from '../../../constants/constants.js';

class BidSuggestionController extends BaseController {
constructor(ctx) {
Expand All @@ -8,14 +10,25 @@ class BidSuggestionController extends BaseController {

async handleRequest(req, res) {
try {
const { blockchain, epochsNumber, assertionSize } = req.query;
const bidSuggestion = (
await this.blockchainModuleManager.getStakeWeightedAverageAsk(blockchain)
)
.mul(epochsNumber)
.mul(assertionSize);
const bidSuggestionString = bidSuggestion.toString();
this.returnResponse(res, 200, { bidSuggestion: bidSuggestionString });
const { blockchain, epochsNumber, assertionSize } = req.body;
const promises = [
this.blockchainModuleManager.getTimeUntilNextEpoch(blockchain),
this.blockchainModuleManager.getEpochLength(blockchain),
this.blockchainModuleManager.getStakeWeightedAverageAsk(blockchain),
];
const [timeUntilNextEpoch, epochLength, stakeWeightedAverageAsk] = await Promise.all(
promises,
);
const timeUntilNextEpochScaled = BigNumber.from(timeUntilNextEpoch)
.mul(ONE_ETHER)
.div(BigNumber.from(epochLength));
const epochsNumberScaled = BigNumber.from(epochsNumber).mul(ONE_ETHER);
const storageTime = timeUntilNextEpochScaled.add(epochsNumberScaled);
const bidSuggestion = BigNumber.from(stakeWeightedAverageAsk)
.mul(storageTime)
.mul(BigNumber.from(assertionSize))
.div(ONE_ETHER);
this.returnResponse(res, 200, { bidSuggestion: bidSuggestion.toString() });
} catch (error) {
this.logger.error(`Unable to get bid suggestion. Error: ${error}`);
this.returnResponse(res, 500, {
Expand Down
10 changes: 9 additions & 1 deletion src/modules/blockchain/blockchain-module-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,15 @@ class BlockchainModuleManager extends BaseModuleManager {
async getStakeWeightedAverageAsk(blockchain) {
return this.callImplementationFunction(blockchain, 'getStakeWeightedAverageAsk', []);
}


async getTimeUntilNextEpoch(blockchain) {
return this.callImplementationFunction(blockchain, 'getTimeUntilNextEpoch', []);
}

async getEpochLength(blockchain) {
return this.callImplementationFunction(blockchain, 'getEpochLength', []);
}

// SUPPORT FOR OLD CONTRACTS
async getLatestAssertionId(blockchain, assetContractAddress, tokenId) {
return this.callImplementationFunction(blockchain, 'getLatestAssertionId', [
Expand Down
8 changes: 8 additions & 0 deletions src/modules/blockchain/implementation/web3-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,14 @@ class Web3Service {
async getStakeWeightedAverageAsk() {
return this.callContractFunction(this.contracts.Ask, 'getStakeWeightedAverageAsk', []);
}

async getTimeUntilNextEpoch() {
return this.callContractFunction(this.contracts.Chronos, 'timeUntilNextEpoch', []);
}

async getEpochLength() {
return this.callContractFunction(this.contracts.Chronos, 'epochLength', []);
}
// SUPPORT FOR OLD CONTRACTS

async getLatestAssertionId(assetContractAddress, tokenId) {
Expand Down

0 comments on commit d30af8b

Please sign in to comment.