NodeJS Script
WEB3.JS - NodeJS Script
Smart contracts can be deployed with just the use of web3.js as well. Below you will find a simple script for using NodeJS and web3.
NOTE: Web3 and solc versions matter for compatibility. Web3 1.0.0-beta.35 and solc version 0.5.4 work well together, but other version combinations can cause unexpected errors.
For more information on using web3.js, please see Web3.js Getting Started Documentation.
1
/*
2
* This nodeJS script will deploy your smart contracts to your new S-Chain.
3
*
4
* @param {String} private key - Provide your private key.
5
* @param {String} account - Provide your account address.
6
* @param {String} SKALE Chain endpoint - provide your SKALE Chain endpoint
7
* @param {String} contractName - Name of your smart contract (i.e. MySmartContract)
8
* @param {String} contractFileName - Complete filename of contract (i.e. MySmartContract.sol)
9
*/
10
11
const Web3 = require('web3'); // version 1.0.0-beta.35
12
const solc = require('solc'); // version 0.5.4
13
const path = require('path');
14
const fs = require('fs');
15
16
let privateKey = "[YOUR_PRIVATE_KEY]";
17
let account = "[YOUR_ACCOUNT_ADDRESS]";
18
let schainEndpoint = "[YOUR_SKALE_CHAIN_ENDPOINT]";
19
20
let contractName = "HelloSKALE"; //replace with your contract name
21
let contractFileName = "HelloSKALE.sol"; //replace with the filename of the contract
22
23
//Retrieve and compile contract source code
24
const contractPath = path.resolve(__dirname, 'contracts', contractFileName);
25
const contractContent = fs.readFileSync(contractPath, 'UTF-8');
26
27
//Format contract for solc reader
28
var contracts = {
29
language: 'Solidity',
30
sources: {},
31
settings: {
32
outputSelection: {
33
'*': {
34
'*': [ '*' ]
35
}
36
}
37
}
38
}
39
40
//add HelloSKALE contract to contract sources
41
contracts.sources[contractFileName] = { content: contractContent };
42
43
//compile data via solc reader
44
let solcOutput = JSON.parse(solc.compile(JSON.stringify(contracts)));
45
46
//get compile HelloSKALE contract
47
let contractCompiled = solcOutput.contracts[contractFileName][contractName];
48
49
//Connect Web3 to your SKALE Chain
50
const web3 = new Web3(new Web3.providers.HttpProvider(schainEndpoint));
51
52
53
//create transaction
54
var tx = {
55
data : '0x' + contractCompiled.evm.bytecode.object,
56
from: account,
57
gasPrice: 0,
58
gas: 80000000
59
};
60
61
//sign transaction to deploy contract
62
web3.eth.accounts.signTransaction(tx, privateKey).then(signed => {
63
web3.eth.sendSignedTransaction(signed.rawTransaction).
64
on('receipt', receipt => {
65
console.log(receipt)
66
}).
67
catch(console.error);
68
});
Copied!
Last modified 1yr ago
Copy link