Skip to content

Commit

Permalink
add evolve script
Browse files Browse the repository at this point in the history
  • Loading branch information
erhant committed Feb 22, 2024
1 parent d16d366 commit 7998aa6
Show file tree
Hide file tree
Showing 7 changed files with 755 additions and 9,756 deletions.
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

0 comments on commit 7998aa6

Please sign in to comment.