This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Web3 = require("web3"); | |
window.web3 = new Web3(window.ethereum); | |
window.ethereum.enable(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MyContract from "./contracts/MyContract.json"; | |
const networkId = await web3.eth.net.getId(); | |
const deployedNetwork = MyContract.networks[networkId]; | |
const myContract = new web3.eth.Contract( | |
MyContract.abi, | |
deployedNetwork && deployedNetwork.address, | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const accounts = await web3.eth.getAccounts(); | |
const account = accounts[0]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
myContract.events.MyEvent({}) | |
.on('data', async function(event){ | |
console.log(event.returnValues); | |
// Do something here | |
}) | |
.on('error', console.error); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.5; | |
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | |
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol"; | |
contract MyERC20 is ERC20, ERC20Detailed { | |
constructor(string memory _name, string memory _symbol, uint8 _decimals) public ERC20Detailed(_name, _symbol, _decimals) { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.5; | |
import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; | |
contract MyERC721 is ERC721 { | |
struct Person { | |
string name; | |
uint8 age; | |
uint8 height; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.5; | |
import "@openzeppelin/contracts/math/SafeMath.sol"; | |
contract BasicSafeMath { | |
using SafeMath for uint; | |
function doSomeMath(uint _a, uint _b) public returns (uint) { | |
return _a.sub(_b); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.5; | |
import "@openzeppelin/contracts/math/SafeCast.sol"; | |
contract BasicSafeCast { | |
using SafeCast for uint; | |
function castToUint8(uint _a) public returns (uint8) { | |
return _a.toUint8(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.5; | |
import "@openzeppelin/contracts/ownership/Ownable.sol"; | |
contract OwnableContract is Ownable { | |
function restrictedFunction() public onlyOwner returns (uint) { | |
return 99; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function getAccount() { | |
const accounts = await ethereum.enable(); | |
const account = accounts[0]; | |
// do something with new account here | |
} | |
ethereum.on('accountsChanged', function (accounts) { | |
getAccount(); | |
}) |