I want to only limited users(addresses) can do a transaction. For example, in a Lottery pool, an only limited user can join it.


There are many ways to implement this sort of functionality, the easiest I suppose would be have an list of users that correspond to an array. And limit access to people who have registered to the system, I think that is the most straightforward approach.

pragma solidity ^0.4.24;

contract Users {

  mapping(address => bytes32) public users;
  event UserCreated(address indexed _address, bytes32 _pseudo);

  function create (bytes32 _pseudo) public {
    users[msg.sender] = _pseudo ;
    emit UserCreated(msg.sender, _pseudo);
  function get (address _address) public view returns(bytes32 _pseudo) {
    return (users[_address]);

If you are looking for role-based access control, I encourage you to consider the open-zeppelin smart contracts, for more customizable logic open-zeppelin RBAC.

If you have a single lottery occurring at once (or at bare minimal, one lottery per contract), these methods should be adequate. Depending on how you implement your lottery contract, the best approach may diff. But just to be clear, a simple lottery contract does not need to have this functionality.

pragma solidity ^0.4.18; 

contract SimpleLottery {
    uint public constant TICKET_PRICE = 1e16; // 0.01 ether

    address[] public tickets;
    address public winner;
    uint public ticketingCloses;

    function SimpleLottery (uint duration) public {
        ticketingCloses = now + duration;

    function buy () public payable {
        require(msg.value == TICKET_PRICE); 
        require(now < ticketingCloses);


    function drawWinner () public {
        require(now > ticketingCloses + 5 minutes);
        require(winner == address(0));

        bytes32 rand = keccak256(
        winner = tickets[uint(rand) % tickets.length];

    function withdraw () public {
        require(msg.sender == winner);

    function () payable public {

Taken from Building Games with Ethereum Smart Contracts, let me know if you have any questions.

Edit: Getting array from other contract.

pragma solidity ^0.4.24;
// contractA.sol
contract contractA {
   function getUsers() public view returns (address[]) { return users; }

First you have the admin contract, lets say it's address is A, using the deployment method I linked in a comment.

// Deploy A, then deploy B, passing in A's newly deployed address
deployer.deploy(A).then(function() {
  return deployer.deploy(B, A.address);

For contract B

import './contractA.sol'
contract contractB {

address public contractAaddress;
constructor(address deployedcontractA) public 
    contractAaddress = deployedcontractA;
function getContractAUsers() public view returns (address[]) {
   contractA contractInstance = contractA(contractAaddress);
   return contractInstance.getUsers.call(); 

Essentially what happens here is you load the abi for the admin contract in the lottery contract, call a specific instance of the contract (for example, deployed on mainnet) and use low level external calls to access other functions. This is one approach to use call function within another function.

I think cryptokitties does this as well and there may be more efficient ways of doing this including creating a smart contract interface like in the ERC20 and ERC721 token standards.

  • First of all, thank you. I created two contracts one is for admin that can add users. It works fine but i want to get the list of address in a different contract. here is get method. function getUsers() public view returns (address[]) { return users; } – Jay Patel Jan 1 at 5:44
  • You have a few options, the first one is to hardcode your "admin" contract address in the second contract, or you can dynamically pass it in during contract deployment, see truffleframework.com/docs/truffle/getting-started/…. Previously the way I got a list of contracts from another contract was to use a low-level call to retrieve an array. – GrandFleet Jan 1 at 5:47

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.