Skip to content
This repository was archived by the owner on May 9, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions dist/services/pricer.service.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/services/pricer.service.js.map

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions src/services/pricer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,10 @@ export class Pricer {

try {
minProfitRateBaseline = (parseFloat(strategy.minProfitRate.toString()) * parseFloat(balance.toString())) / 100
// BigNumber fails for fixed point numbers
minProfitRateBaseline = Math.floor(minProfitRateBaseline)

maxProfitRateBaseline =
(parseFloat(strategy.maxShareOfMyBalancePerOrder.toString()) * parseFloat(balance.toString())) / 100
// BigNumber fails for fixed point numbers
minProfitRateBaseline = Math.floor(minProfitRateBaseline)
maxProfitRateBaseline = Math.ceil(maxProfitRateBaseline)

maxProfitRateBaselineBN = BigNumber.from(this.floatToBigIntString(maxProfitRateBaseline))
Expand Down Expand Up @@ -482,7 +481,8 @@ export class Pricer {
if (!this.ethersProvider && !fromChainProvider) {
throw new Error('No provider URL for the source network was provided.')
}
// Retrieve the pricing information for converting fromAsset to toAsset.
// FIXME: this (often?) returns zero when fromAsset=DOT, toAsset=TRN, making (most?) erc20-erc20 transfers not possible.
// But maybe it's only for certain networks.
const pricing = await this.retrieveAssetPricing(fromAsset, toAsset, fromChain, toChain)

// Convert the maxReward from its Wei representation to the equivalent amount in toAsset, considering the current market price.
Expand All @@ -492,7 +492,6 @@ export class Pricer {
this.ethersProvider = new ethers.providers.JsonRpcProvider(fromChainProvider)
}

// Estimate the gas price on the source network.
const estGasPriceOnNativeInWei = await this.ethersProvider.getGasPrice()

// Calculate the transaction cost in the fromAsset.
Expand Down
77 changes: 69 additions & 8 deletions test/services/pricer-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
CostResult,
networkNameCircuitToPriceProvider,
NetworkNameOnCircuit,
NetworkNameOnPriceProvider,
OrderArbitrageStrategy,
OverpayRatio,
Pricer,
Expand Down Expand Up @@ -519,13 +520,58 @@ describe('Pricer', () => {
priceBInUsd: '0.1',
}

// Evaluate the deal
const result = pricer.evaluateDeal(myBalance, costOfExecutionOnTarget, strategy, order, pricing)
// Assertions
expect(result.isProfitable).to.be.false
expect(result.profit.toString()).to.equal('0')
expect(result.loss.toString()).to.equal('97')
})

it.only('should evaluate a non-profitable deal from realistic values', () => {
const balance = BigNumber.from('26335629951215233423')
const costOnTarget = {
costInWei: BigNumber.from('63000010584000'),
costInEth: '0.000063000010584000',
costInUsd: 0.000063000010584,
costInAsset: BigNumber.from('20444946515'),
asset: 'eth',
}
const strategy = {
minProfitPerOrder: BigNumber.from('10000000000000'),
minProfitRate: 0.00001,
maxAmountPerOrder: BigNumber.from('1000000000000000000'),
minAmountPerOrder: BigNumber.from('100000000000000'),
maxShareOfMyBalancePerOrder: 25,
}
const order = {
id: '0x49415234575cd97e98be39fe4e61c787b19cb136482b1e0c7b02fd0b81b1c88d',
asset: 0,
assetAddress: '0x0000000000000000000000000000000000000000',
assetNative: true,
targetAccount: '0xc447247a786f6ff2e2e6e55d31214bafe2c630b4',
amount: BigNumber.from('238134552669580'),
rewardAsset: '0xfF3f18A66bB4Bd68079b93f70F7F7bE752986Ce8',
insurance: BigNumber.from('0'),
maxReward: BigNumber.from('733652187564595749'),
nonce: 8788344,
destination: 'opsp',
source: 'bssp',
txHash: '0xbd4e0c766ae689996292e89bd48031df39b2fbf563e612410c3009adbe2cf1d6',
} as Order
const pricing = {
assetA: 'eth',
assetB: 'trn',
priceAinB: BigNumber.from('3081446583312169400000'),
priceAInUsd: '3081.4465833121694',
priceBInUsd: '0',
}

const result = pricer.evaluateDeal(balance, costOnTarget, strategy, order, pricing)

expect(result.isProfitable).to.be.false
expect(result.profit.toString()).to.equal('0')
console.log(result.loss.toString())
// expect(result.loss.toString()).to.equal('97')
})
})

describe('assessDealForPublication', () => {
Expand Down Expand Up @@ -921,7 +967,6 @@ describe('Pricer', () => {
const toChain = 'eth'
const maxReward = ethers.utils.parseEther('1') // 1 ETH

// Mock the price fetch and gas estimation
pricer.receiveAssetPriceWithCache = async () => BigNumber.from(ethers.utils.parseUnits('1', 'ether')) // Simplified 1:1 conversion for simplicity
pricer.retrieveCostInAsset = async () => ({
costInWei: ethers.utils.parseUnits('21000', 'wei'), // Mocked gas used
Expand All @@ -931,7 +976,6 @@ describe('Pricer', () => {
asset: fromAsset,
})

// Action
const estimatedReceivedAmount = await pricer.estimateReceivedAmount(
fromAsset,
toAsset,
Expand All @@ -941,12 +985,10 @@ describe('Pricer', () => {
maxReward,
)

// Assert
expect(ethers.utils.formatEther(estimatedReceivedAmount)).to.equal('0.99')
})

it('should correctly estimate the received amount for different assets across chains', async () => {
// Setup
const fromAsset = SupportedAssetPriceProvider.ETH // Sending ETH
const toAsset = SupportedAssetPriceProvider.DOT // Receiving DOT
const fromChain = 'eth'
Expand All @@ -963,7 +1005,6 @@ describe('Pricer', () => {
asset: fromAsset,
})

// Action
const estimatedReceivedAmount = await pricer.estimateReceivedAmount(
fromAsset,
toAsset,
Expand All @@ -973,11 +1014,31 @@ describe('Pricer', () => {
maxReward,
)

// Assert
const expectedAmount = ethers.utils.parseUnits('0.99', 'ether')
expect(estimatedReceivedAmount.toString()).to.equal(expectedAmount.toString())
})

it.only('should correctly estimate received amount for a realistic scenario', async () => {
const fromAsset = SupportedAssetPriceProvider.DOT
const toAsset = SupportedAssetPriceProvider.TRN
const fromChain: NetworkNameOnPriceProvider = 'optimism'
const fromChainProvider =
'https://wandering-patient-patron.optimism-sepolia.quiknode.pro/007f4491c04820fa756d0539c4916579eec3d509/'
const toChain: NetworkNameOnPriceProvider = 'base'
const amountWei = BigNumber.from('239562101995192')
const maxRewardWei = BigNumber.from('289331314549183')

const estimatedReceivedAmount = await pricer.estimateReceivedAmount(
fromAsset,
toAsset,
fromChain,
fromChainProvider,
toChain,
amountWei,
)
console.log(estimatedReceivedAmount.toString())
})

// describe('conversion tests', function () {
// let pricer: Pricer
// let mockJsonRpcProvider: any
Expand Down