Balances
Getting Contract Balance
Access your contract's balance using self.balance within contract methods:
class Contract(gl.Contract):
@gl.public.view
def get_balance(self):
return self.balance # Shows contract's current balanceGetting Another Contract's Balance
Get any contract's balance by address:
other_contract = gl.get_contract_at(contract_address)
print(other_contract.balance)Transferring Funds
Transfer funds from sender to current contract:
other_contract = gl.get_contract_at(gl.message.sender_address)
other_contract.emit_transfer(value=u256(5))
other_contract.emit(value=u256(5), on='finalized').transfer(Address("0x..."))Balance Context
Balance behavior depends on execution context:
- In
writemethods:self.balancereflects actual contract balance after state changes - In
viewmethods: Balance shows current state without modifications - Transfers can only occur in write context
⚠️
If message is emitted on acceptance and previous transaction gets successfully appealed after the emit, the balance will be decremented nonetheless