$ truffle init
contracts
, migrations
and tests
folders. In contracts
and migrations
you can see the smart contract Migrations
, which is responsible for the deployment logic (in terms of truffle - migration). And the logic is something like this: in the migrations
folder you add scripts and name them by the pattern 1_description.js
, 2_another_one.js
... n_etc.js
. The most important thing in the title is the index that goes at the beginning, after which you can add any description that is needed only for readability. The index is used to perform migrations in the order of numbering. Smart contract Migrations
used to save which of the migration scripts have already been executed. So, if in the process of development we add new contracts and a new deployment logic, then the previous successful progress should not be redone. Personally, we do not use it, instead we have a fixed number of migrations, edit and run them again each time with the help of truffle migration --reset
. $ truffle create contract ExampleContract $ truffle create migration ExampleMigration $ truffle create test ExampleContract
truffle init
immediately created a small example (Metacoin and ConvertLib). In order to see this and other examples in recent versions, you can use features called Truffle Boxes. Boxes are designed to get a full-fledged example of a Truffle project and its interaction with various web-based tools, such as ReactJS. Here is a list of boxes, both official and created by the community. Create a directory for the new project and navigate to it. Then execute the command: $ truffle unbox metacoin
truffle init
. This is an example with the basic token Metacoin, which can be sent from user to user and with the help of the ConvertLib library you can watch the balance in the air at a fixed exchange rate. In addition, it shows how to create and use a smart contract and library, there is also an example of tests for JavaScript and Solidity (for more information on writing tests, read here , note that there is just considered an older version of Truffle). Let's take a quick look at how to build and test this project in test mode. First, start the development console: $ truffle develop
Truffle Develop started at http://localhost:9545/ Accounts: (0) 0x627306090abab3a6e1400e9345bc60c78a8bef57 (1) 0xf17f52151ebef6c7334fad080c5704d77216b732 (2) 0xc5fdf4076b8f3a5357c5e395ab970b5b54098fef (3) 0x821aea9a577a9b44299b9c15c88cf3087f3b5544 (4) 0x0d1d4e623d10f9fba5db95830f7d3839406c6af2 (5) 0x2932b7a2355d6fecc4b5c0b6bd44cc31df247a2e (6) 0x2191ef87e392377ec08e7c08eb105ef5448eced5 (7) 0x0f4f2ac550a1b4e2280d04c21cea7ebd822934b5 (8) 0x6330a553fc93768f612722bb8c2ec78ac90b3bbc (9) 0x5aeda56215b167893e80b4fe645ba6d5bab767de Mnemonic: candy maple cake sugar pudding cream honey rich smooth crumble sweet treat truffle(develop)>
truffle(develop)> compile
Compiling ./contracts/ConvertLib.sol... Compiling ./contracts/MetaCoin.sol... Compiling ./contracts/Migrations.sol... Writing artifacts to ./build/contracts
truffle(develop)> migrate
Using network 'develop'. Running migration: 1_initial_migration.js Deploying Migrations... ... 0x29619f8ba9b9e001bef885c8ca2fbee45beab738adc41c7f9e2e8273fbc67e9f Migrations: 0x8cdaf0cd259887258bc13a92c0a6da92698644c0 Saving successful migration to network... ... 0xd7bc86d31bee32fa3988f1c1eabce403a1b5d570340a3a9cdba53a472ee8c956 Saving artifacts... Running migration: 2_deploy_contracts.js Deploying ConvertLib... ... 0x02318651545ac96670af626ef7795cb928d7504afc3f856258058ce579d47fe6 ConvertLib: 0x345ca3e014aaf5dca488057592ee47305d9b3e10 Linking ConvertLib to MetaCoin Deploying MetaCoin... ... 0x486c572bbb2df30bb166f5507423d394807b5b92041860968a7d5eb162e42e48 MetaCoin: 0xf25186b5081ff5ce73482ad761db0eb0d25abfbf Saving successful migration to network... ... 0x059cf1bbc372b9348ce487de910358801bbbd1c89182853439bec0afaee6c7db Saving artifacts...
truffle(develop)> test
Using network 'develop'. Compiling ./contracts/ConvertLib.sol... Compiling ./contracts/MetaCoin.sol... Compiling ./test/TestMetacoin.sol... Compiling truffle/Assert.sol... Compiling truffle/DeployedAddresses.sol... TestMetacoin âś“ testInitialBalanceUsingDeployedContract (62ms) âś“ testInitialBalanceWithNewMetaCoin (47ms) Contract: MetaCoin âś“ should put 10000 MetaCoin in the first account âś“ should call a function that depends on a linked library (54ms) âś“ should send coin correctly (117ms) 5 passing (796ms)
truffle(develop)> var metaCoin truffle(develop)> MetaCoin.deployed().then( function(instance) { metaCoin = instance } ); truffle(develop)> metaCoin.getBalance(web3.eth.coinbase) BigNumber { s: 1, e: 4, c: [ 10000 ] } truffle(develop)> _.toNumber() 10000 truffle(develop)> metaCoin.sendCoin( web3.eth.accounts[2], 3000 ) { tx: '0x9f59085a9f22c0bd691b890370bcffd7eedce1327a3bb525a2de3edf9db0d279', receipt: { transactionHash: '0x9f59085a9f22c0bd691b890370bcffd7eedce1327a3bb525a2de3edf9db0d279', transactionIndex: 0, blockHash: '0x24e8913b6f707bb5e5acbaa054fef9dabd548a561dc988763209f0aeed9a57b5', blockNumber: 12, gasUsed: 51024, cumulativeGasUsed: 51024, contractAddress: null, logs: [ [Object] ] }, logs: [ { logIndex: 0, transactionIndex: 0, transactionHash: '0x9f59085a9f22c0bd691b890370bcffd7eedce1327a3bb525a2de3edf9db0d279', blockHash: '0x24e8913b6f707bb5e5acbaa054fef9dabd548a561dc988763209f0aeed9a57b5', blockNumber: 12, address: '0xf25186b5081ff5ce73482ad761db0eb0d25abfbf', type: 'mined', event: 'Transfer', args: [Object] } ] } truffle(develop)> metaCoin.getBalance(web3.eth.coinbase) BigNumber { s: 1, e: 3, c: [ 7000 ] } truffle(develop)> _.toNumber() 7000
truffle(develop)> .exit
truffle console
command is used, which is completely analogous to develop
, but does not raise the test environment. To demonstrate this, it is not necessary to run geth
, for example, you can use the already mentioned Ganache , which replaced TestRPC. Run the command: $ ganache-cli
truffle unbox metacoin
and haven’t changed the configuration files yet, you won’t be able to connect $ truffle console No network available. Use `truffle develop` or add network to truffle.js config.
truffle.js
: module.exports = { networks: { development: { host: "localhost", port: 8545, network_id: "*" } } };
truffle-config.js
. This is the same, but for Windows. Extra file for your system can be deleted. $ truffle console
truffle(development)> migrate --reset
net_version eth_accounts eth_accounts net_version net_version eth_sendTransaction Transaction: 0x29619f8ba9b9e001bef885c8ca2fbee45beab738adc41c7f9e2e8273fbc67e9f Contract created: 0x922194d35a507e5905fa4f2c9e7172ee8535272a Gas usage: 269607 Block Number: 1 Block Time: Mon Feb 05 2018 10:28:17 GMT+0300 (MSK) eth_newBlockFilter eth_getFilterChanges eth_getTransactionReceipt eth_getCode eth_uninstallFilter eth_sendTransaction Transaction: 0xfafc4352cc1dde57e46954d7ebd3a59232599081a253dd8705847a380ae5b06b Gas usage: 41981 Block Number: 2 Block Time: Mon Feb 05 2018 10:28:17 GMT+0300 (MSK) eth_getTransactionReceipt eth_accounts net_version net_version eth_sendTransaction
truffle migrate --reset
$ npm init -y $ npm install -E zeppelin-solidity
node_modules
folder, you will have zeppelin-solidity
, from which you can connect the required files in the smart contract, for example, ownable, with the line import 'zeppelin-solidity/contracts/ownership/Ownable.sol';
$ truffle install zeppelin
node_modules
folder: import 'zeppelin/contracts/ownership/Ownable.sol';
$ mkdir FaultyContract && cd FaultyContract $ truffle init $ truffle create contract FaultyContract $ truffle create migration deploy
pragma solidity ^0.4.4; contract FaultyContract { int public result; function divideXbyY( int x, int y ) public { y -= y; result = x/y; } }
migration/xxxx_deploy.js
(xxxx - generated id, can be different) add the missing code for the deployment: var FaultyContract = artifacts.require("./FaultyContract.sol"); module.exports = function( deployer ) { deployer.deploy( FaultyContract ); }
$ truffle develop truffle(develop)> migrate
truffle(develop)> var faultyContract; truffle(develop)> FaultyContract.deployed().then( function(instance) { faultyContract = instance } ); truffle(develop)> faultyContract.divideXbyY(16, 4); Error: VM Exception while processing transaction: invalid opcode at Object.InvalidResponse (/usr/lib/node_modules/truffle/build/cli.bundled.js:43303:16) at /usr/lib/node_modules/truffle/build/cli.bundled.js:331156:36 at /usr/lib/node_modules/truffle/build/cli.bundled.js:314196:9 at XMLHttpRequest.request.onreadystatechange (/usr/lib/node_modules/truffle/build/cli.bundled.js:329855:7) at XMLHttpRequestEventTarget.dispatchEvent (/usr/lib/node_modules/truffle/build/cli.bundled.js:70159:18) at XMLHttpRequest._setReadyState (/usr/lib/node_modules/truffle/build/cli.bundled.js:70449:12) at XMLHttpRequest._onHttpResponseEnd (/usr/lib/node_modules/truffle/build/cli.bundled.js:70604:12)
invalid opcode
means. In Truffle, the debug
command is available from version 4 (beta), allowing the transaction to be re-executed line by line. But for this you need to get a transaction hash, and there is not even an error in it. To see the hash, run another instance of truffle develop
with the --log
flag: $ truffle develop --log
truffle(develop)> faultyContract.divideXbyY(16, 4);
develop:testrpc eth_sendTransaction +0ms develop:testrpc +27ms develop:testrpc Transaction: 0x21073e12e7c8fb785347d7bd5d974d4954379dcace7b53d452c03b39ca007b9e +1ms develop:testrpc Gas usage: 6721975 +0ms develop:testrpc Block Number: 6 +0ms develop:testrpc Block Time: Tue Feb 06 2018 10:32:27 GMT+0300 (MSK) +0ms develop:testrpc Runtime Error: invalid opcode +0ms develop:testrpc +0ms
truffle(develop)> debug 0x21073e12e7c8fb785347d7bd5d974d4954379dcace7b53d452c03b39ca007b9e
n
(step next): debug(develop:0x21073e12...)> n FaultyContract.sol | 0x345ca3e014aaf5dca488057592ee47305d9b3e10: 8: int public result; 9: 10: function divideXbyY( int x, int y ) public { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ debug(develop:0x21073e12...)>
debug(develop:0x21073e12...)> FaultyContract.sol | 0x345ca3e014aaf5dca488057592ee47305d9b3e10: 9: 10: function divideXbyY( int x, int y ) public { 11: y -= y; ^ debug(develop:0x21073e12...)> p FaultyContract.sol | 0x345ca3e014aaf5dca488057592ee47305d9b3e10: (55) DUP1 00000000000000000000000000000000000000000000000000000000c6329782 000000000000000000000000000000000000000000000000000000000000009b 0000000000000000000000000000000000000000000000000000000000000010 0000000000000000000000000000000000000000000000000000000000000004 (top)
next step
until we get something like this: 10: function divideXbyY( int x, int y ) public { 11: y -= y; 12: result = x/y; ^^^ debug(develop:0x21073e12...)> Transaction halted with a RUNTIME ERROR. This is likely due to an intentional halting expression, like assert(), require() or revert(). It can also be due to out-of-gas exceptions. Please inspect your transaction parameters and contract code to determine the meaning of this error. truffle(develop)>
$ mkdir metacoin && cd metacoin $ truffle unbox metacoin
$ npm init $ npm install $ npm install truffle-hdwallet-provider
var HDWalletProvider = require("truffle-hdwallet-provider"); var mnemonic = "correct horse battery staple correct horse battery staple correct horse battery staple" module.exports = { networks: { development: { host: "localhost", port: 8545, network_id: "*" }, ropsten: { provider: function() { return new HDWalletProvider(mnemonic, "https://ropsten.infura.io/< >") }, network_id: 3, gas: 4000000, gasPrice: 21000000000 } } };
development
, you can choose between these two networks by launching a truffle with the appropriate name after the --network
flag: $ truffle console --network ropsten
migrate
, you need to replenish the balance of the generated account. Find out the address and balance commands: truffle(ropsten)> web3.eth.getAccounts( function(e,r) { console.log(r[0]); } ); undefined 0x0bb542704819b5e6a28deb2b73245be57ce0e78b truffle(ropsten)> web3.eth.getBalance('0x0bb542704819b5e6a28deb2b73245be57ce0e78b', function(e, r) { console.log( web3.fromWei( r.toNumber() ) ); }) undefined 0
migrate
: truffle(ropsten)> migrate
Compiling ./contracts/ConvertLib.sol... Compiling ./contracts/MetaCoin.sol... Compiling ./contracts/Migrations.sol... Writing artifacts to ./build/contracts Using network 'ropsten'. Running migration: 1_initial_migration.js Deploying Migrations... ... 0x93cf7dbde8c362534dc912926fc4d7df54c9c1f5e0a7dcfd964a0177b42bc7be Migrations: 0x02519d13f61bdcad838d938611e6722c3d1f8034 Saving successful migration to network... ... 0xec501a78cc11c723ab60186167765aa7c422177153cd72a976e66441db2b5b95 Saving artifacts... Running migration: 2_deploy_contracts.js Deploying ConvertLib... ... 0xf172d9a9ff9f1fdfdfabc816d89f5a5e710ba26e3a2ad9e1661c9dea56564f04 ConvertLib: 0xd04bffb73bf546985938a596565141d3a3bf7f0d Linking ConvertLib to MetaCoin Deploying MetaCoin... ... 0x4d9814f1d9a959e83828bf26319dd91d73be977395d88e9e8239bb4c4ed5b0eb MetaCoin: 0x20fd16643d857ce544a91ae4c80385af99dad196 Saving successful migration to network... ... 0x0dff866460d24d56d94dcf5f833aa4fa8ae289cb708ff5c9012ce21447575ce8 Saving artifacts... truffle(ropsten)>
Source: https://habr.com/ru/post/348656/
All Articles