2

I'm building a basic "lottery" where you send 1 ether to a smart contract, I save your address, and after 5 transactions I randomly choose one of the 5 addresses and send the 5 ethers to the "winner".

So I'll be saving ethereum addresses after each transaction, and after 5 transactions deleteing them all to start over.

  • Should I use mapping? array? byte[100]?
  • Do I need an index to select an address, or is there a function to select a random address from the mapping/array?
  • How do I afterwards "delete" all the addresses stored?

Thanks

  • you can't delete mapping elements but you can do using arrays. – Arslan Smal May 26 '17 at 19:42
1

I don't think you need to use mappings or structs for your project. You should probably go to readthedocs.io to go over the arrays in Solidity. Arrays in Solidity are reference types, that's important to know about Solidity.

They have fixed arrays and dynamic arrays. This including mappings and structs are all reference types with their own issues.

A fixed array can never change in length which is very different to arrays in JavaScript.

For a fixed array in Solidity, no change in the length and every single element must be of the same type.

Dynamic arrays in Solidity behave more like JavaScript arrays so it can change in length over time.

Chances are you probably want to go with a dynamic array because I am assuming you want to allow as many people as possible to enter your lottery.

So, because you have not shared any code to what you have done so far, I will give you my own example of what I am talking about:

contract Test {
    uint public myArray;

    function Test() public {
        myArray.push(1);
        myArray.push(10);
        myArray.push(30);
    }

    function getArrayLength() public view returns (uint) {
        return myArray.length;
    }

So here we have a contract called Test which has an unsigned integer called myArray.

Inside the constructor function for this contract you will see that myArray has a method called push(). It behaves the same as it does in JavaScript.

I can retrieve the length of the array with .length.

contract Test {
    uint public myArray;

    function Test() public {
        myArray.push(1);
        myArray.push(10);
        myArray.push(30);
    }

    function getArrayLength() public view returns (uint) {
        return myArray.length;
    }

    function getFirstElement() public view returns (uint) {
        return myArray[0];
    }

Then we can access individual elements in the array using bracket notation and in Solidity arrays are also zero-indexed.

When I mark a variable as public we automatically get a new function created for us that allows me to access the myArray variable and it automatically will provide for me a function called msg or message.

If you try to access myArray, the function that gets generated does not return the entire array, it always accepts one argument and that is the index of the element you want to retrieve in the array.

With an array you have to ask for individual elements in Solidity, it will not give you all the elements in the array.

You can write a function to return the entire array like so:

function getMyArray() public view returns (uint[]) {
  return myArray;
}

With this function you can get all the elements in your array, but you have to write a custom function like this, Solidity arrays does not do this by default.

Now this does not work for arrays of arrays or arrays of structs in Solidity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.