From 7d8240b01ff053442805ba85858d73503b61185c Mon Sep 17 00:00:00 2001 From: Jason Stallings Date: Thu, 5 May 2022 14:18:02 -0500 Subject: [PATCH 1/9] refactor: Return contractAddress from instantiate. --- src/lib/deployment.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib/deployment.ts b/src/lib/deployment.ts index 0a863ec..a559f05 100644 --- a/src/lib/deployment.ts +++ b/src/lib/deployment.ts @@ -33,6 +33,7 @@ type StoreCodeParams = { codeId?: number; arm64?: boolean; }; + export const storeCode = async ({ noRebuild, contract, @@ -187,8 +188,9 @@ export const instantiate = async ({ } cli.action.stop(); - const contractAddress = log[0].events - .find((event: { type: string }) => event.type === 'instantiate') + + const contractAddress: string = log[0].events + .find((event: { type: string }) => event.type === 'instantiate_contract') .attributes.find( (attr: { key: string }) => attr.key === '_contract_address', ).value; @@ -202,6 +204,8 @@ export const instantiate = async ({ saveRefs(updatedRefs, refsPath); cli.log(YAML.stringify(log)); + + return contractAddress; }; type MigrateParams = { From 4eeb9348bb7a003d34e114dbdae2b0ff82ead0be Mon Sep 17 00:00:00 2001 From: Jason Stallings Date: Fri, 6 May 2022 10:44:33 -0500 Subject: [PATCH 2/9] refactor: Move common flags to their own file to reduce duplication. --- src/commands/contract/instantiate.ts | 7 ++++--- src/commands/contract/migrate.ts | 8 +++----- src/commands/contract/store.ts | 5 +++-- src/commands/contract/updateAdmin.ts | 3 ++- src/commands/deploy.ts | 20 ++++++-------------- src/commands/task/run.ts | 1 - src/commands/wallet/new.ts | 2 +- src/lib/flag.ts | 20 ++++++++++++++++++++ 8 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 src/lib/flag.ts diff --git a/src/commands/contract/instantiate.ts b/src/commands/contract/instantiate.ts index 6ade643..36b2860 100644 --- a/src/commands/contract/instantiate.ts +++ b/src/commands/contract/instantiate.ts @@ -3,22 +3,23 @@ import { LCDClient } from '@terra-money/terra.js'; import { loadConfig, loadConnections, loadRefs } from '../../config'; import { instantiate } from '../../lib/deployment'; import { getSigner } from '../../lib/signer'; +import * as flag from '../../lib/flag'; export default class ContractInstantiate extends Command { static description = 'Instantiate the contract.'; static flags = { + signer: flag.signer, + 'set-signer-as-admin': flag.setSignerAsAdmin, network: flags.string({ default: 'localterra' }), 'config-path': flags.string({ default: './config.terrain.json' }), 'refs-path': flags.string({ default: './refs.terrain.json' }), 'keys-path': flags.string({ default: './keys.terrain.js' }), 'instance-id': flags.string({ default: 'default' }), - signer: flags.string({ required: true }), 'code-id': flags.integer({ description: - 'target code id for migration, can do only once after columbus-5 upgrade', + 'specfic codeId to instantiate', }), - 'set-signer-as-admin': flags.boolean({ default: false }), }; static args = [{ name: 'contract', required: true }]; diff --git a/src/commands/contract/migrate.ts b/src/commands/contract/migrate.ts index c32b678..9590c06 100644 --- a/src/commands/contract/migrate.ts +++ b/src/commands/contract/migrate.ts @@ -3,21 +3,19 @@ import { LCDClient } from '@terra-money/terra.js'; import { loadConfig, loadConnections } from '../../config'; import { migrate, storeCode } from '../../lib/deployment'; import { getSigner } from '../../lib/signer'; +import * as flag from '../../lib/flag'; export default class ContractMigrate extends Command { static description = 'Migrate the contract.'; static flags = { - 'no-rebuild': flags.boolean({ - description: 'deploy the wasm bytecode as is.', - default: false, - }), + signer: flag.signer, + 'no-rebuild': flag.noRebuild, network: flags.string({ default: 'localterra' }), 'config-path': flags.string({ default: './config.terrain.json' }), 'refs-path': flags.string({ default: './refs.terrain.json' }), 'keys-path': flags.string({ default: './keys.terrain.js' }), 'instance-id': flags.string({ default: 'default' }), - signer: flags.string({ required: true }), 'code-id': flags.integer({ description: 'target code id for migration', diff --git a/src/commands/contract/store.ts b/src/commands/contract/store.ts index 2c03b38..cc6872b 100644 --- a/src/commands/contract/store.ts +++ b/src/commands/contract/store.ts @@ -3,18 +3,19 @@ import { LCDClient } from '@terra-money/terra.js'; import { loadConfig, loadConnections } from '../../config'; import { storeCode } from '../../lib/deployment'; import { getSigner } from '../../lib/signer'; +import * as flag from '../../lib/flag'; export default class CodeStore extends Command { static description = 'Store code on chain.'; static flags = { - 'no-rebuild': flags.boolean({ default: false }), + signer: flag.signer, + 'no-rebuild': flag.noRebuild, network: flags.string({ default: 'localterra' }), 'config-path': flags.string({ default: './config.terrain.json' }), 'refs-path': flags.string({ default: './refs.terrain.json' }), 'keys-path': flags.string({ default: './keys.terrain.js' }), 'code-id': flags.integer({}), - signer: flags.string({ required: true }), }; static args = [{ name: 'contract', required: true }]; diff --git a/src/commands/contract/updateAdmin.ts b/src/commands/contract/updateAdmin.ts index e750680..1c061ae 100644 --- a/src/commands/contract/updateAdmin.ts +++ b/src/commands/contract/updateAdmin.ts @@ -4,6 +4,7 @@ import { LCDClient, MsgUpdateContractAdmin } from "@terra-money/terra.js"; import { cli } from "cli-ux"; import { loadConnections, loadRefs } from "../../config"; import { getSigner } from "../../lib/signer"; +import * as flag from '../../lib/flag'; import { waitForInclusionInBlock } from '../../lib/waitForInclusionBlock'; @@ -11,12 +12,12 @@ export default class ContractUpdateAdmin extends Command { static description = "Update the admin of a contract."; static flags = { + signer: flag.signer, network: flags.string({ default: "localterra" }), "config-path": flags.string({ default: "./config.terrain.json" }), "refs-path": flags.string({ default: "./refs.terrain.json" }), "keys-path": flags.string({ default: "./keys.terrain.js" }), "instance-id": flags.string({ default: "default" }), - signer: flags.string({ required: true }), }; static args = [ diff --git a/src/commands/deploy.ts b/src/commands/deploy.ts index ae8ad16..cd02844 100644 --- a/src/commands/deploy.ts +++ b/src/commands/deploy.ts @@ -4,35 +4,27 @@ import * as fs from 'fs'; import { loadConfig, loadConnections } from '../config'; import { instantiate, storeCode } from '../lib/deployment'; import { getSigner } from '../lib/signer'; +import * as flag from '../lib/flag'; export default class Deploy extends Command { static description = 'Build wasm bytecode, store code on chain and instantiate.'; static flags = { - 'no-rebuild': flags.boolean({ - description: 'deploy the wasm bytecode as is.', - default: false, - }), + signer: flag.signer, + arm64: flag.arm64, + 'no-rebuild': flag.noRebuild, + 'set-signer-as-admin': flag.setSignerAsAdmin, network: flags.string({ default: 'localterra' }), 'config-path': flags.string({ default: './config.terrain.json' }), 'refs-path': flags.string({ default: './refs.terrain.json' }), 'keys-path': flags.string({ default: './keys.terrain.js' }), - 'instance-id': flags.string({ default: 'default' }), - signer: flags.string({ required: true }), - 'set-signer-as-admin': flags.boolean({ - description: 'set signer (deployer) as admin to allow migration.', - default: false, - }), + 'instance-id': flag.instanceId, 'admin-address': flags.string({ description: 'set custom address as contract admin to allow migration.', }), 'frontend-refs-path': flags.string({ default: './frontend/src/refs.terrain.json', }), - arm64: flags.boolean({ - description: 'use rust-optimizer-arm64 for optimization. Not recommended for production, but it will optimize quicker on arm64 hardware during development.', - default: false, - }), }; static args = [{ name: 'contract', required: true }]; diff --git a/src/commands/task/run.ts b/src/commands/task/run.ts index 7c09039..f3fe736 100644 --- a/src/commands/task/run.ts +++ b/src/commands/task/run.ts @@ -64,7 +64,6 @@ export default class Run extends Command { } } - function runScript( scriptPath: string, env: { diff --git a/src/commands/wallet/new.ts b/src/commands/wallet/new.ts index 8e9871b..c652ba9 100644 --- a/src/commands/wallet/new.ts +++ b/src/commands/wallet/new.ts @@ -3,7 +3,7 @@ import { MnemonicKey } from '@terra-money/terra.js'; import * as fs from 'fs'; export default class WalletNew extends Command { - static description = 'Generate a new wallet.'; + static description = 'Generate a new wallet to use for signing contracts'; static flags = { outfile: flags.string({ diff --git a/src/lib/flag.ts b/src/lib/flag.ts new file mode 100644 index 0000000..f9f0308 --- /dev/null +++ b/src/lib/flag.ts @@ -0,0 +1,20 @@ +import { flags } from '@oclif/command'; + +export const signer = flags.string({ default: 'test1' }); + +export const noRebuild = flags.boolean({ + description: 'deploy the wasm bytecode as is.', + default: false, +}); + +export const arm64 = flags.boolean({ + description: 'use rust-optimizer-arm64 for optimization. Not recommended for production, but it will optimize quicker on arm64 hardware during development.', + default: false, +}); + +export const setSignerAsAdmin = flags.boolean({ + description: 'set signer (deployer) as admin to allow migration.', + default: false, +}); + +export const instanceId = flags.string({ default: 'default', description: 'enable management of multiple instances of the same contract' }); From e05dd5d70ee15fac49774e4308e66d5652ed57f2 Mon Sep 17 00:00:00 2001 From: Jason Stallings Date: Fri, 6 May 2022 10:45:13 -0500 Subject: [PATCH 3/9] refactor: Allow passing a smart contract address. --- src/lib/LCDClientExtra.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib/LCDClientExtra.ts b/src/lib/LCDClientExtra.ts index 66c845d..8ff196a 100644 --- a/src/lib/LCDClientExtra.ts +++ b/src/lib/LCDClientExtra.ts @@ -36,13 +36,14 @@ export class LCDClientExtra extends LCDClient { const msgs = [ new MsgExecuteContract( wallet.key.accAddress, - this.refs[contract].contractAddresses[instanceId], + // Enable supplying a contract address instead of the contract name. + contract.startsWith('terra1') ? contract : this.refs[contract].contractAddresses[instanceId], msg, coins, ), ]; - const _options = options ? { ...options, msgs } : { msgs }; - const tx = await wallet.createAndSignTx(_options); - return await this.tx.broadcast(tx); + const mergedOptions = options ? { ...options, msgs } : { msgs }; + const tx = await wallet.createAndSignTx(mergedOptions); + return this.tx.broadcast(tx); } } From acfc9da4f7dbe4fbd859c62bccf832974bb98352 Mon Sep 17 00:00:00 2001 From: Jason Stallings Date: Fri, 6 May 2022 10:56:32 -0500 Subject: [PATCH 4/9] feat: Expose storeCode and deploy to task runner. This enables more complex deploys. --- README.md | 244 +++++++++++++++++++++++++++++-------------------- src/config.ts | 10 +- src/lib/env.ts | 50 +++++++++- 3 files changed, 202 insertions(+), 102 deletions(-) diff --git a/README.md b/README.md index 04d9741..946fa4f 100644 --- a/README.md +++ b/README.md @@ -328,13 +328,63 @@ task(async ({ wallets, refs, config, client }) => { }); ``` +As of Terrain 0.3.0 it is possible to deploy and instantiate contracts from tasks. This can be useful for multi-contract, or multi-stage deployments. + +```js +const { task } = require("@terra-money/terrain"); + +task(async ({ wallets, client, deploy }) => { + // First deploy the counter smart contract. + const counterCodeId = await deploy.storeCode(wallets.test1, "counter"); + const counterAddress = await deploy.instantiate( + // Signer + wallets.test1, + // Contract name + "counter", + // Code ID + counterCodeId, + // Instance ID + "default", + // Contract admin + wallets.test1.key.accAddress + ); + + // Now deploy a CW20 with the counter contract set as the minter in instantiation. + const cw20CodeId = await deploy.storeCode(wallets.test1, "cw20-base"); + const cw20Address = await deploy.instantiate( + wallets.test1, + "cw20-base", + cw20CodeId, + "default", + wallets.test1.key.accAddress, + // Custom instantiation message. + // with no message provided the default from config.terrain will be used. + { + name: "counter", + symbol: "CTR", + decimals: 6, + initial_balances: [], + mint: { + minter: counterAddress, + }, + } + ); + + // Update the CW20 address in counter. + // Note: It's important to use the address returned by deploy.instantiate + // Refs are only read into memory at the start of the task. + await client.execute(wallets.test1, counterAddress, { + update_token: { token: cw20Address }, + }); + + console.log(`CW20 Address: ${cw20Address}`); +}); +``` --- # Migrating CosmWasm Contracts on Terra -(Thanks to @octalmage) - -On Terra, it is possible to initilize contracts as **_migratable_**. A migratable contract allows an adminstrator to upload a new version of a contract and then send a migrate message to move to the new code. +On Terra it is possible to initilize contracts as migratable. This functionallity allows the adminstrator to upload a new version of the contract, then send a migrate message to move to the new code. This tutorial builds on top of the Terrain Quick Start Guide and walks you through a contract migration. @@ -393,6 +443,29 @@ To use the main branch of the Terrain repo on your local machine, do the followi 1. Clone the repo. + +```sh-session +$ npm install -g @terra-money/terrain +$ terrain COMMAND +running command... +$ terrain (-v|--version|version) +@terra-money/terrain/0.2.0 darwin-x64 node-v16.9.1 +$ terrain --help [COMMAND] +USAGE + $ terrain COMMAND +... +``` + +```sh-session +$ npm install -g @terra-money/terrain +$ terrain COMMAND +running command... +$ terrain (-v|--version|version) +@terra-money/terrain/0.2.0 darwin-x64 node-v16.9.1 +$ terrain --help [COMMAND] +USAGE + $ terrain COMMAND +... ``` git clone --branch main --depth 1 https://github.com/terra-money/terrain ``` @@ -426,6 +499,8 @@ npm unlink terrain # Terrain Commands +* [`terrain code:new NAME`](#terrain-codenew-name) +* [`terrain code:store CONTRACT`](#terrain-codestore-contract) * [`terrain console`](#terrain-console) * [`terrain contract:instantiate CONTRACT`](#terrain-contractinstantiate-contract) * [`terrain contract:migrate [CONTRACT]`](#terrain-contractmigrate-contract) @@ -442,128 +517,120 @@ npm unlink terrain * [`terrain test:coverage [CONTRACT-NAME]`](#terrain-testcoverage-contract-name) * [`terrain wallet:new`](#terrain-walletnew) -## `terrain console` +## `terrain code:new NAME` -Start a repl console that provides context and convinient utilities to interact with the blockchain and your contracts. +Generate new contract. ``` USAGE - $ terrain console [--network ] [--config-path ] [--refs-path ] [--keys-path ] + $ terrain code:new [NAME] [--path ] [--version ] FLAGS - --config-path= [default: config.terrain.json] - --keys-path= [default: keys.terrain.js] - --network= [default: localterra] - --refs-path= [default: refs.terrain.json] + --path= [default: ./contracts] path to keep the contracts + --version= [default: 0.16] DESCRIPTION - Start a repl console that provides context and convinient utilities to interact with the blockchain and your - contracts. + Generate new contract. ``` -_See code: [src/commands/console.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/console.ts)_ +_See code: [src/commands/code/new.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/code/new.ts)_ -## `terrain contract:instantiate CONTRACT` +## `terrain code:store CONTRACT` -Instantiate the contract. +Store code on chain. ``` USAGE - $ terrain contract:instantiate [CONTRACT] --signer [--network ] [--config-path ] [--refs-path - ] [--keys-path ] [--instance-id ] [--code-id ] [--set-signer-as-admin] + $ terrain code:store [CONTRACT] [--signer ] [--no-rebuild] [--network ] [--config-path ] + [--refs-path ] [--keys-path ] [--code-id ] FLAGS - --code-id= target code id for migration, can do only once after columbus-5 upgrade + --code-id= --config-path= [default: ./config.terrain.json] - --instance-id= [default: default] --keys-path= [default: ./keys.terrain.js] --network= [default: localterra] + --no-rebuild deploy the wasm bytecode as is. --refs-path= [default: ./refs.terrain.json] - --set-signer-as-admin - --signer= (required) + --signer= [default: test1] DESCRIPTION - Instantiate the contract. + Store code on chain. ``` -_See code: [src/commands/contract/instantiate.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/contract/instantiate.ts)_ +_See code: [src/commands/code/store.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/code/store.ts)_ -## `terrain contract:migrate [CONTRACT]` +## `terrain console` -Migrate the contract. +Start a repl console that provides context and convinient utilities to interact with the blockchain and your contracts. ``` USAGE - $ terrain contract:migrate [CONTRACT] --signer [--no-rebuild] [--network ] [--config-path ] - [--refs-path ] [--keys-path ] [--instance-id ] [--code-id ] [--arm64] + $ terrain console [--network ] [--config-path ] [--refs-path ] [--keys-path ] FLAGS - --arm64 use rust-optimizer-arm64 for optimization. Not recommended for production, but it will optimize - quicker on arm64 hardware during development. - --code-id= target code id for migration - --config-path= [default: ./config.terrain.json] - --instance-id= [default: default] - --keys-path= [default: ./keys.terrain.js] + --config-path= [default: config.terrain.json] + --keys-path= [default: keys.terrain.js] --network= [default: localterra] - --no-rebuild deploy the wasm bytecode as is. - --refs-path= [default: ./refs.terrain.json] - --signer= (required) + --refs-path= [default: refs.terrain.json] DESCRIPTION - Migrate the contract. + Start a repl console that provides context and convinient utilities to interact with the blockchain and your + contracts. ``` -_See code: [src/commands/contract/migrate.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/contract/migrate.ts)_ +_See code: [src/commands/console.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/console.ts)_ -## `terrain contract:new NAME` +## `terrain contract:instantiate CONTRACT` -Generate new contract. +Instantiate the contract. ``` USAGE - $ terrain contract:new [NAME] [--path ] [--version ] [--authors ] + $ terrain contract:instantiate [CONTRACT] [--signer ] [--set-signer-as-admin] [--network ] [--config-path + ] [--refs-path ] [--keys-path ] [--instance-id ] [--code-id ] FLAGS - --authors= [default: Terra Money ] - --path= [default: ./contracts] path to keep the contracts - --version= [default: 1.0-beta6] + --code-id= specfic codeId to instantiate + --config-path= [default: ./config.terrain.json] + --instance-id= [default: default] + --keys-path= [default: ./keys.terrain.js] + --network= [default: localterra] + --refs-path= [default: ./refs.terrain.json] + --set-signer-as-admin set signer (deployer) as admin to allow migration. + --signer= [default: test1] DESCRIPTION - Generate new contract. - -EXAMPLES - $ terrain code:new awesome_contract - - $ terrain code:new awesome_contract --path path/to/dapp - - $ terrain code:new awesome_contract --path path/to/dapp --authors "ExampleAuthor" + Instantiate the contract. ``` -_See code: [src/commands/contract/new.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/contract/new.ts)_ +_See code: [src/commands/contract/instantiate.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/contract/instantiate.ts)_ -## `terrain contract:store CONTRACT` +## `terrain contract:migrate [CONTRACT]` -Store code on chain. +Migrate the contract. ``` USAGE - $ terrain contract:store [CONTRACT] --signer [--no-rebuild] [--network ] [--config-path ] - [--refs-path ] [--keys-path ] [--code-id ] + $ terrain contract:migrate [CONTRACT] [--signer ] [--no-rebuild] [--network ] [--config-path ] + [--refs-path ] [--keys-path ] [--instance-id ] [--code-id ] [--arm64] FLAGS - --code-id= + --arm64 use rust-optimizer-arm64 for optimization. Not recommended for production, but it will optimize + quicker on arm64 hardware during development. + --code-id= target code id for migration --config-path= [default: ./config.terrain.json] + --instance-id= [default: default] --keys-path= [default: ./keys.terrain.js] --network= [default: localterra] - --no-rebuild + --no-rebuild deploy the wasm bytecode as is. --refs-path= [default: ./refs.terrain.json] - --signer= (required) + --signer= [default: test1] DESCRIPTION - Store code on chain. + Migrate the contract. ``` -_See code: [src/commands/contract/store.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/contract/store.ts)_ +_See code: [src/commands/contract/migrate.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/contract/migrate.ts)_ ## `terrain contract:updateAdmin CONTRACT ADMIN` @@ -571,7 +638,7 @@ Update the admin of a contract. ``` USAGE - $ terrain contract:updateAdmin [CONTRACT] [ADMIN] --signer [--network ] [--config-path ] + $ terrain contract:updateAdmin [CONTRACT] [ADMIN] [--signer ] [--network ] [--config-path ] [--refs-path ] [--keys-path ] [--instance-id ] FLAGS @@ -580,13 +647,13 @@ FLAGS --keys-path= [default: ./keys.terrain.js] --network= [default: localterra] --refs-path= [default: ./refs.terrain.json] - --signer= (required) + --signer= [default: test1] DESCRIPTION Update the admin of a contract. ``` -_See code: [src/commands/contract/updateAdmin.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/contract/updateAdmin.ts)_ +_See code: [src/commands/contract/updateAdmin.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/contract/updateAdmin.ts)_ ## `terrain deploy CONTRACT` @@ -594,9 +661,9 @@ Build wasm bytecode, store code on chain and instantiate. ``` USAGE - $ terrain deploy [CONTRACT] --signer [--no-rebuild] [--network ] [--config-path ] - [--refs-path ] [--keys-path ] [--instance-id ] [--set-signer-as-admin] [--admin-address - ] [--frontend-refs-path ] [--arm64] + $ terrain deploy [CONTRACT] [--signer ] [--arm64] [--no-rebuild] [--set-signer-as-admin] [--network + ] [--config-path ] [--refs-path ] [--keys-path ] [--instance-id ] + [--admin-address ] [--frontend-refs-path ] FLAGS --admin-address= set custom address as contract admin to allow migration. @@ -604,19 +671,19 @@ FLAGS optimize quicker on arm64 hardware during development. --config-path= [default: ./config.terrain.json] --frontend-refs-path= [default: ./frontend/src/refs.terrain.json] - --instance-id= [default: default] + --instance-id= [default: default] enable management of multiple instances of the same contract --keys-path= [default: ./keys.terrain.js] --network= [default: localterra] --no-rebuild deploy the wasm bytecode as is. --refs-path= [default: ./refs.terrain.json] --set-signer-as-admin set signer (deployer) as admin to allow migration. - --signer= (required) + --signer= [default: test1] DESCRIPTION Build wasm bytecode, store code on chain and instantiate. ``` -_See code: [src/commands/deploy.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/deploy.ts)_ +_See code: [src/commands/deploy.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/deploy.ts)_ ## `terrain help [COMMAND]` @@ -668,7 +735,7 @@ EXAMPLES $ terrain new awesome-dapp --path path/to/dapp --framework vue --authors "ExampleAuthor" ``` -_See code: [src/commands/new.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/new.ts)_ +_See code: [src/commands/new.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/new.ts)_ ## `terrain sync-refs [FILE]` @@ -686,7 +753,7 @@ DESCRIPTION Sync configuration with frontend app. ``` -_See code: [src/commands/sync-refs.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/sync-refs.ts)_ +_See code: [src/commands/sync-refs.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/sync-refs.ts)_ ## `terrain task:new [TASK]` @@ -700,7 +767,7 @@ DESCRIPTION create new task ``` -_See code: [src/commands/task/new.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/task/new.ts)_ +_See code: [src/commands/task/new.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/task/new.ts)_ ## `terrain task:run [TASK]` @@ -721,7 +788,7 @@ DESCRIPTION run predefined task ``` -_See code: [src/commands/task/run.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/task/run.ts)_ +_See code: [src/commands/task/run.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/task/run.ts)_ ## `terrain test CONTRACT-NAME` @@ -743,30 +810,11 @@ EXAMPLES $ terrain test counter --no-fail-fast ``` -_See code: [src/commands/test.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/test.ts)_ - -## `terrain test:coverage [CONTRACT-NAME]` - -Runs unit tests for a contract directory. - -``` -USAGE - $ terrain test:coverage [CONTRACT-NAME] - -DESCRIPTION - Runs unit tests for a contract directory. - -EXAMPLES - $ terrain test:coverage - - $ terrain test:coverage counter -``` - -_See code: [src/commands/test/coverage.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/test/coverage.ts)_ +_See code: [src/commands/test.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/test.ts)_ ## `terrain wallet:new` -Generate a new wallet. +Generate a new wallet to use for signing contracts ``` USAGE @@ -777,8 +825,8 @@ FLAGS --outfile= absolute path to store the mnemonic key to. If omitted, output to stdout DESCRIPTION - Generate a new wallet. + Generate a new wallet to use for signing contracts ``` -_See code: [src/commands/wallet/new.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/wallet/new.ts)_ +_See code: [src/commands/wallet/new.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/wallet/new.ts)_ diff --git a/src/config.ts b/src/config.ts index 3cc906f..ab71882 100644 --- a/src/config.ts +++ b/src/config.ts @@ -8,11 +8,15 @@ type Fee = { amount: { [coin: string]: number }; }; +export type InstantiateMessage = Record; + export type ContractConfig = { - store: { fee: Fee }; + // TODO: Remove since fee in config is deprecated. + store?: { fee: Fee }; instantiation: { - fee: Fee; - instantiateMsg: Record; + // TODO: Remove since fee in config is deprecated. + fee?: Fee; + instantiateMsg: InstantiateMessage; }; }; diff --git a/src/lib/env.ts b/src/lib/env.ts index e67a3d5..f951299 100644 --- a/src/lib/env.ts +++ b/src/lib/env.ts @@ -1,20 +1,37 @@ -import { LocalTerra, RawKey, Wallet } from '@terra-money/terra.js'; +import { + AccAddress, LocalTerra, RawKey, Wallet, +} from '@terra-money/terra.js'; import * as R from 'ramda'; import { ContractConfig, ContractRef, + InstantiateMessage, loadConfig, loadConnections, loadKeys, loadRefs, } from '../config'; +import { storeCode, instantiate } from './deployment'; import { LCDClientExtra } from './LCDClientExtra'; +export type DeployHelpers = { + storeCode: (signer: Wallet, contract: string) => Promise; + instantiate: ( + signer: Wallet, + contract: string, + codeId: number, + instanceId: string, + admin?: string, + conf?: ContractConfig + ) => Promise; +}; + export type Env = { config: (contract: string) => ContractConfig; refs: { [contractName: string]: ContractRef }; wallets: { [key: string]: Wallet }; client: LCDClientExtra; + deploy: DeployHelpers; }; export const getEnv = ( @@ -44,5 +61,36 @@ export const getEnv = ( ...userDefinedWallets, }, client: lcd, + // Enable tasks to deploy code. + deploy: { + storeCode: (signer: Wallet, contract: string) => storeCode({ + signer, + contract, + network, + refsPath, + lcd, + conf: config(network, contract), + noRebuild: false, + }), + instantiate: ( + signer: Wallet, + contract: string, + codeId: number, + instanceId: string, + admin?: AccAddress, + init?: InstantiateMessage, + ) => instantiate({ + instanceId, + codeId, + signer, + contract, + network, + refsPath, + lcd, + admin, + // Use the instantiation message passed instead of default. + conf: init ? { instantiation: { instantiateMsg: init } } : config(network, contract), + }), + }, }; }; From c81f353beb87322409eeba4e4e1e13ac7dfefec7 Mon Sep 17 00:00:00 2001 From: Jason Stallings Date: Fri, 6 May 2022 12:16:40 -0500 Subject: [PATCH 5/9] docs: Correct spelling, initilize -> initalize. --- README.md | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 946fa4f..b716c08 100644 --- a/README.md +++ b/README.md @@ -328,7 +328,7 @@ task(async ({ wallets, refs, config, client }) => { }); ``` -As of Terrain 0.3.0 it is possible to deploy and instantiate contracts from tasks. This can be useful for multi-contract, or multi-stage deployments. +As of Terrain 0.4.0 it is possible to deploy and instantiate contracts from tasks. This can be useful for multi-contract, or multi-stage deployments. ```js const { task } = require("@terra-money/terrain"); @@ -384,7 +384,7 @@ task(async ({ wallets, client, deploy }) => { # Migrating CosmWasm Contracts on Terra -On Terra it is possible to initilize contracts as migratable. This functionallity allows the adminstrator to upload a new version of the contract, then send a migrate message to move to the new code. +On Terra it is possible to initalize contracts as migratable. This functionallity allows the adminstrator to upload a new version of the contract, then send a migrate message to move to the new code. This tutorial builds on top of the Terrain Quick Start Guide and walks you through a contract migration. @@ -443,29 +443,6 @@ To use the main branch of the Terrain repo on your local machine, do the followi 1. Clone the repo. - -```sh-session -$ npm install -g @terra-money/terrain -$ terrain COMMAND -running command... -$ terrain (-v|--version|version) -@terra-money/terrain/0.2.0 darwin-x64 node-v16.9.1 -$ terrain --help [COMMAND] -USAGE - $ terrain COMMAND -... -``` - -```sh-session -$ npm install -g @terra-money/terrain -$ terrain COMMAND -running command... -$ terrain (-v|--version|version) -@terra-money/terrain/0.2.0 darwin-x64 node-v16.9.1 -$ terrain --help [COMMAND] -USAGE - $ terrain COMMAND -... ``` git clone --branch main --depth 1 https://github.com/terra-money/terrain ``` From ee0366a5b431cce9b606d1403f7bb8426275194b Mon Sep 17 00:00:00 2001 From: Jason Stallings Date: Fri, 6 May 2022 12:30:54 -0500 Subject: [PATCH 6/9] build: Disable import rules. --- .eslintrc.js | 2 ++ src/config.ts | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index cf8b122..cd51ef1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -15,5 +15,7 @@ module.exports = { '@typescript-eslint', ], rules: { + 'import/extensions': 0, + 'import/no-unresolved': 0, }, }; diff --git a/src/config.ts b/src/config.ts index ab71882..023331a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -11,10 +11,10 @@ type Fee = { export type InstantiateMessage = Record; export type ContractConfig = { - // TODO: Remove since fee in config is deprecated. + // TODO: Remove since fee in config is deprecated. store?: { fee: Fee }; instantiation: { - // TODO: Remove since fee in config is deprecated. + // TODO: Remove since fee in config is deprecated. fee?: Fee; instantiateMsg: InstantiateMessage; }; From b08e9223bfa7d23c9b0227953ba087c9ed230fb4 Mon Sep 17 00:00:00 2001 From: Jason Stallings Date: Fri, 27 May 2022 01:37:50 -0500 Subject: [PATCH 7/9] docs: Add deprecated Co-authored-by: emidev98 <49301655+emidev98@users.noreply.github.com> --- src/config.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/config.ts b/src/config.ts index 023331a..4e07620 100644 --- a/src/config.ts +++ b/src/config.ts @@ -12,6 +12,9 @@ export type InstantiateMessage = Record; export type ContractConfig = { // TODO: Remove since fee in config is deprecated. + /** + * @deprecated The property should not be used + */ store?: { fee: Fee }; instantiation: { // TODO: Remove since fee in config is deprecated. From 256db174842575379e78336efd01e3d67e32ad80 Mon Sep 17 00:00:00 2001 From: Jason Stallings Date: Fri, 27 May 2022 01:37:57 -0500 Subject: [PATCH 8/9] docs: Add deprecated Co-authored-by: emidev98 <49301655+emidev98@users.noreply.github.com> --- src/config.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/config.ts b/src/config.ts index 4e07620..9c618df 100644 --- a/src/config.ts +++ b/src/config.ts @@ -17,7 +17,10 @@ export type ContractConfig = { */ store?: { fee: Fee }; instantiation: { - // TODO: Remove since fee in config is deprecated. + // TODO: Remove since fee in config is deprecated. + /** + * @deprecated The property should not be used + */ fee?: Fee; instantiateMsg: InstantiateMessage; }; From d2fede0976c64591f21adb8d9520f85a06500cf9 Mon Sep 17 00:00:00 2001 From: Jason Stallings Date: Thu, 9 Jun 2022 21:27:46 -0500 Subject: [PATCH 9/9] docs: Autogenerated readme updates. --- README.md | 139 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 82 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index b716c08..a14fc46 100644 --- a/README.md +++ b/README.md @@ -476,11 +476,9 @@ npm unlink terrain # Terrain Commands -* [`terrain code:new NAME`](#terrain-codenew-name) -* [`terrain code:store CONTRACT`](#terrain-codestore-contract) * [`terrain console`](#terrain-console) * [`terrain contract:instantiate CONTRACT`](#terrain-contractinstantiate-contract) -* [`terrain contract:migrate [CONTRACT]`](#terrain-contractmigrate-contract) +* [`terrain contract:migrate CONTRACT`](#terrain-contractmigrate-contract) * [`terrain contract:new NAME`](#terrain-contractnew-name) * [`terrain contract:store CONTRACT`](#terrain-contractstore-contract) * [`terrain contract:updateAdmin CONTRACT ADMIN`](#terrain-contractupdateadmin-contract-admin) @@ -494,48 +492,6 @@ npm unlink terrain * [`terrain test:coverage [CONTRACT-NAME]`](#terrain-testcoverage-contract-name) * [`terrain wallet:new`](#terrain-walletnew) -## `terrain code:new NAME` - -Generate new contract. - -``` -USAGE - $ terrain code:new [NAME] [--path ] [--version ] - -FLAGS - --path= [default: ./contracts] path to keep the contracts - --version= [default: 0.16] - -DESCRIPTION - Generate new contract. -``` - -_See code: [src/commands/code/new.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/code/new.ts)_ - -## `terrain code:store CONTRACT` - -Store code on chain. - -``` -USAGE - $ terrain code:store [CONTRACT] [--signer ] [--no-rebuild] [--network ] [--config-path ] - [--refs-path ] [--keys-path ] [--code-id ] - -FLAGS - --code-id= - --config-path= [default: ./config.terrain.json] - --keys-path= [default: ./keys.terrain.js] - --network= [default: localterra] - --no-rebuild deploy the wasm bytecode as is. - --refs-path= [default: ./refs.terrain.json] - --signer= [default: test1] - -DESCRIPTION - Store code on chain. -``` - -_See code: [src/commands/code/store.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/code/store.ts)_ - ## `terrain console` Start a repl console that provides context and convinient utilities to interact with the blockchain and your contracts. @@ -555,7 +511,7 @@ DESCRIPTION contracts. ``` -_See code: [src/commands/console.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/console.ts)_ +_See code: [src/commands/console.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/console.ts)_ ## `terrain contract:instantiate CONTRACT` @@ -580,9 +536,9 @@ DESCRIPTION Instantiate the contract. ``` -_See code: [src/commands/contract/instantiate.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/contract/instantiate.ts)_ +_See code: [src/commands/contract/instantiate.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/contract/instantiate.ts)_ -## `terrain contract:migrate [CONTRACT]` +## `terrain contract:migrate CONTRACT` Migrate the contract. @@ -607,7 +563,57 @@ DESCRIPTION Migrate the contract. ``` -_See code: [src/commands/contract/migrate.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/contract/migrate.ts)_ +_See code: [src/commands/contract/migrate.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/contract/migrate.ts)_ + +## `terrain contract:new NAME` + +Generate new contract. + +``` +USAGE + $ terrain contract:new [NAME] [--path ] [--version ] [--authors ] + +FLAGS + --authors= [default: Terra Money ] + --path= [default: ./contracts] path to keep the contracts + --version= [default: 1.0-beta6] + +DESCRIPTION + Generate new contract. + +EXAMPLES + $ terrain code:new awesome_contract + + $ terrain code:new awesome_contract --path path/to/dapp + + $ terrain code:new awesome_contract --path path/to/dapp --authors "ExampleAuthor" +``` + +_See code: [src/commands/contract/new.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/contract/new.ts)_ + +## `terrain contract:store CONTRACT` + +Store code on chain. + +``` +USAGE + $ terrain contract:store [CONTRACT] [--signer ] [--no-rebuild] [--network ] [--config-path ] + [--refs-path ] [--keys-path ] [--code-id ] + +FLAGS + --code-id= + --config-path= [default: ./config.terrain.json] + --keys-path= [default: ./keys.terrain.js] + --network= [default: localterra] + --no-rebuild deploy the wasm bytecode as is. + --refs-path= [default: ./refs.terrain.json] + --signer= [default: test1] + +DESCRIPTION + Store code on chain. +``` + +_See code: [src/commands/contract/store.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/contract/store.ts)_ ## `terrain contract:updateAdmin CONTRACT ADMIN` @@ -630,7 +636,7 @@ DESCRIPTION Update the admin of a contract. ``` -_See code: [src/commands/contract/updateAdmin.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/contract/updateAdmin.ts)_ +_See code: [src/commands/contract/updateAdmin.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/contract/updateAdmin.ts)_ ## `terrain deploy CONTRACT` @@ -660,7 +666,7 @@ DESCRIPTION Build wasm bytecode, store code on chain and instantiate. ``` -_See code: [src/commands/deploy.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/deploy.ts)_ +_See code: [src/commands/deploy.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/deploy.ts)_ ## `terrain help [COMMAND]` @@ -712,7 +718,7 @@ EXAMPLES $ terrain new awesome-dapp --path path/to/dapp --framework vue --authors "ExampleAuthor" ``` -_See code: [src/commands/new.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/new.ts)_ +_See code: [src/commands/new.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/new.ts)_ ## `terrain sync-refs [FILE]` @@ -730,7 +736,7 @@ DESCRIPTION Sync configuration with frontend app. ``` -_See code: [src/commands/sync-refs.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/sync-refs.ts)_ +_See code: [src/commands/sync-refs.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/sync-refs.ts)_ ## `terrain task:new [TASK]` @@ -744,7 +750,7 @@ DESCRIPTION create new task ``` -_See code: [src/commands/task/new.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/task/new.ts)_ +_See code: [src/commands/task/new.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/task/new.ts)_ ## `terrain task:run [TASK]` @@ -765,7 +771,7 @@ DESCRIPTION run predefined task ``` -_See code: [src/commands/task/run.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/task/run.ts)_ +_See code: [src/commands/task/run.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/task/run.ts)_ ## `terrain test CONTRACT-NAME` @@ -787,7 +793,26 @@ EXAMPLES $ terrain test counter --no-fail-fast ``` -_See code: [src/commands/test.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/test.ts)_ +_See code: [src/commands/test.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/test.ts)_ + +## `terrain test:coverage [CONTRACT-NAME]` + +Runs unit tests for a contract directory. + +``` +USAGE + $ terrain test:coverage [CONTRACT-NAME] + +DESCRIPTION + Runs unit tests for a contract directory. + +EXAMPLES + $ terrain test:coverage + + $ terrain test:coverage counter +``` + +_See code: [src/commands/test/coverage.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/test/coverage.ts)_ ## `terrain wallet:new` @@ -805,5 +830,5 @@ DESCRIPTION Generate a new wallet to use for signing contracts ``` -_See code: [src/commands/wallet/new.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/wallet/new.ts)_ +_See code: [src/commands/wallet/new.ts](https://github.com/terra-money/terrain/blob/v0.3.1/src/commands/wallet/new.ts)_