Skip to content

Instantly share code, notes, and snippets.

@jsonMartin
Created September 9, 2021 01:27
Show Gist options
  • Save jsonMartin/02a4bddfd9eaa671db93e0a7f41dcbc4 to your computer and use it in GitHub Desktop.
Save jsonMartin/02a4bddfd9eaa671db93e0a7f41dcbc4 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.0+commit.c7dfd78e.js&optimize=false&runs=200&gist=
//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
contract Lottery {
address payable[] public players;
address public manager;
constructor() {
manager = msg.sender;
}
receive() external payable {
require(msg.value == 0.1 ether);
players.push(payable(msg.sender));
}
function getBalance() public view returns(uint) {
require(msg.sender == manager);
return address(this).balance;
}
function random() public view returns(uint) {
return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, players.length)));
}
function pickWinner() public returns(address) {
require(msg.sender == manager);
require(players.length >= 3);
address payable winner = players[random() % players.length];
payWinner(winner);
resetLottery();
return winner;
}
function payWinner(address payable winner) private {
winner.transfer(getBalance());
}
function resetLottery() private {
players = new address payable[](0);
}
}
// Right click on the script name and hit "Run" to execute
(async () => {
try {
console.log('Running deployWithEthers script...')
const contractName = 'Storage' // Change this for other contract
const constructorArgs = [] // Put constructor args (if any) here for your contract
// Note that the script needs the ABI which is generated from the compilation artifact.
// Make sure contract is compiled and artifacts are generated
const artifactsPath = `browser/contracts/artifacts/${contractName}.json` // Change this for different path
const metadata = JSON.parse(await remix.call('fileManager', 'getFile', artifactsPath))
// 'web3Provider' is a remix global variable object
const signer = (new ethers.providers.Web3Provider(web3Provider)).getSigner()
let factory = new ethers.ContractFactory(metadata.abi, metadata.data.bytecode.object, signer);
let contract = await factory.deploy(...constructorArgs);
console.log('Contract Address: ', contract.address);
// The contract is NOT deployed yet; we must wait until it is mined
await contract.deployed()
console.log('Deployment successful.')
} catch (e) {
console.log(e.message)
}
})()
// Right click on the script name and hit "Run" to execute
(async () => {
try {
console.log('Running deployWithWeb3 script...')
const contractName = 'Storage' // Change this for other contract
const constructorArgs = [] // Put constructor args (if any) here for your contract
// Note that the script needs the ABI which is generated from the compilation artifact.
// Make sure contract is compiled and artifacts are generated
const artifactsPath = `browser/contracts/artifacts/${contractName}.json` // Change this for different path
const metadata = JSON.parse(await remix.call('fileManager', 'getFile', artifactsPath))
const accounts = await web3.eth.getAccounts()
let contract = new web3.eth.Contract(metadata.abi)
contract = contract.deploy({
data: metadata.data.bytecode.object,
arguments: constructorArgs
})
const newContractInstance = await contract.send({
from: accounts[0],
gas: 1500000,
gasPrice: '30000000000'
})
console.log('Contract deployed at address: ', newContractInstance.options.address)
} catch (e) {
console.log(e.message)
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment