Skip to content

Commit

Permalink
feat: Add total provision to kandel view function.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxencerb committed Jun 25, 2024
1 parent 02a36de commit 4182fc0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-pens-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mangrovedao/mgv": patch
---

Added total provision to kandel view function
1 change: 1 addition & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,5 @@ export type {
GetKandelStateArgs,
GetKandelStateParams,
GetKandelStateResult,
KandelStatus,
} from './kandel/view.js'
10 changes: 6 additions & 4 deletions src/actions/kandel/view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('view kandel', () => {
pricePoints: 5n,
market: wethUSDC,
baseAmount: parseEther('1'),
quoteAmount: parseUnits('3000', 18),
quoteAmount: parseUnits('3000', 6),
stepSize: 1n,
gasreq: 350_000n,
factor: 3,
Expand All @@ -81,7 +81,8 @@ describe('view kandel', () => {

expect(isValid).toBe(true)

const kandel = await sowAndPopulate(params, minProvision)
const kandel = await sowAndPopulate(params, minProvision * 3n)

const kandelState = await getKandelState(
client,
actionParams,
Expand All @@ -94,14 +95,15 @@ describe('view kandel', () => {

expect(kandelState.baseQuoteTickOffset).toBe(841n)
expect(kandelState.baseAmount).toBe(parseEther('1'))
expect(kandelState.quoteAmount).toBe(parseUnits('3000', 18))
expect(kandelState.quoteAmount).toBe(parseUnits('3000', 6))
expect(kandelState.gasprice).toBe(0)
expect(kandelState.gasreq).toBe(350_000)
expect(kandelState.stepSize).toBe(1)
expect(kandelState.pricePoints).toBe(5)
expect(kandelState.asks.length).toBe(4)
expect(kandelState.bids.length).toBe(4)
expect(kandelState.unlockedProvision).toBe(0n)
expect(kandelState.unlockedProvision).toBe(minProvision * 2n)
expect(kandelState.totalProvision).toBe(minProvision * 3n)
expect(kandelState.kandelStatus).toBe(KandelStatus.Active)
})

Expand Down
15 changes: 13 additions & 2 deletions src/actions/kandel/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type GetKandelStateResult = {
quoteAmount: bigint
baseAmount: bigint
unlockedProvision: bigint
totalProvision: bigint
kandelStatus: KandelStatus
asks: OfferParsed[]
bids: OfferParsed[]
Expand Down Expand Up @@ -356,7 +357,6 @@ async function kandelBidsAndAsks(
],
})
for (let i = 0; i < bids.length && i < provisions.length; i++) {
if (bids[i]!.gives === 0n) continue
const value = provisions[i]
const provision = value?.status === 'success' ? value.result : 0n
bids[i]!.provision = provision
Expand All @@ -366,7 +366,6 @@ async function kandelBidsAndAsks(
i < asks.length && i + bids.length < provisions.length;
i++
) {
if (asks[i]!.gives === 0n) continue
const value = provisions[i + bids.length]
const provision = value?.status === 'success' ? value.result : 0n
asks[i]!.provision = provision
Expand Down Expand Up @@ -410,6 +409,17 @@ function kandelStatus(
return KandelStatus.OutOfRange
}

function getTotalProvision(
{ asks, bids }: GetKandelBidsAndAskResult,
unlockedProvision: bigint,
): bigint {
return (
unlockedProvision +
asks.reduce((acc, ask) => acc + ask.provision, 0n) +
bids.reduce((acc, bid) => acc + bid.provision, 0n)
)
}

export async function getKandelState(
client: Client,
actionsParams: MangroveActionsDefaultParams,
Expand All @@ -433,5 +443,6 @@ export async function getKandelState(
unlockedProvision,
kandelStatus: kandelStatus(result, unlockedProvision, midPrice),
reversed,
totalProvision: getTotalProvision(result, unlockedProvision),
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type {
GetKandelStateArgs,
GetKandelStateParams,
GetKandelStateResult,
KandelStatus,
} from './actions/index.js'

// -- lib functions --
Expand Down

0 comments on commit 4182fc0

Please sign in to comment.