Skip to content

Commit

Permalink
fix: fix all bigint and number mismatches
Browse files Browse the repository at this point in the history
  • Loading branch information
aeolianeth committed Jul 22, 2024
1 parent 6ce357c commit 6064adf
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 83 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "juice-sdk-core",
"version": "10.0.0-alpha",
"version": "10.0.1-alpha",
"type": "module",
"scripts": {
"generate": "wagmi generate",
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
57 changes: 7 additions & 50 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -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<typeof jbSplitsAbi, "view", "splitsOf">[number],
"percent"
> & { percent: SplitPortion };

/**
* The type of split.
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/utils/data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand Down
13 changes: 7 additions & 6 deletions packages/core/src/utils/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions packages/core/src/utils/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
{
Expand All @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/utils/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const getTokenAToBQuote = <D extends number>(
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;
Expand Down Expand Up @@ -109,7 +109,7 @@ export const getTokenRedemptionQuoteEth = (
}: {
overflowWei: bigint;
totalSupply: bigint;
redemptionRate: bigint;
redemptionRate: number;
tokensReserved: bigint;
}
) => {
Expand All @@ -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;
};
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
};
},
Expand Down

0 comments on commit 6064adf

Please sign in to comment.