Questions tagged [contract-design]

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

Filter by
Sorted by
Tagged with
173
votes
20answers
54k views

How can I securely generate a random number in my smart contract?

If I am writing a smart contract for some kind of (gambling) game, how can I generate a random number securely? What are the best practises and security trade-offs to be aware of?
164
votes
13answers
38k views

Upgradeable smart contracts

If the contract issuer wants to have a way to upgrade the contract code, so that account data and other things carry over, can Ethereum provide this? Also can this be done without changing the ...
152
votes
2answers
34k views

What is the difference between a transaction and a call?

What is the difference between a transaction and a call? In some interfaces, I can interact with contracts via calls or via transactions. What is the difference between the two, and can contracts also ...
134
votes
2answers
27k views

Are there well-solved and simple storage patterns for Solidity?

Simple and appropriate data organization can challenge Solidity newcomers. It wants us to organize everything in ways many of us aren’t accustomed to. Are there well-solved general patterns for ...
100
votes
7answers
27k views

How can a contract run itself at a later time?

In my blockchain application, I would like to have an event triggered at a later time, hours or perhaps even days after the application has most recently received a transaction from a user. How can I ...
96
votes
7answers
32k views

How can an Ethereum contract get data from a website?

What is the process/workflow behind the process where an Ethereum contract gets some data from a website?
77
votes
9answers
16k views

Can contracts pay the gas instead of the message sender?

Is it possible for a contract to pay the gas costs (or part of it) that result from the contract being called? Or does the sender of a message always pay the resulting gas costs no matter what?
69
votes
4answers
27k views

Difference between CALL, CALLCODE and DELEGATECALL

CALL and CALLCODE take the same number of operands (in the execution stack). For the exception flag being pushed on top of the stack: 0 means exception, 1 means successful execution. CALL is easy to ...
66
votes
2answers
22k views

What's the difference between 'msg.sender' and 'tx.origin'?

If either are used in the body of a Solidity function that may change the state of a contract (write call), what's the primary difference and/or benefit of using one over the other. For example: ...
58
votes
3answers
14k views

Why can't contracts make API calls?

The model that contracts can only accept and process data, instead of also being able to retrieve data from the Internet, seems limiting (even if not, it's less direct). If a concern is that data or ...
52
votes
5answers
26k views

Why are selfdestructs used in contract programming?

I'm reading bits and pieces that selfdestructs within contracts are a good thing. Can someone explain what the benefits are of doing a contract selfdestruct when it comes to Ethereum programming? Is ...
50
votes
4answers
12k views

Is there anything stopping me from using the ethereum blockchain as data storage?

As I understand it, smart contracts are currently immortal by default. Does this mean that I could create a contract that happened to contain an encoded form of baby photos and have it stored forever? ...
46
votes
3answers
30k views

How do you refer to a contract own address inside it?

I am attempting to use 2 contracts together, and I need my 2nd contract to pass his own address to the first contract. Is there an easy way to do this? Like this.address? Or should I update my ...
44
votes
2answers
11k views

Is there a (theoretical) limit for amount of data that a contract can store?

Is there a theoretical limit to the amount of data that one contract can store while running in a private net in which gas is not a concern? Context: In a financial Dapp which is to replace global ...
42
votes
2answers
33k views

Use string type or bytes32?

What are the benefits to using string datatypes over bytes32 to represent text data. It seems my contract runs into an out of gas exception when I switch the data structures from bytes32 to string.
41
votes
3answers
25k views

How can I create a multisignature address on Ethereum?

I want to store my ether in a form that requires more than one digital signature in order to authorise spends. How do I do on that on Ethereum?
36
votes
3answers
10k views

Difference between Memory and Storage?

What is the difference between Memory and Storage ? From both the EVM point of view and the contract design one. Thanks!
36
votes
1answer
4k views

How do I make my DAPP “Serenity-Proof?”

When Serenity comes around, the network will undergo many changes, some of which may have unexpected consequences on existing Smart Contracts. How can I plan so that I am best prepared to take ...
34
votes
1answer
5k views

Where is the private key for a contract stored?

This is a question that I am asked often by developers who assume that a contract has a private key of its own to sign any transactions that it makes.
33
votes
5answers
10k views

Who is msg.sender when calling a contract from a contract

When I call a function in my contract which calls another function in another contract. In the second called contract function who is msg.sender? the contract calling the second contract or my account?...
33
votes
3answers
15k views

How to write an optimized (gas-cost) smart contract?

As we all know, there are many factors that determine a good smart contract, such as: Security: it has minimal/zero vulnerability so they cannot be exploited by an adversary. Immune to Attacks. Cost:...
31
votes
4answers
17k views

How can I represent decimal values in Solidity?

I just started writing some solidity and I noticed that there are no decimal values such as double or float. What do you guys do when you need to return send to a wallet let's say 1.57 ether? Or when ...
30
votes
3answers
7k views

Can a contract safely rely on block.timestamp?

How safe is it to use block.timestamp as contract expiration time? If a miner provides an incorrect timestamp in a block header, how much can it be off before it is rejected by other nodes? Is there a ...
30
votes
2answers
9k views

Why do Solidity examples use bytes32 type instead of string?

In many Solidity examples I read that use strings for parameters or return values, I see they are typed as bytes32 although there's a string type. What is the real reason for that? Thanks.
29
votes
2answers
12k views

Deploy contract from contract in Solidity

It's a common pattern to use a hub/factory contract to create multiple instances of a standard contract. It's also common to require a way to enumerate the addresses of the created contracts. Is ...
28
votes
2answers
22k views

Can we get all elements stored in a mapping in the contract

Can we get all elements stored in a mapping in the contract using web3.js ?
27
votes
2answers
9k views

How to unit test smart contracts

How are Ethereum contracts tested? I've seen Truffle which referrers to itself as an Ethereum development framework. Are there any other frameworks and what is their level of maturity? Is there a de ...
27
votes
1answer
4k views

When can BLOCKHASH be safely used for a random number? When would it be unsafe?

I've seen more complicated ways for a contract to generate a random number. But the Ethereum Yellow Paper itself suggests a "trivial solution" using the BLOCKHASH opcode (see below, bold is mine). ...
26
votes
6answers
5k views

What are common pitfalls or limitations when coding in Solidity?

What would be on your list to warn other developers about? What works in most other languages that does not work, or behave as expected, in Solidity? For example only: what parameters can be passed ...
26
votes
2answers
15k views

How can you call a payable function in another contract with arguments and send funds?

I have a contract named Call: contract Call { ... function () payable { // TODO: Call the call function in the main contract // and forward all funds (msg.value) sent to this contract ...
23
votes
3answers
6k views

Pass a function as a parameter in Solidity

Can you pass a function as a parameter in Solidity? My guess: There is the concept of address in Solidity, but they represent contracts. Contracts can have fallback functions, but I don't think you ...
22
votes
2answers
2k views

What type of operations have negative gas costs?

I understand that some operations cost negative gas because they free up space on the blockchain, such as selfdestructs. What are all the operations that can have negative gas? Does this only occur ...
22
votes
2answers
3k views

What are effective techniques to encrypt/decrypt data stored in a smart contract?

Rather than sending and storing plain text in a smart contract, the use case objective is to encrypt first and then subsequently access the data and decrypt at the point of use. What is the most ...
22
votes
1answer
3k views

This image claims you can build “decentralized apps”, and each computer “contributes a little to process it”. Is that claim correct?

This image claims that you can put decentralized apps (say, a messaging program) on the Ethereum network. It also claims that each computer on the network contributes a little bit to the processing of ...
22
votes
2answers
9k views

Throw vs. Return

We're trying to figure out whether to use return or throw in Solidity when a condition fails and we don't assume user maliciousness. Here's the pros and cons we've figured so far: Why use throw Any ...
21
votes
6answers
17k views

Solidity: How can we write a error message in “require”?

Im learning Solidity. As far as I'm aware, it's recommended using "require" to handle errors. But, I'd like to inform the users about the kind of errors they make, e.g. an argument is invalid or out ...
21
votes
2answers
12k views

Solidity inherited constructors and calling the parent constructor

I'm trying to inherit from a contract. I'm expecting the child contract to have the same constructor as the parent (or be overwritten) pragma solidity ^0.4.2; contract parent{ string public ...
21
votes
3answers
3k views

Private info in an Ethereum smart contract

Is there any way to store info on a smart contract, and keep it a secret? If a contract has a state variable that is not public like: contract foo{ uint public publicVariable; uint notPublic;...
21
votes
2answers
3k views

Explanation of Parity Library Suicide

So today, A vulnerability found within the Parity walletLibrary which resulted in freezings of potentially hundreds of millions of dollars from wallets using parity multi-sig. Can someone technically ...
20
votes
2answers
22k views

How can I return multiple strings from a contract function?

In solidity it's obvious how to get a single string returned from a function. But, I'd like to have the smart contract return multiple string values. Is there a way to do this?
20
votes
3answers
24k views

Call function on another contract

I've created a contract called "Name" which I've deployed to the testnet. this function just register some text for the user and display it back. My contract is: contract Name is mortal{ ...
19
votes
4answers
17k views

Convert address to string

In Solidity, how can I convert the sender address to a string? The comments in How to convert an address to bytes in Solidity? did not provide a working solution
19
votes
1answer
4k views

How to access external knowledge / data in a smart contract

let’s say a contract wants to execute on knowledge contained outside the blockchain (e.g. whats the price of gold, what was the avg windspeed in NYC, how many people moved to portland in the last ...
18
votes
1answer
3k views

Child contract vs struct

Let`s say I need to store and manipulate collection of complex data in the smart contract. There maybe complex rules defining who and how can change an item. There maybe be several millions of items ...
18
votes
2answers
2k views

Can a contract's internal storage keep increasing forever?

In other words, is there a limit on the amount of storage a contract can hold? Suppose a field is an Array for instance, and the contract has a method for adding items. Can I keep calling the ...
18
votes
2answers
5k views

Contract reading a string returned by another contract

I have 2 contracts, Parents and Child which has a public string variable - name. contract Child { string public name; function child (string _name){ name = _name; } } Parents ...
17
votes
3answers
2k views

What are effective and secure ways of shuffling a deck of cards in a contract?

We want to write a card game in Ethereum. What are effective and secure ways of shuffling a deck of cards in a contract, and dealing them to players? It needs to be done in a way so that no one can ...
17
votes
2answers
4k views

Can a contract listen to events of another contract?

I read that to listen to events you need to use web3.js. Are there other ways of doing it? Can a contract even somehow listen to events of another contract? Thanks!
16
votes
6answers
17k views

How can I return an array of struct from a function?

I know that it's possible to return a struct through argument destructing: contract Test { struct Point { int x; int y; } Point point; function getPoint() returns (...
15
votes
2answers
20k views

how to estimate gas cost?

I have a function that iterate through an array, which could cost a lot of gas. But I still want to test out the estimate gas cost, then decide if I should maintain the design or not. function ...

1
2 3 4 5
36