0

How can I write a smart contract to give role based permission for a transaction to take place. Say there are five people A, B, C, D and E. A wants to send some ethers to B. But the transaction will not take place until and unless C, D and E give confirmation/approve.

Is it possible to do with ethereum smart contracts? Can someone give me sample code for the same?

Thanks in advance.

Mouazzam
  • 469
  • 1
  • 4
  • 15

2 Answers2

1

you can create such smart contract although using a multisig account would be better for this case.

you could write a simple contract which validate a transaction after receiving the different needed signatures. e..g :

contract C{
address A;
address B;
address C;


mapping (address=>bool) permission;
function send_permission(address _to, uint value)
{
if(permission[A]&&permission[A]&&permission[A])
_to.transfer(value);
}


function set_permission(bool state)
{
    permission[msg.sender]=state;
}
}
Badr Bellaj
  • 11,560
  • 2
  • 43
  • 44
0

For a dynamic solution you will need to create 2 mappings one for the user who can approve transactions (lets call them moderator for simplicity) and one for the permissions

the logic will be when a user sends a transaction we see if he is approved in the approval mapping if yes go though with the transaction otherwise revert.

you will need to make a function for a user to get approved.

and a function which allows a moderator to approve an address in which we will check if a moderator is present in the moderators mapping. if yes he can add the non-approved user to the approved mapping.

remember in this solution once an address is approved that address can send multiple transactions with no checks but if you want to limit what address can send which transaction you'll have to modify the second mapping to a nested one in which youll keep a track of the address, the transaction number and a bool value

Sam
  • 55
  • 6