Skip to content

Commit 2994173

Browse files
author
Dev Kalra
authored
[cosmwasm] implement a build script (#733)
* implement a build script * update readme
1 parent 0b9f1f1 commit 2994173

File tree

7 files changed

+103
-29
lines changed

7 files changed

+103
-29
lines changed

target_chains/cosmwasm/.dockerignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

target_chains/cosmwasm/Dockerfile.build

Lines changed: 0 additions & 11 deletions
This file was deleted.

target_chains/cosmwasm/README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ This directory contains the code to perform all the steps. Read below for the de
1818

1919
First, build the contracts within [the current directory](./). You must have Docker installed.
2020

21-
NOTE: In order to build for Injective. We need to enable a feature in [Cargo.toml](./contracts/pyth/Cargo.toml) like this.
22-
23-
```toml
24-
[features]
25-
default=["injective"]
26-
...
2721
```
22+
cd ./tools
23+
npm ci
2824
29-
```sh
30-
bash build.sh
25+
# if you want to build specifically for injective
26+
npm run build-contract -- --injective
27+
28+
# else a generic cosmwasm contract can be build using
29+
npm run build-contract -- --cosmwasm
3130
```
3231

3332
This command will build and save the Pyth contract in the `artifacts` directory.

target_chains/cosmwasm/build.sh

Lines changed: 0 additions & 7 deletions
This file was deleted.

target_chains/cosmwasm/tools/package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

target_chains/cosmwasm/tools/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "deploy-pyth-bridge.ts",
66
"scripts": {
77
"build": "tsc",
8+
"build-contract": "ts-node ./src/build-contract.ts",
89
"deploy-pyth": "ts-node ./src/deploy-pyth-bridge.ts",
910
"deploy": "ts-node ./src/deploy.ts"
1011
},
@@ -16,6 +17,7 @@
1617
"@injectivelabs/networks": "^1.0.55",
1718
"@injectivelabs/sdk-ts": "^1.0.354",
1819
"@injectivelabs/utils": "^1.0.47",
20+
"@ltd/j-toml": "^1.38.0",
1921
"@terra-money/terra.js": "^3.1.3",
2022
"chain-registry": "^1.6.0",
2123
"cosmjs-utils": "^0.1.0",
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { readFileSync, writeFileSync } from "fs";
2+
import toml from "@ltd/j-toml";
3+
import { exec } from "child_process";
4+
import yargs from "yargs";
5+
import { hideBin } from "yargs/helpers";
6+
7+
const argv = yargs(hideBin(process.argv))
8+
.usage("USAGE: npm run build-contract -- <command>")
9+
.option("cosmwasm", {
10+
type: "boolean",
11+
})
12+
.option("injective", {
13+
type: "boolean",
14+
})
15+
.help()
16+
.alias("help", "h")
17+
.wrap(yargs.terminalWidth())
18+
.parseSync();
19+
20+
// we need to update the toml file to have a feature on - default=['injective']
21+
// editing and writing the toml file before building the contract for injective
22+
function injectivePreSetup(contractTomlFilePath: string) {
23+
const tomlContentStr = readFileSync(contractTomlFilePath, "utf-8");
24+
const parsedToml = toml.parse(tomlContentStr);
25+
26+
// add injective feature to the cargo.toml
27+
// @ts-ignore
28+
parsedToml.features.default = ["injective"];
29+
30+
// @ts-ignore
31+
const updatedToml = toml.stringify(parsedToml, {
32+
// don't remove this or else stringify will return an array of strings
33+
// where each string represents a line
34+
// this lets it combine all of those line
35+
newline: "\n",
36+
newlineAround: "section",
37+
forceInlineArraySpacing: 0,
38+
});
39+
40+
writeFileSync(contractTomlFilePath, updatedToml);
41+
}
42+
43+
// we are using `git restore` to restore the toml file to it's original content.
44+
// we can also remove a feature from parsedToml and stringify it as above.
45+
// But stringifying it gives us an output with different indentation
46+
// and other such edits.
47+
function injectivePostCleanup(contractTomlFilePath: string) {
48+
exec(`git restore ${contractTomlFilePath}`, (error, _stdout, _stderr) => {
49+
if (error !== null)
50+
console.log(
51+
"Error restoring cargo.toml file. Please restore it manually."
52+
);
53+
});
54+
}
55+
56+
function build() {
57+
if (argv.cosmwasm !== true && argv.injective !== true) {
58+
console.log("Please provide one of the options: ['cosmwasm', 'injective']");
59+
return;
60+
}
61+
62+
const contractTomlFilePath = "../contracts/pyth/Cargo.toml";
63+
64+
if (argv.injective === true) injectivePreSetup(contractTomlFilePath);
65+
66+
const buildCommand = `
67+
docker run --rm -v "$(cd ..; pwd)":/code \
68+
-v $(cd ../../../wormhole_attester; pwd):/wormhole_attester \
69+
--mount type=volume,source="$(basename "$(cd ..; pwd)")_cache",target=/code/target \
70+
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
71+
cosmwasm/workspace-optimizer-arm64:0.12.11
72+
`;
73+
74+
// build contract by running the command
75+
exec(buildCommand, (_error, stdout, stderr) => {
76+
console.log(stdout);
77+
console.log(stderr);
78+
79+
if (argv.injective === true) injectivePostCleanup(contractTomlFilePath);
80+
});
81+
}
82+
83+
build();

0 commit comments

Comments
 (0)