diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-03-28 17:05:36 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-04-02 17:34:29 +0800 |
commit | 4d9029bb0e3b215efdf165704c80d3bacef0e85a (patch) | |
tree | 8f699dcd0a7b5fba3c31c065a62d640407bba8c3 /packages/metacoin | |
parent | bc49dde4d5d24b9a8b01459bde061f9b23fb5898 (diff) | |
download | dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.gz dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.tar.zst dexon-sol-tools-4d9029bb0e3b215efdf165704c80d3bacef0e85a.zip |
Add metacoin example project
Diffstat (limited to 'packages/metacoin')
-rw-r--r-- | packages/metacoin/artifacts/Metacoin.json | 98 | ||||
-rw-r--r-- | packages/metacoin/contracts/Metacoin.sol | 25 | ||||
-rw-r--r-- | packages/metacoin/coverage/.gitkeep | 0 | ||||
-rw-r--r-- | packages/metacoin/package.json | 60 | ||||
-rw-r--r-- | packages/metacoin/test/global_hooks.ts | 10 | ||||
-rw-r--r-- | packages/metacoin/test/metacoin_test.ts | 62 | ||||
-rw-r--r-- | packages/metacoin/test/utils/chai_setup.ts | 13 | ||||
-rw-r--r-- | packages/metacoin/test/utils/coverage.ts | 22 | ||||
-rw-r--r-- | packages/metacoin/test/utils/deployer.ts | 16 | ||||
-rw-r--r-- | packages/metacoin/test/utils/web3_wrapper.ts | 30 | ||||
-rw-r--r-- | packages/metacoin/tsconfig.json | 7 | ||||
-rw-r--r-- | packages/metacoin/tslint.json | 3 |
12 files changed, 346 insertions, 0 deletions
diff --git a/packages/metacoin/artifacts/Metacoin.json b/packages/metacoin/artifacts/Metacoin.json new file mode 100644 index 000000000..46c3ee71c --- /dev/null +++ b/packages/metacoin/artifacts/Metacoin.json @@ -0,0 +1,98 @@ +{ + "contract_name": "Metacoin", + "networks": { + "50": { + "solc_version": "0.4.21", + "keccak256": "0x2c3aa2e9dbef58abf57cecc148464d0852a83d7f30bbd2066f2a13b8bd3b1dd0", + "source_tree_hash": "0x2c3aa2e9dbef58abf57cecc148464d0852a83d7f30bbd2066f2a13b8bd3b1dd0", + "optimizer_enabled": false, + "abi": [ + { + "constant": true, + "inputs": [ + { + "name": "", + "type": "address" + } + ], + "name": "balances", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "components": [ + { + "name": "to", + "type": "address" + }, + { + "name": "amount", + "type": "uint256" + } + ], + "name": "transferData", + "type": "tuple" + } + ], + "name": "transfer", + "outputs": [ + { + "name": "success", + "type": "bool" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "payable": false, + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "name": "_from", + "type": "address" + }, + { + "indexed": true, + "name": "_to", + "type": "address" + }, + { + "indexed": false, + "name": "_value", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + } + ], + "bytecode": + "0x6060604052341561000f57600080fd5b6127106000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610406806100636000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806327e235e3146100515780632bd14bb914610087575b600080fd5b341561005c57600080fd5b610071600461006c9036906102b9565b6100bd565b60405161007e9190610344565b60405180910390f35b341561009257600080fd5b6100a760046100a29036906102e2565b6100d5565b6040516100b49190610329565b60405180910390f35b60006020528060005260406000206000915090505481565b600081602001516000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561012a5760009050610240565b81602001516000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508160200151600080846000015173ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550816000015173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84602001516040516102339190610344565b60405180910390a3600190505b919050565b600061025182356103a2565b905092915050565b60006040828403121561026b57600080fd5b610275604061035f565b9050600061028584828501610245565b6000830152506020610299848285016102a5565b60208301525092915050565b60006102b182356103c2565b905092915050565b6000602082840312156102cb57600080fd5b60006102d984828501610245565b91505092915050565b6000604082840312156102f457600080fd5b600061030284828501610259565b91505092915050565b6103148161038c565b82525050565b61032381610398565b82525050565b600060208201905061033e600083018461030b565b92915050565b6000602082019050610359600083018461031a565b92915050565b6000604051905081810181811067ffffffffffffffff8211171561038257600080fd5b8060405250919050565b60008115159050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60008190509190505600a265627a7a72305820d15828219194e8ddaa624e10f9c8823c05268d79753b4c60ef401fb4fe5f09dc6c6578706572696d656e74616cf50037", + "runtime_bytecode": + "0x60606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806327e235e3146100515780632bd14bb914610087575b600080fd5b341561005c57600080fd5b610071600461006c9036906102b9565b6100bd565b60405161007e9190610344565b60405180910390f35b341561009257600080fd5b6100a760046100a29036906102e2565b6100d5565b6040516100b49190610329565b60405180910390f35b60006020528060005260406000206000915090505481565b600081602001516000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561012a5760009050610240565b81602001516000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508160200151600080846000015173ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550816000015173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84602001516040516102339190610344565b60405180910390a3600190505b919050565b600061025182356103a2565b905092915050565b60006040828403121561026b57600080fd5b610275604061035f565b9050600061028584828501610245565b6000830152506020610299848285016102a5565b60208301525092915050565b60006102b182356103c2565b905092915050565b6000602082840312156102cb57600080fd5b60006102d984828501610245565b91505092915050565b6000604082840312156102f457600080fd5b600061030284828501610259565b91505092915050565b6103148161038c565b82525050565b61032381610398565b82525050565b600060208201905061033e600083018461030b565b92915050565b6000602082019050610359600083018461031a565b92915050565b6000604051905081810181811067ffffffffffffffff8211171561038257600080fd5b8060405250919050565b60008115159050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60008190509190505600a265627a7a72305820d15828219194e8ddaa624e10f9c8823c05268d79753b4c60ef401fb4fe5f09dc6c6578706572696d656e74616cf50037", + "updated_at": 1522318279735, + "source_map": "60:662:0:-;;;290:72;;;;;;;;350:5;327:8;:20;336:10;327:20;;;;;;;;;;;;;;;:28;;;;60:662;;;;;;", + "source_map_runtime": + "60:662:0:-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;84:41;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;368:352;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;84:41;;;;;;;;;;;;;;;;;:::o;368:352::-;429:12;480;:19;;;457:8;:20;466:10;457:20;;;;;;;;;;;;;;;;:42;453:60;;;508:5;501:12;;;;453:60;547:12;:19;;;523:8;:20;532:10;523:20;;;;;;;;;;;;;;;;:43;;;;;;;;;;;605:12;:19;;;576:8;:25;585:12;:15;;;576:25;;;;;;;;;;;;;;;;:48;;;;;;;;;;;655:12;:15;;;634:58;;643:10;634:58;;;672:12;:19;;;634:58;;;;;;;;;;;;;;;709:4;702:11;;368:352;;;;:::o;5:118:-1:-;;72:46;110:6;97:20;72:46;;;63:55;;57:66;;;;;165:469;;282:4;270:9;265:3;261:19;257:30;254:2;;;300:1;297;290:12;254:2;318:20;333:4;318:20;;;309:29;;386:1;417:49;462:3;453:6;442:9;438:22;417:49;;;411:3;404:5;400:15;393:74;348:130;530:2;563:49;608:3;599:6;588:9;584:22;563:49;;;556:4;549:5;545:16;538:75;488:136;248:386;;;;;641:118;;708:46;746:6;733:20;708:46;;;699:55;;693:66;;;;;766:241;;870:2;858:9;849:7;845:23;841:32;838:2;;;886:1;883;876:12;838:2;921:1;938:53;983:7;974:6;963:9;959:22;938:53;;;928:63;;900:97;832:175;;;;;1014:297;;1146:2;1134:9;1125:7;1121:23;1117:32;1114:2;;;1162:1;1159;1152:12;1114:2;1197:1;1214:81;1287:7;1278:6;1267:9;1263:22;1214:81;;;1204:91;;1176:125;1108:203;;;;;1318:101;1385:28;1407:5;1385:28;;;1380:3;1373:41;1367:52;;;1426:110;1499:31;1524:5;1499:31;;;1494:3;1487:44;1481:55;;;1543:181;;1645:2;1634:9;1630:18;1622:26;;1659:55;1711:1;1700:9;1696:17;1687:6;1659:55;;;1616:108;;;;;1731:193;;1839:2;1828:9;1824:18;1816:26;;1853:61;1911:1;1900:9;1896:17;1887:6;1853:61;;;1810:114;;;;;1931:256;;1993:2;1987:9;1977:19;;2031:4;2023:6;2019:17;2130:6;2118:10;2115:22;2094:18;2082:10;2079:34;2076:62;2073:2;;;2151:1;2148;2141:12;2073:2;2171:10;2167:2;2160:22;1971:216;;;;;2194:92;;2274:5;2267:13;2260:21;2249:32;;2243:43;;;;2293:79;;2362:5;2351:16;;2345:27;;;;2379:128;;2459:42;2452:5;2448:54;2437:65;;2431:76;;;;2514:79;;2583:5;2572:16;;2566:27;;;", + "sources": ["Metacoin.sol"] + } + } +} diff --git a/packages/metacoin/contracts/Metacoin.sol b/packages/metacoin/contracts/Metacoin.sol new file mode 100644 index 000000000..6b6814b21 --- /dev/null +++ b/packages/metacoin/contracts/Metacoin.sol @@ -0,0 +1,25 @@ +pragma solidity ^0.4.21; +pragma experimental ABIEncoderV2; + +contract Metacoin { + mapping (address => uint) public balances; + + event Transfer(address indexed _from, address indexed _to, uint256 _value); + + struct TransferData { + address to; + uint256 amount; + } + + function Metacoin() public { + balances[msg.sender] = 10000; + } + + function transfer(TransferData transferData) public returns (bool success) { + if (balances[msg.sender] < transferData.amount) return false; + balances[msg.sender] -= transferData.amount; + balances[transferData.to] += transferData.amount; + Transfer(msg.sender, transferData.to, transferData.amount); + return true; + } +} diff --git a/packages/metacoin/coverage/.gitkeep b/packages/metacoin/coverage/.gitkeep new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/packages/metacoin/coverage/.gitkeep diff --git a/packages/metacoin/package.json b/packages/metacoin/package.json new file mode 100644 index 000000000..74771c2e5 --- /dev/null +++ b/packages/metacoin/package.json @@ -0,0 +1,60 @@ +{ + "name": "@0xproject/metacoin", + "version": "0.0.1", + "private": true, + "description": "Example solidity project using 0x dev tools", + "scripts": { + "build:watch": "tsc -w", + "lint": "tslint --project .", + "clean": "shx rm -rf lib", + "prebuild": "run-s clean generate_contract_wrappers", + "build": "tsc", + "test": "run-s build run_mocha", + "test:coverage": "SOLIDITY_COVERAGE=true run-s build run_mocha coverage:report:text coverage:report:lcov", + "run_mocha": "mocha 'lib/test/**/*.js'", + "generate_contract_wrappers": "node ../abi-gen/lib/index.js --abis 'artifacts/Metacoin.json' --template ../contract_templates/contract.handlebars --partials '../contract_templates/partials/**/*.handlebars' --output src/contract_wrappers --backend ethers && prettier --write 'src/contract_wrappers/**.ts'", + "coverage:report:text": "istanbul report text", + "coverage:report:html": "istanbul report html && open coverage/index.html", + "coverage:report:lcov": "istanbul report lcov", + "test:circleci": "yarn test:coverage", + "compile": "node ../deployer/lib/src/cli.js compile --contracts Metacoin --contracts-dir contracts --artifacts-dir artifacts" + }, + "repository": { + "type": "git", + "url": "https://github.com/0xProject/0x-monorepo.git" + }, + "author": "", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/0xProject/0x-monorepo/issues" + }, + "homepage": "https://github.com/0xProject/0x-monorepo/packages/metacoin/README.md", + "dependencies": { + "@0xproject/abi-gen": "^0.2.7", + "@0xproject/base-contract": "^0.0.5", + "@0xproject/deployer": "^0.3.3", + "@0xproject/types": "^0.4.1", + "@0xproject/tslint-config": "^0.4.12", + "@0xproject/sol-cov": "^0.0.4", + "@0xproject/subproviders": "^0.8.2", + "@0xproject/web3-wrapper": "^0.3.1", + "@0xproject/utils": "^0.4.3", + "ethers-contracts": "^2.2.1", + "lodash": "^4.17.4", + "web3-provider-engine": "^13.0.1" + }, + "devDependencies": { + "@0xproject/dev-utils": "^0.3.2", + "npm-run-all": "^4.1.2", + "dirty-chai": "^2.0.1", + "shx": "^0.2.2", + "tslint": "5.8.0", + "chai": "^4.0.1", + "chai-as-promised": "^7.1.0", + "chai-bignumber": "^2.0.1", + "typescript": "2.7.1" + }, + "publishConfig": { + "access": "private" + } +} diff --git a/packages/metacoin/test/global_hooks.ts b/packages/metacoin/test/global_hooks.ts new file mode 100644 index 000000000..509dc6837 --- /dev/null +++ b/packages/metacoin/test/global_hooks.ts @@ -0,0 +1,10 @@ +import { env, EnvVars } from '@0xproject/dev-utils'; + +import { coverage } from './utils/coverage'; + +after('generate coverage report', async () => { + if (env.parseBoolean(EnvVars.SolidityCoverage)) { + const coverageSubprovider = coverage.getCoverageSubproviderSingleton(); + await coverageSubprovider.writeCoverageAsync(); + } +}); diff --git a/packages/metacoin/test/metacoin_test.ts b/packages/metacoin/test/metacoin_test.ts new file mode 100644 index 000000000..f2b396ac2 --- /dev/null +++ b/packages/metacoin/test/metacoin_test.ts @@ -0,0 +1,62 @@ +import { BlockchainLifecycle, devConstants } from '@0xproject/dev-utils'; +import { LogWithDecodedArgs } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import * as chai from 'chai'; + +import { MetacoinContract, TransferContractEventArgs } from '../src/contract_wrappers/metacoin'; + +import { chaiSetup } from './utils/chai_setup'; +import { deployer } from './utils/deployer'; +import { web3Wrapper } from './utils/web3_wrapper'; + +chaiSetup.configure(); +const { expect } = chai; +const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); + +describe('Metacoin', () => { + let metacoin: MetacoinContract; + const ownerAddress = devConstants.TESTRPC_FIRST_ADDRESS; + const INITIAL_BALANCE = new BigNumber(10000); + before(async () => { + const metacoinInstance = await deployer.deployAsync('Metacoin'); + web3Wrapper.abiDecoder.addABI(metacoinInstance.abi); + metacoin = new MetacoinContract(web3Wrapper, metacoinInstance.abi, metacoinInstance.address); + }); + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); + describe('#constructor', () => { + it(`should initialy give ${INITIAL_BALANCE} tokens to the creator`, async () => { + const balance = await metacoin.balances.callAsync(ownerAddress); + expect(balance).to.be.bignumber.equal(INITIAL_BALANCE); + }); + }); + describe('#transfer', () => { + it(`should successfully transfer tokens`, async () => { + const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; + const amount = INITIAL_BALANCE.div(2); + const oldBalance = await metacoin.balances.callAsync(ZERO_ADDRESS); + expect(oldBalance).to.be.bignumber.equal(0); + const txHash = await metacoin.transfer.sendTransactionAsync( + { + to: ZERO_ADDRESS, + amount, + }, + { from: devConstants.TESTRPC_FIRST_ADDRESS }, + ); + const txReceipt = await web3Wrapper.awaitTransactionMinedAsync(txHash); + const transferLogs = txReceipt.logs[0] as LogWithDecodedArgs<TransferContractEventArgs>; + expect(transferLogs.args).to.be.deep.equal({ + _to: ZERO_ADDRESS, + _from: devConstants.TESTRPC_FIRST_ADDRESS, + _value: amount, + }); + const newBalance = await metacoin.balances.callAsync(ZERO_ADDRESS); + expect(newBalance).to.be.bignumber.equal(amount); + }); + }); +}); diff --git a/packages/metacoin/test/utils/chai_setup.ts b/packages/metacoin/test/utils/chai_setup.ts new file mode 100644 index 000000000..078edd309 --- /dev/null +++ b/packages/metacoin/test/utils/chai_setup.ts @@ -0,0 +1,13 @@ +import * as chai from 'chai'; +import chaiAsPromised = require('chai-as-promised'); +import ChaiBigNumber = require('chai-bignumber'); +import * as dirtyChai from 'dirty-chai'; + +export const chaiSetup = { + configure() { + chai.config.includeStack = true; + chai.use(ChaiBigNumber()); + chai.use(dirtyChai); + chai.use(chaiAsPromised); + }, +}; diff --git a/packages/metacoin/test/utils/coverage.ts b/packages/metacoin/test/utils/coverage.ts new file mode 100644 index 000000000..5115dec0e --- /dev/null +++ b/packages/metacoin/test/utils/coverage.ts @@ -0,0 +1,22 @@ +import { CoverageSubprovider } from '@0xproject/sol-cov'; +import * as _ from 'lodash'; + +import { devConstants } from '@0xproject/dev-utils'; + +let coverageSubprovider: CoverageSubprovider; + +export const coverage = { + getCoverageSubproviderSingleton(): CoverageSubprovider { + if (_.isUndefined(coverageSubprovider)) { + coverageSubprovider = coverage._getCoverageSubprovider(); + } + return coverageSubprovider; + }, + _getCoverageSubprovider(): CoverageSubprovider { + const artifactsPath = 'artifacts'; + const contractsPath = 'contracts'; + const networkId = 50; + const defaultFromAddress = devConstants.TESTRPC_FIRST_ADDRESS; + return new CoverageSubprovider(artifactsPath, contractsPath, networkId, defaultFromAddress); + }, +}; diff --git a/packages/metacoin/test/utils/deployer.ts b/packages/metacoin/test/utils/deployer.ts new file mode 100644 index 000000000..d7dbc6d36 --- /dev/null +++ b/packages/metacoin/test/utils/deployer.ts @@ -0,0 +1,16 @@ +import { Deployer } from '@0xproject/deployer'; +import { devConstants } from '@0xproject/dev-utils'; +import * as path from 'path'; + +import { web3Wrapper } from './web3_wrapper'; + +const deployerOpts = { + web3Provider: web3Wrapper.getProvider(), + artifactsDir: path.resolve('artifacts'), + networkId: 50, + defaults: { + from: devConstants.TESTRPC_FIRST_ADDRESS, + }, +}; + +export const deployer = new Deployer(deployerOpts); diff --git a/packages/metacoin/test/utils/web3_wrapper.ts b/packages/metacoin/test/utils/web3_wrapper.ts new file mode 100644 index 000000000..216a1de5a --- /dev/null +++ b/packages/metacoin/test/utils/web3_wrapper.ts @@ -0,0 +1,30 @@ +import { env, EnvVars } from '@0xproject/dev-utils'; +import { GanacheSubprovider } from '@0xproject/subproviders'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import * as fs from 'fs'; +import * as _ from 'lodash'; +import ProviderEngine = require('web3-provider-engine'); + +import { coverage } from './coverage'; + +export const web3Provider = new ProviderEngine(); +const isCoverageEnabled = env.parseBoolean(EnvVars.SolidityCoverage); +if (isCoverageEnabled) { + web3Provider.addProvider(coverage.getCoverageSubproviderSingleton()); +} +web3Provider.addProvider( + new GanacheSubprovider({ + logger: { + log: (arg: any) => { + fs.appendFileSync('ganache.log', `${arg}\n`); + }, + }, + verbose: env.parseBoolean(EnvVars.SolidityCoverage), + port: 8545, + networkId: 50, + mnemonic: 'concert load couple harbor equip island argue ramp clarify fence smart topic', + }), +); +web3Provider.start(); + +export const web3Wrapper = new Web3Wrapper(web3Provider); diff --git a/packages/metacoin/tsconfig.json b/packages/metacoin/tsconfig.json new file mode 100644 index 000000000..8b4cd47a2 --- /dev/null +++ b/packages/metacoin/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig", + "compilerOptions": { + "outDir": "lib" + }, + "include": ["src/**/*", "test/**/*"] +} diff --git a/packages/metacoin/tslint.json b/packages/metacoin/tslint.json new file mode 100644 index 000000000..ffaefe83a --- /dev/null +++ b/packages/metacoin/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": ["@0xproject/tslint-config"] +} |