2

I am trying to convert my solidity code into vyper but I can't find a way to declare a storage variable.

My solidity function was this:

struct Account {
uint256 balance; // Account's balance
uint256 timeLocked; // If not 0, then account will be allowed to withdraw 
}
mapping (address => Account) public accounts;

function addBalance() public payable {
Account storage a = accounts[msg.sender];
a.balance = a.balance + msg.value;

}

How can I make ato become a storage variable ?

I have another question, how can I make a require statement like this ?

require(_operator != address(0));

Seems that address(0) doesn't exist in vyper as it throws me errors.

1 Answer 1

2

Can't you just bypass the use of a completely?

struct Account:
    accountBalance: wei_value
    timeLocked: uint256

accounts: public(map(address, Account))

@public
@payable
def addBalance():
    self.accounts[msg.sender].accountBalance += msg.value

Note that balance is a reserved keyword, so I've used accountBalance.


Seems that address(0) doesn't exist in vyper as it throws me errors.

Use:

assert _operator != ZERO_ADDRESS

ZERO_ADDRESS is a built-in constant.

9
  • Actually I use a to emit an event emit Balance(msg.sender, a.balance); Commented Jun 4, 2019 at 13:59
  • Thanks for answering my question, there isn't a lot of documentation out there Commented Jun 4, 2019 at 14:31
  • Just poking around at Vyper examples, it doesn't look like storage or memory pointers are implemented, so I think you might have to use self.accounts[msg.sender] explicitly in your event call: log.Balance(msg.sender, self.accounts[msg.sender].accountBalance). Commented Jun 4, 2019 at 14:38
  • That worked, thanks! Do you know how to declare a getter function, I've tried like this def getBalance() -> uint256(wei): but it creates a setter function instead Commented Jun 4, 2019 at 19:46
  • Glad it worked :-) Getter functions can be created implicitly (as they are in Solidity) by using the public() function. So when we declared the mapping, you'll notice it was wrapped in a public() call. This means the getter function should be automagically created, and so you don't need to explicitly create one. Commented Jun 4, 2019 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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