Skip to content

Commit

Permalink
fix: boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
songwongtp committed Sep 26, 2023
1 parent 129a97c commit 745878b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
18 changes: 13 additions & 5 deletions src/lib/components/abi/args-form/field/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const validateUint = (uintType: string) => (v: string) => {
: `Input must be ‘${uintType}’`;
};
const validateBool = (v: string) =>
v === "true" || v === "false" ? undefined : "Input must be ‘boolean’";
v.toLowerCase() === "true" || v.toLowerCase() === "false"
? undefined
: "Input must be ‘boolean’";

const validateAddress =
(isValidArgAddress: (input: string) => boolean) => (v: string) =>
Expand All @@ -29,6 +31,9 @@ const validateVector = (
isValidArgAddress: (input: string) => boolean
) => {
const value = v.trim();
if (!value.startsWith("[") || !value.endsWith("]"))
return "Input must be ‘vector’";

const [, elementType] = vectorType.split(/<(.*)>/);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -41,10 +46,13 @@ const validateVector = (
// TODO: handle Vector?

let error: Option<string>;
value.split(",").forEach((elementValue) => {
const res = validateElement(elementValue.trim());
if (res !== undefined) error = `Invalid element: ${res}`;
});
value
.slice(1, -1)
.split(",")
.forEach((elementValue) => {
const res = validateElement(elementValue.trim());
if (res !== undefined) error = `Invalid element: ${res}`;
});

return error;
};
Expand Down
8 changes: 4 additions & 4 deletions src/lib/pages/pools/data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Big } from "big.js";
import big from "big.js";

import { UPPERBOUND_COUNT } from "lib/data";
import type { Order_By } from "lib/gql/graphql";
import { useAssetInfos } from "lib/services/assetService";
import { usePoolByPoolId, usePoolListQuery } from "lib/services/poolService";
Expand Down Expand Up @@ -134,11 +135,10 @@ export const usePoolTxsCount = (
const loading = isLoading || txsIsLoading;
if (error && txs?.length === 0)
return { count: 0, countDisplay: "0", isLoading: loading };
const upperboundCount = 10000;
const showActualCount = data !== undefined && data <= upperboundCount;
const showActualCount = data !== undefined && data <= UPPERBOUND_COUNT;
return {
count: showActualCount ? data : upperboundCount,
countDisplay: showActualCount ? data.toString() : `${upperboundCount}+`,
count: showActualCount ? data : UPPERBOUND_COUNT,
countDisplay: showActualCount ? data.toString() : `${UPPERBOUND_COUNT}+`,
isLoading: loading,
};
};
29 changes: 19 additions & 10 deletions src/lib/utils/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,38 +66,47 @@ const getArgType = (argType: string) =>
.replaceAll("0x1::option::Option", "option");

const getArgValue = ({
// type,
type,
value,
}: {
// type: string;
type: string;
value: Nullable<string>;
}) => {
try {
if (value === null) return null;
// if (type.startsWith("vector")) return JSON.parse(value) as string[];
if (type.startsWith("vector")) {
const [, elementType] = type.split(/<(.*)>/);
const values = value
.split(/\[(.*)\]/)[1]
.split(",")
.map((element) => element.trim());
return elementType === "bool"
? values.map((element) => element.toLowerCase() === "true")
: values;
}
if (type === "bool") return value === "true";
return value.trim();
} catch (e) {
return "";
}
};

const serializeArg = (
arg: { type: string; value: Nullable<string> },
bcs: BCS
) => {
const BUFFER_SIZE = 1024 * 1024;
const bcs = BCS.getInstance();

const serializeArg = (arg: { type: string; value: Nullable<string> }) => {
try {
const argType = getArgType(arg.type);
const argValue = getArgValue(arg);
return bcs.serialize(argType, argValue, 1024 * 1024);
return bcs.serialize(argType, argValue, BUFFER_SIZE);
} catch (e) {
return "";
}
};

export const serializeAbiData = (fn: ExposedFunction, abiData: AbiFormData) => {
const bcs = BCS.getInstance();
const serializedArgs = fn.params.map((type, index) =>
serializeArg({ type, value: abiData.args[index] }, bcs)
serializeArg({ type, value: abiData.args[index] })
);
return {
typeArgs: fn.generic_type_params.map((_, index) => abiData.typeArgs[index]),
Expand Down

0 comments on commit 745878b

Please sign in to comment.