Skip to content

Commit feea178

Browse files
authored
Merge pull request #370 from proto-kit/feat/deployment-check
Added a function to SettlmentModule for querying the deployment status of the contracts settlement contracts.
2 parents 1448c8e + 26145ab commit feea178

File tree

2 files changed

+96
-7
lines changed

2 files changed

+96
-7
lines changed

packages/sequencer/src/settlement/SettlementModule.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ import {
99
SettlementSmartContractBase,
1010
DynamicBlockProof,
1111
} from "@proto-kit/protocol";
12-
import { AccountUpdate, Mina, PublicKey, TokenContract, TokenId } from "o1js";
12+
import {
13+
AccountUpdate,
14+
fetchAccount,
15+
Field,
16+
Mina,
17+
PublicKey,
18+
TokenContract,
19+
TokenId,
20+
} from "o1js";
1321
import { inject } from "tsyringe";
1422
import {
1523
EventEmitter,
@@ -351,4 +359,49 @@ export class SettlementModule
351359
).bridgeContractMina(),
352360
};
353361
}
362+
363+
public async checkDeployment(
364+
tokenBridges?: Array<{ address: PublicKey; tokenId: Field }>
365+
): Promise<void> {
366+
const contracts: Array<{ address: PublicKey; tokenId?: Field }> = [
367+
...this.getContractAddresses().map((addr) => ({ address: addr })),
368+
...(tokenBridges ?? []),
369+
];
370+
371+
const isLocal = this.baseLayer.isLocalBlockChain();
372+
const missing: Array<{ address: string; error: string }> = [];
373+
374+
await Promise.all(
375+
contracts.map(async ({ address, tokenId }) => {
376+
if (isLocal) {
377+
if (!Mina.hasAccount(address, tokenId)) {
378+
missing.push({
379+
address: address.toBase58(),
380+
error: "Not found on local chain",
381+
});
382+
}
383+
} else {
384+
const { account, error } = await fetchAccount({
385+
publicKey: address,
386+
tokenId,
387+
});
388+
if (account === null || account === undefined) {
389+
missing.push({
390+
address: address.toBase58(),
391+
error: error?.statusText ?? "Not found on chain",
392+
});
393+
}
394+
}
395+
})
396+
);
397+
398+
if (missing.length > 0) {
399+
const errorList = missing
400+
.map((m) => ` ${m.address}: ${m.error}`)
401+
.join("\n");
402+
throw new Error(`
403+
Missing contracts:\n${errorList}
404+
`);
405+
}
406+
}
354407
}

packages/sequencer/test/settlement/Settlement.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,20 @@ export const settlementTestFn = (
312312
let user0Nonce = 0;
313313
let acc0L2Nonce = 0;
314314

315+
it("should throw error", async () => {
316+
const deploymentPromise =
317+
tokenConfig === undefined
318+
? settlementModule.checkDeployment()
319+
: settlementModule.checkDeployment([
320+
{
321+
address: tokenBridgeKey.toPublicKey(),
322+
tokenId: tokenOwner!.deriveTokenId(),
323+
},
324+
]);
325+
326+
await expect(deploymentPromise).rejects.toThrow();
327+
});
328+
315329
it(
316330
"should deploy",
317331
async () => {
@@ -578,6 +592,12 @@ export const settlementTestFn = (
578592
.proveAndSendTransaction(tx, "included");
579593

580594
const actions = await Mina.fetchActions(dispatch.address);
595+
if (baseLayerConfig.network.type !== "local") {
596+
await fetchAccount({
597+
publicKey: tokenBridgeKey.toPublicKey(),
598+
tokenId: bridgedTokenId,
599+
});
600+
}
581601
const balanceDiff = bridge.account.balance
582602
.get()
583603
.sub(contractBalanceBefore);
@@ -684,12 +704,11 @@ export const settlementTestFn = (
684704

685705
expect(settlementResult.bridgeTransactions).toHaveLength(2);
686706

687-
if (baseLayerConfig.network.type !== "local") {
688-
await fetchAccount({
689-
publicKey: userKey.toPublicKey(),
690-
tokenId: bridgingContract.deriveTokenId(),
691-
});
692-
}
707+
await settlementModule.utils.fetchContractAccounts({
708+
address: userKey.toPublicKey(),
709+
tokenId: bridgingContract.deriveTokenId(),
710+
});
711+
693712
const account = Mina.getAccount(
694713
userKey.toPublicKey(),
695714
bridgingContract.deriveTokenId()
@@ -780,4 +799,21 @@ export const settlementTestFn = (
780799
},
781800
timeout
782801
);
802+
803+
it("should not throw error after settlement", async () => {
804+
expect.assertions(1);
805+
806+
// Obtain promise of deployment check
807+
const deploymentCheckPromise =
808+
tokenConfig === undefined
809+
? settlementModule.checkDeployment()
810+
: settlementModule.checkDeployment([
811+
{
812+
address: tokenBridgeKey.toPublicKey(),
813+
tokenId: tokenOwner!.deriveTokenId(),
814+
},
815+
]);
816+
817+
await expect(deploymentCheckPromise).resolves.toBeUndefined();
818+
});
783819
};

0 commit comments

Comments
 (0)