Skip to content

Commit

Permalink
Bug: correct fill rate in ER Limo simple searcher example (#2049)
Browse files Browse the repository at this point in the history
* correct fill rate, choose simpler option

* address comments, make python updates

* minor fixes

* fixed comment
  • Loading branch information
anihamde authored Oct 22, 2024
1 parent 0331d3c commit d1d73d4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion express_relay/sdk/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/express-relay-js",
"version": "0.12.0",
"version": "0.12.2",
"description": "Utilities for interacting with the express relay protocol",
"homepage": "https://github.com/pyth-network/pyth-crosschain/tree/main/express_relay/sdk/js",
"author": "Douro Labs",
Expand Down
16 changes: 11 additions & 5 deletions express_relay/sdk/js/src/examples/simpleSearcherLimo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,27 @@ class SimpleSearcherLimo {
const outputMintDecimals = await this.getMintDecimalsCached(
order.state.outputMint
);
const effectiveFillRate = Math.min(
this.fillRate,
(100 * order.state.remainingInputAmount.toNumber()) /
order.state.initialInputAmount.toNumber()
);
const inputAmountDecimals = new Decimal(
order.state.remainingInputAmount.toNumber()
order.state.initialInputAmount.toNumber()
)
.div(new Decimal(10).pow(inputMintDecimals))
.mul(this.fillRate)
.mul(effectiveFillRate)
.div(100);

const outputAmountDecimals = new Decimal(
order.state.expectedOutputAmount.toNumber()
)
.div(new Decimal(10).pow(outputMintDecimals))
.mul(this.fillRate)
.mul(effectiveFillRate)
.div(100);

console.log("Order address", order.address.toBase58());
console.log("Fill rate", this.fillRate);
console.log("Fill rate", effectiveFillRate);
console.log(
"Sell token",
order.state.inputMint.toBase58(),
Expand Down Expand Up @@ -218,7 +223,8 @@ const argv = yargs(hideBin(process.argv))
demandOption: true,
})
.option("fill-rate", {
description: "How much of the order to fill in percentage. Default is 100%",
description:
"How much of the initial order size to fill in percentage. Default is 100%",
type: "number",
default: 100,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,17 @@ async def assess_opportunity(self, opp: OpportunitySvm) -> BidSvm:
order: OrderStateAndAddress = {"address": opp.order_address, "state": opp.order}
input_mint_decimals = await self.get_mint_decimals(order["state"].input_mint)
output_mint_decimals = await self.get_mint_decimals(order["state"].output_mint)
input_amount_decimals = Decimal(
order["state"].remaining_input_amount
) / Decimal(10**input_mint_decimals)
effective_fill_rate = min(
self.fill_rate,
100
* order["state"].remaining_input_amount
/ order["state"].initial_input_amount,
)
input_amount_decimals = Decimal(order["state"].initial_input_amount) / Decimal(
10**input_mint_decimals
)
input_amount_decimals = (
input_amount_decimals * Decimal(self.fill_rate) / Decimal(100)
input_amount_decimals * Decimal(effective_fill_rate) / Decimal(100)
)
output_amount_decimals = Decimal(
order["state"].expected_output_amount
Expand All @@ -127,7 +133,9 @@ async def assess_opportunity(self, opp: OpportunitySvm) -> BidSvm:
self.private_key.pubkey(),
order,
input_amount_decimals,
output_amount_decimals,
input_mint_decimals,
output_mint_decimals,
self.svm_config["express_relay_program"],
)
router = self.limo_client.get_pda_authority(
Expand Down Expand Up @@ -215,7 +223,7 @@ async def main():
"--fill-rate",
type=int,
default=100,
help="How much of the order to fill in percentage. Default is 100%",
help="How much of the initial order size to fill in percentage. Default is 100%",
)

args = parser.parse_args()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

class TakeOrderArgs(typing.TypedDict):
input_amount: int
output_amount: int


layout = borsh.CStruct("input_amount" / borsh.U64)
layout = borsh.CStruct("input_amount" / borsh.U64, "output_amount" / borsh.U64)


class TakeOrderAccounts(typing.TypedDict):
Expand Down Expand Up @@ -90,6 +91,7 @@ def take_order(
encoded_args = layout.build(
{
"input_amount": args["input_amount"],
"output_amount": args["output_amount"],
}
)
data = identifier + encoded_args
Expand Down
11 changes: 9 additions & 2 deletions express_relay/sdk/python/express_relay/svm/limo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,20 @@ async def take_order_ix(
taker: Pubkey,
order: OrderStateAndAddress,
input_amount_decimals: Decimal,
output_amount_decimals: Decimal,
input_mint_decimals: int,
output_mint_decimals: int,
express_relay_program_id: Pubkey,
) -> List[Instruction]:
"""
Returns the instructions to fulfill an order as a taker.
Args:
taker: The taker's public key
order: The order to fulfill
input_amount_decimals: The amount of input tokens to take multiplied by 10 ** input_mint_decimals
input_amount_decimals: The amount of input tokens to take. Will be multiplied by 10 ** input_mint_decimals in this method.
output_amount_decimals: The amount of output tokens to provide. Will be multiplied by 10 ** output_mint_decimals in this method.
input_mint_decimals: input mint decimals (can be fetched via get_mint_decimals)
output_mint_decimals: output mint decimals (can be fetched via get_mint_decimals)
express_relay_program_id: Express relay program id
Returns:
Expand Down Expand Up @@ -260,7 +264,10 @@ async def take_order_ix(
TakeOrderArgs(
input_amount=int(
input_amount_decimals * (10**input_mint_decimals)
)
),
output_amount=int(
output_amount_decimals * (10**output_mint_decimals)
),
),
{
"taker": taker,
Expand Down
2 changes: 1 addition & 1 deletion express_relay/sdk/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "express-relay"
version = "0.10.1"
version = "0.12.2"
description = "Utilities for searchers and protocols to interact with the Express Relay protocol."
authors = ["dourolabs"]
license = "Apache-2.0"
Expand Down

0 comments on commit d1d73d4

Please sign in to comment.