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

Add evolve-many script as an example #74

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/evolveMany/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
cache
25 changes: 25 additions & 0 deletions examples/evolveMany/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Evolve Many

Sometimes, you will have multiple HollowDB contracts and you would like to evolve all of them to a newer version. Doing this via the CLI in HollowDB may be cumbersome, especially for a large number of contracts.

## Installation

This script only makes use Warp and Chalk (for nice outputs), install them via:

```sh
yarn install
```

## Usage

The main script is `index.js`, to configure the script you must:

- Specify the contracts to be evolved as an array within [contract.js](./contracts.js)
- Provide the source txID for the new contract source code, within [index.js](./index.js)
- Provide a path to the wallet that is the owner of all these contracts, again within [index.js](./index.js)

With these configured, simply:

```sh
yarn evolve
```
3 changes: 3 additions & 0 deletions examples/evolveMany/contracts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const contractTxIds = [
/** your list of contracts here */
];
42 changes: 42 additions & 0 deletions examples/evolveMany/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {readFileSync} from 'fs';
import {LoggerFactory, WarpFactory, sleep} from 'warp-contracts';
import {contractTxIds} from './contracts.js';
import chalk from 'chalk';

// new source txId for evolve
const srcTxId = 'your-txid-here';

// your wallet should be the contract owner for evolve to happen
const walletPath = '/path/to/wallet.json';
const owner = JSON.parse(readFileSync(walletPath, 'utf-8'));

// create warp instance
const warp = WarpFactory.forMainnet();

// `none` hides Warp logs, can be a different level if you want
LoggerFactory.INST.logLevel('none');

// each evolve takes around 200-300ms to finish
// if you want to start from a later index, just change the `i` below
// to something like `i = startIdx`
for (let i = 0; i < contractTxIds.length; i++) {
try {
const idxStr = chalk.yellow(`[${i.toString().padEnd(5, '')}]`);
const contractTxId = contractTxIds[i];
console.log(`${idxStr} ${contractTxId} beginning to evolve.`);
const msg = chalk.green(`${idxStr} ${contractTxId} evolved`);

// the main evolve logic
console.time(msg);
const contract = warp.contract(contractTxId).connect(owner);
await contract.evolve(srcTxId);
console.timeEnd(msg);

// sleep a bit to avoid rate-limiting
await sleep(1000);
} catch {
// timed-out, try again after a long sleep
i--;
await sleep(7000);
}
}
14 changes: 14 additions & 0 deletions examples/evolveMany/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "evolve-many",
"version": "0.1.0",
"private": true,
"main": "index.js",
"type": "module",
"scripts": {
"evolve": "node index.js"
},
"dependencies": {
"chalk": "^5.3.0",
"warp-contracts": "^1.4.36"
}
}
Loading
Loading