Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prototype localnet #202

Merged
merged 62 commits into from
Jul 4, 2024
Merged

feat: prototype localnet #202

merged 62 commits into from
Jul 4, 2024

Conversation

skosito
Copy link
Contributor

@skosito skosito commented Jul 1, 2024

start local hardhat node and worker which deploys evm/zevm contracts and listen to Call/WithdrawAndCall/Deposit events

yarn localnet

also, adding 4 hardhat tasks to interact with localnet (we can add more in follow up PRs if needed):

npx hardhat zevm-call --network localhost
npx hardhat zevm-withdraw-and-call --network localhost
npx hardhat evm-call --network localhost
npx hardhat evm-deposit-and-call --network localhost

Summary by CodeRabbit

  • New Features

    • Introduced tasks enabling interaction with prototype contracts on a local network, facilitating cross-environment contract calls and asset management.
    • Added a worker script for orchestrating contract deployment, event handling, and communication in EVM and ZEVM environments.
  • Documentation

    • Added a README for the worker script, outlining its functionality and usage.

Copy link
Contributor

coderabbitai bot commented Jul 1, 2024

Walkthrough

The changes involve refactoring Sender to SenderZEVM in the ZEVM contract to create consistency with the ZEVM environment, adding new tasks in localnet.ts for interacting with EVM and ZEVM contracts, and introducing a worker script (worker.ts) to deploy and interact across environments. A new scripts/readme.md file documents the worker script functionalities.

Changes

File(s) Change Summary
pkg/contracts/.../senderzevm.go Refactor: Renamed entities related to Sender contract to SenderZEVM for consistent naming.
tasks/localnet.ts New Tasks: Added tasks for calling contracts and managing assets between ZEVM and EVM environments.
scripts/worker.ts New Script: Introduced worker script for deployment and interaction across EVM and ZEVM contracts.
scripts/readme.md Documentation: Added documentation for the worker script, outlining its purpose and usage.

Sequence Diagram(s)

sequenceDiagram
    participant Worker as worker.ts
    participant EVM as EVM
    participant ZEVM as ZEVM
    participant GatewayEVM as GatewayEVM
    participant GatewayZEVM as GatewayZEVM
    
    Worker->>EVM: Deploy ReceiverEVM Contract
    Worker->>ZEVM: Deploy SenderZEVM Contract
    
    loop Management
        note over Worker: Mint and Transfer Tokens
        Worker->>EVM: Mint ERC-20
        Worker->>ZEVM: Mint ZRC-20
    end
    
    loop Interactions
        note over Worker: Calls, Withdrawals, Deposits through Gateways
        Worker->>GatewayZEVM: Call ReceiverEVM
        GatewayZEVM->>EVM: Execute Call
        Worker->>GatewayEVM: Call SenderZEVM
        GatewayEVM->>ZEVM: Execute Call
    end
Loading

Poem

In a world of EVM and ZEVM blend,
We renamed contracts to better portend.
Tasks now call, withdraw, and send,
Local nets and workers, they extend.
With code aligned from start to end,
Deployments easier, changes mend.
🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov-commenter
Copy link

codecov-commenter commented Jul 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 59.25%. Comparing base (6bf41ee) to head (be1f176).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #202   +/-   ##
=======================================
  Coverage   59.25%   59.25%           
=======================================
  Files          31       31           
  Lines        1048     1048           
  Branches      262      217   -45     
=======================================
  Hits          621      621           
  Misses        427      427           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (3)
scripts/readme.md (3)

9-9: Possible missing article.

Consider adding "the" before "hardhat local node and worker script" for clarity.

- This will run hardhat local node and worker script, which will deploy all contracts, and listen and react to events, facilitating communication between contracts.
+ This will run the hardhat local node and worker script, which will deploy all contracts, and listen and react to events, facilitating communication between contracts.
Tools
LanguageTool

[uncategorized] ~9-~9: Possible missing article found.
Context: ...: yarn localnet This will run hardhat local node and worker script, which wil...

(AI_HYDRA_LEO_MISSING_THE)


10-10: Possible missing article.

Consider adding "the" before "same addresses" for clarity.

- To make use of default contract addresses on localnet, start localnet from scratch, so contracts are deployed on same addresses. Otherwise, provide custom addresses as tasks parameters.
+ To make use of the default contract addresses on localnet, start localnet from scratch, so contracts are deployed on the same addresses. Otherwise, provide custom addresses as tasks parameters.
Tools
LanguageTool

[uncategorized] ~10-~10: Possible missing article found.
Context: ...m scratch, so contracts are deployed on same addresses. Otherwise, provide custom ad...

(AI_HYDRA_LEO_MISSING_THE)


6-6: Specify language for fenced code block.

For better syntax highlighting, specify the language for the code block.

- ```
+ ```bash
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4275805 and e62b051.

Files selected for processing (3)
  • scripts/readme.md (1 hunks)
  • scripts/worker.ts (1 hunks)
  • tasks/localnet.ts (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • scripts/worker.ts
  • tasks/localnet.ts
Additional context used
Path-based instructions (1)
scripts/readme.md (1)

Pattern scripts/**: Review the Hardhat scripts for best practices.

LanguageTool
scripts/readme.md

[uncategorized] ~9-~9: Possible missing article found.
Context: ...: yarn localnet This will run hardhat local node and worker script, which wil...

(AI_HYDRA_LEO_MISSING_THE)


[uncategorized] ~10-~10: Possible missing article found.
Context: ...m scratch, so contracts are deployed on same addresses. Otherwise, provide custom ad...

(AI_HYDRA_LEO_MISSING_THE)

Markdownlint
scripts/readme.md

5-5: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)

@lumtis
Copy link
Member

lumtis commented Jul 4, 2024

I get this error when starting localnet:

[NODE] <--- JS stacktrace --->
[NODE]
[NODE] FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
[NODE]  1: 0x1026f2984 node::Abort() [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  2: 0x1026f2b74 node::ModifyCodeGenerationFromStrings(v8::Local<v8::Context>, v8::Local<v8::Value>, bool) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  3: 0x1028356e8 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  4: 0x1028356ac v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  5: 0x1029b74d4 v8::internal::Heap::GarbageCollectionReasonToString(v8::internal::GarbageCollectionReason) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  6: 0x1029b5f70 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  7: 0x1029c173c v8::internal::Heap::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  8: 0x1029c17d0 v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  9: 0x102993e10 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE] 10: 0x102cd96c8 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE] 11: 0x102fef8ec Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE] 12: 0x107eb3b70

As suggested on Internet, you can increase the memory for JS, just wondering why this is necessary? I'm gonna invetstigate further

export NODE_OPTIONS="--max-old-space-size=8192"

@fadeev
Copy link
Member

fadeev commented Jul 4, 2024

@lumtis I was getting the same error, then switched to nodejs v20 it's gone.

@skosito
Copy link
Contributor Author

skosito commented Jul 4, 2024

I get this error when starting localnet:

[NODE] <--- JS stacktrace --->
[NODE]
[NODE] FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
[NODE]  1: 0x1026f2984 node::Abort() [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  2: 0x1026f2b74 node::ModifyCodeGenerationFromStrings(v8::Local<v8::Context>, v8::Local<v8::Value>, bool) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  3: 0x1028356e8 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  4: 0x1028356ac v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  5: 0x1029b74d4 v8::internal::Heap::GarbageCollectionReasonToString(v8::internal::GarbageCollectionReason) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  6: 0x1029b5f70 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  7: 0x1029c173c v8::internal::Heap::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  8: 0x1029c17d0 v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE]  9: 0x102993e10 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE] 10: 0x102cd96c8 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE] 11: 0x102fef8ec Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit [/Users/lucas/.nvm/versions/node/v16.20.2/bin/node]
[NODE] 12: 0x107eb3b70

As suggested on Internet, you can increase the memory for JS, just wondering why this is necessary? I'm gonna invetstigate further

export NODE_OPTIONS="--max-old-space-size=8192"

thanks for checking, i didnt have this error locally

it could be that hardhat is spending a lot of resources, did you also try this https://hardhat.org/hardhat-runner/docs/troubleshooting/common-problems#out-of-memory-errors-when-compiling-large-projects

using different node version might also help as @fadeev suggested

Copy link
Member

@lumtis lumtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good.

Not blocking for this PR, but I would see two improvement for the developer.

  • Packaging the command to reduce the logs to what is essential to developer: showing account the two gateway addresses:
Local ZetaChain contracts environment deployed
Account 1: ...
...
Gateway EVM: 0x...
Gateway ZEVM: 0x...

Maybe could be done with some flags.

  • Even further to simplify dev development, shipping a Hardhat plugin for devs to use to integrate the gateway contracts and worker in their own localnet.
import "zeta-contracts-localnet";

function deployScript{
    // ...

    // Deploy ZetaChain contracts environment, return gateway addresses and start the work process in the background
    var gatewayZEVM, gatewayEVM = setupLocalZetaChain(...)

    // dev do their own deployment here with their contracts using gateways
    // ...
}

Not knowledgeable enough on hardhat to know if this is possible

These are not blocking for this PR, this is further work for devEx purposes

scripts/worker.ts Outdated Show resolved Hide resolved
scripts/worker.ts Outdated Show resolved Hide resolved
@lumtis
Copy link
Member

lumtis commented Jul 4, 2024

@fadeev I think this makes sense if you can review this one

@skosito
Copy link
Contributor Author

skosito commented Jul 4, 2024

This looks good.

Not blocking for this PR, but I would see two improvement for the developer.

  • Packaging the command to reduce the logs to what is essential to developer: showing account the two gateway addresses:
Local ZetaChain contracts environment deployed
Account 1: ...
...
Gateway EVM: 0x...
Gateway ZEVM: 0x...

Maybe could be done with some flags.

  • Even further to simplify dev development, shipping a Hardhat plugin for devs to use to integrate the gateway contracts and worker in their own localnet.
import "zeta-contracts-localnet";

function deployScript{
    // ...

    // Deploy ZetaChain contracts environment, return gateway addresses and start the work process in the background
    var gatewayZEVM, gatewayEVM = setupLocalZetaChain(...)

    // dev do their own deployment here with their contracts using gateways
    // ...
}

Not knowledgeable enough on hardhat to know if this is possible

These are not blocking for this PR, this is further work for devEx purposes

thanks for review, I fixed the comments, except hardhat plugin part, maybe we can discuss that and research options

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e62b051 and bb27a54.

Files selected for processing (2)
  • scripts/worker.ts (1 hunks)
  • tasks/localnet.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tasks/localnet.ts
Additional context used
Path-based instructions (1)
scripts/worker.ts (1)

Pattern scripts/**: Review the Hardhat scripts for best practices.

GitHub Check: lint
scripts/worker.ts

[failure] 11-11:
Insert ·


[failure] 40-40:
Expected object keys to be in ascending order. 'custody' should be before 'gatewayEVM'


[failure] 44-44:
Insert ;


[failure] 46-46:
Replace systemContracts,·ownerEVM:·SignerWithAddress,·ownerZEVM:·SignerWithAddress,·fungibleModuleSigner:·SignerWithAddress with ⏎··systemContracts,⏎··ownerEVM:·SignerWithAddress,⏎··ownerZEVM:·SignerWithAddress,⏎··fungibleModuleSigner:·SignerWithAddress⏎


[failure] 96-96:
Expected object keys to be in ascending order. 'ZRC20Contract' should be before 'senderZEVM'


[failure] 172-172:
Replace [systemContracts.gatewayZEVM.address,·fungibleModuleSigner.address,·1],·asset,·parseEther("0"),·receiver,·payload with ⏎··········[systemContracts.gatewayZEVM.address,·fungibleModuleSigner.address,·1],⏎··········asset,⏎··········parseEther("0"),⏎··········receiver,⏎··········payload⏎········

Additional comments not posted (1)
scripts/worker.ts (1)

184-191: LGTM!

The initialization code looks good to me.

gatewayZEVM,
systemContract,
};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add missing semicolon.

Add a semicolon at the end of the function for better readability.

}
+};
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
}
}
;
Tools
GitHub Check: lint

[failure] 44-44:
Insert ;

console.log("Worker: Calling TestZContract through GatewayZEVM...");
const executeTx = await systemContracts.gatewayZEVM
.connect(fungibleModuleSigner)
.execute([systemContracts.gatewayZEVM.address, fungibleModuleSigner.address, 1], asset, parseEther("0"), receiver, payload);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reformat function parameters.

Reformat the function parameters for better readability.

const executeTx = await systemContracts.gatewayZEVM
  .connect(fungibleModuleSigner)
  .execute(
    [systemContracts.gatewayZEVM.address, fungibleModuleSigner.address, 1],
    asset,
    parseEther("0"),
    receiver,
    payload
  );
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
.execute([systemContracts.gatewayZEVM.address, fungibleModuleSigner.address, 1], asset, parseEther("0"), receiver, payload);
.execute(
[systemContracts.gatewayZEVM.address, fungibleModuleSigner.address, 1],
asset,
parseEther("0"),
receiver,
payload
);
Tools
GitHub Check: lint

[failure] 172-172:
Replace [systemContracts.gatewayZEVM.address,·fungibleModuleSigner.address,·1],·asset,·parseEther("0"),·receiver,·payload with ⏎··········[systemContracts.gatewayZEVM.address,·fungibleModuleSigner.address,·1],⏎··········asset,⏎··········parseEther("0"),⏎··········receiver,⏎··········payload⏎········


export const FUNGIBLE_MODULE_ADDRESS = "0x735b14BB79463307AAcBED86DAf3322B1e6226aB";

const deploySystemContracts = async(tss: SignerWithAddress) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insert missing space.

Add a space after the import statement for better readability.

const deploySystemContracts = async(tss: SignerWithAddress) => {
+ const deploySystemContracts = async (tss: SignerWithAddress) => {
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const deploySystemContracts = async(tss: SignerWithAddress) => {
const deploySystemContracts = async (tss: SignerWithAddress) => {
Tools
GitHub Check: lint

[failure] 11-11:
Insert ·

return {
receiverEVM,
senderZEVM,
ZRC20Contract,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort object keys.

Sort the object keys in ascending order for better readability.

return {
  receiverEVM,
  senderZEVM,
  ZRC20Contract,
  testZContract,
};
+return {
+  receiverEVM,
+  senderZEVM,
+  testZContract,
+  ZRC20Contract,
+};

Committable suggestion was skipped due to low confidence.

Tools
GitHub Check: lint

[failure] 96-96:
Expected object keys to be in ascending order. 'ZRC20Contract' should be before 'senderZEVM'

};
}

const deployTestContracts = async (systemContracts, ownerEVM: SignerWithAddress, ownerZEVM: SignerWithAddress, fungibleModuleSigner: SignerWithAddress) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reformat function parameters.

Reformat the function parameters for better readability.

const deployTestContracts = async (
  systemContracts,
  ownerEVM: SignerWithAddress,
  ownerZEVM: SignerWithAddress,
  fungibleModuleSigner: SignerWithAddress
) => {
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const deployTestContracts = async (systemContracts, ownerEVM: SignerWithAddress, ownerZEVM: SignerWithAddress, fungibleModuleSigner: SignerWithAddress) => {
const deployTestContracts = async (
systemContracts,
ownerEVM: SignerWithAddress,
ownerZEVM: SignerWithAddress,
fungibleModuleSigner: SignerWithAddress
) => {
Tools
GitHub Check: lint

[failure] 46-46:
Replace systemContracts,·ownerEVM:·SignerWithAddress,·ownerZEVM:·SignerWithAddress,·fungibleModuleSigner:·SignerWithAddress with ⏎··systemContracts,⏎··ownerEVM:·SignerWithAddress,⏎··ownerZEVM:·SignerWithAddress,⏎··fungibleModuleSigner:·SignerWithAddress⏎

Comment on lines 11 to 44
const deploySystemContracts = async(tss: SignerWithAddress) => {
// Prepare EVM
// Deploy system contracts (gateway and custody)
const GatewayEVM = await ethers.getContractFactory("GatewayEVM");
const Custody = await ethers.getContractFactory("ERC20CustodyNew");

const gatewayEVM = await upgrades.deployProxy(GatewayEVM, [tss.address], {
initializer: "initialize",
kind: "uups",
});
console.log("GatewayEVM:", gatewayEVM.address);

const custody = await Custody.deploy(gatewayEVM.address);
await gatewayEVM.setCustody(custody.address);

// Prepare ZEVM
// Deploy system contracts (gateway and system)
const SystemContractFactory = await ethers.getContractFactory("SystemContractMock");
const systemContract = (await SystemContractFactory.deploy(AddressZero, AddressZero, AddressZero)) as SystemContract;

const GatewayZEVM = await ethers.getContractFactory("GatewayZEVM");
const gatewayZEVM = await upgrades.deployProxy(GatewayZEVM, [], {
initializer: "initialize",
kind: "uups",
});
console.log("GatewayZEVM:", gatewayZEVM.address);

return {
gatewayEVM,
custody,
gatewayZEVM,
systemContract,
};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve function structure and add comments.

Move deployment to a separate function and add comments to separate test contracts from system contracts.

// Separate deployment logic into a new function
const deploySystemContracts = async (tss: SignerWithAddress) => {
  // Prepare EVM
  // Deploy system contracts (gateway and custody)
  const GatewayEVM = await ethers.getContractFactory("GatewayEVM");
  const Custody = await ethers.getContractFactory("ERC20CustodyNew");

  const gatewayEVM = await upgrades.deployProxy(GatewayEVM, [tss.address], {
    initializer: "initialize",
    kind: "uups",
  });
  console.log("GatewayEVM:", gatewayEVM.address);

  const custody = await Custody.deploy(gatewayEVM.address);
  await gatewayEVM.setCustody(custody.address);

  // Prepare ZEVM
  // Deploy system contracts (gateway and system)
  const SystemContractFactory = await ethers.getContractFactory("SystemContractMock");
  const systemContract = (await SystemContractFactory.deploy(AddressZero, AddressZero, AddressZero)) as SystemContract;

  const GatewayZEVM = await ethers.getContractFactory("GatewayZEVM");
  const gatewayZEVM = await upgrades.deployProxy(GatewayZEVM, [], {
    initializer: "initialize",
    kind: "uups",
  });
  console.log("GatewayZEVM:", gatewayZEVM.address);

  return {
    gatewayEVM,
    custody,
    gatewayZEVM,
    systemContract,
  };
};

// Add comments to separate test contracts from system contracts
const deployTestContracts = async (systemContracts, ownerEVM: SignerWithAddress, ownerZEVM: SignerWithAddress, fungibleModuleSigner: SignerWithAddress) => {
  // Prepare EVM
  // Deploy test contracts (erc20, receiver) and mint funds to test accounts
  const TestERC20 = await ethers.getContractFactory("TestERC20");
  const ReceiverEVM = await ethers.getContractFactory("ReceiverEVM");

  const token = await TestERC20.deploy("Test Token", "TTK");
  const receiverEVM = await ReceiverEVM.deploy();
  await token.mint(ownerEVM.address, ethers.utils.parseEther("1000"));

  // Transfer some tokens to the custody contract
  await token.transfer(systemContracts.custody.address, ethers.utils.parseEther("500"));

  // Prepare ZEVM
  // Deploy test contracts (test zContract, zrc20, sender) and mint funds to test accounts
  const TestZContract = await ethers.getContractFactory("TestZContract");
  const testZContract = await TestZContract.deploy();

  const ZRC20Factory = await ethers.getContractFactory("ZRC20New");
  const ZRC20Contract = (await ZRC20Factory.connect(fungibleModuleSigner).deploy(
    "TOKEN",
    "TKN",
    18,
    1,
    1,
    0,
    systemContracts.systemContract.address,
    systemContracts.gatewayZEVM.address
  )) as ZRC20;

  await systemContracts.systemContract.setGasCoinZRC20(1, ZRC20Contract.address);
  await systemContracts.systemContract.setGasPrice(1, ZRC20Contract.address);
  await ZRC20Contract.connect(fungibleModuleSigner).deposit(ownerZEVM.address, parseEther("100"));
  await ZRC20Contract.connect(ownerZEVM).approve(systemContracts.gatewayZEVM.address, parseEther("100"));

  // Include abi of gatewayZEVM events, so hardhat can decode them automatically
  const senderArtifact = await hre.artifacts.readArtifact("SenderZEVM");
  const gatewayZEVMArtifact = await hre.artifacts.readArtifact("GatewayZEVM");
  const senderABI = [
    ...senderArtifact.abi,
    ...gatewayZEVMArtifact.abi.filter((f: ethers.utils.Fragment) => f.type === "event"),
  ];

  const SenderZEVM = new ethers.ContractFactory(senderABI, senderArtifact.bytecode, ownerZEVM);
  const senderZEVM = await SenderZEVM.deploy(systemContracts.gatewayZEVM.address);
  await ZRC20Contract.connect(fungibleModuleSigner).deposit(senderZEVM.address, parseEther("100"));

  return {
    receiverEVM,
    senderZEVM,
    ZRC20Contract,
    testZContract,
  };
};
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const deploySystemContracts = async(tss: SignerWithAddress) => {
// Prepare EVM
// Deploy system contracts (gateway and custody)
const GatewayEVM = await ethers.getContractFactory("GatewayEVM");
const Custody = await ethers.getContractFactory("ERC20CustodyNew");
const gatewayEVM = await upgrades.deployProxy(GatewayEVM, [tss.address], {
initializer: "initialize",
kind: "uups",
});
console.log("GatewayEVM:", gatewayEVM.address);
const custody = await Custody.deploy(gatewayEVM.address);
await gatewayEVM.setCustody(custody.address);
// Prepare ZEVM
// Deploy system contracts (gateway and system)
const SystemContractFactory = await ethers.getContractFactory("SystemContractMock");
const systemContract = (await SystemContractFactory.deploy(AddressZero, AddressZero, AddressZero)) as SystemContract;
const GatewayZEVM = await ethers.getContractFactory("GatewayZEVM");
const gatewayZEVM = await upgrades.deployProxy(GatewayZEVM, [], {
initializer: "initialize",
kind: "uups",
});
console.log("GatewayZEVM:", gatewayZEVM.address);
return {
gatewayEVM,
custody,
gatewayZEVM,
systemContract,
};
}
const deploySystemContracts = async (tss: SignerWithAddress) => {
// Prepare EVM
// Deploy system contracts (gateway and custody)
const GatewayEVM = await ethers.getContractFactory("GatewayEVM");
const Custody = await ethers.getContractFactory("ERC20CustodyNew");
const gatewayEVM = await upgrades.deployProxy(GatewayEVM, [tss.address], {
initializer: "initialize",
kind: "uups",
});
console.log("GatewayEVM:", gatewayEVM.address);
const custody = await Custody.deploy(gatewayEVM.address);
await gatewayEVM.setCustody(custody.address);
// Prepare ZEVM
// Deploy system contracts (gateway and system)
const SystemContractFactory = await ethers.getContractFactory("SystemContractMock");
const systemContract = (await SystemContractFactory.deploy(AddressZero, AddressZero, AddressZero)) as SystemContract;
const GatewayZEVM = await ethers.getContractFactory("GatewayZEVM");
const gatewayZEVM = await upgrades.deployProxy(GatewayZEVM, [], {
initializer: "initialize",
kind: "uups",
});
console.log("GatewayZEVM:", gatewayZEVM.address);
return {
gatewayEVM,
custody,
gatewayZEVM,
systemContract,
};
};
// Add comments to separate test contracts from system contracts
const deployTestContracts = async (systemContracts, ownerEVM: SignerWithAddress, ownerZEVM: SignerWithAddress, fungibleModuleSigner: SignerWithAddress) => {
// Prepare EVM
// Deploy test contracts (erc20, receiver) and mint funds to test accounts
const TestERC20 = await ethers.getContractFactory("TestERC20");
const ReceiverEVM = await ethers.getContractFactory("ReceiverEVM");
const token = await TestERC20.deploy("Test Token", "TTK");
const receiverEVM = await ReceiverEVM.deploy();
await token.mint(ownerEVM.address, ethers.utils.parseEther("1000"));
// Transfer some tokens to the custody contract
await token.transfer(systemContracts.custody.address, ethers.utils.parseEther("500"));
// Prepare ZEVM
// Deploy test contracts (test zContract, zrc20, sender) and mint funds to test accounts
const TestZContract = await ethers.getContractFactory("TestZContract");
const testZContract = await TestZContract.deploy();
const ZRC20Factory = await ethers.getContractFactory("ZRC20New");
const ZRC20Contract = (await ZRC20Factory.connect(fungibleModuleSigner).deploy(
"TOKEN",
"TKN",
18,
1,
1,
0,
systemContracts.systemContract.address,
systemContracts.gatewayZEVM.address
)) as ZRC20;
await systemContracts.systemContract.setGasCoinZRC20(1, ZRC20Contract.address);
await systemContracts.systemContract.setGasPrice(1, ZRC20Contract.address);
await ZRC20Contract.connect(fungibleModuleSigner).deposit(ownerZEVM.address, parseEther("100"));
await ZRC20Contract.connect(ownerZEVM).approve(systemContracts.gatewayZEVM.address, parseEther("100"));
// Include abi of gatewayZEVM events, so hardhat can decode them automatically
const senderArtifact = await hre.artifacts.readArtifact("SenderZEVM");
const gatewayZEVMArtifact = await hre.artifacts.readArtifact("GatewayZEVM");
const senderABI = [
...senderArtifact.abi,
...gatewayZEVMArtifact.abi.filter((f: ethers.utils.Fragment) => f.type === "event"),
];
const SenderZEVM = new ethers.ContractFactory(senderABI, senderArtifact.bytecode, ownerZEVM);
const senderZEVM = await SenderZEVM.deploy(systemContracts.gatewayZEVM.address);
await ZRC20Contract.connect(fungibleModuleSigner).deposit(senderZEVM.address, parseEther("100"));
return {
receiverEVM,
senderZEVM,
ZRC20Contract,
testZContract,
};
};
Tools
GitHub Check: lint

[failure] 11-11:
Insert ·


[failure] 40-40:
Expected object keys to be in ascending order. 'custody' should be before 'gatewayEVM'


[failure] 44-44:
Insert ;

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bb27a54 and d80596e.

Files selected for processing (1)
  • scripts/worker.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • scripts/worker.ts

@lumtis
Copy link
Member

lumtis commented Jul 4, 2024

thanks for review, I fixed the comments, except hardhat plugin part, maybe we can discuss that and research options

For the cleaned up log, is there maybe a solution, like a flag, to just show the WORKER logs?

@fadeev
Copy link
Member

fadeev commented Jul 4, 2024

This is really cool! A few thoughts.

Should this eventually move into a separate repo? Pros: we don't mix tools and protocol contracts, we don't have to explicitly exclude this code from the audit. Cons: slower iterations as protocol contracts live in one repo, localnet lives in a different repo.

Package localnet as a Hardhat task that can be imported either by the toolkit or directly by the template, so that it can be distributed a bit more easily.

Should we find a different name for this? This feels more like a simulation rather than a local network.

@fadeev
Copy link
Member

fadeev commented Jul 4, 2024

Would it make sense to start testing this with the examples we have in the example-contracts repo?

Copy link
Member

@fadeev fadeev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥🔥🔥

@skosito
Copy link
Contributor Author

skosito commented Jul 4, 2024

thanks for review, I fixed the comments, except hardhat plugin part, maybe we can discuss that and research options

For the cleaned up log, is there maybe a solution, like a flag, to just show the WORKER logs?

yes there is a way, i added to scripts/readme.md

@skosito
Copy link
Contributor Author

skosito commented Jul 4, 2024

This is really cool! A few thoughts.

Should this eventually move into a separate repo? Pros: we don't mix tools and protocol contracts, we don't have to explicitly exclude this code from the audit. Cons: slower iterations as protocol contracts live in one repo, localnet lives in a different repo.

Package localnet as a Hardhat task that can be imported either by the toolkit or directly by the template, so that it can be distributed a bit more easily.

Should we find a different name for this? This feels more like a simulation rather than a local network.

thanks for review - i would say currently this can help us speed up development as we work on v2 contracts so until its in development phase i would say it is beneficial to stay in same repo as it would evolve with contracts

later we can discuss moving it and packaging it differently

@lumtis what do you think?

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range and nitpick comments (7)
scripts/readme.md (7)

5-5: Specify the language for the code block.

Fenced code blocks should have a language specified for syntax highlighting.

-```
+yarn localnet
+```shell
Tools
Markdownlint

5-5: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


9-9: Add missing article.

There is a missing article before "Hardhat local node."

-This will run hardhat local node and worker script, which will deploy all contracts, and listen and react to events, facilitating communication between contracts.
+This will run the Hardhat local node and worker script, which will deploy all contracts, and listen and react to events, facilitating communication between contracts.
Tools
LanguageTool

[uncategorized] ~9-~9: Possible missing article found.
Context: ...: yarn localnet This will run hardhat local node and worker script, which wil...

(AI_HYDRA_LEO_MISSING_THE)


10-10: Add missing article.

There is a missing article before "same addresses."

-Tasks to interact with localnet are located in `tasks/localnet`. To make use of default contract addresses on localnet, start localnet from scratch, so contracts are deployed on same addresses. Otherwise, provide custom addresses as tasks parameters.
+Tasks to interact with localnet are located in `tasks/localnet`. To make use of default contract addresses on localnet, start localnet from scratch, so contracts are deployed on the same addresses. Otherwise, provide custom addresses as tasks parameters.
Tools
LanguageTool

[uncategorized] ~10-~10: Possible missing article found.
Context: ...m scratch, so contracts are deployed on same addresses. Otherwise, provide custom ad...

(AI_HYDRA_LEO_MISSING_THE)


12-12: Correct grammatical number.

The grammatical number of "tasks parameters" is incorrect.

-Otherwise, provide custom addresses as tasks parameters.
+Otherwise, provide custom addresses as task parameters.
Tools
LanguageTool

[uncategorized] ~12-~12: The grammatical number of this noun doesn’t look right. Consider replacing it.
Context: ...sks parameters. To only show logs from worker and hide hardhat node logs: ``` yarn lo...

(AI_EN_LECTOR_REPLACEMENT_NOUN_NUMBER)


13-13: Surround fenced code block with blank lines.

Fenced code blocks should be surrounded by blank lines for better readability.

-To only show logs from worker and hide hardhat node logs:
+To only show logs from the worker and hide Hardhat node logs:

```shell
yarn localnet --hide="NODE"

<details>
<summary>Tools</summary>

<details>
<summary>Markdownlint</summary><blockquote>

13-13: null
Fenced code blocks should be surrounded by blank lines

(MD031, blanks-around-fences)

---

13-13: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)

</blockquote></details>

</details>

---

`13-13`: **Specify the language for the code block.**

Fenced code blocks should have a language specified for syntax highlighting.

```diff
-```
+yarn localnet --hide="NODE"
+```shell
Tools
Markdownlint

13-13: null
Fenced code blocks should be surrounded by blank lines

(MD031, blanks-around-fences)


13-13: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


15-15: Add a trailing newline.

Files should end with a single newline character.

-```
+```
+
Tools
Markdownlint

15-15: null
Files should end with a single newline character

(MD047, single-trailing-newline)

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d80596e and be1f176.

Files selected for processing (1)
  • scripts/readme.md (1 hunks)
Additional context used
Path-based instructions (1)
scripts/readme.md (1)

Pattern scripts/**: Review the Hardhat scripts for best practices.

LanguageTool
scripts/readme.md

[uncategorized] ~9-~9: Possible missing article found.
Context: ...: yarn localnet This will run hardhat local node and worker script, which wil...

(AI_HYDRA_LEO_MISSING_THE)


[uncategorized] ~10-~10: Possible missing article found.
Context: ...m scratch, so contracts are deployed on same addresses. Otherwise, provide custom ad...

(AI_HYDRA_LEO_MISSING_THE)


[uncategorized] ~12-~12: The grammatical number of this noun doesn’t look right. Consider replacing it.
Context: ...sks parameters. To only show logs from worker and hide hardhat node logs: ``` yarn lo...

(AI_EN_LECTOR_REPLACEMENT_NOUN_NUMBER)

Markdownlint
scripts/readme.md

13-13: null
Fenced code blocks should be surrounded by blank lines

(MD031, blanks-around-fences)


5-5: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


13-13: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


15-15: null
Files should end with a single newline character

(MD047, single-trailing-newline)

@lumtis
Copy link
Member

lumtis commented Jul 4, 2024

This is really cool! A few thoughts.
Should this eventually move into a separate repo? Pros: we don't mix tools and protocol contracts, we don't have to explicitly exclude this code from the audit. Cons: slower iterations as protocol contracts live in one repo, localnet lives in a different repo.
Package localnet as a Hardhat task that can be imported either by the toolkit or directly by the template, so that it can be distributed a bit more easily.
Should we find a different name for this? This feels more like a simulation rather than a local network.

thanks for review - i would say currently this can help us speed up development as we work on v2 contracts so until its in development phase i would say it is beneficial to stay in same repo as it would evolve with contracts

later we can discuss moving it and packaging it differently

@lumtis what do you think?

Yeah, I would tend toward keep the main command in this repo as being part of the development workflow and provide a packaged plugin or so reusable in a utility

@skosito skosito merged commit 41920d0 into main Jul 4, 2024
9 checks passed
@skosito skosito deleted the prototype-localnet branch July 4, 2024 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setup single chain localnet for V2 contracts
4 participants