10

As of Solidity 0.6.0, the keywords virtual and override are available natively in Solidity for function overriding. The purpose of these keywords is to be more explicit when overriding a function. Base functions can be overridden by inheriting contracts to change their behavior if they are marked as virtual. The overriding function must then use the override ...


9

Yes. Solidity version 0.6.2 introduced a high-level way to use the create2 opcode. From the release docs: When creating a contract, you can specify the salt as a "function call option": new Contract{salt: 0x1234}(arg1, arg2) As an example, the following deploy() function will deploy the Test contract using a salt of 0x1234 and a constructor param of 123. ...


8

As of version 0.5.12, Solidity includes an assembly function chainid() that provides access to the new CHAINID opcode: function getChainID() external view returns (uint256) { uint256 id; assembly { id := chainid() } return id; } To use it, ensure you set the compiler's EVM version to Istanbul with the --evm-version istanbul flag. ...


8

transfer() and send() should be avoided. Gas specific code (call.gas().value()()) should also be avoided. call.value()() should be used, for example: contractB.call.value(1000)() It is also critical that you make sure to guard against reentrancy by making all state changes, before call.value()(). https://diligence.consensys.net/blog/2019/09/stop-using-...


7

I think you could do worse than to start with something like this: pragma solidity 0.5.14; import "./HitchensUnorderedKeySet.sol"; contract DirectedGraph { using HitchensUnorderedKeySetLib for HitchensUnorderedKeySetLib.Set; HitchensUnorderedKeySetLib.Set nodeIds; struct NodeStruct { HitchensUnorderedKeySetLib.Set parents; // in ...


7

It sounds like you are being front run. This means that a bot is watching you send a transaction, reading the input (specifically, _answer), and submitting a transaction with the correct answer but a higher gasPrice. A miner will accept their transaction before yours, causing theirs to succeed and yours to fail. You can see that this is, in fact, what is ...


6

Solidity 0.6.0 and Greater (Updated 2020) As of Solidity 0.6.0, there is array slice functionality built into Solidity. The syntax is similar to existing languages in that the array takes the following parameters x[start:end]. Here, start and end are ints that represent the starting and ending index to be sliced. If start is greater than end or if end is ...


6

Fixed-Point ABDK Math 64.64 binary scalar with 2^64 precision one of the more popular fixed-point libraries, it's been praised as being super efficient Exponential decimal scalar with 18 decimals developed by Compound.Finance Fixidity decimal scalar with arbitrary number of decimals slower than 64.64, but with a somewhat more palatable API DecimalMath ...


5

There is more to this than the question suggests so I'll just focus on the prose part of the question. Comments are not compiled into contracts, therefore there will be no record of them in the bytecode. You can, however, create a convincing record of the document contents and acceptance by the parties. Put the legal prose in a document such as a PDF and ...


5

I managed to fix it by explicitly defining the EVM version in my truffle config file. It's still weird because Remix supports this (as seen in the attached image) as well but still fails. compilers: { solc: { version: "0.6.0", settings: { optimizer: { enabled: true }, evmVersion: "petersburg" } } ...


5

You can use HDWalletProvider to connect to a provider (infura or your local node) using a mnemonic phrase. In case you are using truffle, you can do it by seting you config to something like this: ... networks: { kovan: { provider: () => new HDWalletProvider(mnemonic, `https://kovan.infura.io/v3/${infuraKey}`), network_id: 3, ...


4

Update as of 2020. Solidity 0.6.8 introduced min and max keywords that can now natively tell you the min and max of an expected type. From the release page: Implemented type(T).min and type(T).max for every integer type T that returns the smallest and largest value representable by the type. You can try it out with the following code. Note that the ...


4

you can make use of abi.encodePacked function function random() private view returns(uint){ uint source = block.difficulty + now; return uint(keccak256(abi.encodePacked(source))); }


4

That's because the value has to be 32 bytes long in the new layout, like: 0x6c00000000000000000000000000000000000000000000000000000000000000 From remix-ide github: if you specify bytes32, then the value has to be 32 bytes long. If you don't mean that, you could use a shorter length, the types string or bytes. Duplicated issue with the same question ...


4

Call me an idiot, but I believe you can use negative numbers in Solidity. Data Type Keyword: int Here is an example I just wrote for you: pragma solidity ^0.5.12; contract TestContract { int public globalNumber; // Add a positive or negative number to globalNumber function addToGlobalVar(int num) public returns (int){ globalNumber += ...


4

Solidity 0.6.0 and Greater (Updated Apr 2020) As of Solidity 0.6.0, there is try-catch functionality built into the language. The syntax is similar to existing languages, however functionality is limited at this time to try expressions representing an external function call or a contract creation. The try part of the try-catch functionality is only able to ...


4

Surely, you may do this. Here are high-level steps: To the end of your bytecode append a few additional opcodes that copy stack and storage content into memory and return them together with memory content. Prepend you bytecode with a simple constructor that will just deploy your bytecode as a contract. Deploy modified bytecode and obtain deployed smart ...


4

You're right, the contract can't call itself the "approve" method. Instead you can write a script. An exemple with web3js 1.2 could be : var erc20Instance = new web3.eth.Contract(abi,Token_address); erc20Instance.methods.approve(contractAddress, amount).send({from: userAddress}, function(err, transactionHash) { //some code }); with : abi : the ...


4

Solidity doesn't support floating-point arithmetic. You'll need to represent e with a pair of an integer numerator and an integer denominator. For example: uint256 eN = 271828; uint256 eD = 100000; Then, you'll need to replace every plan for x * e in your code with x * eN / eD. In order to achieve maximum accuracy while reducing the number of erroneous ...


4

This contract is quite "old" (pragma solidity ^0.4.15;). The logic is actually taken from the solidity compiler. When targeting older versions of the evm it was not possible to send along all gas. The solidity code still exists even now: https://github.com/ethereum/solidity/blob/develop/libsolidity/codegen/ExpressionCompiler.cpp#L2343 Solidity changed this ...


4

The 'exploit' no longer applies to solc v0.6, since then the compiler generates an error when there's an ambiguous call. As the author says solc v0.5 uses C3 linearization to determine the order in which functions will be called. In the cited example the C3 linearization order is [Bank, MultiAdmin, TempAdmin, Admin]. When funct() is called the first ...


4

You need to add () to the opcode returndatasize. For 0.6.12, check the EVM Dialect to see inline assembly instruction syntax. Change this line: switch returndatasize Into: switch returndatasize()


4

The basic unit of work of the EVM, usually called a word, is 32 bytes. This means that for the EVM, no matter whether we want to express 1 or 2^150, it will take 32 bytes, or 256 bits, to do so. Everything costs gas in the EVM, including memory, so the fewer words the better. A bytes is similar to byte[], but it is packed tightly in calldata and memory What ...


3

Yes. There are a number of approaches using which you can upgrade a Contract1 to Contract2, keeping its state(data & balance) with the same address as before. How does this work? A way is to use a proxy contract with a fallback function where each method call/trx is delegated to the implementation contract (which contains all the logic). A delegate ...


3

Here's the simplest way: keccak256(bytes(a)) == keccak256(bytes(b)); Just use keccak256() while converting the string to bytes.


3

Explaining further what atc mentioned in their comment, Fixed vs variable length Memory arrays have fixed length, which means their length cannot be modified after it is initialized. Since push() appends a new element at the end of the array, it is not available for memory arrays. Storage arrays, on the other hand, can have variable length. Yet, they're ...


3

In case of invalid opcode, use a local variable of type address payable as workaround: address payable self = address(this); uint256 balance = self.balance; In solidity 0.5.14, I get an invalid opcode, debugging showed me, that it is exactly here: address(this).balance Test result: Error: Returned error: VM Exception while ...


3

Below calculations are valid as of 09 December 2019 on Ethereum mainnet. My guess is that most of the confusion comes from the fact that constant of 2300 is used both by Solidity compiler and EVM itself. Here is what I've found so far (assuming that target account is an already created, and not self destructed, contract). EVM: During the CALL(gas, ..., ...


3

The vulnerabilities with tx.origin are from its semantics themselves, so there is no direct alternative. The common advice is to design your contract in a way that does not need tx.origin. From: https://github.com/ethereum/solidity/issues/683: tx.origin is a security vulnerability. As we recently saw with the Mist wallet, using tx.origin makes you ...


3

You're using compelete in the declaration and complete in the initialization!


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