Skip to content

Commit

Permalink
add npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
RnkSngh committed May 14, 2024
1 parent aabf701 commit 17c29fd
Show file tree
Hide file tree
Showing 25 changed files with 9,881 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ artifacts
report/
lcov.info

src/evm/contracts
123 changes: 103 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# vIBC Core Smart Contracts

This project includes the core smart contracts for the vIBC protocol, and a few demo contracts that simulate testing and serve as a template for integrating dapp devs.
This project includes the core smart contracts for the vIBC protocol, a few demo contracts that simulate testing and serve as a template for integrating dapp devs, and an npm package to aid with deploying and sending transactions to deployed contracts.

![](./diagrams/vibcContractsOverview.jpg)

Expand Down Expand Up @@ -35,35 +35,118 @@ The optimisticProofVerifier verifies proofs for the optimistic light client.
## UniversalChannelHandler
The UniversalChannelHandler is a middleware contract that can be used to save dapps from having to go through the 4-step channel handshake to send or receive Ibc packets.

## Quick Start with Forge/Foundry
## Building Contracts and Testing
This repository uses Foundry for testing and development of smart contracts

### Install Forge
## Deploying Contracts
All deployments can either be done through the command line, or through javascript code through importing modules.
After each deployment, deployment files are saved in deployment artifacts as json files, structured similar to how [hardhat deploy stores its deploy files](https://github.com/wighawag/hardhat-deploy).

```sh
curl -L https://foundry.paradigm.xyz | bash
```

This will install Foundryup, then simply follow the instructions on-screen, which will make the `foundryup` command available in your CLI.
Before deploying, the accounts used to deploy and any constructor arguments must be configured. This configuration can either be read from a yaml file or set through environment variables (see the sections below on how to configure each deployments).

Running `foundryup` by itself will install the latest (nightly) precompiled binaries: `forge`, `cast`, `anvil`, and `chisel`. See `foundryup --help` for more options, like installing from a specific version or commit.
The constructor arguments for each deployment. This supports syntax - which looks through written. You can also specify.
This file is read in-order, so each entry in this file should be in-order where dependencies come first and the contract that depends on them comes later.

Or go to https://book.getfoundry.sh/getting-started/installation for more installation options.
### Deploying via Command Line
This npm package exposes two commands - one to deploy new contacts (which automatically creates persisted deployment files), and one to send transactions to contracts from persisted artifact files. The following steps are needed to deploy contracts via the command line:

### Build contracts
1. Ensure that your deployer account and constructor arguments are configured. This can either be done through adding contract spec yaml files located in the specs/ from the root of where this npm module is installed from (requires adding a `specs/evm.accounts.yaml` file and either a `specs/contracts.spec.yaml` or `specs/upgrade.spec.yaml`), or by setting the KEY_POLYMER, RPC_URL, DEPLOYMENT_CHAIN_ID, CHAIN_NAME environment variables. For examples of contract and account spec files, see the `/specs` folder in this repo.
2. Run either `npx deploy-vibc-core-smart-contracts` to deploy contracts from the contract spec, or `npx upgrade-vibc-core-smart-contracts` to send an upgrade transaction.

```sh
forge build
```
### Deploying via imports
Deployments can also be done through calls through the `deployToChain` and the `sendTxToChain` methods.

### Run Tests
#### Deploying new contracts via imports

```sh
forge test
```
import {
AccountRegistry,
Chain,
ContractRegistryLoader,
deployToChain,
parseObjFromFile,
} from "@open-ibc/vibc-core-smart-contracts";
import { getMainLogger } from "@open-ibc/vibc-core-smart-contracts/utils/cli";
import { DEFAULT_RPC_URL } from "../utils/constants";
// Or can parse it form the env
const accountConfig = {
name: "local",
registry: [
{
name: "KEY_POLYMER",
privateKey: process.env.KEY_POLYMER
},
],
};
const accounts = AccountRegistry.loadMultiple([accountConfig]);
const contracts = ContractRegistryLoader.loadSingle(
parseObjFromFile("specs/contracts.spec.yaml")
);
const chain: Chain = {
rpc: process.env.RPC_URL ,
chainId: process.env.DEPLOYMENT_CHAIN_ID,
chainName: process.env.CHAIN_NAME,
vmType: "evm",
description: "local chain",
};
deployToChain(
chain,
accounts.mustGet(chain.chainName),
contracts.subset(),
getMainLogger(),
false
);
```

similar to the command line deploy, this will create a deployment artifact file in the `deployments/` folder.

### Clean environment
#### Upgrading existing contracts via imports
Proxy upgrades to existing contracts can be done through the `sendTxToChain` method :

```sh
forge clean
```
#!/usr/bin/env node
import {
AccountRegistry,
Chain,
parseObjFromFile,
} from "@open-ibc/vibc-core-smart-contracts";
import { loadTxRegistry } from "@open-ibc/vibc-core-smart-contracts/evm/schemas/tx";
import { sendTxToChain } from "@open-ibc/vibc-core-smart-contracts";
import { getOutputLogger } from "@open-ibc/vibc-core-smart-contracts/utils/cli";
// Or can parse it form the env
const accountConfig = {
name: "local",
registry: [
{
name: "KEY_POLYMER",
privateKey: process.env.KEY_POLYMER,
},
],
};
const accounts = AccountRegistry.loadMultiple([accountConfig]);
const upgradeTxs = loadTxRegistry(parseObjFromFile("specs/upgrade.spec.yaml"));
const chain: Chain = {
rpc: process.env.RPC_URL,
chainId: process.env.CHAIN_ID,
chainName: "local",
vmType: "evm",
description: "local chain",
};
sendTxToChain(
chain,
accounts.mustGet(chain.chainName),
upgradeTxs.subset(),
getOutputLogger(),
false
);
```

6 changes: 6 additions & 0 deletions hardhat.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require("@nomicfoundation/hardhat-toolbox");

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.24",
};
Loading

0 comments on commit 17c29fd

Please sign in to comment.