fix: use BigInt instead of number of token values (#17)

* Use BigInt instead of number of token values

* Remove useless triple =

* Update tests
This commit is contained in:
Arnaud 2025-05-30 16:12:29 +02:00 committed by GitHub
parent 59837ece13
commit 26a3caacc8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 70 additions and 38 deletions

View File

@ -33,16 +33,16 @@ describe("marketplace", async () => {
describe("create", async () => {
it("verifies that the availability was created successfully", async () => {
assert.ok(availability.id);
assert.strictEqual(availability.duration, duration);
assert.strictEqual(availability.freeSize, totalSize);
assert.strictEqual(
assert.equal(availability.duration, duration);
assert.equal(availability.freeSize, totalSize);
assert.equal(
availability.minPricePerBytePerSecond,
minPricePerBytePerSecond
BigInt(minPricePerBytePerSecond)
);
assert.strictEqual(availability.totalCollateral, totalCollateral);
assert.strictEqual(
assert.equal(availability.totalCollateral, BigInt(totalCollateral));
assert.equal(
availability.totalRemainingCollateral,
totalCollateral
BigInt(totalCollateral)
);
assert.strictEqual(availability.totalSize, totalSize);
assert.strictEqual(availability.until, 0);
@ -60,7 +60,7 @@ describe("marketplace", async () => {
const field = Object.keys(err)[0] as keyof typeof err;
assert.ok(field);
it(`fails to create availability with wrong ${field}`, async () => {
it(`fails to create availability with wrong ${field} = ${err[field]}`, async () => {
const response = await spMarketplace.createAvailability({
...body,
[field]: err[field],
@ -73,9 +73,7 @@ describe("marketplace", async () => {
response.data.errors[0]?.received,
err[field]?.toString()
);
assert.ok(
response.data.errors[0]?.message.startsWith("Invalid value:")
);
assert.ok(response.data.errors[0]?.message.startsWith("Invalid"));
});
}
});
@ -90,9 +88,9 @@ describe("marketplace", async () => {
const updates: Omit<CodexAvailabilityPatchInput, "id">[] = [
{ enabled: false },
{ duration: 3000 },
{ minPricePerBytePerSecond: 1 },
{ minPricePerBytePerSecond: BigInt(1) },
{ totalSize: 3000 },
{ totalCollateral: 3000 },
{ totalCollateral: BigInt(3000) },
{ until: 5000 },
];
@ -137,9 +135,7 @@ describe("marketplace", async () => {
response.data.errors[0]?.received,
err[field]?.toString()
);
assert.ok(
response.data.errors[0]?.message.startsWith("Invalid value:")
);
assert.ok(response.data.errors[0]?.message.startsWith("Invalid"));
});
}
});

View File

@ -78,9 +78,9 @@ export class CodexMarketplace {
}: CodexAvailabilityWithoutTypes) {
const availability: CodexAvailability = {
...a,
minPricePerBytePerSecond: parseInt(a.minPricePerBytePerSecond, 10),
totalCollateral: parseInt(a.totalCollateral, 10),
totalRemainingCollateral: parseInt(a.totalRemainingCollateral, 10),
minPricePerBytePerSecond: BigInt(a.minPricePerBytePerSecond),
totalCollateral: BigInt(a.totalCollateral),
totalRemainingCollateral: BigInt(a.totalRemainingCollateral),
};
if (freeSize) {

View File

@ -20,18 +20,14 @@ export type CodexAvailabilityWithoutTypes =
export type CodexAvailability = Omit<
CodexAvailabilityWithoutTypes,
| "freeSize"
| "totalSize"
| "minPricePerBytePerSecond"
| "duration"
| "totalCollateral"
| "totalRemainingCollateral"
> & {
freeSize?: number;
totalSize: number;
duration: number;
minPricePerBytePerSecond: number;
totalCollateral: number;
totalRemainingCollateral: number;
minPricePerBytePerSecond: BigInt;
totalCollateral: BigInt;
totalRemainingCollateral: BigInt;
};
export type CodexAvailabilityCreateResponse =
@ -45,8 +41,22 @@ export type CodexAvailabilityCreateBody = Exclude<
export const CodexCreateAvailabilityInput = v.strictObject({
totalSize: v.pipe(v.number(), v.minValue(1)),
duration: v.pipe(v.number(), v.minValue(1)),
minPricePerBytePerSecond: v.pipe(v.number(), v.minValue(0)),
totalCollateral: v.pipe(v.number(), v.minValue(0)),
minPricePerBytePerSecond: v.union([
v.pipe(v.bigint(), v.minValue(BigInt(0))),
v.pipe(
v.number(),
v.minValue(0),
v.transform((input) => BigInt(input))
),
]),
totalCollateral: v.union([
v.pipe(v.bigint(), v.minValue(BigInt(0))),
v.pipe(
v.number(),
v.minValue(0),
v.transform((input) => BigInt(input))
),
]),
enabled: v.optional(v.boolean()),
until: v.optional(v.pipe(v.number(), v.minValue(0))),
});
@ -61,23 +71,49 @@ export type CodexAvailabilityPatchBody = Partial<
>["content"]["application/json"]
>;
export type CodexCreateAvailabilityInput = v.InferOutput<
typeof CodexCreateAvailabilityInput
>;
export type CodexCreateAvailabilityInput = Omit<
v.InferOutput<typeof CodexCreateAvailabilityInput>,
"minPricePerBytePerSecond" | "totalCollateral"
> & {
minPricePerBytePerSecond?: number | BigInt;
totalCollateral?: number | BigInt;
};
export const CodexAvailabilityPatchInput = v.strictObject({
id: v.string(),
totalSize: v.optional(v.pipe(v.number(), v.minValue(1))),
duration: v.optional(v.pipe(v.number(), v.minValue(1))),
minPricePerBytePerSecond: v.optional(v.pipe(v.number(), v.minValue(1))),
totalCollateral: v.optional(v.pipe(v.number(), v.minValue(0))),
minPricePerBytePerSecond: v.optional(
v.union([
v.pipe(v.bigint(), v.minValue(BigInt(0))),
v.pipe(
v.number(),
v.minValue(0),
v.transform((input) => BigInt(input))
),
])
),
totalCollateral: v.optional(
v.union([
v.pipe(v.bigint(), v.minValue(BigInt(0))),
v.pipe(
v.number(),
v.minValue(0),
v.transform((input) => BigInt(input))
),
])
),
enabled: v.optional(v.boolean()),
until: v.optional(v.pipe(v.number(), v.minValue(0))),
});
export type CodexAvailabilityPatchInput = v.InferOutput<
typeof CodexAvailabilityPatchInput
>;
export type CodexAvailabilityPatchInput = Omit<
v.InferOutput<typeof CodexAvailabilityPatchInput>,
"minPricePerBytePerSecond" | "totalCollateral"
> & {
minPricePerBytePerSecond?: number | BigInt;
totalCollateral?: number | BigInt;
};
export type CodexReservationsResponse =
paths["/sales/availability/{id}/reservations"]["get"]["responses"][200]["content"]["application/json"];

View File

@ -57,7 +57,7 @@ export class CodexNode {
): Promise<SafeValue<CodexSpr<CodexSprContentType>>> {
const url = this.url + Api.config.prefix + "/spr";
if (type === "json") {
if (type == "json") {
return Fetch.safeJson<CodexSprJsonResponse>(url, {
method: "GET",
headers: {
@ -84,7 +84,7 @@ export class CodexNode {
): Promise<SafeValue<CodexPeerId<CodexPeerIdContentType>>> {
const url = this.url + Api.config.prefix + "/node/peerid";
if (type === "json") {
if (type == "json") {
return Fetch.safeJson<CodexPeerIdJsonResponse>(url, {
method: "GET",
headers: {