From 6064adf71b646ef54c2f129ccb920c710ca0df5f Mon Sep 17 00:00:00 2001 From: aeolian <94939382+aeolianeth@users.noreply.github.com> Date: Tue, 23 Jul 2024 07:58:15 +1000 Subject: [PATCH] fix: fix all bigint and number mismatches --- packages/core/package.json | 2 +- packages/core/src/constants.ts | 12 ++-- packages/core/src/types.ts | 57 +++---------------- packages/core/src/utils/data.test.ts | 4 +- packages/core/src/utils/data.ts | 14 ++--- packages/core/src/utils/ruleset.ts | 13 +++-- packages/core/src/utils/token.test.ts | 4 +- packages/core/src/utils/token.ts | 10 ++-- packages/react/package.json | 2 +- .../JBRulesetContext/JBRulesetContext.tsx | 6 +- 10 files changed, 41 insertions(+), 83 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index f13f2ec..f489a1e 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "juice-sdk-core", - "version": "10.0.0-alpha", + "version": "10.0.1-alpha", "type": "module", "scripts": { "generate": "wagmi generate", diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts index c047029..f14bd9f 100644 --- a/packages/core/src/constants.ts +++ b/packages/core/src/constants.ts @@ -22,31 +22,31 @@ export const DEFAULT_ALLOW_OVERSPENDING = true; * * @link JBConstants.sol */ -export const MAX_RESERVED_PERCENT = 10_000n; +export const MAX_RESERVED_PERCENT = 10_000; /** * The maximum value for a ruleset's Redemption Rate. * * @link JBConstants.sol */ -export const MAX_REDEMPTION_RATE = 10_000n; +export const MAX_REDEMPTION_RATE = 10_000; /** * The maximum value for a ruleset's Decay Rate. * * @link JBConstants.sol */ -export const MAX_DECAY_PERCENT = 1_000_000_000n; +export const MAX_DECAY_PERCENT = 1_000_000_000; /** * @link JBConstants.sol */ -export const MAX_FEE = 1_000_000_000n; +export const MAX_FEE = 1_000_000_000; /** * @link JBConstants.sol */ -export const MAX_FEE_DISCOUNT = 1_000_000_000n; +export const MAX_FEE_DISCOUNT = 1_000_000_000; /** * The 100% representation for a ruleset's Splits. @@ -55,7 +55,7 @@ export const MAX_FEE_DISCOUNT = 1_000_000_000n; * * @link JBConstants.sol */ -export const SPLITS_TOTAL_PERCENT = 1_000_000_000n; +export const SPLITS_TOTAL_PERCENT = 1_000_000_000; /** * The number of decimals that the internal JB Token has. diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 7f78fd5..6e29b39 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -1,10 +1,11 @@ -import { Address, ContractFunctionReturnType } from "viem"; -import { jbControllerAbi } from "./generated/juicebox.js"; +import { ContractFunctionReturnType } from "viem"; +import { jbControllerAbi, jbSplitsAbi } from "./generated/juicebox.js"; import { DecayPercent, RedemptionRate, ReservedPercent, RulesetWeight, + SplitPortion, } from "./utils/data.js"; export const projectTagOptions = [ @@ -87,54 +88,10 @@ export type JBProjectMetadata = { softTargetCurrency: string; }>; -/** - * A split of a project's payout funds to a beneficiary, project or allocator. - * - * @type - */ -export type JBSplit = { - /** - * The address of the beneficiary. - */ - beneficiary: Address; - /** - * The percentage of funds to send to the beneficiary. - */ - percent: number; - /** - * TODO: What is preferClaimed? - */ - preferClaimed: boolean; - /** - * The timestamp at which the split is locked until and cannot be changed. - */ - lockedUntil: number; - /** - * The ID of the project that is acting as the beneficiary. - */ - projectId: bigint; - /** - * The address of the allocator contract. - * - * If an allocator is specified, funds will be sent to the allocator contract - * along with the projectId, beneficiary, preferClaimed properties. - */ - allocator: Address; -}; - -/** - * Splits as they are given to transactions such as reconfigureRulesetsOf - * - * Used when interpreting data from Gnosis Safe transactions - */ -export type JBSplitParams = { - beneficiary: Address; - percent: bigint; - preferClaimed: boolean; - lockedUntil: number; - projectId: bigint; - allocator: Address; -}; +export type JBSplit = Omit< + ContractFunctionReturnType[number], + "percent" +> & { percent: SplitPortion }; /** * The type of split. diff --git a/packages/core/src/utils/data.test.ts b/packages/core/src/utils/data.test.ts index aaf6729..87b319f 100644 --- a/packages/core/src/utils/data.test.ts +++ b/packages/core/src/utils/data.test.ts @@ -3,7 +3,7 @@ import { DecayPercent, ReservedPercent } from "./data.js"; describe("jb", () => { test("reserved rate", () => { - const reservedPercentRaw = 2_345n; // 23.45% + const reservedPercentRaw = 2_345; // 23.45% const reservedPercent: ReservedPercent = new ReservedPercent(reservedPercentRaw); expect(reservedPercent.format()).toEqual("0.2345"); expect(reservedPercent.toFloat()).toEqual(0.2345); @@ -15,7 +15,7 @@ describe("jb", () => { }); test("decay rate", () => { - const decayPercentRaw = 200_000_000n; // 20% + const decayPercentRaw = 200_000_000; // 20% const decayPercent = new DecayPercent(decayPercentRaw); expect(decayPercent.format()).toEqual("0.2"); expect(decayPercent.toFloat()).toEqual(0.2); diff --git a/packages/core/src/utils/data.ts b/packages/core/src/utils/data.ts index 2b6c4f4..33f3da4 100644 --- a/packages/core/src/utils/data.ts +++ b/packages/core/src/utils/data.ts @@ -14,8 +14,8 @@ import { * @extends FixedPortion */ export class ReservedPercent extends FixedPortion<4> { - constructor(value: bigint) { - super(value, 4, MAX_RESERVED_PERCENT); + constructor(value: number) { + super(BigInt(value), 4, BigInt(MAX_RESERVED_PERCENT)); } } @@ -27,8 +27,8 @@ export class ReservedPercent extends FixedPortion<4> { * @extends FixedPortion */ export class RedemptionRate extends FixedPortion<4> { - constructor(value: bigint) { - super(value, 4, MAX_REDEMPTION_RATE); + constructor(value: number) { + super(BigInt(value), 4, BigInt(MAX_REDEMPTION_RATE)); } } @@ -40,14 +40,14 @@ export class RedemptionRate extends FixedPortion<4> { * @extends FixedPortion */ export class DecayPercent extends FixedPortion<9> { - constructor(value: bigint) { - super(value, 9, MAX_DECAY_PERCENT); + constructor(value: number) { + super(BigInt(value), 9, BigInt(MAX_DECAY_PERCENT)); } } export class SplitPortion extends FixedPortion<9> { constructor(value: bigint) { - super(value, 9, SPLITS_TOTAL_PERCENT); + super(BigInt(value), 9, BigInt(SPLITS_TOTAL_PERCENT)); } } diff --git a/packages/core/src/utils/ruleset.ts b/packages/core/src/utils/ruleset.ts index e5d7820..379a9d0 100644 --- a/packages/core/src/utils/ruleset.ts +++ b/packages/core/src/utils/ruleset.ts @@ -8,11 +8,12 @@ import { MAX_DECAY_PERCENT } from "../constants.js"; */ export function getNextRulesetWeight(currentRuleset: { weight: bigint; - decayPercent: bigint; + decayPercent: number; }) { const nextRulesetWeight = - (currentRuleset.weight * (MAX_DECAY_PERCENT - currentRuleset.decayPercent)) / - MAX_DECAY_PERCENT; + (currentRuleset.weight * + BigInt(MAX_DECAY_PERCENT - currentRuleset.decayPercent)) / + BigInt(MAX_DECAY_PERCENT); return nextRulesetWeight; } @@ -22,12 +23,12 @@ export function getNextRulesetWeight(currentRuleset: { */ export function getPrevRulesetWeight(currentRuleset: { weight: bigint; - decayPercent: bigint; + decayPercent: number; }) { // reverse of getNextRulesetWeight const prevRulesetWeight = - (currentRuleset.weight * MAX_DECAY_PERCENT) / - (MAX_DECAY_PERCENT - currentRuleset.decayPercent); + (currentRuleset.weight * BigInt(MAX_DECAY_PERCENT)) / + BigInt(MAX_DECAY_PERCENT - currentRuleset.decayPercent); return prevRulesetWeight; } diff --git a/packages/core/src/utils/token.test.ts b/packages/core/src/utils/token.test.ts index 996e9a3..4972cdd 100644 --- a/packages/core/src/utils/token.test.ts +++ b/packages/core/src/utils/token.test.ts @@ -14,7 +14,7 @@ describe("token", () => { "should return the correct token B quote when amount is $tokenAAmount and decimals is $tokenADecimals", ({ tokenAAmount, tokenADecimals, expectedPayerTokens }) => { const weight = FixedInt.parse<18>("1", 18); // 1 token per USDC - const reservedPercent = new ReservedPercent(0n); + const reservedPercent = new ReservedPercent(0); const { payerTokens } = getTokenAToBQuote( new FixedInt(tokenAAmount, tokenADecimals), { @@ -36,7 +36,7 @@ describe("token", () => { `( "should return the correct token A quote when amount is $tokenBAmount and decimals is $tokenADecimals", ({ tokenADecimals, expectedTokenBPrice, weight }) => { - const reservedPercent = new ReservedPercent(0n); + const reservedPercent = new ReservedPercent(0); const tokenBPrice = getTokenBPrice(tokenADecimals, { weight, reservedPercent, diff --git a/packages/core/src/utils/token.ts b/packages/core/src/utils/token.ts index 457b11f..ceae81e 100644 --- a/packages/core/src/utils/token.ts +++ b/packages/core/src/utils/token.ts @@ -30,7 +30,7 @@ export const getTokenAToBQuote = ( const totalTokens = (weight.value * tokenAAmount.value) / weightRatio; const reservedTokens = (weight.value * reservedPercent.value * tokenAAmount.value) / - MAX_RESERVED_PERCENT / + BigInt(MAX_RESERVED_PERCENT) / weightRatio; const payerTokens = totalTokens - reservedTokens; @@ -109,7 +109,7 @@ export const getTokenRedemptionQuoteEth = ( }: { overflowWei: bigint; totalSupply: bigint; - redemptionRate: bigint; + redemptionRate: number; tokensReserved: bigint; } ) => { @@ -122,12 +122,12 @@ export const getTokenRedemptionQuoteEth = ( if (redemptionRate === MAX_REDEMPTION_RATE) return base; const frac = - (tokensAmount * (MAX_REDEMPTION_RATE - redemptionRate)) / realTotalSupply; + (tokensAmount * BigInt(MAX_REDEMPTION_RATE - redemptionRate)) / realTotalSupply; // numerator = r + (x(1 - r)/s) - const numerator = redemptionRate + frac; + const numerator = BigInt(redemptionRate) + frac; // y = base * numerator ==> ox/s * ( r + (x(1 - r)/s) ) - const y = (base * numerator) / MAX_REDEMPTION_RATE; + const y = (base * numerator) / BigInt(MAX_REDEMPTION_RATE); return y / 10n; }; diff --git a/packages/react/package.json b/packages/react/package.json index a7422d6..81f9054 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -1,6 +1,6 @@ { "name": "juice-sdk-react", - "version": "10.0.0-alpha", + "version": "10.0.1-alpha", "description": "Wagmi hooks for Juicebox V4", "type": "module", "scripts": { diff --git a/packages/react/src/contexts/JBRulesetContext/JBRulesetContext.tsx b/packages/react/src/contexts/JBRulesetContext/JBRulesetContext.tsx index 513dd07..2c16ceb 100644 --- a/packages/react/src/contexts/JBRulesetContext/JBRulesetContext.tsx +++ b/packages/react/src/contexts/JBRulesetContext/JBRulesetContext.tsx @@ -100,12 +100,12 @@ export const JBRulesetProvider = ({ data: { ...ruleset, weight: new RulesetWeight(ruleset.weight), - decayPercent: new DecayPercent(BigInt(ruleset.decayPercent)), + decayPercent: new DecayPercent(ruleset.decayPercent), }, metadata: { ...rulesetMetadata, - redemptionRate: new RedemptionRate(BigInt(rulesetMetadata.redemptionRate)), - reservedPercent: new ReservedPercent(BigInt(rulesetMetadata.reservedPercent)) + redemptionRate: new RedemptionRate(rulesetMetadata.redemptionRate), + reservedPercent: new ReservedPercent(rulesetMetadata.reservedPercent) }, }; },