From 8b21162f59afdd28f8bf2744654e2f908403f2f4 Mon Sep 17 00:00:00 2001 From: Tal Kol Date: Thu, 5 May 2022 18:06:03 +0100 Subject: [PATCH] Add support for glitch --- .gitignore | 5 ++++- .setup.glitch.sh | 8 ++++++++ README.md | 7 +++++-- build/.gitignore | 3 +-- build/build.ts | 6 ++++++ build/deploy.ts | 28 +++++++++++++++------------- package-lock.json | 17 +++++++++++++++++ package.json | 4 +++- 8 files changed, 59 insertions(+), 19 deletions(-) create mode 100755 .setup.glitch.sh diff --git a/.gitignore b/.gitignore index b512c09..04ba952 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ -node_modules \ No newline at end of file +node_modules +.env +.wget-hsts +bin \ No newline at end of file diff --git a/.setup.glitch.sh b/.setup.glitch.sh new file mode 100755 index 0000000..434f6b6 --- /dev/null +++ b/.setup.glitch.sh @@ -0,0 +1,8 @@ +# this will download all dependencies for Ubuntu 16 (fift, func executables) and place them in ./bin +mkdir bin +wget https://github.com/ton-defi-org/ton-binaries/releases/download/ubuntu-16/fift -P ./bin +chmod +x ./bin/fift +wget https://github.com/ton-defi-org/ton-binaries/releases/download/ubuntu-16/func -P ./bin +chmod +x ./bin/func +wget https://github.com/ton-defi-org/ton-binaries/releases/download/fiftlib/fiftlib.zip -P ./bin +unzip ./bin/fiftlib.zip -d ./bin/fiftlib \ No newline at end of file diff --git a/README.md b/README.md index 2d7e321..5993e93 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Some of the opinionated choices made here include: ## Dependencies and requirements -To setup your machine for development, please make sure you have the following: +To setup your local machine for development, please make sure you have the following: * A modern version of Node.js * Installation instructions can be found [here](https://nodejs.org/) @@ -45,6 +45,8 @@ To setup your machine for development, please make sure you have the following: * A decent IDE with FunC and TypeScript support * We recommend using [Visual Studio Code](https://code.visualstudio.com/) with the [FunC plugin](https://marketplace.visualstudio.com/items?itemName=tonwhales.func-vscode) installed +Alternatively, you can ignore the above requirements and develop in your web browser with an online IDE and zero setup. Simply open this repo inside [Glitch](https://glitch.com/) without installing anything. + ## Development instructions * Install @@ -77,7 +79,8 @@ To setup your machine for development, please make sure you have the following: * Deploy * Make sure all contracts are built and your setup is ready to deploy: * Each contract to deploy should have a script `build/mycontract.deploy.ts` to return its init data cell - * The deployment wallet is configured in `build/deploy.config.json` (file will be created if not found) + * The deployment wallet is configured in `.env` (will be created automatically if not exists), with contents:
+ `DEPLOYER_MNEMONIC="mad nation chief flavor ..."` * To deploy to mainnet (production), run in terminal `npm run deploy` * To deploy to testnet instead (where TON is free), run `npm run deploy:testnet` * Follow the on-screen instructions of the deploy script diff --git a/build/.gitignore b/build/.gitignore index 3872f92..22e613e 100644 --- a/build/.gitignore +++ b/build/.gitignore @@ -1,4 +1,3 @@ *.fif *.fc -*.cell -deploy.config.json \ No newline at end of file +*.cell \ No newline at end of file diff --git a/build/build.ts b/build/build.ts index edad113..ead5264 100644 --- a/build/build.ts +++ b/build/build.ts @@ -16,6 +16,12 @@ async function main() { console.log(`=================================================================`); console.log(`Build script running, let's find some FunC contracts to compile..`); + // if we have an explicit bin directory, use the executables there (needed for glitch.com) + if (fs.existsSync("bin")) { + process.env.PATH = __dirname + "/../bin/:" + process.env.PATH; + process.env.FIFTPATH = __dirname + "/../bin/fiftlib"; + } + // make sure func compiler is available let funcVersion = ""; try { diff --git a/build/deploy.ts b/build/deploy.ts index 3b551c6..2ab2219 100644 --- a/build/deploy.ts +++ b/build/deploy.ts @@ -2,12 +2,15 @@ // Every contract you want to deploy should have a mycontract.deploy.ts script that returns its init data // The script assumes that it is running from the repo root, and the directories are organized this way: // ./build/ - directory for build artifacts (mycontract.cell) and deploy init data scripts (mycontract.deploy.ts) -// ./build/deploy.config.json - JSON config file with secret mnemonic of deploying wallet (will be created if not found) +// ./.env - config file with DEPLOYER_MNEMONIC - secret mnemonic of deploying wallet (will be created if not found) import axios from "axios"; import axiosThrottle from "axios-request-throttle"; axiosThrottle.use(axios, { requestsPerSecond: 0.5 }); // required since toncenter jsonRPC limits to 1 req/sec without API key +import dotenv from "dotenv"; +dotenv.config(); + import fs from "fs"; import path from "path"; import glob from "fast-glob"; @@ -28,27 +31,26 @@ async function main() { // initialize globals const client = new TonClient({ endpoint: `https://${process.env.TESTNET ? "testnet." : ""}toncenter.com/api/v2/jsonRPC` }); - const deployerWalletType = "org.ton.wallets.v3.r2"; // see WalletV3R2Source class used below + const deployerWalletType = "org.ton.wallets.v3.r2"; // also see WalletV3R2Source class used below const newContractFunding = toNano(0.02); // this will be (almost in full) the balance of a new deployed contract and allow it to pay rent const workchain = 0; // normally 0, only special contracts should be deployed to masterchain (-1) // make sure we have a wallet mnemonic to deploy from (or create one if not found) - const deployConfigJson = `build/deploy.config.json`; + const deployConfigEnv = ".env"; let deployerMnemonic; - if (!fs.existsSync(deployConfigJson)) { - console.log(`\n* Config file '${deployConfigJson}' not found, creating a new wallet for deploy..`); + if (!fs.existsSync(deployConfigEnv)) { + console.log(`\n* Config file '${deployConfigEnv}' not found, creating a new wallet for deploy..`); deployerMnemonic = (await mnemonicNew(24)).join(" "); - const deployWalletJsonContent = { created: new Date().toISOString(), deployerWalletType, deployerMnemonic }; - fs.writeFileSync(deployConfigJson, JSON.stringify(deployWalletJsonContent, null, 2)); - console.log(` - Created new wallet in '${deployConfigJson}' - keep this file secret!`); + const deployWalletEnvContent = `DEPLOYER_WALLET=${deployerWalletType}\nDEPLOYER_MNEMONIC="${deployerMnemonic}"\n`; + fs.writeFileSync(deployConfigEnv, deployWalletEnvContent); + console.log(` - Created new wallet in '${deployConfigEnv}' - keep this file secret!`); } else { - console.log(`\n* Config file '${deployConfigJson}' found and will be used for deployment!`); - const deployConfigJsonContent = require(__dirname + "/../" + deployConfigJson); - if (!deployConfigJsonContent.deployerMnemonic) { - console.log(` - ERROR: '${deployConfigJson}' does not have the key 'deployerMnemonic'`); + console.log(`\n* Config file '${deployConfigEnv}' found and will be used for deployment!`); + if (!process.env.DEPLOYER_MNEMONIC) { + console.log(` - ERROR: '${deployConfigEnv}' does not contain key 'DEPLOYER_MNEMONIC'`); process.exit(1); } - deployerMnemonic = deployConfigJsonContent.deployerMnemonic; + deployerMnemonic = process.env.DEPLOYER_MNEMONIC; } // open the wallet and make sure it has enough TON diff --git a/package-lock.json b/package-lock.json index aa3e891..a8f4af5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,7 @@ "": { "name": "tonstarter-contracts", "version": "0.0.0", + "hasInstallScript": true, "license": "MIT", "devDependencies": { "@types/bn.js": "^5.1.0", @@ -15,6 +16,7 @@ "axios-request-throttle": "^1.0.0", "chai": "^4.3.4", "chai-bn": "^0.3.1", + "dotenv": "^16.0.0", "fast-glob": "^3.2.11", "mocha": "^9.1.3", "prando": "^6.0.1", @@ -522,6 +524,15 @@ "node": ">=0.3.1" } }, + "node_modules/dotenv": { + "version": "16.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.0.tgz", + "integrity": "sha512-qD9WU0MPM4SWLPJy/r2Be+2WgQj8plChsyrCNQzW/0WjvcJQiKQJ9mH3ZgB3fxbUUxgc/11ZJ0Fi5KiimWGz2Q==", + "dev": true, + "engines": { + "node": ">=12" + } + }, "node_modules/emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", @@ -2092,6 +2103,12 @@ "integrity": "sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w==", "dev": true }, + "dotenv": { + "version": "16.0.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.0.tgz", + "integrity": "sha512-qD9WU0MPM4SWLPJy/r2Be+2WgQj8plChsyrCNQzW/0WjvcJQiKQJ9mH3ZgB3fxbUUxgc/11ZJ0Fi5KiimWGz2Q==", + "dev": true + }, "emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", diff --git a/package.json b/package.json index 31ad7e0..163d412 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "test": "mocha --exit test/**/*.spec.ts", "build": "ts-node ./build/build.ts", "deploy": "ts-node ./build/deploy.ts", - "deploy:testnet": "export TESTNET=1 && ts-node ./build/deploy.ts" + "deploy:testnet": "export TESTNET=1 && ts-node ./build/deploy.ts", + "postinstall": "! test -f /app/.glitchdotcom.json || test -d /app/bin || bash .setup.glitch.sh" }, "devDependencies": { "@types/bn.js": "^5.1.0", @@ -18,6 +19,7 @@ "axios-request-throttle": "^1.0.0", "chai": "^4.3.4", "chai-bn": "^0.3.1", + "dotenv": "^16.0.0", "fast-glob": "^3.2.11", "mocha": "^9.1.3", "prando": "^6.0.1",