Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: kandel reverve balance #135

Merged
merged 1 commit into from
Sep 16, 2024
Merged
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
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
Loading