From 99d23df12261b515e7a17fbf2422dbfef37505a8 Mon Sep 17 00:00:00 2001 From: daniel <4954577+jaensen@users.noreply.github.com> Date: Sun, 13 Oct 2024 18:16:52 +0200 Subject: [PATCH] fix: circles/static circles conversion --- packages/data/src/circlesData.ts | 18 ++++++++----- packages/utils/src/index.ts | 43 +++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/packages/data/src/circlesData.ts b/packages/data/src/circlesData.ts index 524fbec..97d93c0 100644 --- a/packages/data/src/circlesData.ts +++ b/packages/data/src/circlesData.ts @@ -4,7 +4,13 @@ import {TrustListRow} from './rows/trustListRow'; import {TokenBalanceRow} from './rows/tokenBalanceRow'; import {CirclesRpc} from './circlesRpc'; import {AvatarRow} from './rows/avatarRow'; -import {crcToTc, hexStringToUint8Array, tcToCrc, uint8ArrayToCidV0} from '@circles-sdk/utils'; +import { + attoCirclesToStaticAttoCircles, + crcToTc, + hexStringToUint8Array, staticAttoCirclesToAttoCircles, + tcToCrc, + uint8ArrayToCidV0 +} from '@circles-sdk/utils'; import {ethers} from 'ethers'; import {TrustRelation, TrustRelationRow} from './rows/trustRelationRow'; import {CirclesDataInterface, GroupQueryParams} from './circlesDataInterface'; @@ -120,15 +126,15 @@ function calculateBalances(row: TransactionHistoryRow) { circles = crcToTc(new Date(), attoCrc); attoCircles = circlesToAttoCircles(circles); - staticCircles = crc * 3.0; - staticAttoCircles = attoCircles * 3n; + staticAttoCircles = attoCirclesToStaticAttoCircles(attoCircles); + staticCircles = attoCirclesToCircles(staticAttoCircles); } else { if (tokenInfo?.isInflationary) { staticAttoCircles = BigInt(rawBalance); staticCircles = attoCirclesToCircles(staticAttoCircles); - circles = crcToTc(new Date(), staticAttoCircles / 3n); - attoCircles = circlesToAttoCircles(circles); + attoCircles = staticAttoCirclesToAttoCircles(staticAttoCircles) ; + circles = attoCirclesToCircles(attoCircles); attoCrc = tcToCrc(new Date(), circles); crc = attoCirclesToCircles(attoCrc); @@ -139,7 +145,7 @@ function calculateBalances(row: TransactionHistoryRow) { attoCrc = tcToCrc(new Date(), circles); crc = attoCirclesToCircles(attoCrc); - staticAttoCircles = tcToCrc(new Date(), circles) * 3n; + staticAttoCircles = attoCirclesToStaticAttoCircles(attoCircles); staticCircles = attoCirclesToCircles(staticAttoCircles); } } diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index d26a78f..e4bde61 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,26 +1,13 @@ import { BigNumber } from 'bignumber.js'; import { ethers, parseEther } from 'ethers'; import multihash from 'multihashes'; - -/** - * Formats the token balance in time circles. - * @param tokenBalance The token balance as a BigNumber. - * @return The formatted token balance as a string. - */ -function formatTimeCircles(tokenBalance: BigNumber): string { - const ether = tokenBalance.dividedToIntegerBy(new BigNumber(10).pow(18)); - const remainder = tokenBalance.mod(new BigNumber(10).pow(18)); - let remainderString = remainder.toFixed(0).padStart(18, '0').replace(/0+$/, ''); - - return remainderString.length > 0 - ? `${ether.toString()}.${remainderString}` - : ether.toString(); -} +import { attoCirclesToCircles, circlesToAttoCircles } from '@circles-sdk/data'; const CirclesInceptionTimestamp = new Date('2020-10-15T00:00:00.000Z').getTime(); const OneDayInMilliseconds = new BigNumber(86400).multipliedBy(1000); const OneCirclesYearInDays = new BigNumber(365.25); const OneCirclesYearInMilliseconds = OneCirclesYearInDays.multipliedBy(24).multipliedBy(60).multipliedBy(60).multipliedBy(1000); +const Beta = 1.0001987074682146291562714890133039617432343970799554367508; function getCrcPayoutAt(timestamp: number): number { const daysSinceCirclesInception = new BigNumber(timestamp - CirclesInceptionTimestamp).dividedBy(OneDayInMilliseconds); @@ -216,4 +203,30 @@ export function parseError(errorData: string): ethers.ErrorDescription | null { } catch (error) { throw new Error(`Error decoding the revert data: ${error}. Original error data: ${errorData}`); } +} + +export function staticCirclesToCircles(value: number): number { + const lastUpdate = new Date(); + const lastUpdateDay = (lastUpdate.getTime() - 1609459200000) / 86400000; + const f = Math.pow(Beta, lastUpdateDay); + return value / f; +} + +export function staticAttoCirclesToAttoCircles(value: bigint): bigint { + const staticCircles = attoCirclesToCircles(value); + const circles = staticCirclesToCircles(staticCircles); + return circlesToAttoCircles(circles); +} + +export function circlesToStaticCircles(value: number): number { + const lastUpdate = new Date(); + const lastUpdateDay = (lastUpdate.getTime() - 1609459200000) / 86400000; + const f = Math.pow(Beta, lastUpdateDay); + return value * f; +} + +export function attoCirclesToStaticAttoCircles(value: bigint): bigint { + const circles = attoCirclesToCircles(value); + const staticCircles = circlesToStaticCircles(circles); + return circlesToAttoCircles(staticCircles); } \ No newline at end of file