Hot answers tagged

40

It is a good practice to always declare the type access to your methods. If you forget to declare a function internal previous version will compile happily and will not warn about the potential security issue. function setFee(uint _fee) public { fee = Sf.mul(_fee,10); } The constant modifier is being replaced by view and pure. With view you cannot ...


39

One way is to use Remix, the browser-based Solidity realtime compiler and runtime Solidity (formerly named browser-solidity). Paste the code in Remix. Wrap the function inside a contract if needed. (Example: contract C { ... }.) Click Contract tab and then Create to instantiate the contract. Enter the desired input next to the function. For bytes1 ... ...


32

Anatomy of a Mapping A mapping is used to structure value types, such as booleans, integers, addresses, and structs. It consists of two main parts: a _KeyType and a _ValueType; they appear in the following syntax: mapping (_KeyType => _ValueType) mapName In the example contract provided above, mapping (uint256 => CampaignData) campaigns the ...


26

The JSON is called an ABI. You do need the source code, as you have, and one way to get the ABI is to paste it in Solidity Browser, then copy the Interface value.


22

You can pass bytes parameters in Remix or browser-solidity as the array of single bytes, for example ["0x00","0xaa", "0xff"] is equivalent to "0x00aaff" Do not know why, but the Remix IDE and browser-solidity interprets "0xaabb11..." as string. For development and testing purpose on private or test net you can use the code below. Function hexStrToBytes will ...


19

I hope this answer helps to provide a more complete picture alongside the accepted answer. Transaction costs are based on the cost of sending data to the blockchain. There are 4 items which make up the full transaction cost: the base cost of a transaction (21000 gas) the cost of a contract deployment (32000 gas) the cost for every zero byte of data or ...


16

I personally use Dapple for writing Solidity unit tests. It has the advantage of not requiring a Javascript testing framework layer, so you can stay entirely in Solidity. Plus I can use it to test contracts that use imports, which I'm not sure is possible in chriseth's browser-based Solidity. Edit: The accepted answer is definitely a better option for ...


15

I'll leave the precise interpretation of the OpCodes to someone else and just point out that they are different instructions so different implementations are to be expected. According to this: http://solidity.readthedocs.io/en/develop/control-structures.html#error-handling-assert-require-revert-and-exceptions Starting with Metropolis, revert; will return ...


15

Try require(address(this).balance >= 1000000000000000)


14

The address needs be surrounded with quotes. "0xca35b7d915458ef540ade6068dfe2f44e8fa733c" should work.


14

Try using .getData(). .getData() returns the encoded parameters of a function in order to send the transaction manually. You can then stick this in web3.eth.estimateGas() (the one on web3.eth, not on a given method.) to simulate sending the transaction. Here's an untested example, but hopefully it can help you on your way: var contractData = ...


14

If you can make your function pure, you should always make it pure. Otherwise, if you can make your function view, you should always make it view. Advantages: If you make your function pure or view, you can call it for example through web3.js without needing a transaction, without any gas cost and without confirmation delay. Currently, it will not reduce ...


13

There are a few ways to do testing of Solidity contracts. The easiest, at least in my opinion, is blackbox testing with Truffle. Contracts tend to be relatively small and self-contained, so blackbox testing seems appropriate here. Truffle lets you write unit tests in Javascript using Pudding, an extension of web3, Mocha and Chai. A typical test looks like ...


13

MetaMask accounts are EOA accounts. They are not seen in Geth because the keys have not been imported from MetaMask. Basically, MetaMask creates a hierarchical deterministic wallet hd-wallets from the seed (words). An HD wallet has many keys and each of these are EOA. (The MetaMask UI only shows some of them, but more keys can be continuously derived.) ...


13

When you are trying to compile contract with private constructor, for example this one: pragma solidity ^0.4.15; contract PrivateConstructor { string public title; function PrivateConstructor() private { title = "Private Constructor"; } } you receive this error: TypeError: Constructor must be public or internal. So looks like ...


13

It states in the v0.5 breaking changes list that: Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables. The docs are implicit in stating that strings and bytes are included - as they are array implementations, rather than an error in the compiler.


13

From this: pragma solidity^0.5.0; contract Contract { string public name; function Contracts(string passedName) public { name = passedName; } function setName(string newName) public { name = newName; } } To This: pragma solidity^0.5.0; contract Contract { string public name; function Contracts(string ...


12

Transaction costs are the costs for sending the contract code to the ethereum blockchain, they depend on the size of the contract. Check out the source code: var getGasUsedOutput = function (result, vmResult) { var $gasUsed = $('<div class="gasUsed">'); var caveat = lookupOnly ? '<em>(<span class="caveat" title="Cost only ...


12

As of Solidity 0.5.0. https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables. For example, change uint[] x = m_x to uint[] storage x = m_x, and function f(uint[][] x) to function f(uint[...


11

Install https://metamask.io/ and Navigate to https://remix.ethereum.org/ Click on settings -> choose your compiler (e.g. 0.4.19+commit.c4cbbb05). Note that optimization is unchecked. Login to metmask and choose your network (i would suggest first testing using ropsten test network and using the buy button to get ETH from a free faucet) Go back to the compile ...


10

Another option is using EthereumJ implementation. We have recently released version 1.2.0 and it has nice feature specially for such cases. You may check the sample here StandaloneBlockchain bc = new StandaloneBlockchain(); SolidityContract contract = bc.submitNewContract( "contract A { uint a; ... }" ); contract.callFunction("funcName", "arg1", 2,...


10

Browser-solidity is able to import library code from GitHub (importing from Swarm is on the way). E.g. you can write: pragma solidity ^0.4.0; import "github.com/ethereum/dapp-bin/library/stringUtils.sol"; contract test { function foo() returns (bool){ string memory a = "hello"; string memory b = "world"; return StringUtils....


10

you could use events to debug(log) the desired variable.Please read the official documentation to get more details. e.g : pragma solidity ^0.4.0; contract test { event test_value(uint256 indexed value1); uint256 value_test; function test_f() { //value_test processing test_value(value_test); // log ...


10

This is the Swarm hash. It is documented at https://solidity.readthedocs.io/en/develop/metadata.html Extract follows Contract Metadata The Solidity compiler automatically generates a JSON file, the contract metadata, that contains information about the current contract. It can be used to query the compiler version, the sources used, the ABI ...


10

Quick Lower Bound You can put a lower bound on the cost, because each address will include one SSTORE, at the very least. So the cost to hard-code 50k addresses, at 20 kgas each would be more than 1 Gigagas total. The latest block limit is 6.7 Megagas. It will not fit, by a long shot. Gsset - 20000 gas - Paid for an SSTORE operation when the storage ...


10

Here are instructions to find contract ABI using online remix solidity compiler. In the "Compile" tab, click on the "Details" button. On the modal, go down to the "Interface" section. Click the copy button.


9

The downside is that optimization adds extra complexity, and more complexity means more bugs. See this post for a previous bug in the optimizer: https://blog.ethereum.org/2017/05/03/solidity-optimizer-bug/ Solidity bugs can be rather terrifying: If the EVM is buggy we may be able to upgrade the network to fix it, but it's not clear that there's anything we ...


9

Currently etherscan does not support verification of multifile contracts. But there is discussion on etherscan's reddit about this issue. Concatenating files into one worked some time ago, but since version 0.4.7 Solidity includes hash of contract's metadata at the end of compiled contract. As this metadata contains file names and hashes, you may get the ...


9

I cannot find the contract details to get the json interface anymore. If you select the compile tab, there you will find a button called details. When the details is clicked, a pop-up window will appear. You can find the ABI at under the metadata section, and there output you will see the ABI (json interface). OR under the interface section Click green ...


9

The easiest is to use remixd to share your build directory with remix. Here are indication how to configure it Accessing a shared folder in Remix IDE using Remixd. Another option is to use the remix feature that allows importing directly from github, so you do not need the openzeppelin files. pragma solidity ^0.4.15; import "github.com/OpenZeppelin/...


Only top voted, non community-wiki answers of a minimum length are eligible