From 65a95c36e23cc000a243c163a2f7aedaaf3b1e10 Mon Sep 17 00:00:00 2001 From: Aniket-Engg Date: Thu, 19 May 2022 14:54:13 +0530 Subject: [PATCH] 0x erc20 ws template added --- .../workspace/src/lib/remix-ui-workspace.tsx | 1 + libs/remix-ws-templates/src/index.ts | 2 ++ .../zeroxErc20/contracts/SampleERC20.sol | 31 +++++++++++++++++++ .../src/templates/zeroxErc20/index.ts | 16 ++++++++++ .../zeroxErc20/scripts/deploy_with_ethers.ts | 10 ++++++ .../zeroxErc20/scripts/deploy_with_web3.ts | 10 ++++++ .../templates/zeroxErc20/scripts/ethers.ts | 24 ++++++++++++++ .../src/templates/zeroxErc20/scripts/web3.ts | 24 ++++++++++++++ .../zeroxErc20/tests/SampleERC20_test.sol | 18 +++++++++++ 9 files changed, 136 insertions(+) create mode 100644 libs/remix-ws-templates/src/templates/zeroxErc20/contracts/SampleERC20.sol create mode 100644 libs/remix-ws-templates/src/templates/zeroxErc20/index.ts create mode 100644 libs/remix-ws-templates/src/templates/zeroxErc20/scripts/deploy_with_ethers.ts create mode 100644 libs/remix-ws-templates/src/templates/zeroxErc20/scripts/deploy_with_web3.ts create mode 100644 libs/remix-ws-templates/src/templates/zeroxErc20/scripts/ethers.ts create mode 100644 libs/remix-ws-templates/src/templates/zeroxErc20/scripts/web3.ts create mode 100644 libs/remix-ws-templates/src/templates/zeroxErc20/tests/SampleERC20_test.sol diff --git a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx index 293b6d63cc..a32aec4b0a 100644 --- a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx +++ b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx @@ -134,6 +134,7 @@ export function Workspace () { + diff --git a/libs/remix-ws-templates/src/index.ts b/libs/remix-ws-templates/src/index.ts index 011ddba170..0e1863913a 100644 --- a/libs/remix-ws-templates/src/index.ts +++ b/libs/remix-ws-templates/src/index.ts @@ -1,4 +1,6 @@ export { default as remixDefault } from './templates/remixDefault' export { default as blank } from './templates/blank' export { default as ozerc20 } from './templates/ozerc20' +export { default as zeroxErc20 } from './templates/zeroxErc20' export { default as ozerc721 } from './templates/ozerc721' + diff --git a/libs/remix-ws-templates/src/templates/zeroxErc20/contracts/SampleERC20.sol b/libs/remix-ws-templates/src/templates/zeroxErc20/contracts/SampleERC20.sol new file mode 100644 index 0000000000..ecea8b4aa3 --- /dev/null +++ b/libs/remix-ws-templates/src/templates/zeroxErc20/contracts/SampleERC20.sol @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-3.0 + +pragma solidity ^0.5.9; + +import "@0x/contracts-erc20/contracts/src/ERC20Token.sol"; + +/** + * @title SampleERC20 + * @dev Create a sample ERC20 standard token + */ +contract SampleERC20 is ERC20Token { + + string public name; + string public symbol; + uint256 public decimals; + + constructor ( + string memory _name, + string memory _symbol, + uint256 _decimals, + uint256 _totalSupply + ) + public + { + name = _name; + symbol = _symbol; + decimals = _decimals; + _totalSupply = _totalSupply; + balances[msg.sender] = _totalSupply; + } +} \ No newline at end of file diff --git a/libs/remix-ws-templates/src/templates/zeroxErc20/index.ts b/libs/remix-ws-templates/src/templates/zeroxErc20/index.ts new file mode 100644 index 0000000000..cc1f087b55 --- /dev/null +++ b/libs/remix-ws-templates/src/templates/zeroxErc20/index.ts @@ -0,0 +1,16 @@ +export default async () => { + return { + // @ts-ignore + 'contracts/SampleERC20.sol': (await import('raw-loader!./contracts/SampleERC20.sol')).default, + // @ts-ignore + 'scripts/deploy_with_ethers.ts': (await import('!!raw-loader!./scripts/deploy_with_ethers.ts')).default, + // @ts-ignore + 'scripts/deploy_with_web3.ts': (await import('!!raw-loader!./scripts/deploy_with_web3.ts')).default, + // @ts-ignore + 'scripts/ethers.ts': (await import('!!raw-loader!./scripts/ethers.ts')).default, + // @ts-ignore + 'scripts/web3.ts': (await import('!!raw-loader!./scripts/web3.ts')).default, + // @ts-ignore + 'tests/SampleERC20_test.sol': (await import('raw-loader!./tests/SampleERC20_test.sol')).default + } +} \ No newline at end of file diff --git a/libs/remix-ws-templates/src/templates/zeroxErc20/scripts/deploy_with_ethers.ts b/libs/remix-ws-templates/src/templates/zeroxErc20/scripts/deploy_with_ethers.ts new file mode 100644 index 0000000000..c1e0b0fff9 --- /dev/null +++ b/libs/remix-ws-templates/src/templates/zeroxErc20/scripts/deploy_with_ethers.ts @@ -0,0 +1,10 @@ +import { deploy } from './ethers' + +(async () => { + try { + const result = await deploy('SampleERC20', ["TestToken", "TST", 18, 1000]) + console.log(`address: ${result.address}`) + } catch (e) { + console.log(e.message) + } + })() \ No newline at end of file diff --git a/libs/remix-ws-templates/src/templates/zeroxErc20/scripts/deploy_with_web3.ts b/libs/remix-ws-templates/src/templates/zeroxErc20/scripts/deploy_with_web3.ts new file mode 100644 index 0000000000..0611415d34 --- /dev/null +++ b/libs/remix-ws-templates/src/templates/zeroxErc20/scripts/deploy_with_web3.ts @@ -0,0 +1,10 @@ +import { deploy } from './web3' + +(async () => { + try { + const result = await deploy('SampleERC20', ["TestToken", "TST", 18, 1000]) + console.log(`address: ${result.address}`) + } catch (e) { + console.log(e.message) + } +})() \ No newline at end of file diff --git a/libs/remix-ws-templates/src/templates/zeroxErc20/scripts/ethers.ts b/libs/remix-ws-templates/src/templates/zeroxErc20/scripts/ethers.ts new file mode 100644 index 0000000000..0cb00c7e47 --- /dev/null +++ b/libs/remix-ws-templates/src/templates/zeroxErc20/scripts/ethers.ts @@ -0,0 +1,24 @@ +export const deploy = async (contractName: string, args: Array, from?: string): Promise => { + + 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() + + const 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 +} \ No newline at end of file diff --git a/libs/remix-ws-templates/src/templates/zeroxErc20/scripts/web3.ts b/libs/remix-ws-templates/src/templates/zeroxErc20/scripts/web3.ts new file mode 100644 index 0000000000..62f43b6611 --- /dev/null +++ b/libs/remix-ws-templates/src/templates/zeroxErc20/scripts/web3.ts @@ -0,0 +1,24 @@ +export const deploy = async (contractName: string, args: Array, from?: string, gas?: number): Promise => { + + 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 +} \ No newline at end of file diff --git a/libs/remix-ws-templates/src/templates/zeroxErc20/tests/SampleERC20_test.sol b/libs/remix-ws-templates/src/templates/zeroxErc20/tests/SampleERC20_test.sol new file mode 100644 index 0000000000..b167e1a478 --- /dev/null +++ b/libs/remix-ws-templates/src/templates/zeroxErc20/tests/SampleERC20_test.sol @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-3.0 + +pragma solidity ^0.5.9; +import "remix_tests.sol"; +import "../contracts/SampleERC20.sol"; + +contract SampleERC20Test { + + SampleERC20 s; + function beforeAll () public { + s = new SampleERC20("TestToken", "TST", 18, 1000); + } + + 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