Skip to content

Commit

Permalink
fix: circles/static circles conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jaensen committed Oct 13, 2024
1 parent 0fb1197 commit 99d23df
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
18 changes: 12 additions & 6 deletions packages/data/src/circlesData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
Expand Down
43 changes: 28 additions & 15 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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);
}

0 comments on commit 99d23df

Please sign in to comment.