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

Feature: Create algob.config.ts for typescript project. #817

Merged
merged 7 commits into from
Oct 25, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ from `algosdk` and sends it to the network.
- Added `MultiSignature` support for all methods `MyAlgo Wallet`.
- Added support for unfunded accounts in `Runtime`.
- Added `waitRounds` as second param to `sendAndWait` method. It is optional and has a default value of 10.
- Added support for to generate the `algob.config.ts` file generation via algob (`yarn run algob . --typescript`) and read the algob.config.ts` file, if present in root directory.

### Bug Fixes

Expand Down
28 changes: 26 additions & 2 deletions docs/guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,36 @@ or
algob -h deploy
```

### Using algob with a JavaScript project

You can write your scripts and tests in JavaScript. If you don't add any flag to `init`, then default is javascript. Example, if you follow the [Create an algob project](#create-an-algob-project) section then:

```shell
yarn run algob init <path-where-to-create>
```
#### Example on `algob init` with javascript

You can use below command to initialize the javascript project in `sample-project` folder.
If `sample-project` folder is not present then it will create one for you.

```shell
yarn run algob init ./sample-project
```

### Using algob with a TypeScript project

You can write your scripts and tests in TS. To initialize a new typescript project add `--typescript` to the init flag. Example, if you follow the [Create an algob project](#create-an-algob-project) section then:
You can write your scripts and tests in TS. To initialize a new typescript project add `--typescript` to the `init` flag. If you don't add any flag to `init`, then default is javascript. Example, if you follow the [Create an algob project](#create-an-algob-project) section then:

```shell
yarn run algob init <path-where-to-create> --typescript
```
#### Example on `algob init` with typescript

You can use below command to initialize the typescript project in `sample-project` folder.
If `sample-project` folder is not present then it will create one for you.

```shell
yarn run algob init --typescript
yarn run algob init ./sample-project --typescript
```

You can also copy our [htlc-pyteal-ts](https://github.com/scale-it/algo-builder/tree/master/examples/htlc-pyteal-ts) example project.
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export REACH_PATH = ~/.algorand-reach
export INDEXER_REMOTE_REPO = https://github.com/algorand/indexer.git
export INDEXER_PATH = ~/.algorand-indexer-download
# path to indexer .tar file (https://github.com/algorand/indexer/releases) for local setup
export INDEXER_LATEST_VERSION = 2.14.0
export INDEXER_LATEST_VERSION = 2.14.1
export INDEXER_TAR_NAME = algorand-indexer_$(OS)_amd64_$(INDEXER_LATEST_VERSION).tar.bz2
export INDEXER_TAR_URL = https://github.com/algorand/indexer/releases/download/$(INDEXER_LATEST_VERSION)/$(INDEXER_TAR_NAME)
# default postgres config vars
Expand Down
2 changes: 1 addition & 1 deletion packages/algob/sample-project/infrastructure/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export REACH_PATH = ~/.algorand-reach
export INDEXER_REMOTE_REPO = https://github.com/algorand/indexer.git
export INDEXER_PATH = ~/.algorand-indexer-download
# path to indexer .tar file (https://github.com/algorand/indexer/releases) for local setup
export INDEXER_LATEST_VERSION = 2.11.1
export INDEXER_LATEST_VERSION = 2.14.1
export INDEXER_TAR_NAME = algorand-indexer_$(OS)_amd64_$(INDEXER_LATEST_VERSION).tar.bz2
export INDEXER_TAR_URL = https://github.com/algorand/indexer/releases/download/$(INDEXER_LATEST_VERSION)/$(INDEXER_TAR_NAME)
# default postgres config vars
Expand Down
4 changes: 2 additions & 2 deletions packages/algob/sample-project/js/test/sample-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe("Sample Test", function () {
const initialMasterBalance = master.balance();

try {
runtime.executeTx([
assert.throws(() => runtime.executeTx([
{
type: types.TransactionType.TransferAlgo,
sign: types.SignType.LogicSignature,
Expand All @@ -66,7 +66,7 @@ describe("Sample Test", function () {
amountMicroAlgos: amount,
payFlags: { totalFee: invalidTxFee },
},
]);
]));
} catch (error) {
console.log(error);
}
Expand Down
89 changes: 89 additions & 0 deletions packages/algob/sample-project/ts/algob.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// NOTE: below we provide some example accounts.
// DON'T this account in any working environment because everyone can check it and use
// the private keys (this accounts are visible to everyone).

// NOTE: to be able to execute transactions, you need to use an active account with
// a sufficient ALGO balance.

/**
Check our /docs/algob-config.md documentation (https://algobuilder.dev/guide/algob-config.html) for more configuration options and ways how to
load a private keys:
+ using mnemonic
+ using binary secret key
+ using KMD daemon
+ loading from a file
+ loading from an environment variable
+ ...
*/

// ## ACCOUNTS USING mnemonic ##
const { mkAccounts, algodCredentialsFromEnv } = require("@algo-builder/algob");
const accounts = mkAccounts([
{
// This account is created using `make setup-master-account` command from our
// `/infrastructure` directory. It already has many ALGOs
name: "master",
addr: "WWYNX3TKQYVEREVSW6QQP3SXSFOCE3SKUSEIVJ7YAGUPEACNI5UGI4DZCE",
mnemonic:
"enforce drive foster uniform cradle tired win arrow wasp melt cattle chronic sport dinosaur announce shell correct shed amused dismiss mother jazz task above hospital",
},
]);

// ## ACCOUNTS loaded from a FILE ##
// const { loadAccountsFromFileSync } = require("@algo-builder/algob");
// const accFromFile = loadAccountsFromFileSync("assets/accounts_generated.yaml");
// accounts = accounts.concat(accFromFile);

/// ## Load accounts from KMD ##
/// Please check https://github.com/scale-it/algo-builder/blob/master/docs/guide/algob-config.md#network-credentials for more details and more methods.
// let kmdCred = KMDCredentialsFromEnv();

let defaultCfg = {
host: "http://localhost",
port: 4001,
/// Below is a token created through our script in `/infrastructure`
/// If you use other setup, update it accordignly (eg content of algorand-node-data/algod.token)
token: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
/// you can also pass token as an object:
// token: {
// "X-Algo-API-Token": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
// },
accounts: accounts,
/// if you want to load accounts from KMD, you need to add the kmdCfg object. Please read
/// Algob Config documentation for details.
// kmdCfg: {wallets: [{name: "mywallet", password: process.env.KMD_PASSWD, accounts: [...]}], ...kmdCred},

/// you can pass config of indexer (ideally it should be attached to this network's algod node)
// indexerCfg: {
// host: "http:localhost",
// port: 8980,
// token: ""
// }
};

// purestake testnet config
let purestakeTestNetCfg = {
host: "https://testnet-algorand.api.purestake.io/ps2",
port: "",
token: {
"X-API-Key": "Xhkn7v7h972hj7Egx3fGr9RFbfXeGuoD6wSLKDyG",
},
};

// You can also use Environment variables to get Algod credentials
// Please check https://algobuilder.dev/guide/algob-config.html#network-credentials for more details.
process.env.ALGOD_ADDR = "127.0.0.1:4001";
process.env.ALGOD_TOKEN = "algod_token";
let algodCred = algodCredentialsFromEnv();
let envCfg = {
...algodCred,
accounts: accounts,
};

module.exports = {
networks: {
default: defaultCfg,
prod: envCfg,
purestake: purestakeTestNetCfg,
},
};
4 changes: 2 additions & 2 deletions packages/algob/sample-project/ts/test/sample-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe("Sample Test", function () {
const initialMasterBalance = master.balance();

try {
runtime.executeTx([
assert.throws(() => runtime.executeTx([
{
type: types.TransactionType.TransferAlgo,
sign: types.SignType.LogicSignature,
Expand All @@ -66,7 +66,7 @@ describe("Sample Test", function () {
amountMicroAlgos: amount,
payFlags: { totalFee: invalidTxFee },
},
]);
]));
} catch (error) {
console.log(error);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/algob/src/internal/core/project-structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { join } from "path";
const fsp = fs.promises;

export const JS_CONFIG_FILENAME = "algob.config.js";
// export const TS_CONFIG_FILENAME = "algob.config.ts";
export const TS_CONFIG_FILENAME = "algob.config.ts";

export const ASSETS_DIR = "assets";
export const ARTIFACTS_DIR = "artifacts";
export const CACHE_DIR = join(ARTIFACTS_DIR, "cache");

export function isCwdInsideProject(): boolean {
return Boolean(findupSync(JS_CONFIG_FILENAME));
return Boolean(findupSync([JS_CONFIG_FILENAME, TS_CONFIG_FILENAME]));
}

export function getUserConfigPath(): string | undefined {
return findupSync(JS_CONFIG_FILENAME) ?? undefined;
return findupSync([JS_CONFIG_FILENAME, TS_CONFIG_FILENAME]) ?? undefined;
}

export async function assertAllDirs(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion packages/algob/test/internal/cli/project-creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("Init project", function () {
withInfrastructure: boolean,
isNpm: boolean
) {
assert.isTrue(fs.existsSync(`./${location}/algob.config.js`));
assert.isTrue(fs.existsSync(`./${location}/algob.config.js`) || fs.existsSync(`./${location}/algob.config.ts`));
if (isTs) {
assert.isTrue(fs.existsSync(`./${location}/scripts/0-sampleScript.ts`));
} else {
Expand Down