How to deploy ganache server on cloud

Donghun Seol·2022년 12월 31일
1

Setting cloud ganache server for dev

To provide blockchain integrity between dev team members, uninterrupted blockchain server is needed. and public test network such as goerli is too slow. Heres how to deploy your own ganache network on cloud vm

1. Get a VM from any clouds

start a vm. aws freetier t2 micro is ok
go to network setting allow port 8545 inbound traffic
ssh into vm

2. Install NVM and Node.js 16

sudo apt install curl
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.profile
nvm install 16

3. Install Ganache CLI

npm install -g ganache-cli

4. Start Ganache

ganache-cli --host 0.0.0.0 --port 8545 --networkId 5777

5. check connection from local env, http and websocket providers both work

use below code to test connection
ref : https://dev.to/zenika/build-your-own-remote-private-blockchain-with-aws-and-ganache-4330

const Web3 = require('web3');
const web3 = new Web3(`http://<VM_PUBLIC_IP>:8545`); // http provider
// const web3 = new Web3(`ws://<VM_PUBLIC_IP>:8545`); // websocket provider

const main = async () => {
  const accounts = await web3.eth.getAccounts();

  const accountOneBalance = await web3.eth
    .getBalance(accounts[0])
    .then((b) => web3.utils.fromWei(b, 'ether'));

  console.log('accountOneBalance', accountOneBalance);
};
main();

Setting advanced features

1. locale setting for vm

ref : https://www.joinc.co.kr/w/Site/Linux/Locale

sudo locale-gen ko_KR.UTF-8

cat /etc/default/locale
LANG="ko_KR.UTF-8"

TZ='Asia/Seoul' date "+%Y-%m-%d:%T"

2. daemonized ganache setup shellscript for vm

use init.sh to make ganache runs on background even when terminal disconnected.

since it's running on background you need to use teardown.sh to stop ganache

logfile will be created upon every execution of init.sh

ref : https://unix.stackexchange.com/questions/420594/why-process-killed-with-nohup

init.sh

#!/bin/bash
touch ganache_$(TZ="Asia/Seoul" date "+%Y-%m-%d:%T").log
ganache-cli --host 0.0.0.0 --port 8545 --networkId 5777 --verbose > ganache_$(TZ="Asia/Seoul" date "+%Y-%m-%d:%T").log & disown
tail -f ganache_$(TZ="Asia/Seoul" date "+%Y-%m-%d:%T").log

teardown.sh

#!/bin/bash
kill $(pgrep node)
profile
I'm going from failure to failure without losing enthusiasm

0개의 댓글