From 0a51ed95fc68f73e1a0f6d8eeea68f25e501c783 Mon Sep 17 00:00:00 2001 From: Aniket-Engg Date: Tue, 10 May 2022 11:35:13 +0530 Subject: [PATCH] erc20 template --- .../erc20/contracts/SampleERC20.sol | 15 ++++++++++++ libs/remix-ws-templates/erc20/index.js | 8 +++++++ .../erc20/scripts/deploy_with_ethers.ts | 10 ++++++++ .../erc20/scripts/deploy_with_web3.ts | 10 ++++++++ .../erc20/scripts/ethers.ts | 24 +++++++++++++++++++ libs/remix-ws-templates/erc20/scripts/web3.ts | 24 +++++++++++++++++++ .../erc20/tests/SampleERC20_test.sol | 18 ++++++++++++++ libs/remix-ws-templates/index.js | 2 +- 8 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 libs/remix-ws-templates/erc20/contracts/SampleERC20.sol create mode 100644 libs/remix-ws-templates/erc20/index.js create mode 100644 libs/remix-ws-templates/erc20/scripts/deploy_with_ethers.ts create mode 100644 libs/remix-ws-templates/erc20/scripts/deploy_with_web3.ts create mode 100644 libs/remix-ws-templates/erc20/scripts/ethers.ts create mode 100644 libs/remix-ws-templates/erc20/scripts/web3.ts create mode 100644 libs/remix-ws-templates/erc20/tests/SampleERC20_test.sol diff --git a/libs/remix-ws-templates/erc20/contracts/SampleERC20.sol b/libs/remix-ws-templates/erc20/contracts/SampleERC20.sol new file mode 100644 index 0000000000..23aa792466 --- /dev/null +++ b/libs/remix-ws-templates/erc20/contracts/SampleERC20.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-3.0 + +pragma solidity >=0.7.0 <0.9.0; + +/** + * @title SampleERC20 + * @dev Create a sample ERC20 standard token + */ + +import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; + +contract SampleERC20 is ERC20 { + + constructor(string memory tokenName, string memory tokenSymbol) ERC20(tokenName, tokenSymbol) {} +} \ No newline at end of file diff --git a/libs/remix-ws-templates/erc20/index.js b/libs/remix-ws-templates/erc20/index.js new file mode 100644 index 0000000000..e3a5cbf078 --- /dev/null +++ b/libs/remix-ws-templates/erc20/index.js @@ -0,0 +1,8 @@ +export default { + 'contracts/SampleERC20.sol': require('./contracts/SampleERC20.sol').default, + 'scripts/deploy_with_ethers.ts': require('./scripts/deploy_with_ethers.ts').default, + 'scripts/deploy_with_web3.ts': require('./scripts/deploy_with_web3.ts').default, + 'scripts/ethers.ts': require('./scripts/ethers.ts').default, + 'scripts/web3.ts': require('./scripts/web3.ts').default, + 'tests/SampleERC20_test.sol': require('./tests/SampleERC20_test.sol').default +} \ No newline at end of file diff --git a/libs/remix-ws-templates/erc20/scripts/deploy_with_ethers.ts b/libs/remix-ws-templates/erc20/scripts/deploy_with_ethers.ts new file mode 100644 index 0000000000..dc2f5286c4 --- /dev/null +++ b/libs/remix-ws-templates/erc20/scripts/deploy_with_ethers.ts @@ -0,0 +1,10 @@ +import { deploy } from './ethers.ts' + +(async () => { + try { + const result = await deploy('SampleERC20', ['testToken', 'TST']) + console.log(`address: ${result.address}`) + } catch (e) { + console.log(e.message) + } + })() \ No newline at end of file diff --git a/libs/remix-ws-templates/erc20/scripts/deploy_with_web3.ts b/libs/remix-ws-templates/erc20/scripts/deploy_with_web3.ts new file mode 100644 index 0000000000..e6dddf65f0 --- /dev/null +++ b/libs/remix-ws-templates/erc20/scripts/deploy_with_web3.ts @@ -0,0 +1,10 @@ +import { deploy } from './web3.ts' + +(async () => { + try { + const result = await deploy('SampleERC20', ['testToken', 'TST']) + console.log(`address: ${result.address}`) + } catch (e) { + console.log(e.message) + } +})() \ No newline at end of file diff --git a/libs/remix-ws-templates/erc20/scripts/ethers.ts b/libs/remix-ws-templates/erc20/scripts/ethers.ts new file mode 100644 index 0000000000..16fbd8c4d7 --- /dev/null +++ b/libs/remix-ws-templates/erc20/scripts/ethers.ts @@ -0,0 +1,24 @@ +export const deploy = async (contractName: string, args: Array, from?: string) => { + + 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)) + // '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 + if (from) { + contract = await factory.connect(from).deploy(...args); + } else { + contract = await factory.deploy(...args); + } + + // The contract is NOT deployed yet; we must wait until it is mined + await contract.deployed() + return contract +}: Promise \ No newline at end of file diff --git a/libs/remix-ws-templates/erc20/scripts/web3.ts b/libs/remix-ws-templates/erc20/scripts/web3.ts new file mode 100644 index 0000000000..6fc26b2030 --- /dev/null +++ b/libs/remix-ws-templates/erc20/scripts/web3.ts @@ -0,0 +1,24 @@ +export const deploy = async (contractName: string, args: Array, from?: string, gas?: number) => { + + 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() + + let contract = new web3.eth.Contract(metadata.abi) + + contract = contract.deploy({ + data: metadata.data.bytecode.object, + arguments: args + }) + + const newContractInstance = await contract.send({ + from: from || accounts[0], + gas: gas || 1500000 + }) + return newContractInstance.options +}: Promise \ No newline at end of file diff --git a/libs/remix-ws-templates/erc20/tests/SampleERC20_test.sol b/libs/remix-ws-templates/erc20/tests/SampleERC20_test.sol new file mode 100644 index 0000000000..45087c816c --- /dev/null +++ b/libs/remix-ws-templates/erc20/tests/SampleERC20_test.sol @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-3.0 + +pragma solidity >=0.7.0 <0.9.0; +import "remix_tests.sol"; +import "../contracts/SampleERC20.sol"; + +contract SampleERC20Test { + + SampleERC20 s; + function beforeAll () public { + s = new SampleERC20("TestToken", "TST"); + } + + function testTokenNameAndSymbol () public { + Assert.equal(s.name(), "TestToken", "token name did not match"); + Assert.equal(s.symbol(), "TST", "token symbol did not match"); + } +} \ No newline at end of file diff --git a/libs/remix-ws-templates/index.js b/libs/remix-ws-templates/index.js index 949ea8cdda..a076cfa671 100644 --- a/libs/remix-ws-templates/index.js +++ b/libs/remix-ws-templates/index.js @@ -1,3 +1,3 @@ export { default as remixDefault } from './remixDefault' -// export { default as erc20 } from './erc20' +export { default as erc20 } from './erc20' // export { default as blank } from './blank'