Skip to content

Commit

Permalink
tmp: rate conversion(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
norwnd committed Dec 1, 2024
1 parent 1891689 commit 2f75b77
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 113 deletions.
14 changes: 7 additions & 7 deletions client/webserver/site/src/js/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,24 @@ export function likelyTaker (ord: Order, rate: number): boolean {
return ord.rate > rate
}

const preparcelQuantity = (ord: Order, mkt?: Market, midGap?: number) => {
const preparcelQuantity = (ord: Order, mkt?: Market, midGapAtom?: number) => {
const qty = ord.qty - ord.filled
if (ord.type === OrderTypeLimit) return qty
if (ord.sell) return qty * ord.rate / RateEncodingFactor
const rate = midGap || mkt?.spot?.rate || 0
const rateAtom = midGapAtom || mkt?.spot?.rate || 0
// Caller should not call this for market orders without a mkt arg.
if (!mkt) return 0
// This is tricky. The server will use the mid-gap rate to convert the
// order qty. We don't have a mid-gap rate, only a spot rate.
if (rate && (mkt?.spot?.bookVolume || 0) > 0) return qty * RateEncodingFactor / rate
if (rateAtom && (mkt?.spot?.bookVolume || 0) > 0) return qty * RateEncodingFactor / rateAtom
return mkt.lotsize // server uses same fallback if book is empty
}

export function epochWeight (ord: Order, mkt: Market, midGap?: number) {
export function epochWeight (ord: Order, mkt: Market, midGapAtom?: number) {
if (ord.status !== StatusEpoch) return 0
const qty = preparcelQuantity(ord, mkt, midGap)
const rate = midGap || mkt.spot?.rate || 0
if (likelyTaker(ord, rate)) return qty * 2
const qty = preparcelQuantity(ord, mkt, midGapAtom)
const rateAtom = midGapAtom || mkt.spot?.rate || 0
if (likelyTaker(ord, rateAtom)) return qty * 2
return qty
}

Expand Down
8 changes: 7 additions & 1 deletion client/webserver/site/src/js/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function formatter (formatters: Record<string, Intl.NumberFormat>, min: number,
}

/*
* convertToConventional converts the value in atomic units to conventional
* convertToConventional converts coin value in atomic units to conventional
* units.
*/
function convertToConventional (v: number, unitInfo?: UnitInfo) {
Expand Down Expand Up @@ -386,6 +386,12 @@ export default class Doc {
* associated with the conventional unit's conversion factor.
*/
static formatFullPrecision (vAtomic: number, unitInfo?: UnitInfo): string {
// TODO
console.log('formatFullPrecision -> vAtomic:')
console.log(vAtomic)
console.log('formatFullPrecision -> unitInfo:')
console.log(unitInfo)

const [v, prec] = convertToConventional(vAtomic, unitInfo)
return fullPrecisionFormatter(prec).format(v)
}
Expand Down
Loading

0 comments on commit 2f75b77

Please sign in to comment.