Skip to content

Commit

Permalink
feat: kandel reverve balance (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxencerb authored Sep 16, 2024
1 parent bcadd89 commit d939aeb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-glasses-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": patch
---

Added base and quote reserve to kandel view function
21 changes: 21 additions & 0 deletions src/actions/kandel/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
offeredVolumeParams,
provisionOfParams,
quoteParams,
reserveBalanceParams,
tickSpacingParams,
} from '../../builder/kandel/view.js'
import {
Expand Down Expand Up @@ -59,6 +60,8 @@ export type GetKandelStateResult = {
pricePoints: number
quoteAmount: bigint
baseAmount: bigint
reserveBalanceQuote: bigint
reserveBalanceBase: bigint
unlockedProvision: bigint
totalProvision: bigint
kandelStatus: KandelStatus
Expand All @@ -77,6 +80,8 @@ type KandelInitCallResult = {
}
baseAmount: bigint
quoteAmount: bigint
reserveBalanceQuote: bigint
reserveBalanceBase: bigint
unlockedProvision: bigint
// mid price from the book
midPrice: number
Expand All @@ -99,6 +104,8 @@ async function kandelInitCall(
params,
_quoteAmount,
_baseAmount,
_reserveBalanceQuote,
_reserveBalanceBase,
_base,
_quote,
_tickSpacing,
Expand Down Expand Up @@ -140,6 +147,14 @@ async function kandelInitCall(
address: kandel,
...offeredVolumeParams(BA.asks),
},
{
address: kandel,
...reserveBalanceParams(BA.bids),
},
{
address: kandel,
...reserveBalanceParams(BA.asks),
},
{
address: kandel,
...baseParams,
Expand Down Expand Up @@ -187,6 +202,10 @@ async function kandelInitCall(
const baseAmount = _baseAmount.status === 'success' ? _baseAmount.result : 0n
const quoteAmount =
_quoteAmount.status === 'success' ? _quoteAmount.result : 0n
const reserveBalanceQuote =
_reserveBalanceQuote.status === 'success' ? _reserveBalanceQuote.result : 0n
const reserveBalanceBase =
_reserveBalanceBase.status === 'success' ? _reserveBalanceBase.result : 0n

const asks =
bestAsk.status === 'success'
Expand Down Expand Up @@ -231,6 +250,8 @@ async function kandelInitCall(
: { gasprice: 0, gasreq: 0, stepSize: 0, pricePoints: 0 },
baseAmount: reversed ? quoteAmount : baseAmount,
quoteAmount: reversed ? baseAmount : quoteAmount,
reserveBalanceQuote: reversed ? reserveBalanceBase : reserveBalanceQuote,
reserveBalanceBase: reversed ? reserveBalanceQuote : reserveBalanceBase,
reversed,
unlockedProvision:
unlockedProvision.status === 'success' ? unlockedProvision.result : 0n,
Expand Down
12 changes: 12 additions & 0 deletions src/builder/kandel/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const viewKandelABI = parseAbi([
olKeyABIRaw,
'function baseQuoteTickOffset() public view returns (uint)',
'function params() public view returns (Params memory)',
'function reserveBalance(uint8 ba) public view returns (uint balance)',
'function offeredVolume(uint8 ba) public view returns (uint volume)',
'function getOffer(uint8 ba, uint index) public view returns (uint offer)',
'function offerIdOfIndex(uint8 ba, uint index) public view returns (uint offerId)',
Expand Down Expand Up @@ -69,6 +70,17 @@ function parseBA(ba: BA) {
return ba === BA.bids ? 0 : 1
}

export function reserveBalanceParams(ba: BA) {
return {
abi: viewKandelABI,
functionName: 'reserveBalance',
args: [parseBA(ba)],
} satisfies Omit<
ContractFunctionParameters<typeof viewKandelABI, 'view', 'reserveBalance'>,
'address'
>
}

export function offeredVolumeParams(ba: BA) {
return {
abi: viewKandelABI,
Expand Down

0 comments on commit d939aeb

Please sign in to comment.