Skip to content

Commit aecfedc

Browse files
authored
Fixed parsing of buy and sell amounts in pricing (#326)
1 parent 6588d70 commit aecfedc

7 files changed

Lines changed: 45 additions & 31 deletions

File tree

docs/ACTIONS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ The mainnet `setPrices*` actions use `--amount` as the DEX swap amount when
1717
fetching the reference price quote. This is separate from `--buy-amount` and
1818
`--sell-amount`, which set the buy-side liquidity-asset and sell-side base-asset
1919
liquidity remaining on the Ethena, USDC, and WETH ARMs. If omitted, each limit is
20-
set to the maximum `uint128` value. Liquidity amounts are integer native token
21-
units (for example, `100000000` is 100 tokens for an asset with 6 decimals).
20+
set to the maximum `uint128` value. Liquidity amounts are token-denominated:
21+
`1` is one liquidity or base token, with the appropriate token decimals applied
22+
by the action.
2223

2324
`--buy-price` and `--sell-price` bypass DEX-derived pricing and set an exact
2425
pair. Both must be supplied together; `--amount` is not used in this mode.

src/js/tasks/actions/setPricesEthena.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ action({
3232
)
3333
.addOptionalParam(
3434
"buyAmount",
35-
"Liquidity asset amount remaining at the buy price for multi-base ARMs, as an integer in native token units.",
35+
"USDe remaining at the buy price, in token units (1 = 1 USDe).",
3636
undefined,
37-
types.string,
37+
types.float,
3838
)
3939
.addOptionalParam(
4040
"sellAmount",
41-
"Base asset amount remaining at the sell price for multi-base ARMs, as an integer in native token units.",
41+
"sUSDe remaining at the sell price, in token units (1 = 1 sUSDe).",
4242
undefined,
43-
types.string,
43+
types.float,
4444
)
4545
.addOptionalParam(
4646
"maxBuyPrice",

src/js/tasks/actions/setPricesUSDC.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ action({
3636
)
3737
.addOptionalParam(
3838
"buyAmount",
39-
"Liquidity asset amount remaining at the buy price for multi-base ARMs, as an integer in native token units.",
39+
"USDC remaining at the buy price, in token units (1 = 1 USDC).",
4040
undefined,
41-
types.string,
41+
types.float,
4242
)
4343
.addOptionalParam(
4444
"sellAmount",
45-
"Base asset amount remaining at the sell price for multi-base ARMs, as an integer in native token units.",
45+
"Base asset remaining at the sell price, in token units (1 = 1 token).",
4646
undefined,
47-
types.string,
47+
types.float,
4848
)
4949
.addOptionalParam(
5050
"maxBuyPrice",

src/js/tasks/actions/setPricesWETH.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ action({
5656
)
5757
.addOptionalParam(
5858
"buyAmount",
59-
"WETH remaining at the buy price, as an integer in native token units.",
59+
"WETH remaining at the buy price, in token units (1 = 1 WETH).",
6060
undefined,
61-
types.string,
61+
types.float,
6262
)
6363
.addOptionalParam(
6464
"sellAmount",
65-
"Base asset remaining at the sell price, as an integer in native token units.",
65+
"Base asset remaining at the sell price, in token units (1 = 1 token).",
6666
undefined,
67-
types.string,
67+
types.float,
6868
)
6969
.addOptionalParam(
7070
"maxBuyPrice",

src/js/tasks/armPrices.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ const setPrices = async (options) => {
8888
// Base asset decimals used to scale aggregator quote amounts. Legacy ARM
8989
// configs don't expose baseAssetDecimals as all their assets are 18 decimals.
9090
const baseDecimals = Number(config.baseAssetDecimals ?? 18);
91+
const liquidityDecimals = Number(
92+
await new Contract(
93+
liquidityAddress,
94+
["function decimals() view returns (uint8)"],
95+
signer,
96+
).decimals(),
97+
);
9198

9299
log(`Getting current ARM prices:`);
93100
log(`base asset : ${baseSymbol}`);
@@ -111,17 +118,6 @@ const setPrices = async (options) => {
111118
? 10n
112119
: 30n;
113120

114-
// The liquidity asset decimals are not in the base asset config so read
115-
// them on-chain. Can differ from the base asset decimals, eg an 18
116-
// decimals base asset over a 6 decimals USDC liquidity asset.
117-
const liquidityDecimals = Number(
118-
await new Contract(
119-
liquidityAddress,
120-
["function decimals() view returns (uint8)"],
121-
signer,
122-
).decimals(),
123-
);
124-
125121
// 2.1 Get reference prices
126122
let referencePrices;
127123
if (midPrice) {
@@ -361,8 +357,8 @@ const setPrices = async (options) => {
361357
const toleranceScaled = parseUnits(tolerance.toString(), 36 - 4);
362358
log(`tolerance : ${formatUnits(toleranceScaled, 32)} basis points`);
363359

364-
const targetBuyAmount = parseSwapCap(buyAmount);
365-
const targetSellAmount = parseSwapCap(sellAmount);
360+
const targetBuyAmount = parseSwapCap(buyAmount, liquidityDecimals);
361+
const targetSellAmount = parseSwapCap(sellAmount, baseDecimals);
366362
const swapCapsChanged = haveSwapCapsChanged(
367363
baseContext,
368364
targetBuyAmount,

src/js/utils/arm.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,10 @@ const getArmBaseSymbols = async ({ arm, armName, base, blockTag }) => {
136136
return [defaultBaseSymbol(armName)];
137137
};
138138

139-
const parseSwapCap = (amount) => {
139+
const parseSwapCap = (amount, decimals = 18) => {
140140
if (amount === undefined || amount === null) return MAX_SWAP_LIQUIDITY;
141141
if (typeof amount === "bigint") return amount;
142-
if (typeof amount === "number") return parseUnits(amount.toString(), 18);
143-
const value = amount.toString();
144-
return value.includes(".") ? parseUnits(value, 18) : BigInt(value);
142+
return parseUnits(amount.toString(), decimals);
145143
};
146144

147145
const toConfigObject = (config) => ({

test/js/armPrices.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
const assert = require("assert");
22

33
const { haveSwapCapsChanged } = require("../../src/js/utils/priceUpdate");
4+
const { parseSwapCap } = require("../../src/js/utils/arm");
5+
6+
assert.strictEqual(
7+
parseSwapCap("1", 18),
8+
10n ** 18n,
9+
"one 18-decimal token should not be parsed as one wei",
10+
);
11+
12+
assert.strictEqual(
13+
parseSwapCap("1", 6),
14+
10n ** 6n,
15+
"one 6-decimal token should use the token's native decimals",
16+
);
17+
18+
assert.strictEqual(
19+
parseSwapCap("1.5", 6),
20+
1_500_000n,
21+
"fractional token amounts should be supported",
22+
);
423

524
const multiBaseContext = (buyLiquidityRemaining, sellLiquidityRemaining) => ({
625
version: "multiBase",

0 commit comments

Comments
 (0)