Created
March 23, 2023 01:25
-
-
Save ruttosa/135913107c0b6f4a9bbcd42eac47ed1b 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.19+commit.7dd6d404.js&optimize=false&runs=200&gist=
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
{ | |
"overrides": [ | |
{ | |
"files": "*.sol", | |
"options": { | |
"printWidth": 80, | |
"tabWidth": 4, | |
"useTabs": false, | |
"singleQuote": false, | |
"bracketSpacing": false | |
} | |
}, | |
{ | |
"files": "*.yml", | |
"options": {} | |
}, | |
{ | |
"files": "*.yaml", | |
"options": {} | |
}, | |
{ | |
"files": "*.toml", | |
"options": {} | |
}, | |
{ | |
"files": "*.json", | |
"options": {} | |
}, | |
{ | |
"files": "*.js", | |
"options": {} | |
}, | |
{ | |
"files": "*.ts", | |
"options": {} | |
} | |
] | |
} |
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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.4; | |
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | |
contract MyToken is ERC20 { | |
constructor() ERC20("MyToken", "MTK") {} | |
} |
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 { deploy } from './ethers-lib' | |
(async () => { | |
try { | |
const result = await deploy('MyToken', []) | |
console.log(`address: ${result.address}`) | |
} catch (e) { | |
console.log(e.message) | |
} | |
})() |
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 { deploy } from './web3-lib' | |
(async () => { | |
try { | |
const result = await deploy('MyToken', []) | |
console.log(`address: ${result.address}`) | |
} catch (e) { | |
console.log(e.message) | |
} | |
})() |
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 { ethers } from 'ethers' | |
/** | |
* Deploy the given contract | |
* @param {string} contractName name of the contract to deploy | |
* @param {Array<any>} args list of constructor' parameters | |
* @param {Number} accountIndex account index from the exposed account | |
* @return {Contract} deployed contract | |
*/ | |
export const deploy = async (contractName: string, args: Array<any>, accountIndex?: number): Promise<ethers.Contract> => { | |
console.log(`deploying ${contractName}`) | |
// 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(accountIndex) | |
const factory = new ethers.ContractFactory(metadata.abi, metadata.data.bytecode.object, signer) | |
const contract = await factory.deploy(...args) | |
// The contract is NOT deployed yet; we must wait until it is mined | |
await contract.deployed() | |
return contract | |
} |
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 Web3 from 'web3' | |
import { Contract, ContractSendMethod, Options } from 'web3-eth-contract' | |
/** | |
* Deploy the given contract | |
* @param {string} contractName name of the contract to deploy | |
* @param {Array<any>} args list of constructor' parameters | |
* @param {string} from account used to send the transaction | |
* @param {number} gas gas limit | |
* @return {Options} deployed contract | |
*/ | |
export const deploy = async (contractName: string, args: Array<any>, from?: string, gas?: number): Promise<Options> => { | |
const web3 = new Web3(web3Provider) | |
console.log(`deploying ${contractName}`) | |
// 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` | |
const metadata = JSON.parse(await remix.call('fileManager', 'getFile', artifactsPath)) | |
const accounts = await web3.eth.getAccounts() | |
const contract: Contract = new web3.eth.Contract(metadata.abi) | |
const contractSend: ContractSendMethod = contract.deploy({ | |
data: metadata.data.bytecode.object, | |
arguments: args | |
}) | |
const newContractInstance = await contractSend.send({ | |
from: from || accounts[0], | |
gas: gas || 1500000 | |
}) | |
return newContractInstance.options | |
} |
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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
import "remix_tests.sol"; | |
import "../contracts/MyToken.sol"; | |
contract MyTokenTest is MyToken { | |
function testTokenInitialValues() public { | |
Assert.equal(name(), "MyToken", "token name did not match"); | |
Assert.equal(symbol(), "MTK", "token symbol did not match"); | |
Assert.equal(decimals(), 18, "token decimals did not match"); | |
Assert.equal(totalSupply(), 0, "token supply should be zero"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment