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

View File

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

View File

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

View File

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