Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
octalmage committed Jun 14, 2022
2 parents 5f6322a + 70122be commit 5abdd10
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ docker-compose up

While WASM smart contracts can be written in any programming language, **it is strongly recommended that you utilize Rust**, as it is the only language for which mature libraries and tooling exist for CosmWasm. To complete this tutorial, install the latest version of Rust by following the instructions <a href="https://www.rust-lang.org/tools/install" target="_blank">here</a>. Once Rust is installed on your computer, do the following:

1. Set the default release channel used to update Rust to stable:
1. Set the default release channel used to update Rust to stable.

```sh
rustup default stable
```

2. Add wasm as the compilation target:
2. Add wasm as the compilation target.

```sh
rustup target add wasm32-unknown-unknown
```

3. Install the necessary dependencies for generating contracts:
3. Install the necessary dependencies for generating contracts.

```sh
cargo install cargo-run-script
Expand All @@ -97,15 +97,15 @@ To run Terrain, you will need to install Node.js and NPM. We recommend that you

# Getting Started

Now that you have completed the initial setup, generate your first smart contract using the procedure described below:
Now that you have completed the initial setup, generate your first smart contract using the procedure described below.

1. Install the terrain package globally.

```sh
npm install -g @terra-money/terrain
```

<sub>**Note:** _If you would like to install terrain locally, you can execute the command `npm install @terra-money/terrain`, without the `-g` flag, while in the directory in which you would like to be able to use the package. You can then execute any terrain commands by prefixing them with `npx`. For example, to scaffold a new project named `my-terra-dapp` with a locally installed terrain package, you would utilize the command `npx terrain new my-terra-dapp`._</sub>
<sub>**Note:** _If you would like to install terrain locally, you can execute the command `npm install @terra-money/terrain`, without the `-g` flag, while in the directory in which you would like to be able to utilize the package. You can then execute any terrain commands by prefixing them with `npx`. For example, to scaffold a new project named `my-terra-dapp` with a locally installed terrain package, you would utilize the command `npx terrain new my-terra-dapp`._</sub>

2. Generate your smart contract and corresponding frontend templates.

Expand All @@ -127,7 +127,7 @@ npm install

## Project Structure

The `terrain new` command generates a project with the following structure:
The `terrain new` command generates a project that contains a template smart contract called `counter` and a corresponding frontend. Other supporting files are generated to provide further functionality. You may view the project structure below.

```
.
Expand Down Expand Up @@ -167,7 +167,7 @@ You can also specify the network on which you would like to deploy your contract

The predefined accounts in the `keys.terrain.js` file shown below can be utilized as signers on `testnet`. We will demonstrate how to deploy your smart contract utilizing the preconfigured `custom_tester_1` account. You may also add a personal account to the `keys.terrain.js` file by adding the account name as well as its corresponding private key. You can then use that account as the signer specifying the account name after the `--signer` flag in the `terrain deploy` command.

<sub>**Warning:** _Utilizing a personal account for deployment requires the use of a private key or mnemonic. These are private keys that are generated upon creation of your personal wallet. Saving or utilizing these keys on your personal computer may expose them to malicious actors who could gain access to your personal wallet if they are able to obtain them. You can create a wallet solely for testing purposes to eliminate risk. Alternatively, you can store your private keys as secret enviroment variables which you can then reference utilizing `process.env.SECRET_VAR` in `keys.terrain.json`. Use your private key or mnemonic at your own discretion._</sub>
<sub>**Warning:** _Utilizing a personal account for deployment requires the use of a private key or mnemonic. These are private keys that are generated upon the creation of your personal wallet. Saving or utilizing these keys on your personal computer may expose them to malicious actors who could gain access to your personal wallet if they are able to obtain them. You can create a wallet solely for testing purposes to eliminate risk. Alternatively, you can store your private keys as secret enviroment variables which you can then reference utilizing `process.env.SECRET_VAR` in `keys.terrain.json`. Use your private key or mnemonic at your own discretion._</sub>

```js
// can use `process.env.SECRET_MNEMONIC` or `process.env.SECRET_PRIV_KEY`
Expand Down Expand Up @@ -209,7 +209,7 @@ terrain deploy counter --signer custom_tester_1 --network testnet

## Initializing the Frontend Template

After deployment, the `refs.terrain.json` file is updated in the project directory as well as the `frontend/src` directory. These files contain all contract references on all networks. This information is utilized by terrain's utility functions and also the frontend template. An example of `refs.terrain.json` can be found below:
After deployment, the `refs.terrain.json` file will be updated in the project directory as well as the `frontend/src` directory. These files contain all contract references on all networks. This information is utilized by terrain's utility functions and also the frontend template. An example of `refs.terrain.json` can be found below:

```json
{
Expand Down Expand Up @@ -252,7 +252,7 @@ npm run start

## Run Contract Functions with Terrain

Once you have successfully deployed your project, you can interact with the deployed contract and the underlying blockchain by utilizing functions defined in the `lib/index.js` file. You may also create your own abstractions in this file for querying or executing transactions. The default contents of the `lib/index.js` file are presented below:
Once you have successfully deployed your project, you can interact with the deployed contract and the underlying blockchain by utilizing functions defined in the `lib/index.js` file. You may also create your own abstractions in this file for querying or executing transactions. The default contents of the `lib/index.js` file are presented below.

```js
// lib/index.js
Expand All @@ -264,7 +264,7 @@ module.exports = ({ wallets, refs, config, client }) => ({
});
```

You can call the functions defined above inside of the `terrain console`. An example of this using the `counter` contract is shown below.
You can call the functions defined above inside of the `terrain console`. An example using the `counter` smart contract is shown below.

```sh
terrain console
Expand All @@ -275,7 +275,7 @@ terrain > await lib.getCount()
{ count: 1 }
```
You can also specify which network you would like to interact with by utilizing the `--network` flag.
You may also specify which network you would like to interact with by utilizing the `--network` flag with the `terrain console` command.
```
terrain console --network NETWORK
Expand Down Expand Up @@ -305,7 +305,7 @@ To run the example task shown above, which is located in the `tasks/example-with
terrain task:run example-with-lib
```
In order to create a new task, run the following command, replacing `<task-name>` with the desired name for your new task.
In order to create a new task, run the following command replacing `<task-name>` with the desired name for your new task.
```sh
terrain task:new <task-name>
Expand Down
34 changes: 30 additions & 4 deletions src/lib/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
MsgMigrateContract,
MsgStoreCode,
Wallet,
SignerData,
CreateTxOptions,
} from '@terra-money/terra.js';
import { parse } from 'toml';
import { execSync } from 'child_process';
Expand All @@ -21,6 +23,7 @@ import {
setCodeId,
setContractAddress,
} from '../config';
import TerrainCLI from '../TerrainCLI';

type StoreCodeParams = {
conf: ContractConfig;
Expand Down Expand Up @@ -81,7 +84,7 @@ export const storeCode = async ({
.toString('base64');

cli.action.start('storing wasm bytecode on chain');

const storeCodeTx = await signer.createAndSignTx({
msgs: [
typeof codeId !== 'undefined'
Expand Down Expand Up @@ -158,18 +161,41 @@ export const instantiate = async ({
// Allow manual account sequences.
const manualSequence = sequence || (await signer.sequence());

const instantiateTx = await signer.createAndSignTx({
sequence: manualSequence,
// Create signerData and txOptions for fee estimation.
const accountInfo = await lcd.auth.accountInfo(signer.key.accAddress);
const signerData: [SignerData] = [{
sequenceNumber: manualSequence,
publicKey: accountInfo.getPublicKey(),
}];
const txOptions: CreateTxOptions = {
msgs: [
new MsgInstantiateContract(
signer.key.accAddress,
admin, // can migrate
codeId,
instantiation.instantiateMsg,
undefined,
"Instantiate"
'Instantiate',
),
],
};

// Set default terraDenom and feeDenoms value if not specified.
if (!txOptions.feeDenoms) {
txOptions.feeDenoms = ['uluna'];
}
const terraDenom = 'LUNA';

// Prompt user to accept gas fee for contract initialization if network is mainnet.
if (network === 'mainnet') {
const feeEstimate = await lcd.tx.estimateFee(signerData, txOptions);
const gasFee = Number(feeEstimate.amount.get(txOptions.feeDenoms[0])!.amount) / 1000000;
await TerrainCLI.anykey(`The gas needed to deploy the '${contract}' contact is estimated to be ${gasFee} ${terraDenom}. Press any key to continue or "ctl+c" to exit`);
}

const instantiateTx = await signer.createAndSignTx({
sequence: manualSequence,
...txOptions,
});

const result = await lcd.tx.broadcastSync(instantiateTx);
Expand Down

0 comments on commit 5abdd10

Please sign in to comment.