Skip to content

Instantly share code, notes, and snippets.

@dylangolow
Created February 8, 2021 08:03
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 dylangolow/4a9a22904c76bebd4dad0e84e6c60e16 to your computer and use it in GitHub Desktop.
Save dylangolow/4a9a22904c76bebd4dad0e84e6c60e16 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.7.6+commit.7338295f.js&optimize=false&runs=200&gist=
// 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 = 'HotPotato' // Change this for other contract
const constructorArgs = [5] // 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)
}
})()
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.8.0;
/**
* @title HotPotato
* @dev Implements voting process along with vote delegation
*/
contract HotPotato {
uint public potatoCount;
address public creator;
bool public gameIsOn;
struct Player {
uint potatoCount;
}
event PotatoPassed(address _oldOwner, address _newOwner);
event GameEvent(bool on);
mapping(address => Player) public players;
constructor(uint _startingPotatoes) {
creator = msg.sender;
potatoCount = _startingPotatoes;
gameIsOn = true;
emit GameEvent(true);
}
function passPotato(address _to) public {
players[_to].potatoCount += 1;
emit PotatoPassed(msg.sender, _to);
if (players[_to].potatoCount > 2) {
gameIsOn = false;
emit GameEvent(false);
}
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.8.0;
import "remix_tests.sol"; // this import is automatically injected by Remix.
// import "remix_accounts.sol";
import "../contracts/1_HotPotato.sol";
contract PotatoTest {
HotPotato potatoGameTest;
function beforeAll () public {
potatoGameTest = new HotPotato(5);
Assert.ok(potatoGameTest.gameIsOn() == true, "Game is on!");
}
function checkRemainingPotatoes () public {
// Assert.ok(potatoGameTest.players[address(msg.sender)].potatoCount() == 0, "Player should have 0 potatoes");
// potatoGameTest.passPotato(msg.sender);
// Assert.equal(uint(potatoGameTest.players[msg.sender].potatoCount), uint(1), "Player should have 1 potato");
// potatoGameTest.passPotato(msg.sender);
// potatoGameTest.passPotato(msg.sender);
// Assert.equal(potatoGameTest.players[msg.sender].potatoCount, uint(3), "Player should have 3 potatoes");
// Assert.ok(potatoGameTest.gameIsOn() == false, "Game is over!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment