Skip to content

Commit

Permalink
update network utils and bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
phn210 committed Apr 10, 2024
1 parent a4d40d5 commit 105780b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 40 deletions.
30 changes: 15 additions & 15 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@auxo-dev/auxo-libs",
"version": "0.4.11",
"version": "0.5.0",
"description": "",
"author": "",
"license": "Apache-2.0",
Expand Down Expand Up @@ -59,6 +59,6 @@
"typescript": "^4.7.2"
},
"dependencies": {
"o1js": "./pkg/o1js-0.17.0.tgz"
"o1js": "0.18.0"
}
}
Binary file removed pkg/o1js-0.17.0.tgz
Binary file not shown.
5 changes: 3 additions & 2 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ type Profiler = {
};

type Logger = {
info: boolean;
memoryUsage: boolean;
info?: boolean;
error?: boolean;
memoryUsage?: boolean;
};

type Program = {
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export {
proveAndSendTx,
deployZkApps,
sendTx,
fetchNonce,
fetchActions,
fetchEvents,
fetchZkAppState,
Expand Down
11 changes: 6 additions & 5 deletions src/utils/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('Network', () => {
const cache = Cache.FileSystem('caches');
const logger = {
info: true,
error: true,
memoryUsage: true,
};
const profiler = getProfiler('Test', fs);
Expand Down Expand Up @@ -108,7 +109,7 @@ describe('Network', () => {
});

it('should compile contract', async () => {
if (doProofs) await compile(TestContract, cache, logger, profiler);
if (doProofs) await compile(TestContract, cache, profiler, logger);
});

it('should deploy zkApp', async () => {
Expand All @@ -128,8 +129,8 @@ describe('Network', () => {
TestContract.name,
'test',
(testZkApp.contract as TestContract).test(Field(1), Field(1)),
logger,
profiler
profiler,
logger
);
});

Expand All @@ -155,9 +156,9 @@ describe('Network', () => {
async () =>
(testZkApp.contract as TestContract).test(Field(1), Field(1)),
feePayer,
logger,
true,
profiler,
true
logger
);
});

Expand Down
38 changes: 22 additions & 16 deletions src/utils/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ function randomAccounts<K extends string>(
async function compile(
program: Program,
cache?: Cache,
logger?: Logger,
profiler?: Profiler
profiler?: Profiler,
logger?: Logger
): Promise<{
verificationKey: { data: string; hash: Field };
}> {
Expand All @@ -79,8 +79,8 @@ async function prove<T>(
programName: string,
methodName: string,
proofGeneration: Promise<T>,
logger?: Logger,
profiler?: Profiler
profiler?: Profiler,
logger?: Logger
): Promise<T> {
if (logger && logger.memoryUsage)
console.log('Current memory usage:', getMemoryUsage(), 'MB');
Expand All @@ -95,7 +95,8 @@ async function prove<T>(

async function sendTx(
tx: Transaction,
waitForBlock = false
waitForBlock = false,
logger?: Logger
): Promise<TxResult> {
let retries = MAX_RETRY;
let result;
Expand All @@ -106,27 +107,30 @@ async function sendTx(
if (waitForBlock)
return await (result as PendingTransaction).wait();
} else if (result.status === 'rejected') {
console.error('Transaction failed with errors:');
if (logger && logger.error)
console.error('Failed to send Tx with errors:');
throw result.errors;
}
return result;
} catch (error) {
retries--;
console.log('Transaction failed to be sent with error:', error);
console.log(`Retrying...`);
if (logger && logger.error) {
console.error('Failed to send Tx with errors:', error);
console.log(`Retrying...`);
}
}
}
throw new Error(`Transaction cannot be sent after ${MAX_RETRY} retries!`);
throw new Error(`Failed to send Tx after ${MAX_RETRY} retries!`);
}

async function proveAndSendTx(
contractName: string,
methodName: string,
functionCall: () => Promise<void>,
feePayer: FeePayer,
logger?: Logger,
waitForBlock = false,
profiler?: Profiler,
waitForBlock = false
logger?: Logger
) {
if (logger && logger.memoryUsage)
console.log('Current memory usage:', getMemoryUsage(), 'MB');
Expand Down Expand Up @@ -163,8 +167,8 @@ async function deployZkApps(
}
],
feePayer: FeePayer,
logger?: Logger,
waitForBlock = false
waitForBlock = false,
logger?: Logger
): Promise<TxResult> {
for (let i = 0; i < deployData.length; i++) {
if (deployData[i].zkApp.contract === undefined)
Expand Down Expand Up @@ -214,16 +218,18 @@ async function deployZkApps(
return result;
}

async function fetchNonce(publicKey: PublicKey): Promise<number | undefined> {
async function fetchNonce(
publicKey: PublicKey,
logger?: Logger
): Promise<number | undefined> {
try {
let { account, error } = await fetchAccount({
publicKey: publicKey,
});
if (account == undefined) throw error;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return Number(account.nonce);
} catch (error) {
console.error(error);
if (logger && logger.error) console.error(error);
}
}

Expand Down

0 comments on commit 105780b

Please sign in to comment.