Skip to content

Instantly share code, notes, and snippets.

@codinghistorian
Created October 14, 2023 05:15
Show Gist options
  • Select an option

  • Save codinghistorian/34d14362a044c55658f10ca44b3d578b to your computer and use it in GitHub Desktop.

Select an option

Save codinghistorian/34d14362a044c55658f10ca44b3d578b to your computer and use it in GitHub Desktop.

Revisions

  1. codinghistorian created this gist Oct 14, 2023.
    38 changes: 38 additions & 0 deletions .prettierrc.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    {
    "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": {}
    }
    ]
    }
    28 changes: 28 additions & 0 deletions README.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    REMIX DEFAULT WORKSPACE

    Remix default workspace is present when:
    i. Remix loads for the very first time
    ii. A new workspace is created with 'Default' template
    iii. There are no files existing in the File Explorer

    This workspace contains 3 directories:

    1. 'contracts': Holds three contracts with increasing levels of complexity.
    2. 'scripts': Contains four typescript files to deploy a contract. It is explained below.
    3. 'tests': Contains one Solidity test file for 'Ballot' contract & one JS test file for 'Storage' contract.

    SCRIPTS

    The 'scripts' folder has four typescript files which help to deploy the 'Storage' contract using 'web3.js' and 'ethers.js' libraries.

    For the deployment of any other contract, just update the contract's name from 'Storage' to the desired contract and provide constructor arguments accordingly
    in the file `deploy_with_ethers.ts` or `deploy_with_web3.ts`

    In the 'tests' folder there is a script containing Mocha-Chai unit tests for 'Storage' contract.

    To run a script, right click on file name in the file explorer and click 'Run'. Remember, Solidity file must already be compiled.
    Output from script will appear in remix terminal.

    Please note, require/import is supported in a limited manner for Remix supported modules.
    For now, modules supported by Remix are ethers, web3, swarmgw, chai, multihashes, remix and hardhat only for hardhat.ethers object/plugin.
    For unsupported modules, an error like this will be thrown: '<module_name> module require is not supported by Remix IDE' will be shown.
    22 changes: 22 additions & 0 deletions contracts...1_AttackAdapter.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // SPDX-License-Identifier: GPL-3.0

    pragma solidity >=0.8.2 <0.9.0;

    interface IERC20 {
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    }

    contract AttackAdapter{
    address public collateralToken;
    uint256 public counter;
    constructor(address _collateralToken) {
    collateralToken = _collateralToken;
    }
    function deposit(address _positionAddress, uint256 _value, bytes32 _data ) external {
    IERC20(collateralToken).transferFrom(msg.sender, address(this), _value);
    }
    function onAdjustPosition(address src, address dst, int256 collateralValue, int256 debtShare, bytes calldata data) external {
    counter++;
    }

    }
    9 changes: 9 additions & 0 deletions contracts...2_IERC20.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    // SPDX-License-Identifier: AGPL-3.0-or-later
    pragma solidity 0.8.17;

    interface IERC20 {
    function balanceOf(address _owner) external view returns (uint256);
    function approve(address _spender, uint256 amount) external;
    function mint(address _to, uint256 _amount) external;
    }

    4,673 changes: 4,673 additions & 0 deletions contracts...artifacts...AttackAdapter.json
    4,673 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    139 changes: 139 additions & 0 deletions contracts...artifacts...AttackAdapter_metadata.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,139 @@
    {
    "compiler": {
    "version": "0.8.17+commit.8df45f5f"
    },
    "language": "Solidity",
    "output": {
    "abi": [
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "_collateralToken",
    "type": "address"
    }
    ],
    "stateMutability": "nonpayable",
    "type": "constructor"
    },
    {
    "inputs": [],
    "name": "collateralToken",
    "outputs": [
    {
    "internalType": "address",
    "name": "",
    "type": "address"
    }
    ],
    "stateMutability": "view",
    "type": "function"
    },
    {
    "inputs": [],
    "name": "counter",
    "outputs": [
    {
    "internalType": "uint256",
    "name": "",
    "type": "uint256"
    }
    ],
    "stateMutability": "view",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "_positionAddress",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "_value",
    "type": "uint256"
    },
    {
    "internalType": "bytes32",
    "name": "_data",
    "type": "bytes32"
    }
    ],
    "name": "deposit",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "src",
    "type": "address"
    },
    {
    "internalType": "address",
    "name": "dst",
    "type": "address"
    },
    {
    "internalType": "int256",
    "name": "collateralValue",
    "type": "int256"
    },
    {
    "internalType": "int256",
    "name": "debtShare",
    "type": "int256"
    },
    {
    "internalType": "bytes",
    "name": "data",
    "type": "bytes"
    }
    ],
    "name": "onAdjustPosition",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
    }
    ],
    "devdoc": {
    "kind": "dev",
    "methods": {},
    "version": 1
    },
    "userdoc": {
    "kind": "user",
    "methods": {},
    "version": 1
    }
    },
    "settings": {
    "compilationTarget": {
    "contracts/1_AttackAdapter.sol": "AttackAdapter"
    },
    "evmVersion": "london",
    "libraries": {},
    "metadata": {
    "bytecodeHash": "ipfs"
    },
    "optimizer": {
    "enabled": false,
    "runs": 200
    },
    "remappings": []
    },
    "sources": {
    "contracts/1_AttackAdapter.sol": {
    "keccak256": "0xea4fda6bfe81ce43814a601e2ed9592052e9be2b1a32bd68f38647acf51d6085",
    "license": "GPL-3.0",
    "urls": [
    "bzz-raw://ccfeca257bdf12c74765ee963a5d72e3ec99902b1ecf7ced62f849a8a1328962",
    "dweb:/ipfs/QmWXdYNfzhqCBnjsPHwFjrv899F491shkjeh5orNPNkU6F"
    ]
    }
    },
    "version": 1
    }
    86 changes: 86 additions & 0 deletions contracts...artifacts...IERC20.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    {
    "deploy": {
    "VM:-": {
    "linkReferences": {},
    "autoDeployLib": true
    },
    "main:1": {
    "linkReferences": {},
    "autoDeployLib": true
    },
    "ropsten:3": {
    "linkReferences": {},
    "autoDeployLib": true
    },
    "rinkeby:4": {
    "linkReferences": {},
    "autoDeployLib": true
    },
    "kovan:42": {
    "linkReferences": {},
    "autoDeployLib": true
    },
    "goerli:5": {
    "linkReferences": {},
    "autoDeployLib": true
    },
    "Custom": {
    "linkReferences": {},
    "autoDeployLib": true
    }
    },
    "data": {
    "bytecode": {
    "functionDebugData": {},
    "generatedSources": [],
    "linkReferences": {},
    "object": "",
    "opcodes": "",
    "sourceMap": ""
    },
    "deployedBytecode": {
    "functionDebugData": {},
    "generatedSources": [],
    "immutableReferences": {},
    "linkReferences": {},
    "object": "",
    "opcodes": "",
    "sourceMap": ""
    },
    "gasEstimates": null,
    "methodIdentifiers": {
    "transferFrom(address,address,uint256)": "23b872dd"
    }
    },
    "abi": [
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "sender",
    "type": "address"
    },
    {
    "internalType": "address",
    "name": "recipient",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "amount",
    "type": "uint256"
    }
    ],
    "name": "transferFrom",
    "outputs": [
    {
    "internalType": "bool",
    "name": "",
    "type": "bool"
    }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
    }
    ]
    }
    75 changes: 75 additions & 0 deletions contracts...artifacts...IERC20_metadata.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    {
    "compiler": {
    "version": "0.8.17+commit.8df45f5f"
    },
    "language": "Solidity",
    "output": {
    "abi": [
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "sender",
    "type": "address"
    },
    {
    "internalType": "address",
    "name": "recipient",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "amount",
    "type": "uint256"
    }
    ],
    "name": "transferFrom",
    "outputs": [
    {
    "internalType": "bool",
    "name": "",
    "type": "bool"
    }
    ],
    "stateMutability": "nonpayable",
    "type": "function"
    }
    ],
    "devdoc": {
    "kind": "dev",
    "methods": {},
    "version": 1
    },
    "userdoc": {
    "kind": "user",
    "methods": {},
    "version": 1
    }
    },
    "settings": {
    "compilationTarget": {
    "contracts/1_AttackAdapter.sol": "IERC20"
    },
    "evmVersion": "london",
    "libraries": {},
    "metadata": {
    "bytecodeHash": "ipfs"
    },
    "optimizer": {
    "enabled": false,
    "runs": 200
    },
    "remappings": []
    },
    "sources": {
    "contracts/1_AttackAdapter.sol": {
    "keccak256": "0xea4fda6bfe81ce43814a601e2ed9592052e9be2b1a32bd68f38647acf51d6085",
    "license": "GPL-3.0",
    "urls": [
    "bzz-raw://ccfeca257bdf12c74765ee963a5d72e3ec99902b1ecf7ced62f849a8a1328962",
    "dweb:/ipfs/QmWXdYNfzhqCBnjsPHwFjrv899F491shkjeh5orNPNkU6F"
    ]
    }
    },
    "version": 1
    }
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,446 @@
    {
    "id": "81691cf1790e1f1411bc2045b761e567",
    "_format": "hh-sol-build-info-1",
    "solcVersion": "0.8.17",
    "solcLongVersion": "0.8.17+commit.8df45f5f",
    "input": {
    "language": "Solidity",
    "sources": {
    "contracts/2_IERC20.sol": {
    "content": "// SPDX-License-Identifier: AGPL-3.0-or-later\npragma solidity 0.8.17;\n\ninterface IERC20 {\n function balanceOf(address _owner) external view returns (uint256);\n function approve(address _spender, uint256 amount) external;\n function mint(address _to, uint256 _amount) external;\n}\n\n"
    }
    },
    "settings": {
    "optimizer": {
    "enabled": false,
    "runs": 200
    },
    "outputSelection": {
    "*": {
    "": [
    "ast"
    ],
    "*": [
    "abi",
    "metadata",
    "devdoc",
    "userdoc",
    "storageLayout",
    "evm.legacyAssembly",
    "evm.bytecode",
    "evm.deployedBytecode",
    "evm.methodIdentifiers",
    "evm.gasEstimates",
    "evm.assembly"
    ]
    }
    }
    }
    },
    "output": {
    "contracts": {
    "contracts/2_IERC20.sol": {
    "IERC20": {
    "abi": [
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "_spender",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "amount",
    "type": "uint256"
    }
    ],
    "name": "approve",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "_owner",
    "type": "address"
    }
    ],
    "name": "balanceOf",
    "outputs": [
    {
    "internalType": "uint256",
    "name": "",
    "type": "uint256"
    }
    ],
    "stateMutability": "view",
    "type": "function"
    },
    {
    "inputs": [
    {
    "internalType": "address",
    "name": "_to",
    "type": "address"
    },
    {
    "internalType": "uint256",
    "name": "_amount",
    "type": "uint256"
    }
    ],
    "name": "mint",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
    }
    ],
    "devdoc": {
    "kind": "dev",
    "methods": {},
    "version": 1
    },
    "evm": {
    "assembly": "",
    "bytecode": {
    "functionDebugData": {},
    "generatedSources": [],
    "linkReferences": {},
    "object": "",
    "opcodes": "",
    "sourceMap": ""
    },
    "deployedBytecode": {
    "functionDebugData": {},
    "generatedSources": [],
    "immutableReferences": {},
    "linkReferences": {},
    "object": "",
    "opcodes": "",
    "sourceMap": ""
    },
    "gasEstimates": null,
    "legacyAssembly": null,
    "methodIdentifiers": {
    "approve(address,uint256)": "095ea7b3",
    "balanceOf(address)": "70a08231",
    "mint(address,uint256)": "40c10f19"
    }
    },
    "metadata": "{\"compiler\":{\"version\":\"0.8.17+commit.8df45f5f\"},\"language\":\"Solidity\",\"output\":{\"abi\":[{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_owner\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"_amount\",\"type\":\"uint256\"}],\"name\":\"mint\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}],\"devdoc\":{\"kind\":\"dev\",\"methods\":{},\"version\":1},\"userdoc\":{\"kind\":\"user\",\"methods\":{},\"version\":1}},\"settings\":{\"compilationTarget\":{\"contracts/2_IERC20.sol\":\"IERC20\"},\"evmVersion\":\"london\",\"libraries\":{},\"metadata\":{\"bytecodeHash\":\"ipfs\"},\"optimizer\":{\"enabled\":false,\"runs\":200},\"remappings\":[]},\"sources\":{\"contracts/2_IERC20.sol\":{\"keccak256\":\"0x4d3c5edbac229dbf0f2dfbd6f8ce40eb62af06f7d55ff2453404c6a9bbae947a\",\"license\":\"AGPL-3.0-or-later\",\"urls\":[\"bzz-raw://f9c54f70121a9a9029e3345987a2315d99eb8111fa4a6032af2738d59ca38bdc\",\"dweb:/ipfs/QmYbNykSRu1kKqz7ZfJPuAu3EwhNpEeGTYxfa4deSSLKYS\"]}},\"version\":1}",
    "storageLayout": {
    "storage": [],
    "types": null
    },
    "userdoc": {
    "kind": "user",
    "methods": {},
    "version": 1
    }
    }
    }
    },
    "sources": {
    "contracts/2_IERC20.sol": {
    "ast": {
    "absolutePath": "contracts/2_IERC20.sol",
    "exportedSymbols": {
    "IERC20": [
    23
    ]
    },
    "id": 24,
    "license": "AGPL-3.0-or-later",
    "nodeType": "SourceUnit",
    "nodes": [
    {
    "id": 1,
    "literals": [
    "solidity",
    "0.8",
    ".17"
    ],
    "nodeType": "PragmaDirective",
    "src": "46:23:0"
    },
    {
    "abstract": false,
    "baseContracts": [],
    "canonicalName": "IERC20",
    "contractDependencies": [],
    "contractKind": "interface",
    "fullyImplemented": false,
    "id": 23,
    "linearizedBaseContracts": [
    23
    ],
    "name": "IERC20",
    "nameLocation": "81:6:0",
    "nodeType": "ContractDefinition",
    "nodes": [
    {
    "functionSelector": "70a08231",
    "id": 8,
    "implemented": false,
    "kind": "function",
    "modifiers": [],
    "name": "balanceOf",
    "nameLocation": "103:9:0",
    "nodeType": "FunctionDefinition",
    "parameters": {
    "id": 4,
    "nodeType": "ParameterList",
    "parameters": [
    {
    "constant": false,
    "id": 3,
    "mutability": "mutable",
    "name": "_owner",
    "nameLocation": "121:6:0",
    "nodeType": "VariableDeclaration",
    "scope": 8,
    "src": "113:14:0",
    "stateVariable": false,
    "storageLocation": "default",
    "typeDescriptions": {
    "typeIdentifier": "t_address",
    "typeString": "address"
    },
    "typeName": {
    "id": 2,
    "name": "address",
    "nodeType": "ElementaryTypeName",
    "src": "113:7:0",
    "stateMutability": "nonpayable",
    "typeDescriptions": {
    "typeIdentifier": "t_address",
    "typeString": "address"
    }
    },
    "visibility": "internal"
    }
    ],
    "src": "112:16:0"
    },
    "returnParameters": {
    "id": 7,
    "nodeType": "ParameterList",
    "parameters": [
    {
    "constant": false,
    "id": 6,
    "mutability": "mutable",
    "name": "",
    "nameLocation": "-1:-1:-1",
    "nodeType": "VariableDeclaration",
    "scope": 8,
    "src": "152:7:0",
    "stateVariable": false,
    "storageLocation": "default",
    "typeDescriptions": {
    "typeIdentifier": "t_uint256",
    "typeString": "uint256"
    },
    "typeName": {
    "id": 5,
    "name": "uint256",
    "nodeType": "ElementaryTypeName",
    "src": "152:7:0",
    "typeDescriptions": {
    "typeIdentifier": "t_uint256",
    "typeString": "uint256"
    }
    },
    "visibility": "internal"
    }
    ],
    "src": "151:9:0"
    },
    "scope": 23,
    "src": "94:67:0",
    "stateMutability": "view",
    "virtual": false,
    "visibility": "external"
    },
    {
    "functionSelector": "095ea7b3",
    "id": 15,
    "implemented": false,
    "kind": "function",
    "modifiers": [],
    "name": "approve",
    "nameLocation": "175:7:0",
    "nodeType": "FunctionDefinition",
    "parameters": {
    "id": 13,
    "nodeType": "ParameterList",
    "parameters": [
    {
    "constant": false,
    "id": 10,
    "mutability": "mutable",
    "name": "_spender",
    "nameLocation": "191:8:0",
    "nodeType": "VariableDeclaration",
    "scope": 15,
    "src": "183:16:0",
    "stateVariable": false,
    "storageLocation": "default",
    "typeDescriptions": {
    "typeIdentifier": "t_address",
    "typeString": "address"
    },
    "typeName": {
    "id": 9,
    "name": "address",
    "nodeType": "ElementaryTypeName",
    "src": "183:7:0",
    "stateMutability": "nonpayable",
    "typeDescriptions": {
    "typeIdentifier": "t_address",
    "typeString": "address"
    }
    },
    "visibility": "internal"
    },
    {
    "constant": false,
    "id": 12,
    "mutability": "mutable",
    "name": "amount",
    "nameLocation": "209:6:0",
    "nodeType": "VariableDeclaration",
    "scope": 15,
    "src": "201:14:0",
    "stateVariable": false,
    "storageLocation": "default",
    "typeDescriptions": {
    "typeIdentifier": "t_uint256",
    "typeString": "uint256"
    },
    "typeName": {
    "id": 11,
    "name": "uint256",
    "nodeType": "ElementaryTypeName",
    "src": "201:7:0",
    "typeDescriptions": {
    "typeIdentifier": "t_uint256",
    "typeString": "uint256"
    }
    },
    "visibility": "internal"
    }
    ],
    "src": "182:34:0"
    },
    "returnParameters": {
    "id": 14,
    "nodeType": "ParameterList",
    "parameters": [],
    "src": "225:0:0"
    },
    "scope": 23,
    "src": "166:60:0",
    "stateMutability": "nonpayable",
    "virtual": false,
    "visibility": "external"
    },
    {
    "functionSelector": "40c10f19",
    "id": 22,
    "implemented": false,
    "kind": "function",
    "modifiers": [],
    "name": "mint",
    "nameLocation": "240:4:0",
    "nodeType": "FunctionDefinition",
    "parameters": {
    "id": 20,
    "nodeType": "ParameterList",
    "parameters": [
    {
    "constant": false,
    "id": 17,
    "mutability": "mutable",
    "name": "_to",
    "nameLocation": "253:3:0",
    "nodeType": "VariableDeclaration",
    "scope": 22,
    "src": "245:11:0",
    "stateVariable": false,
    "storageLocation": "default",
    "typeDescriptions": {
    "typeIdentifier": "t_address",
    "typeString": "address"
    },
    "typeName": {
    "id": 16,
    "name": "address",
    "nodeType": "ElementaryTypeName",
    "src": "245:7:0",
    "stateMutability": "nonpayable",
    "typeDescriptions": {
    "typeIdentifier": "t_address",
    "typeString": "address"
    }
    },
    "visibility": "internal"
    },
    {
    "constant": false,
    "id": 19,
    "mutability": "mutable",
    "name": "_amount",
    "nameLocation": "266:7:0",
    "nodeType": "VariableDeclaration",
    "scope": 22,
    "src": "258:15:0",
    "stateVariable": false,
    "storageLocation": "default",
    "typeDescriptions": {
    "typeIdentifier": "t_uint256",
    "typeString": "uint256"
    },
    "typeName": {
    "id": 18,
    "name": "uint256",
    "nodeType": "ElementaryTypeName",
    "src": "258:7:0",
    "typeDescriptions": {
    "typeIdentifier": "t_uint256",
    "typeString": "uint256"
    }
    },
    "visibility": "internal"
    }
    ],
    "src": "244:30:0"
    },
    "returnParameters": {
    "id": 21,
    "nodeType": "ParameterList",
    "parameters": [],
    "src": "283:0:0"
    },
    "scope": 23,
    "src": "231:53:0",
    "stateMutability": "nonpayable",
    "virtual": false,
    "visibility": "external"
    }
    ],
    "scope": 24,
    "src": "71:215:0",
    "usedErrors": []
    }
    ],
    "src": "46:242:0"
    },
    "id": 0
    }
    }
    }
    }
    13,309 changes: 13,309 additions & 0 deletions contracts...artifacts...build-info...abfc26dedca2df67fa43e40d6bbe4b09.json
    13,309 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    13,309 changes: 13,309 additions & 0 deletions contracts...artifacts...build-info...e1003d76e589e020916f13ceecef422d.json
    13,309 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
    14 changes: 14 additions & 0 deletions scripts...deploy_with_ethers.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    // This script can be used to deploy the "Storage" contract using ethers.js library.
    // Please make sure to compile "./contracts/1_Storage.sol" file before running this script.
    // And use Right click -> "Run" from context menu of the file to run the script. Shortcut: Ctrl+Shift+S

    import { deploy } from './ethers-lib'

    (async () => {
    try {
    const result = await deploy('Storage', [])
    console.log(`address: ${result.address}`)
    } catch (e) {
    console.log(e.message)
    }
    })()
    14 changes: 14 additions & 0 deletions scripts...deploy_with_web3.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    // This script can be used to deploy the "Storage" contract using Web3 library.
    // Please make sure to compile "./contracts/1_Storage.sol" file before running this script.
    // And use Right click -> "Run" from context menu of the file to run the script. Shortcut: Ctrl+Shift+S

    import { deploy } from './web3-lib'

    (async () => {
    try {
    const result = await deploy('Storage', [])
    console.log(`address: ${result.address}`)
    } catch (e) {
    console.log(e.message)
    }
    })()
    29 changes: 29 additions & 0 deletions scripts...ethers-lib.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    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
    }
    36 changes: 36 additions & 0 deletions scripts...web3-lib.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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
    }
    28 changes: 28 additions & 0 deletions tests...Ballot_test.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    // SPDX-License-Identifier: GPL-3.0

    pragma solidity >=0.7.0 <0.9.0;
    import "remix_tests.sol"; // this import is automatically injected by Remix.
    import "hardhat/console.sol";
    import "../contracts/3_Ballot.sol";

    contract BallotTest {

    bytes32[] proposalNames;

    Ballot ballotToTest;
    function beforeAll () public {
    proposalNames.push(bytes32("candidate1"));
    ballotToTest = new Ballot(proposalNames);
    }

    function checkWinningProposal () public {
    console.log("Running checkWinningProposal");
    ballotToTest.vote(0);
    Assert.equal(ballotToTest.winningProposal(), uint(0), "proposal at index 0 should be the winning proposal");
    Assert.equal(ballotToTest.winnerName(), bytes32("candidate1"), "candidate1 should be the winner name");
    }

    function checkWinninProposalWithReturnValue () public view returns (bool) {
    return ballotToTest.winningProposal() == 0;
    }
    }
    22 changes: 22 additions & 0 deletions tests...storage.test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // Right click on the script name and hit "Run" to execute
    const { expect } = require("chai");
    const { ethers } = require("hardhat");

    describe("Storage", function () {
    it("test initial value", async function () {
    const Storage = await ethers.getContractFactory("Storage");
    const storage = await Storage.deploy();
    await storage.deployed();
    console.log('storage deployed at:'+ storage.address)
    expect((await storage.retrieve()).toNumber()).to.equal(0);
    });
    it("test updating and retrieving updated value", async function () {
    const Storage = await ethers.getContractFactory("Storage");
    const storage = await Storage.deploy();
    await storage.deployed();
    const storage2 = await ethers.getContractAt("Storage", storage.address);
    const setValue = await storage2.store(56);
    await setValue.wait();
    expect((await storage2.retrieve()).toNumber()).to.equal(56);
    });
    });