Skip to content

Instantly share code, notes, and snippets.

@trongdth
Created September 3, 2019 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trongdth/633b4a1d241b3e0619ae8beeeb01c891 to your computer and use it in GitHub Desktop.
Save trongdth/633b4a1d241b3e0619ae8beeeb01c891 to your computer and use it in GitHub Desktop.
ethers.js
var fs = require('fs');
var solc = require('solc');
var ethers = require('ethers');
var input = {
language: 'Solidity',
sources: {
'SecuredLoan.sol': fs.readFileSync('/Users/trongdth/Golang/src/github.com/mroom-software/blockpass-smart-contract/Ethereum/contracts/SecuredLoan.sol', 'utf-8')
}
}
var output = solc.compile(input, 1)
const bytecode = output.contracts['SecuredLoan.sol:SecuredLoan'].bytecode
const abi = JSON.parse(output.contracts['SecuredLoan.sol:SecuredLoan'].interface)
// Connect to the network
let provider = ethers.getDefaultProvider('mainnet');
// Load the wallet to deploy the contract with
let privateKey = '';
let wallet = new ethers.Wallet(privateKey, provider);
// Deployment is asynchronous, so we use an async IIFE
(async function() {
// Create an instance of a Contract Factory
let factory = new ethers.ContractFactory(abi, bytecode, wallet);
// Notice we pass in "Hello World" as the parameter to the constructor
let contract = await factory.deploy();
// The address the Contract WILL have once mined
// See: https://ropsten.etherscan.io/address/0x2bd9aaa2953f988153c8629926d22a6a5f69b14e
console.log(contract.address);
// "0x0C4e25a3e40C26B73F566f619E2a6Ca72b340880"
// The transaction that was sent to the network to deploy the Contract
// See: https://ropsten.etherscan.io/tx/0x159b76843662a15bd67e482dcfbee55e8e44efad26c5a614245e12a00d4b1a51
console.log(contract.deployTransaction.hash);
// "0xad62b23ac537beafe8f301793bd6e121ec430dfdb20cd41a667b85e7ef0d5222"
// The contract is NOT deployed yet; we must wait until it is mined
await contract.deployed()
// Done! The contract is deployed.
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment