Back
Lesson 16:
Transacting Ether
Introduction to transacting Ether on Vyper
Progress: 0%
Visit desktop version for better experiences.
Transacting Ether
Vyper has special built-in functions to help with transacting ether. The send
keyword can be used to send Ether from the contract to a specified Ethereum address.
The contract will also need to be marked as @payable
to transact Ether.
EthSenderContract.vy
# pragma version 0.4.0
balances: public(uint256)
recipient_to_balances_mapping: HashMap[address, uint256]
# General function to send Ether
@external
@payable
def send_ether(_recipient: address, _amount: uint256):
assert _amount > 0, "Amount must be greater than zero"
send(_recipient, _amount)
# You may also define `_gas` to attach it to the `send` function so state-altering functions can be invoked
@external
@payable
def send_to_other_address(_recipient: address, _amount: uint256, _gas: uint256):
assert _amount > 0, "Amount must be greater than zero"
self.recipient_to_balances_mapping[_recipient] = _amount
self.balances += _amount
send(_recipient, _amount, gas=_gas)
RecipientContract.vy
# pragma version 0.4.0
interaction_count: public(uint256)
event SendEtherEvent:
sender: indexed(address)
amount: uint256
# The attached `_gas` from `send_to_other_address` used to change state
@external
@payable
def __default__():
self.interaction_count += 1
log SendEtherEvent(msg.sender, msg.value)
Vyper Differentiators
Transacting Ether on Vyper is straightforward relative to Solidity's use of different keywords like
transfer
,call
andsend
.