Questions tagged [contract-design]

for high-level questions about the choices made during contract construction

Filter by
Sorted by
Tagged with
1
vote
1answer
17 views

Is it gas efficient to store the msg.sender in a local var?

Suppose I have a function that refers to msg.sender many times: function foo() external { doSomething(msg.sender); // ... doSomethingElse(msg.sender) // ... emit LogSomething(msg....
2
votes
0answers
17 views

how does Ethereum singing request work?

What's the need so that some dapp websites ask you to sign messages via metamask the first time you get on their website ? as I read, it's so that they make sure you get authenticated. So you sign the ...
1
vote
0answers
27 views

Smart contract copying code beyong code length

I've seen a couple smart contracts (eg the contract decompiled in this answer) use this pattern: Code[Code.Length:Code.Length+x] This surprised me because I would expect Code.Length to mark the end of ...
0
votes
1answer
29 views

Is it possible that a smart contract receives ether and if condition is true then send those ether to specific account?

I am planning to create an app that connects to the Binance account of the user (API) and trades automatically with his credentials. If transactions (only one buy and sell pair) successfully earn BTC, ...
2
votes
1answer
35 views

solidity math overflows

function nice(uint8 x) public returns(uint8 z){ uint8 z = 0; z = z + 240; require(z = z + x, "overflow"); } I have been using this code with the solidity versions, ...
2
votes
2answers
101 views

why require() is checked after executing transfer()

From OpenZeppelin's ERC721 contract: function _safeTransfer(address from, address to, uint256 tokenId, bytes memory _data) internal virtual { _transfer(from, to, tokenId); require(...
0
votes
1answer
10 views

How to restrict function to only callable from one external contract?

I have a Reputation contract which has a function foo() that sets some point. In another contract called Market I want to use the foo() function. But I don't want any one to be able to call foo() ...
0
votes
1answer
24 views

Why this delegatecall attack not working?

I am confused by this simple contract. The main purpose is to bypass the OnlyContract() modifier. Victim contract. pragma solidity 0.7.6; contract Victim { modifier OnlyContract() { ...
1
vote
1answer
38 views

Why is storage cheaper than a memory?

Let's consider the following completely useless code (tested in Remix): // solium-disable linebreak-style pragma solidity ^0.8.0; contract MyContract { struct TestVars { uint8 a; ...
0
votes
1answer
18 views

Chaining smart contract deployments through instantiation in constructors

As the title suggests, I'm not sure what is considered to be good practice when it comes to deploying smart contracts that interact with each-other. My example: migrate contract A. Contract A creates ...
2
votes
2answers
76 views

variables is brackets ()

What's the meaning of the code, wherein the return values of a function are stored in variables which are initialized inside brackets, like the following: (bool success, bytes memory data) = _contract....
2
votes
1answer
29 views

Issues with extreme large solidity mapping

Is there any scalability problem if the number of items of a solidity mapping increase too much? For example, any problem when adding, changing or removing some item in the dataset? Or any other ...
0
votes
0answers
23 views

test return value of transaction

contractA function test() internal { stateVariable = 20; uint num = 20; assembly { mstore(0x00, num) return(0x00, 0x20) } } How do I test the return ? I am using hardhat , but i don'...
1
vote
1answer
58 views

Random distribution of large amount of NFT

I'm an beginner in solidity, and with a teamate we have a project for an NFT contract. I have a contract with 5000 NFT limite (cards, and 20 specific card form a familily, each family and cards are ...
2
votes
1answer
37 views

how do we use `commit/reveal` to count votes?

Let's say we are doing an election and it's easy to note that we need commit/reveal scheme to hide votes. So, we store hashes while users commit their votes. Now, let's say the election is over. How ...
1
vote
3answers
92 views

how does commit/reveal solve front-running?

I am trying to make sure I understand something correctly, so I will pose a problem with a solution. Problem: Let's say commit has been called. After this, we call the reveal. attacker listens to ...
0
votes
2answers
41 views

how can we stop front running for `approve` [duplicate]

function approve(address _spender, uint256 _value) returns (bool success) This is the ordinary ERC20 approval method. It's easy to figure out that we have a problem of front-running. Alice gives ...
1
vote
1answer
11 views

How to empty uint array after value is assigned?

Here is an uint array defined in smart contract: uint256[] public bidderItemID; And bidderItemID is assigned value in contract: bidderItemID = [1,2,3]; How can I empty bidderItemID after it is ...
0
votes
0answers
25 views

How to implement crypto refunds in a smart contract?

I am quite new to cryptocurrencies and after some investigation I thought this might be the right place to ask. Problem: I have a small business and I would like to offer to my customers the option to ...
0
votes
1answer
23 views

What is the legitimate use case of using library contract without using contract address?

Let us look at the following contract contract LibraryA { function getSenderAddress() public returns (address); } contract LibraryB { function getSenderAddress() public returns (address);...
0
votes
0answers
10 views

How to create a smart contract which hold different erc20 tokens or ETH?

I want to make a simple smart contract. where any user can send ETH (or any ERC20 token) to the smart contract. but how to keep track of every token, their weight in the smart contract, and the ...
0
votes
0answers
15 views

Saving gas on large amount of token distribution

I was wondering if this function is safe to use? is there any way to reentrancy or cause arithmetic issues? function sendBatchCS(address[] _recipients, uint[] _values) external returns (bool) { ...
1
vote
0answers
18 views

ethereum makes extra space for no reason in memory

pragma solidity ^0.6.8; contract C { function test3(uint f) external returns(uint256[3] memory) { uint256[3] memory arr; arr[0] = 1; arr[1] = 2; arr[2] = 3; ...
1
vote
1answer
31 views

what does call stack of 1024 mean in solidity?

Assumption 1: If we create a local variable, call stack increases by 1, because we put this new local variable on the stack. if I create another local variable, it again increases by 1. So, with this ...
0
votes
0answers
13 views

ERC165 - why is it a good idea?

I am trying to understand ERC165. Let's say we have a contract - contractB and we have a contract contractA. I think, what this ERC says is that before contractA can call the contractB's function, it ...
1
vote
0answers
22 views

scope of references to external smart contract, global variables, eleminiate redundant code

Imagine that there's a smart contract deployed which contains a number of sub-contracts like so: pragma solidity ^0.5.0; contract A { address contract = 0x6663130dDfF21E3BC963Ee902005223b9A202106; ...
0
votes
0answers
26 views

Are there any simple, simulated smart contracts to try out?

The barriers to buy, sell and trade cryptocurrencies are fairly low. Anyone can try it now. But very few of the same people can say that they've ever tried to enter into a smart contract. Are there ...
0
votes
1answer
52 views

Solidity: Wrapping an ERC20 token inside of a new token

Is it possible to wrap and track a ERC20 token amount in another token? Would I need to use ERC-721 to do this? Let's say I send my ERC20 token to a specific smart contract which will wrap it in a new ...
1
vote
0answers
27 views

why does it copy into memory?

Let's say I have the following.. contract D { function again(uint256[3] memory bla) external returns(uint) { return bla[0]; } } contract A { function test(D f, ...
1
vote
0answers
10 views

why would I need to use `memory` in front-of parameter?

Let's say I have the following contract. contract D { function again(uint256[] memory bla) public returns(uint) { return bla[0]; } } contract A { function test(...
1
vote
0answers
20 views

How to implement variable interest rates efficiently

If a smart contract wants to pay interest on a balance of a user that's pretty much straight forward. You record the balance and the timestamp when a token was deposited and when the token is ...
0
votes
0answers
20 views

estimating gas costs for dapps

I am new to ethereum and I am trying to build something like a vault where you can withdraw and deposit assets, based on the dApps protocols you are using (e.g :- yEarn, aave). I know there will be ...
1
vote
1answer
14 views

Several small or one big contract? (deloyment cost)

Does deploying several small contracts use more gas than deploying them as one, bigger contract with the same functions (that don't interact with each other)? How much is the difference?
0
votes
1answer
29 views

how does gas get taken from the account?

Let's say we call the function: function nice(uint a) public returns(uint256){ k = 100; z = 100; f = 100; require(a == 5); ...
0
votes
0answers
17 views

why can I specify `memory` if function is internal?

In the latest compiler versions, we can do the following: function balanceOf(uint[5] memory data) external view returns (uint256) { Until some version of the compiler, i couldn't specify memory, ...
0
votes
0answers
20 views

how does calldata not work in this case?

pragma solidity ^0.7.4; contract D { function test2(uint[1] calldata good) external { uint c = good[0]; } } This code is perfectly valid. What I do is deploy ...
0
votes
0answers
10 views

what's the difference between assembly part and opcode part in remix?

In the remix, there's compilations details button. When you click, one can see Bytecode and also assembly parts. Bytecode is an object which has object key and opcodes key in it. As far as I can see, ...
0
votes
0answers
18 views

ethereum solidity - gas cost wrong

This is bugging me so much time and spent hours and hours on this, I'd really appreciate your help so much.... // this costs 21394 function test2() external { } // this costs 21507 function ...
1
vote
1answer
21 views

Can I use an emoji as part of a string in Solidity?

Can I use an emoji in Solidity?
0
votes
0answers
14 views

Smart contract for selling log-in subscription service

If not ethereum, which smart contract token would be best suited for fulfilling the sale of a month-long anonaymous subscription for cryptocurrency that enables the buyer to have their own username ...
0
votes
1answer
22 views

selfdestruct: why am I able to execute the function containing selfdestruct(…) twice?

I have created the following contract with a destroy function invoking selfdestruct: pragma solidity >=0.5.0 < 0.7.0; contract Terminate{ function destroy() public { selfdestruct(msg....
1
vote
1answer
67 views

How much C++ is needed to write smart contracts?

Solidity seems to be written in C++ so does that mean that smart contracts require some level of C++ programming skills to get by? How much background knowledge is required for writing a simple one or ...
1
vote
1answer
26 views

Ethereum storage - how does this cost happen?

contract A { uint x = 10; function test2() public { x = 0; } } I can see that: Transaction cost is 13204 Execution cost is 5136 How can this be ? I ...
1
vote
0answers
54 views

Ethereum cost - how does it work in memory

This is really bugging me and doesn't seem any article that can help at all. wiki says further the gas costs for using memory: The fee for expanding memory is determined via a subtract-the-integrals ...
1
vote
1answer
20 views

what is expansion cost?

I am trying to understand what expansion cost is. We can see that there's this formula to count the memory cost - Now, It's also said that this formula doesn't include the expansion cost. Can anyone ...
3
votes
2answers
79 views

how can library change state?

I have Very Interesting cases about Libraries. CASE 1: I read the following in the official solidity docs: Library functions can only be called directly (i.e. without the use of DELEGATECALL) if they ...
0
votes
1answer
14 views

how does library end up in contract?

library C { function a() public returns (uint) { return 15; } } contract A { function a() public returns (uint) { return C.a(); } } This is the code I am using. I ...
0
votes
0answers
18 views

how do i call a constructor from another constructor?

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.3.0/contracts/token/ERC20/ERC20.sol"; contract MyToken is ERC20 { constructor(uint supply) public { // ...
0
votes
1answer
23 views

Returning a dynamic array of structs

I'm trying to return a dynamic array of structs created on the fly from storage data, similar to how we return ViewModel from DB data in MVC. What is the best way of doing it in Solidity? Tried ...
1
vote
2answers
168 views

How two untrusted parties can make transactions without any involvement of the third-party?

I'm trying to create a contract where where PartyA and PartyB had to trade. PartyA sends money to a contract and PartyB sends goods to partyA. After partyA receives the goods it allows PartyB to ...

1
2 3 4 5
36