Skip to content

Commit

Permalink
update utils
Browse files Browse the repository at this point in the history
  • Loading branch information
phn210 committed Oct 11, 2024
1 parent 62e311d commit 3c6b56a
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@auxo-dev/auxo-libs",
"version": "1.0.2",
"version": "1.0.3",
"description": "",
"author": "",
"license": "Apache-2.0",
Expand Down
8 changes: 4 additions & 4 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ type ZkApp = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
initArgs?: Record<string, any>;
actionStates: Field[];
actions: Field[][];
events: Field[][];
actionss: Field[][][];
eventss: Field[][][];
};

type FeePayer = {
Expand Down Expand Up @@ -134,8 +134,8 @@ type ZkAppOptions = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
initArgs?: Record<string, any>;
actionStates?: Field[];
actions?: Field[][];
events?: Field[][];
actionss?: Field[][][];
eventss?: Field[][][];
};

type UtilsOptions = {
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export {
packNumberArray,
unpackNumberArray,
buildAssertMessage,
checkInvalidAction,
requireSignature,
requireCaller,
checkCondition,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ function getZkApp(
name: options.name || 'Unknown',
initArgs: options.initArgs || {},
actionStates: options.actionStates || [Reducer.initialActionState],
actions: options.actions || [],
events: options.events || [],
actionss: options.actionss || [],
eventss: options.eventss || [],
};
}

Expand Down
37 changes: 37 additions & 0 deletions src/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Bool,
Cache,
Field,
Mina,
Expand Down Expand Up @@ -30,6 +31,7 @@ import {
import { FeePayer, TX_FEE, ZkApp } from './constants.js';
import { getProfiler } from './benchmark.js';
import { fromUInt64ToScalar } from './math.js';
import { checkInvalidAction } from './zkApp.js';

describe('Math', () => {
it('Should convert UInt64 to Scalar', async () => {
Expand Down Expand Up @@ -66,6 +68,17 @@ describe('Network', () => {
this.reducer.dispatch(value1);
this.emitEvent('test', value2);
}

@method
async checkAction(action: Field) {
let flag = Bool(false);
flag = checkInvalidAction(
flag,
action.equals(Field(1)),
'Test error'
);
Provable.log(flag);
}
}

const TestProgram = ZkProgram({
Expand Down Expand Up @@ -234,6 +247,30 @@ describe('Network', () => {
);
});

it('Should check and log invalid action', async () => {
await Mina.transaction(
{
sender: feePayer.sender.publicKey,
fee: feePayer.fee || TX_FEE,
memo: feePayer.memo,
nonce: feePayer.nonce,
},
async () =>
(testZkApp1.contract as TestContract).checkAction(Field(1))
);

await Mina.transaction(
{
sender: feePayer.sender.publicKey,
fee: feePayer.fee || TX_FEE,
memo: feePayer.memo,
nonce: feePayer.nonce,
},
async () =>
(testZkApp1.contract as TestContract).checkAction(Field(0))
);
});

it('Should fetch actions', async () => {
let actions = await fetchActions(
testZkApp1.key.publicKey,
Expand Down
14 changes: 12 additions & 2 deletions src/utils/zkApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export {
packNumberArray,
unpackNumberArray,
buildAssertMessage,
checkInvalidAction,
requireSignature,
requireCaller,
checkCondition,
Expand Down Expand Up @@ -45,9 +46,18 @@ function unpackNumberArray(packed: Field, maxSize: number): number[] {
function buildAssertMessage(
circuit: string,
method: string,
errorEnum: string
errorMsg: string
): string {
return `${circuit}::${method}: ${errorEnum}`;
return `${circuit}::${method}: ${errorMsg}`;
}

function checkInvalidAction(flag: Bool, check: Bool, message?: string) {
Provable.witness(Void, () => {
if (check.not().toBoolean()) {
console.info(message || 'Unknown error!');
}
});
return flag.or(check.not());
}

function requireSignature(address: PublicKey) {
Expand Down

0 comments on commit 3c6b56a

Please sign in to comment.