Skip to content

Commit

Permalink
[#7] Separate environments (#31)
Browse files Browse the repository at this point in the history
* Add dotenv

* Pass a test sheet url before running apps script api

* Update readme

* [#29] Add a missing "include=code" query for MBADDRESS function (#32)
  • Loading branch information
daenamkim authored Oct 22, 2020
1 parent a00022f commit 6518546
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ node_modules
yarn-error.log
.*.json
.vscode
.env
Binary file added .readme/config-gas-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .readme/config-gas-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .readme/config-gas-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .readme/config-gcp-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .readme/config-gcp-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .readme/config-gcp-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .readme/config-gcp-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .readme/config-sheet-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .readme/config-sheet-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
104 changes: 99 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ This spreadsheet plugin was originally developed as part of the [ETHGlobal HackM
- [Demo Video](https://youtu.be/YsbzTZ6Cfvc)
- [Presentation Slides](Sunset%20Supreme%20Spreadsheet%20Blockchain%20Machine.pdf)

## Prerequisite

You should deploy a MultiBaas instance before following steps below.

- [Deploy MultiBaas](https://www.curvegrid.com/docs/3-0-getting-started-creating-a-multibaas-deployment/)

## Developing

- node.js v12.16.0 or higher
Expand Down Expand Up @@ -37,21 +43,109 @@ or
npm install
```

## Linter
## Testing in a command

**Spreadsheet**

You need to prepare for a spreadsheet with small configuration.

![Sheet Step 0](./.readme/config-sheet-0.png)
![Sheet Step 1](./.readme/config-sheet-1.png)

**Link Apps Script to GCP**

In order to run tests you need to link Apps Script project to GCP project.

Copy a `Project number` from GCP project settings.

![GAS Step 0](./.readme/config-gas-0.png)

Link **Apps Script** project to **GCP** project.

![GAS Step 1](./.readme/config-gas-1.png)
![GAS Step 2](./.readme/config-gas-2.png)

**Credentials and Environments Variables**

You need to create `.client-secret.json`, `.clasp.json`, and `.env` files

- .client-secret.json are used in `auth.js`
- .clasp.json and .env files are used in `mbSheetsAddOn.js`

`.clasp.json` is to set your Apps Script project after you create a new one.

```json
{"scriptId":"[YOUR APPS SCRIPT ID]"}
```

`.client-secret.json` is to set your GCP project. You should download form credential settings as follows:

![GCP Step 0](./.readme/config-gcp-0.png)
![GCP Step 1](./.readme/config-gcp-1.png)
![GCP Step 2](./.readme/config-gcp-2.png)
![GCP Step 3](./.readme/config-gcp-3.png)

This is the credential file.

```json
{
"installed": {
"client_id": "[YOUR GCP PROJECT CLIENT ID]",
"project_id": "[YOUR GCP PROJECT PROJECT ID]",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "[YOUR GCP PROJECT CLIENT SECRET",
"redirect_uris": [
"urn:ietf:wg:oauth:2.0:oob",
"http://localhost"
]
}
}
```

`.env` file in the project root is to set a test sheet URL.

```sh
yarn lint
TEST_SHEET_URL=[YOUR TEST SHEET URL]
```

## Testing
Run `yarn test` if you are ready.

```sh
yarn test
```

If you don't have `.credentials.json` then you will see this prompt:

```sh
yarn test
yarn run v1.22.10
$ clasp push && cd test && node mbSheetsAddOn.js
└─ appsscript.json
└─ src/Code.js
└─ src/Code.spec.js
└─ src/library/Build.js
└─ src/library/Property.js
└─ src/library/Query.js
└─ src/library/Util.js
└─ src/library/Validate.js
Pushed 8 files.
authorize this app by visiting this url: https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fscript.external_request%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets&response_type=code&client_id=966627203108-e6125d7hosngl429qh6b52old6b7r98t.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob
Enter the code from that page here:
```

Just follow the URL and authenticate your account then you can retrieve a code to create .credentials.json file.

## Linter

```sh
yarn lint
```

## Update version

First, run `yarn version` to add tag into git history and update `package.json` file
First, run `yarn version` to add tag into git history and update `package.json` file.

```sh
yarn version
Expand All @@ -63,4 +157,4 @@ And run `yarn update` to update the version in `src/Code.js`.
yarn update
```

DO NOT commit the change after running these command above, please use these only for test.
DO NOT commit the version change after running these command above, please use these only for test in your project.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
],
"devDependencies": {},
"dependencies": {
"dotenv": "^8.2.0",
"eslint": "^7.0.0",
"eslint-config-airbnb": "^18.1.0",
"eslint-plugin-import": "^2.20.2",
Expand Down
2 changes: 1 addition & 1 deletion src/Code.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ function MBADDRESS(address, headers, code) {
const isHeaders = clampBool(headers, true);
const isCode = clampBool(code, false);

const queryPath = `chains/ethereum/addresses/${address}?include=balance`;
const queryPath = `chains/ethereum/addresses/${address}?include=balance${isCode ? '&include=code' : ''}`;
let results;
try {
results = query(
Expand Down
36 changes: 33 additions & 3 deletions src/Code.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ function run(test, config, testCase) {
* Called from outside to run tests and return test results
*/
// eslint-disable-next-line no-unused-vars
function testRunner() {
function testRunner(testSheetURL) {
const test = initTest(loggerAPI);
const testSheetURL = 'https://docs.google.com/spreadsheets/d/12jQjyXkqUooDK5wlDyazBa3xZb5NVa-YyixNtTRZIsI/edit#gid=0';
const ss = SpreadsheetApp.openByUrl(testSheetURL);
SpreadsheetApp.setActiveSpreadsheet(ss);

Expand All @@ -114,7 +113,7 @@ function testRunner() {
debug: false,
func: MBADDRESS,
isTemplate: false,
args: ['0xe9f2E2B0105B683b436Fd0d7A2895BE25c310Af7', '', false],
args: ['0xe9f2E2B0105B683b436Fd0d7A2895BE25c310Af7', true, false],
expected: [
[
'label',
Expand All @@ -136,6 +135,37 @@ function testRunner() {
],
],
},
{
name: 'TestMBADDRESS with Code',
skip: false,
only: false,
debug: false,
func: MBADDRESS,
isTemplate: false,
args: ['0xe9f2E2B0105B683b436Fd0d7A2895BE25c310Af7', true, true],
expected: [
[
'label',
'address',
'balance',
'chain',
'isContract',
'modules',
'contracts',
'codeAt',
],
[
'privatefaucet',
'0xe9f2E2B0105B683b436Fd0d7A2895BE25c310Af7',
2000000000000000000,
'ethereum',
true,
'',
'multibaasfaucet 1.0',
'0x6080604052600436106101145760003560e01c8063893d20e8116100a0578063e64a32d211610064578063e64a32d214610320578063e7f43c6814610335578063f1f959a61461034a578063f8b2cb4f1461035f578063f8e8c47f1461039257610114565b8063893d20e8146102765780638f435f071461028b578063a87430ba146102a0578063d0e30db0146102e5578063de0982a4146102ed57610114565b80633e58c58c116100e75780633e58c58c146101ed5780633f4ba83a1461022057806343b02e0a146102375780634f4b8c001461024c5780638456cb591461026157610114565b80630c432e061461014b5780631c2d3d4914610174578063270edf63146101a75780633ccfd60b146101d8575b60405162461bcd60e51b81526004018080602001828103825260238152602001806113356023913960400191505060405180910390fd5b34801561015757600080fd5b506101606103a7565b604080519115158252519081900360200190f35b34801561018057600080fd5b506101606004803603602081101561019757600080fd5b50356001600160a01b0316610441565b3480156101b357600080fd5b506101bc6105ba565b604080516001600160a01b039092168252519081900360200190f35b3480156101e457600080fd5b506101606105c9565b3480156101f957600080fd5b506101606004803603602081101561021057600080fd5b50356001600160a01b03166106ce565b34801561022c57600080fd5b50610235610981565b005b34801561024357600080fd5b506101bc610a59565b34801561025857600080fd5b50610160610a68565b34801561026d57600080fd5b50610235610b5b565b34801561028257600080fd5b506101bc610c3e565b34801561029757600080fd5b50610160610c4d565b3480156102ac57600080fd5b506102d3600480360360208110156102c357600080fd5b50356001600160a01b0316610cfc565b60408051918252519081900360200190f35b610160610d0e565b3480156102f957600080fd5b506101606004803603602081101561031057600080fd5b50356001600160a01b0316610d4c565b34801561032c57600080fd5b50610160610f1d565b34801561034157600080fd5b506101bc611014565b34801561035657600080fd5b506102d3611023565b34801561036b57600080fd5b506102d36004803603602081101561038257600080fd5b50356001600160a01b0316611027565b34801561039e57600080fd5b50610160611034565b6003546000906001600160a01b031633146103f35760405162461bcd60e51b81526004018080602001828103825260238152602001806113926023913960400191505060405180910390fd5b60028054336001600160a01b031991821681179092556003805490911690556040517fa4d7e78cbe7f44930def1dc63960dd8e4ed6b9ce7b97b357afd463ec8c95862890600090a250600190565b600080546001600160a01b0316331461048f576040805162461bcd60e51b815260206004820152601460248201526000805160206111f8833981519152604482015290519081900360640190fd5b6001546001600160a01b0316156104d75760405162461bcd60e51b81526004018080602001828103825260318152602001806112186031913960400191505060405180910390fd5b6001600160a01b03821661051c5760405162461bcd60e51b81526004018080602001828103825260218152602001806111d76021913960400191505060405180910390fd5b6000546001600160a01b03838116911614156105695760405162461bcd60e51b81526004018080602001828103825260378152602001806112ab6037913960400191505060405180910390fd5b600180546001600160a01b0319166001600160a01b03841690811790915560405133907fc9bcaee7558c5ff2404643f68fa276a8735a0c147123d31d1c3545dba95e727490600090a3506001919050565b6001546001600160a01b031690565b600080546001600160a01b03163314610617576040805162461bcd60e51b815260206004820152601460248201526000805160206111f8833981519152604482015290519081900360640190fd5b60004711610660576040805162461bcd60e51b81526020600482015260116024820152706e6f206661756365742062616c616e636560781b604482015290519081900360640190fd5b6040514790339082156108fc029083906000818181858888f1935050505015801561068f573d6000803e3d6000fd5b5060408051828152905133917fb60710c0d07c81fbd8703b496e191aae3ffc9e6d1ee11f044fb9dbf80f81d8b1919081900360200190a2600191505090565b6002546000906001600160a01b03163314610730576040805162461bcd60e51b815260206004820152601960248201527f73656e6465722073686f756c64206265206f70657261746f7200000000000000604482015290519081900360640190fd5b600154600160a01b900460ff1615610786576040805162461bcd60e51b81526020600482015260146024820152731cda1bdd5b19081b9bdd081899481c185d5cd95960621b604482015290519081900360640190fd5b670de0b6b3a764000047116107db576040805162461bcd60e51b81526020600482015260166024820152753330bab1b2ba1034b99037baba1037b31032ba3432b960511b604482015290519081900360640190fd5b6001600160a01b038216610836576040805162461bcd60e51b815260206004820152601a60248201527f72656365697665722073686f756c64206e6f7420626520307830000000000000604482015290519081900360640190fd5b6001600160a01b038216600090815260046020526040902054421161088c5760405162461bcd60e51b815260040180806020018281038252603e815260200180611249603e913960400191505060405180910390fd5b670de0b6b3a7640000826001600160a01b031631106108dc5760405162461bcd60e51b81526004018080602001828103825260538152602001806112e26053913960600191505060405180910390fd5b6108ee4261546063ffffffff61112d16565b6001600160a01b038316600081815260046020526040808220939093559151670de0b6b3a76400009290839082818181858883f19350505050158015610938573d6000803e3d6000fd5b506040805182815290516001600160a01b038516917f514687833669e436050a6109bfcf65fc63b44af6d5fd457d169761f3c3c89aa2919081900360200190a250600192915050565b6000546001600160a01b031633146109ce576040805162461bcd60e51b815260206004820152601460248201526000805160206111f8833981519152604482015290519081900360640190fd5b600154600160a01b900460ff16610a1f576040805162461bcd60e51b815260206004820152601060248201526f1cda1bdd5b19081899481c185d5cd95960821b604482015290519081900360640190fd5b6001805460ff60a01b1916905560405133907ff779549bb18027d8b598371be0088b09fcca4a91e09288bc6fc485e0865b52d690600090a2565b6003546001600160a01b031690565b600080546001600160a01b03163314610ab6576040805162461bcd60e51b815260206004820152601460248201526000805160206111f8833981519152604482015290519081900360640190fd5b6002546001600160a01b0316610b0b576040805162461bcd60e51b81526020600482015260156024820152746e6f206f70657261746f7220746f207265766f6b6560581b604482015290519081900360640190fd5b6002546040516001600160a01b039091169033907fedc095f99476acb6b4bcdc9715767369f8d423a2ca208c458234b1addabebf1490600090a350600280546001600160a01b0319169055600190565b6000546001600160a01b03163314610ba8576040805162461bcd60e51b815260206004820152601460248201526000805160206111f8833981519152604482015290519081900360640190fd5b600154600160a01b900460ff1615610bfe576040805162461bcd60e51b81526020600482015260146024820152731cda1bdd5b19081b9bdd081899481c185d5cd95960621b604482015290519081900360640190fd5b6001805460ff60a01b1916600160a01b17905560405133907fce3af5a3fdaee3c4327c1c434ea1d2186d7a9495f005d2a876dca182bd14571490600090a2565b6000546001600160a01b031690565b6001546000906001600160a01b03163314610caf576040805162461bcd60e51b815260206004820152601760248201527f73686f756c64206e6f742061636365707420616761696e000000000000000000604482015290519081900360640190fd5b60008054336001600160a01b0319918216811783556001805490921690915560405190917f2f68a0b3010e34d13a0d06c38a40584536548684eb0056ede21aaf532de3bc4b91a250600190565b60046020526000908152604090205481565b60408051348152905160009133917f62fde48a1f1e1f53dbcc1bdf035a07786f0bb6b69ec77b897bd1f42f4cd735a39181900360200190a250600190565b600080546001600160a01b03163314610d9a576040805162461bcd60e51b815260206004820152601460248201526000805160206111f8833981519152604482015290519081900360640190fd5b6003546001600160a01b031615610de25760405162461bcd60e51b81526004018080602001828103825260228152602001806111b56022913960400191505060405180910390fd5b6001600160a01b038216610e275760405162461bcd60e51b81526004018080602001828103825260248152602001806112876024913960400191505060405180910390fd5b610e2f610c3e565b6001600160a01b0316826001600160a01b03161415610e7f5760405162461bcd60e51b815260040180806020018281038252602681526020018061118f6026913960400191505060405180910390fd5b6002546001600160a01b0383811691161415610ecc5760405162461bcd60e51b815260040180806020018281038252603a815260200180611358603a913960400191505060405180910390fd5b600380546001600160a01b0319166001600160a01b03841690811790915560405133907f8524af1c68633766abb9b9dd9284a71f054a6c21cebc36c0a0dc9d70bcfa5fbb90600090a3506001919050565b600080546001600160a01b03163314610f6b576040805162461bcd60e51b815260206004820152601460248201526000805160206111f8833981519152604482015290519081900360640190fd5b6001546001600160a01b0316610fc8576040805162461bcd60e51b815260206004820152601c60248201527f6e6f206f776e65722063616e64696461746520746f207265766f6b6500000000604482015290519081900360640190fd5b6001546040516001600160a01b03909116907f1666cd85208a12e36d84f0213694d7fa987cc021f41c91a4520950176d98293790600090a250600180546001600160a01b031916815590565b6002546001600160a01b031690565b4790565b6001600160a01b03163190565b600080546001600160a01b03163314611082576040805162461bcd60e51b815260206004820152601460248201526000805160206111f8833981519152604482015290519081900360640190fd5b6003546001600160a01b03166110df576040805162461bcd60e51b815260206004820152601f60248201527f6e6f206f70657261746f722063616e64696461746520746f207265766f6b6500604482015290519081900360640190fd5b6003546040516001600160a01b03909116907fcb4610e28c7f8296925fbbec63951ed15ed3092521ea2490d967d215a36c277790600090a250600380546001600160a01b0319169055600190565b600082820183811015611187576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b939250505056fe6f70657261746f722063616e6469646174652073686f756c64206e6f74206265206f776e65726f70657261746f722063616e6469646174652073686f756c6420626520756e7365746f776e65722063616e6469646174652073686f756c64206e6f742062652030783073686f756c64206265206f6e6c79206f776e657200000000000000000000000073686f756c64206e6f742072657175657374207468652073616d65206f776e65722063616e64696461746520616761696e72656365697665722063616e206f6e6c792072657175657374206574686572206f6e636520696e20612073697820686f75722074696d652077696e646f776f70657261746f722063616e6469646174652073686f756c64206e6f74206265203078306f776e65722063616e6469646174652073686f756c64206e6f74206265207468652073616d652077697468207468652063757272656e7466617563657420776f6e27742073656e6420657468657220746f20726563697069656e7473207769746820612062616c616e63652067726561746572207468616e206f7220657175616c20746f2031206574686e6f7420616c6c6f7720746f2063616c6c2066616c6c6261636b2066756e6374696f6e6f70657261746f722063616e6469646174652073686f756c64206e6f74206265207468652073616d652077697468207468652063757272656e7473656e6465722073686f756c64206265206f70657261746f722063616e646964617465a265627a7a72315820a1887191706f4f62020796e883a22a5f25ad857f517fb96d3afda2390b84d13a64736f6c63430005110032',
],
],
},
{
name: 'TestMBBLOCK',
skip: false,
Expand Down
8 changes: 3 additions & 5 deletions src/library/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ function extractSelectFilterCounts(header) {
function clampBool(value, def) {
// clamp value to a valid bool with a default
let final;
if (!value) {
if (value === undefined || value === null) {
final = def;
} else if (value === true) {
final = true;
} else if (value === false) {
} else if (value === '') {
final = false;
} else {
final = def;
final = value;
}

return final;
Expand Down
8 changes: 7 additions & 1 deletion test/mbSheetsAddOn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const { google } = require('googleapis');
const { authenticate } = require('./auth');
require('dotenv').config({ path: '../.env' });

/**
* Call the "testRunner" to test function on the deployed script.
Expand All @@ -11,9 +12,14 @@ function callAppsScript(auth) {
const script = google.script({ version: 'v1' });
const { scriptId } = JSON.parse(fs.readFileSync('../.clasp.json'));

const testSheetURL = process.env.TEST_SHEET_URL;
if (!testSheetURL) {
throw new Error('No test sheet URL');
}

const request = {
function: 'testRunner',
parameters: [],
parameters: [testSheetURL],
devMode: true, // Sue always the latest pushed codes
};

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ doctrine@^3.0.0:
dependencies:
esutils "^2.0.2"

dotenv@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==

[email protected], ecdsa-sig-formatter@^1.0.11:
version "1.0.11"
resolved "https://registry.yarnpkg.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz#ae0f0fa2d85045ef14a817daa3ce9acd0489e5bf"
Expand Down

0 comments on commit 6518546

Please sign in to comment.