New answers tagged

0

Change contract EnemyCon to interface EnemyCon. Also all methods inside interface has to be declared as external, so for method setNum remove the public keyword and add external.


1

Subhod's answer above is good, but for variety's sake, here's a version I wrote that avoids ints entirely and keeps everything uints. I've kicked the tires a bit but you should test further before deploying it. Also of course, as Nick Johnson noted on Twitter, quicksort's worst case is slow (O(n^2)) so you might want to find a faster O(n log n) one. ...


1

It's not clear if you edited out important sections or if they are missing. Possibility 1 - the way you are testing Possibility 2 - msg.sender doesn't have 100 tokens to spend Possibility 3 - you haven't instantiated your token instance In the example, token has no address for a contract that is actually deployed, just an interface to a hypothetical ...


0

actually there's no need to waste gas on this. address is available and can be used in constructor using address(this)


0

Actually when you're creating project inside Infura you're allowed to set restrictions like: Public/ private key encryption What is the contract address the node is going to interact with Set requests per second ( or set total for each day ) Allowed origins or user agents Allowed methods ( you can allow methods that your project needs and disallow all other ...


0

To call a contract fallback function you can use the below way using Javascript: Consider I have to call a set function that has a uint256 as a parameter. const demo = await Demo.new(); const valueToUpdate = 1234; const signature = web3.utils.sha3('set(uint256)').slice(0,10); const msgData = signature + web3.utils.toHex(...


0

Here is an example. Contract 0x00000000002bde777710c370e08fc83d61b2b8e1 is killed in tx : https://etherscan.io/tx/0x026a971c7cf5476c93be05742abe56fe640331eb9bb36c53926ff95cb0529b62, and new contract at same address is created in tx: https://etherscan.io/tx/0x272dd4eb0b2073d3e5ecee1c62155790760ec818e6f37c86df02e60d75d7be2f#internal


1

The concept of decimals is purely for user-friendliness - inside the blockchain there are no decimals. That's why it's optional. If there are no decimals then you simply display what the token amount inside the contract shows - things get easier than with decimals. So it behaves like decimals was 0. This concept is rather confusing for many people. You can ...


1

You can use: token = await Token.new('name', 'symbol', decimals) in order to deploy and interact with a new Token instance token = await Token.at('0xSomeAddress') in order to interact with an already-deployed Token instance Depending on your Truffle version, the await before the at may be redundant. But you can leave it there in order to stay on the safe ...


0

In step 1, the user just transfers to your contract in the ERC20 token contract. Your contract is not even aware this transfer took place. In step 2, the user buys some token X from your contract, for a price, and your contract sends the tokens, if the contract has enough, or the transaction fails and the funds are returned to the user, which is what you ...


0

The problem with the 2300 gas is when the call originates from a smart contract. If you trigger the transaction manually you are able to control the gas required for the fallback function so there will be no problem. But if you trigger a .send() or .transfer() from a smart contract, the destination fallback function will be called with only 2300 gas limit. ...


0

I was returning a result that i assumed was 0 by default. Changed the function to; function getNFTBalance(address _owner) public view returns (uint256){ return nft.balanceOf(_owner); } Getting the right balance now


0

//SPDX-License-Identifier: GPL-3.0-or-later // A simple smart wallet to be used for testing. // Based on code from https://github.com/argentlabs/argent-contracts/blob/develop/contracts/wallet/BaseWallet.sol pragma solidity ^0.7.1; contract TestSmartWallet { address payable owner; constructor(address payable _owner) payable { owner = _owner; ...


1

pragma solidity ^0.7.0; contract Wallet { address private owner; constructor () payable { owner = msg.sender; } receive () external payable { // Do nothing } function execute (address payable _to, uint _value, bytes memory _data) public payable returns (bytes memory) { require (msg.sender == owner); (bool success, bytes ...


0

Turns out this is trivial to do in web3js but ethersjs does not offer any functionality to do it. In web3js: myContract.events.MyEvent() .on("data", async (error, event) => { myDatabase.insert(event); }) .on("changed", async (error, event) => { // Called when event is no longer valid myDatabase....


0

You should try it: let currentValue = await contract.methods.getBadgeOwner('withdraw').call(); Good Luck!


0

The uri need to point to a json file. This is the "ERC721 Metadata JSON Schema" referenced above. { "title": "Asset Metadata", "type": "object", "properties": { "name": { "type": "string", "description": "Identifies ...


2

For an array, you'll need to add a function in your contract, for example: uint[] private someArray; function getSomeArray() external view returns (uint[] memory) { return someArray; } For a mapping, you'll need to extend your contract design, for example: Every time you add/remove an item to/from the mapping, also add/remove that item from/to an array ...


0

You cannot use Tron addresses directly on Ethereum (or in Solidity, as you have shown).


0

You still need to supply the mixing smart contract the actual address to which you want to transfer the tokens to at some point. And since all (state-changing) transactions to smart contracts are publicly visible in the corresponding block, others could easily infer the sending and receiving parties.


2

A copy of structTest is saved separately in the array and in the mapping. In order to get rid of this redundancy, you can maintain an array of keys instead of values: uint[] all; // instead of `test[] all;` mappings (uint => test) map; function bla() { test memory structTest = test(5); all.push(10); // instead of `all.push(structTest);` map[...


1

Is this how it's supposed to be? No, you can (and should) omit the uint amount parameter. Instead, the user should pass the desired amount within the msg.value. This way, the user (and not your contract) will be the one providing the funds.


2

A function selector is the first 4 bytes in the hash of the function's prototype. A function prototype is defined as the function's name and its argument types by order. It allows you, for example, to call a function without knowing its exact return-value type: bytes4 private constant FUNC_SELECTOR = bytes4(keccak256("someFunc(address,uint256)")); ...


0

npm ERR! enoent spawn git ENOENT npm ERR! enoent This is related to npm not being able to find a file. This suggests that Git is not installed. One of the dependencies requires Git to be installed. You can download it from here: https://git-scm.com/downloads.


1

When you declare a parameter as storage you are limiting the function to only accept parameters with that qualifier. The qualifiers storage, memory and calldata specify where the data is located. You can convert implicitly storage to memory and calldata to memory, but the reverse is not possible. string public stor = "banana"; function test(string ...


1

The problem is that a ends up pointing to a very large memory address and that causes the EVM to run out of gas, since you need to pay for memory used. The first storage slot was modified by this line test = "good"; Since it is a "short" string (less than 31 bytes) it is stored in one slot in compact form (data at the right and length x ...


0

Any smart contract can interact with any other smart contract (unless it's somehow restricted in code). All you need is: The address of the other contract Knowledge of the other contract's functionality you want to access. Typically this is provided as an interface. Here's a minimal example on how to interact with another contract which is already ...


1

The article is wrong. Value types like bool, uintXXX, bytesYY, etc. are stored in stack, for dynamic types you are forced to choose memory or storage. To read the array data just mload the address. Fixed size data arrays only contains the data, they don't store the length at the beginning like dynamic arrays. function test() public view returns (uint r) { ...


1

As you said the struct is packed. Since sload(0) returns 32 bytes it is returning the whole struct. To access the indiviual parts of the struct use bit shifts and masks. assembly { let w := sload(0) a := and(w, 0xffffffffffffffffffffffffffffffff) b := shr(128, w) }


1

This description distinguishes between storage and non-storage. It seems to be using 'memory' in order to refer to non-storage. It's probably not the best choice of words, as the keyword memory is used in order to indicate that a given variable is allocated on the heap. In other words, you have 3 data sections: Storage, where consistent data is located ...


0

msg.value is the transaction value in Wei, not the equivalent amount in Ether. If you send 1 Ether (= 10^18 Wei), msg.value will be 10^18.


1

When a smart contract has been deployed its bytecode is saved on the chain. To execute the contract you send a transaction with the smart contracts address as recipient. Additional data can be passed to the contract as calldata which can be imagined as parameters to the contracts execution. When the node validates the transaction it notices that the ...


0

It's possible by Oct 01,2020 You just have to specify correct complier version, If you deployed your contract using Truffle, then your complier version by default will be the version of "solc" package used to compile your contract code. By default "solc" package set "optizations" to "Yes" So when you verify your ...


0

ATM, what you can do is: constructor(address[] memory addresses, uint256[] memory balances, bool[] memory flags) public { uint256 length = addresses.length; require(length == balances.length, "INVALID_INPUT"); require(length == flags.length, "INVALID_INPUT"); for (uint256 i = 0; i < length; i++) { // do whatever ...


0

No, solidity doesn't support that syntax. You can create a temporary reference and modify that instead. Data storage myData = data[schedule()]; myData.x = 1254; myData.z = 0x576657220676f6e6e61206769766520; myData.otherRandomThing = "u"; myData.foo = 8309; myData.lala = "p";


2

Due to their unpredictable size, dynamically-sized array types use a Keccak-256 hash computation to find the starting position of the array data. So in your example: The position of storageArr[0] is at keccak256(uint256(0)) + 0 The position of storageArr[1] is at keccak256(uint256(0)) + 1 The position of storageArr[2] is at keccak256(uint256(0)) + 2 Here ...


1

I used the below fallback function and it worked. receive() external payable {} This post was what I was actually looking for in my original question. Everything works well now. How do you send Ether as a function to a contract using Remix?


1

I don't think number division is very expensive in Solidity, but not sure. Depending on your needs, I would be probably more worried about the precision: as there are no floating point numbers in Solidity a division is bound to lose some precision. In any case, if at all possible, do it off-chain. Basically you should always do everything off-chain unless ...


0

The last steps in the vmtrace in etherscan [106] 6358 DUP2 5220808 3 1 [107] 6359 DUP7 5220805 3 1 [108] 6360 DUP1 5220802 3 1 [109] 6361 EXTCODESIZE 5220799 700 1 [110] 6362 ISZERO 5220099 3 1 [111] 6363 DUP1 5220096 3 1 [112] 6364 ISZERO 5220093 3 1 [113] 6365 PUSH2 5220090 3 1 [114] ...


0

It has to do with addressing storage. Let's break it down. Consider a bool and an address. The bool takes 1 byte (limitation of optimization) and the address takes 20 bytes. So we need 21 bytes of storage. The EVM uses 32-byte words which are the smallest chunk of storage that is addressable. If you go contract A { mapping(uint => bool) b; mapping(...


2

This is about how the variables are packed together and about how smart the compiler is (hint: not very smart). The compiler only tries to pack subsequent variables together but it can't pack variables which have something else between them. In your first case the variables could all fit inside a single 32 byte slot, but if I'm not mistaken the compiler ...


2

To add to goodvibration's answer, the problem with sending ERC-20 to any contract is that the contract is not aware of that transaction. There's no function that's called when the tokens are received, so it's not possible to assign that balance to a specific sender. This is why things like decentralised exchanges use approve and transferFrom. In this case a ...


0

No, you can't refer to stablecoins (stabletokens, more likely) like that. You need to make external contract calls to get the value of the desired token from somewhere and then compare that to something.


0

Use a different testnet! At the time of this writing, the latest blocks in each testnet had a block gas limit of: Rinkeby: 10 million (see block) Kovan: 12.5 million (see block) Goerli: 8 million (see block) Otherwise you can also always try and reduce the contract size, see here.


1

You have probably guessed you have a fundamental issue with the storage. I will try to explain. The whole idea of a "proxy" is that the proxy is running, so everything that happens unfolds in the context of the proxy contract and uses the proxy contract for storage. The "implementation" contract provides code that proxy injests and runs ...


0

Could you share your contract code so we can try to see what's causing the high deployment cost? There may or may not be an easy solution to this problem. You should check how much gas is being spent on the size of your compiled contract code, and how much is being spent on executing the contructor function. Try to reduce the size of your contract by: ...


0

It turns out this was a small issue with EthersJS. The estimated gas is not input correctly. By manually defining the gas limit, it works: myContract.address_to_addresses(<someAddress>, 0, { gasLimit: 60000 }); It's unclear why this is. If I set the gas limit to the actual cost of the function as defined in the abi (1654) then it fails - I have to set ...


0

As the amount of players grows, that loop will consume more and more gas. If you get a lot of players, it will start to cost too much in transaction fees. If the amount of players should be unlimited, you should maintain a mapping to track which addresses are already participating: mapping(address => bool) public playersMapping; That way you can run one ...


0

I've never heard of a concept called hierarchical blockchains. If your concept requires some interaction between the blockchains you have to use something else than Geth, but I have no idea where you could start. The whole concept is rather... weird. Which doesn't necessarily mean anything bad, just unheard of.


0

You problem is with line: for(uint i=0; i<=playersAddressList.length; i++). Arrays are zero-based so you are iterating too far. If for example your array has 3 entries the length will be 3. But your loop checks for 4 entries (at indexes 0, 1, 2 and 3) - the last index doesn't exist so it crashes. Just change your condition to for(uint i = 0; i < ...


Top 50 recent answers are included