Skip to content

Commit

Permalink
Adapt test vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
lmuntaner committed Feb 10, 2025
1 parent ffb11a0 commit f4e8305
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 44 deletions.
44 changes: 24 additions & 20 deletions scripts/test-vectors/20250205-vectors-new-decimals-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
defaultCaller,
formatTokenUlps,
splitAccount,
splitPrincipal,
writeToJson,
} from "./utils";

Expand Down Expand Up @@ -63,12 +62,9 @@ const createTestVector = (params: Params) => {
let outputTxType = isStakeNeuron
? "Stake Neuron"
: nonNullish(token)
? `Send ${token.tokenSymbol}`
: "Send Tokens";
const canisterIdOutputs = splitPrincipal(params.canisterId).map(
(data, i, elements) =>
`Canister Id [${i + 1}/${elements.length}] : ${data}`,
);
? `Send ${token.tokenSymbol}`
: "Send Tokens";
const canisterIdOutputs = [`Canister Id : ${params.canisterId.toText()}`];

const fromOutputs = splitAccount(
{
Expand All @@ -77,6 +73,7 @@ const createTestVector = (params: Params) => {
},
"From account",
);

const toOutputs = splitAccount(
{
owner: params.to.owner,
Expand All @@ -95,8 +92,8 @@ const createTestVector = (params: Params) => {
})
: BigInt(params.amount);
const paymentOutput = nonNullish(token)
? `Payment (${token.tokenSymbol}) : ${amountToken}`
: `Payment (Tokens) : ${amountToken}`;
? `Amount (${token.tokenSymbol}) : ${amountToken}`
: `Amount (Tokens) : ${amountToken}`;

// Do not show fee if it's not present in the request body.
// Except if it's ICP, in which case we always show the fee.
Expand All @@ -121,23 +118,30 @@ const createTestVector = (params: Params) => {
? `Memo : ${uint8ArrayToBigInt(params.memo).toString()}`
: "Memo : 0";

const output = [
outputTxType,
...(isICP ? [] : canisterIdOutputs),
...fromOutputs,
...(isStakeNeuron ? [] : toOutputs),
paymentOutput,
...(feeOutput !== undefined ? [feeOutput] : []),
memoOutput,
];
// An array of arrays because each array of message shoule have the same index.
const output = [[outputTxType]];
if (!isICP) {
output.push(canisterIdOutputs);
}
output.push(fromOutputs);
if (!isStakeNeuron) {
output.push(toOutputs);
}
output.push([paymentOutput]);
if (feeOutput !== undefined) {
output.push([feeOutput]);
}
output.push([memoOutput]);
return {
blob_candid: createBlob({
blob: createBlob({
arg: IDL.encode(transferFn.argTypes, [rawRequestBody]),
methodName: "icrc1_transfer",
canisterId: params.canisterId,
caller: params.owner,
}),
output: output.map((element, index) => `${index + 1} | ${element}`),
output: output.flatMap((elements, index) =>
elements.map((element) => `${index} | ${element}`),
),
isICP,
name: `ICRC1 Transfer ${
nonNullish(token)
Expand Down
70 changes: 46 additions & 24 deletions scripts/test-vectors/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,50 +114,72 @@ export const writeToJson = ({
};

export const splitPrincipal = (principal: Principal): string[] => {
const text = principal.toText();
const split = text.split("-");
return splitPrincipalText(principal.toText());
};

const splitPrincipalText = (principalText: string): string[] => {
const split = principalText.split("-");
const groupedInThrees = [];
for (let i = 0; i < split.length; i += 3) {
groupedInThrees.push(split.slice(i, i + 3).join("-"));
}
const lines = [];
for (let i = 0; i < groupedInThrees.length; i += 2) {
lines.push(groupedInThrees.slice(i, i + 2).join(" : "));
lines.push(groupedInThrees.slice(i, i + 2).join(" "));
}
return lines;
};

const splitText = (textToSplit: string): string[] => {
return (
textToSplit.match(/.{1,8}/g)?.reduce((acc, curr) => {
if (acc.length === 0) {
return [curr];
}
const lastItem = acc[acc.length - 1];
if (lastItem.length > 35) {
return [...acc, curr];
} else if (lastItem.length < 35) {
return [...acc.slice(0, -1), `${lastItem} ${curr}`];
}
return acc;
}, [] as string[]) ?? []
);
};

export const splitString = (
textToSplit: string,
screenText: string,
): string[] => {
return (
textToSplit
.match(/.{1,8}/g)
?.reduce((acc, curr) => {
if (acc.length === 0) {
return [curr];
}
const lastItem = acc[acc.length - 1];
if (lastItem.length > 18) {
return [...acc, curr];
} else if (lastItem.length < 18) {
return [...acc.slice(0, -1), `${lastItem} : ${curr}`];
}
return acc;
}, [] as string[])
?.map(
(data, i, elements) =>
`${screenText} [${i + 1}/${elements.length}] : ${data}`,
) || []
splitText(textToSplit).map(
(data, i, elements) =>
`${screenText} [${i + 1}/${elements.length}] ${data}`,
) || []
);
};

export const splitAccount = (
account: IcrcAccount,
screenText: string,
): string[] => {
return splitString(encodeIcrcAccount(account), screenText);
const accountText = encodeIcrcAccount(account);
const split = accountText.split(".");
if (split.length === 1) {
return splitPrincipalText(accountText).map(
(data, i, elements) =>
`${screenText} [${i + 1}/${elements.length}] : ${data}`,
);
}

const firstPart = split[0];
const lines = splitPrincipalText(firstPart);
const secondPart = split[1];
const secondPartSplits = splitText(`.${secondPart}`);
return [...lines, ...(secondPartSplits ?? [])].map(
(data, i, elements) =>
`${screenText} [${i + 1}/${elements.length}] : ${data}`,
);
};

export const permissionMapper: Record<SnsNeuronPermissionType, string> = {
Expand Down Expand Up @@ -220,8 +242,8 @@ export const formatTokenUlps = ({
converted < 0.01 && extraDetailForSmallAmount
? Math.max(countDecimals(converted), defaultDisplayedDecimals)
: detailed
? Math.min(countDecimals(converted), maxDisplayedDecimals)
: defaultDisplayedDecimals;
? Math.min(countDecimals(converted), maxDisplayedDecimals)
: defaultDisplayedDecimals;

const decimals =
detailed === "height_decimals" ? maxDisplayedDecimals : decimalsForAmount();
Expand Down

0 comments on commit f4e8305

Please sign in to comment.