How many addresses can be stored inside an address [ ] in solidity
I'm building a contract that requires new users to send exactly 0.1 ether in order to enter an investment round.
I've been struggling with this for a while an I don't know how to store the addresses of new investors inside the contract so I can use the "address index" later.
For what I saw, dynamic arrays are not recommended because they can easily use "too much gas" and get the contract stuck forever.
- How can I know how many addresses can actually be stored inside address [ ] ?
I assume a simple test can be done but I'm not sure how to do it.
This is the code I'm using. It's based on the great article Rob Hitchens wrote.
address userIndex; // New user address gets stored in dynamic array
function invest() public payable {
require(msg.value == 0.1 ether); // checks if new investor sent 0.1 ether
userIndex.push(msg.sender); // adds new user to userIndex
}
ethereum solidity smartcontracts
add a comment |
I'm building a contract that requires new users to send exactly 0.1 ether in order to enter an investment round.
I've been struggling with this for a while an I don't know how to store the addresses of new investors inside the contract so I can use the "address index" later.
For what I saw, dynamic arrays are not recommended because they can easily use "too much gas" and get the contract stuck forever.
- How can I know how many addresses can actually be stored inside address [ ] ?
I assume a simple test can be done but I'm not sure how to do it.
This is the code I'm using. It's based on the great article Rob Hitchens wrote.
address userIndex; // New user address gets stored in dynamic array
function invest() public payable {
require(msg.value == 0.1 ether); // checks if new investor sent 0.1 ether
userIndex.push(msg.sender); // adds new user to userIndex
}
ethereum solidity smartcontracts
add a comment |
I'm building a contract that requires new users to send exactly 0.1 ether in order to enter an investment round.
I've been struggling with this for a while an I don't know how to store the addresses of new investors inside the contract so I can use the "address index" later.
For what I saw, dynamic arrays are not recommended because they can easily use "too much gas" and get the contract stuck forever.
- How can I know how many addresses can actually be stored inside address [ ] ?
I assume a simple test can be done but I'm not sure how to do it.
This is the code I'm using. It's based on the great article Rob Hitchens wrote.
address userIndex; // New user address gets stored in dynamic array
function invest() public payable {
require(msg.value == 0.1 ether); // checks if new investor sent 0.1 ether
userIndex.push(msg.sender); // adds new user to userIndex
}
ethereum solidity smartcontracts
I'm building a contract that requires new users to send exactly 0.1 ether in order to enter an investment round.
I've been struggling with this for a while an I don't know how to store the addresses of new investors inside the contract so I can use the "address index" later.
For what I saw, dynamic arrays are not recommended because they can easily use "too much gas" and get the contract stuck forever.
- How can I know how many addresses can actually be stored inside address [ ] ?
I assume a simple test can be done but I'm not sure how to do it.
This is the code I'm using. It's based on the great article Rob Hitchens wrote.
address userIndex; // New user address gets stored in dynamic array
function invest() public payable {
require(msg.value == 0.1 ether); // checks if new investor sent 0.1 ether
userIndex.push(msg.sender); // adds new user to userIndex
}
ethereum solidity smartcontracts
ethereum solidity smartcontracts
asked Nov 20 '18 at 13:15
Ian BrittoIan Britto
236
236
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
There's no limit to how many items can be stored in a dynamic array. (Technically, there's a limit of 2^256, but that's on the order of how many atoms there are in the known universe.)
The number of items in the array doesn't affect gas usage at all. What can consume a lot of gas is enumerating a large array. (What matters in that case is that you're executing a lot of code by running a long loop.)
Thanks for the quick answer. I didn't understand what you mean about enumerating. I'm planning to create a loop that iterates over the address array in order like this: for (uint i = 0; i < userIndex.length; i++). So it starts with the first investor address and then the second and so on.. will that be enumerated?
– Ian Britto
Nov 20 '18 at 14:10
Yes, that's what enumeration means. Reading a value from storage costs 200 gas, and the current gas limit for a block is 8,000,000, so you wouldn't be able to do this at all past 40,000 addresses. And, of course, I assume you're going to do something with each address other than just look at it. E.g. if you modify a value in storage at each iteration, that's 5,000 gas, so 1,600 addresses would consume all the available gas for a block. It's generally better to avoid loops altogether. (Without knowing what you're doing, it's hard to suggest an alternative.)
– smarx
Nov 20 '18 at 14:19
That's exactly what I needed to know thanks a lot. I already voted and accepted your answer but it doesnt appear because im under 15 of reputation. I'm actually trying to build a dapp based on a real world system called "abundance flower" where investors get 8X what they invested. The thing is that is a ponzi scheme in real life, I want to make a dapp that is not. So im looking for ways to evolve the idea based on what can I do on blockchain. It would mean a lot to me if you want to read more about it here: github.com/ianbritto/EIGTH
– Ian Britto
Nov 20 '18 at 14:27
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393844%2fhow-many-addresses-can-be-stored-inside-an-address-in-solidity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There's no limit to how many items can be stored in a dynamic array. (Technically, there's a limit of 2^256, but that's on the order of how many atoms there are in the known universe.)
The number of items in the array doesn't affect gas usage at all. What can consume a lot of gas is enumerating a large array. (What matters in that case is that you're executing a lot of code by running a long loop.)
Thanks for the quick answer. I didn't understand what you mean about enumerating. I'm planning to create a loop that iterates over the address array in order like this: for (uint i = 0; i < userIndex.length; i++). So it starts with the first investor address and then the second and so on.. will that be enumerated?
– Ian Britto
Nov 20 '18 at 14:10
Yes, that's what enumeration means. Reading a value from storage costs 200 gas, and the current gas limit for a block is 8,000,000, so you wouldn't be able to do this at all past 40,000 addresses. And, of course, I assume you're going to do something with each address other than just look at it. E.g. if you modify a value in storage at each iteration, that's 5,000 gas, so 1,600 addresses would consume all the available gas for a block. It's generally better to avoid loops altogether. (Without knowing what you're doing, it's hard to suggest an alternative.)
– smarx
Nov 20 '18 at 14:19
That's exactly what I needed to know thanks a lot. I already voted and accepted your answer but it doesnt appear because im under 15 of reputation. I'm actually trying to build a dapp based on a real world system called "abundance flower" where investors get 8X what they invested. The thing is that is a ponzi scheme in real life, I want to make a dapp that is not. So im looking for ways to evolve the idea based on what can I do on blockchain. It would mean a lot to me if you want to read more about it here: github.com/ianbritto/EIGTH
– Ian Britto
Nov 20 '18 at 14:27
add a comment |
There's no limit to how many items can be stored in a dynamic array. (Technically, there's a limit of 2^256, but that's on the order of how many atoms there are in the known universe.)
The number of items in the array doesn't affect gas usage at all. What can consume a lot of gas is enumerating a large array. (What matters in that case is that you're executing a lot of code by running a long loop.)
Thanks for the quick answer. I didn't understand what you mean about enumerating. I'm planning to create a loop that iterates over the address array in order like this: for (uint i = 0; i < userIndex.length; i++). So it starts with the first investor address and then the second and so on.. will that be enumerated?
– Ian Britto
Nov 20 '18 at 14:10
Yes, that's what enumeration means. Reading a value from storage costs 200 gas, and the current gas limit for a block is 8,000,000, so you wouldn't be able to do this at all past 40,000 addresses. And, of course, I assume you're going to do something with each address other than just look at it. E.g. if you modify a value in storage at each iteration, that's 5,000 gas, so 1,600 addresses would consume all the available gas for a block. It's generally better to avoid loops altogether. (Without knowing what you're doing, it's hard to suggest an alternative.)
– smarx
Nov 20 '18 at 14:19
That's exactly what I needed to know thanks a lot. I already voted and accepted your answer but it doesnt appear because im under 15 of reputation. I'm actually trying to build a dapp based on a real world system called "abundance flower" where investors get 8X what they invested. The thing is that is a ponzi scheme in real life, I want to make a dapp that is not. So im looking for ways to evolve the idea based on what can I do on blockchain. It would mean a lot to me if you want to read more about it here: github.com/ianbritto/EIGTH
– Ian Britto
Nov 20 '18 at 14:27
add a comment |
There's no limit to how many items can be stored in a dynamic array. (Technically, there's a limit of 2^256, but that's on the order of how many atoms there are in the known universe.)
The number of items in the array doesn't affect gas usage at all. What can consume a lot of gas is enumerating a large array. (What matters in that case is that you're executing a lot of code by running a long loop.)
There's no limit to how many items can be stored in a dynamic array. (Technically, there's a limit of 2^256, but that's on the order of how many atoms there are in the known universe.)
The number of items in the array doesn't affect gas usage at all. What can consume a lot of gas is enumerating a large array. (What matters in that case is that you're executing a lot of code by running a long loop.)
answered Nov 20 '18 at 13:51
smarxsmarx
48.8k46273
48.8k46273
Thanks for the quick answer. I didn't understand what you mean about enumerating. I'm planning to create a loop that iterates over the address array in order like this: for (uint i = 0; i < userIndex.length; i++). So it starts with the first investor address and then the second and so on.. will that be enumerated?
– Ian Britto
Nov 20 '18 at 14:10
Yes, that's what enumeration means. Reading a value from storage costs 200 gas, and the current gas limit for a block is 8,000,000, so you wouldn't be able to do this at all past 40,000 addresses. And, of course, I assume you're going to do something with each address other than just look at it. E.g. if you modify a value in storage at each iteration, that's 5,000 gas, so 1,600 addresses would consume all the available gas for a block. It's generally better to avoid loops altogether. (Without knowing what you're doing, it's hard to suggest an alternative.)
– smarx
Nov 20 '18 at 14:19
That's exactly what I needed to know thanks a lot. I already voted and accepted your answer but it doesnt appear because im under 15 of reputation. I'm actually trying to build a dapp based on a real world system called "abundance flower" where investors get 8X what they invested. The thing is that is a ponzi scheme in real life, I want to make a dapp that is not. So im looking for ways to evolve the idea based on what can I do on blockchain. It would mean a lot to me if you want to read more about it here: github.com/ianbritto/EIGTH
– Ian Britto
Nov 20 '18 at 14:27
add a comment |
Thanks for the quick answer. I didn't understand what you mean about enumerating. I'm planning to create a loop that iterates over the address array in order like this: for (uint i = 0; i < userIndex.length; i++). So it starts with the first investor address and then the second and so on.. will that be enumerated?
– Ian Britto
Nov 20 '18 at 14:10
Yes, that's what enumeration means. Reading a value from storage costs 200 gas, and the current gas limit for a block is 8,000,000, so you wouldn't be able to do this at all past 40,000 addresses. And, of course, I assume you're going to do something with each address other than just look at it. E.g. if you modify a value in storage at each iteration, that's 5,000 gas, so 1,600 addresses would consume all the available gas for a block. It's generally better to avoid loops altogether. (Without knowing what you're doing, it's hard to suggest an alternative.)
– smarx
Nov 20 '18 at 14:19
That's exactly what I needed to know thanks a lot. I already voted and accepted your answer but it doesnt appear because im under 15 of reputation. I'm actually trying to build a dapp based on a real world system called "abundance flower" where investors get 8X what they invested. The thing is that is a ponzi scheme in real life, I want to make a dapp that is not. So im looking for ways to evolve the idea based on what can I do on blockchain. It would mean a lot to me if you want to read more about it here: github.com/ianbritto/EIGTH
– Ian Britto
Nov 20 '18 at 14:27
Thanks for the quick answer. I didn't understand what you mean about enumerating. I'm planning to create a loop that iterates over the address array in order like this: for (uint i = 0; i < userIndex.length; i++). So it starts with the first investor address and then the second and so on.. will that be enumerated?
– Ian Britto
Nov 20 '18 at 14:10
Thanks for the quick answer. I didn't understand what you mean about enumerating. I'm planning to create a loop that iterates over the address array in order like this: for (uint i = 0; i < userIndex.length; i++). So it starts with the first investor address and then the second and so on.. will that be enumerated?
– Ian Britto
Nov 20 '18 at 14:10
Yes, that's what enumeration means. Reading a value from storage costs 200 gas, and the current gas limit for a block is 8,000,000, so you wouldn't be able to do this at all past 40,000 addresses. And, of course, I assume you're going to do something with each address other than just look at it. E.g. if you modify a value in storage at each iteration, that's 5,000 gas, so 1,600 addresses would consume all the available gas for a block. It's generally better to avoid loops altogether. (Without knowing what you're doing, it's hard to suggest an alternative.)
– smarx
Nov 20 '18 at 14:19
Yes, that's what enumeration means. Reading a value from storage costs 200 gas, and the current gas limit for a block is 8,000,000, so you wouldn't be able to do this at all past 40,000 addresses. And, of course, I assume you're going to do something with each address other than just look at it. E.g. if you modify a value in storage at each iteration, that's 5,000 gas, so 1,600 addresses would consume all the available gas for a block. It's generally better to avoid loops altogether. (Without knowing what you're doing, it's hard to suggest an alternative.)
– smarx
Nov 20 '18 at 14:19
That's exactly what I needed to know thanks a lot. I already voted and accepted your answer but it doesnt appear because im under 15 of reputation. I'm actually trying to build a dapp based on a real world system called "abundance flower" where investors get 8X what they invested. The thing is that is a ponzi scheme in real life, I want to make a dapp that is not. So im looking for ways to evolve the idea based on what can I do on blockchain. It would mean a lot to me if you want to read more about it here: github.com/ianbritto/EIGTH
– Ian Britto
Nov 20 '18 at 14:27
That's exactly what I needed to know thanks a lot. I already voted and accepted your answer but it doesnt appear because im under 15 of reputation. I'm actually trying to build a dapp based on a real world system called "abundance flower" where investors get 8X what they invested. The thing is that is a ponzi scheme in real life, I want to make a dapp that is not. So im looking for ways to evolve the idea based on what can I do on blockchain. It would mean a lot to me if you want to read more about it here: github.com/ianbritto/EIGTH
– Ian Britto
Nov 20 '18 at 14:27
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393844%2fhow-many-addresses-can-be-stored-inside-an-address-in-solidity%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown