Skip to content

Instantly share code, notes, and snippets.

@KardanovIR
Created September 22, 2020 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KardanovIR/2d0e5026cee070e47609f9b003b7bfa9 to your computer and use it in GitHub Desktop.
Save KardanovIR/2d0e5026cee070e47609f9b003b7bfa9 to your computer and use it in GitHub Desktop.
# In this example multiple accounts can deposit their funds and safely take them back.
# User balances are stored in the dApp state as mapping `address => waves`.
{-# STDLIB_VERSION 4 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}
@Callable(i)
func deposit() = {
# deposit function can be invoked from UI by user to top up the balance
let pmt = extract(i.payment) # creating variable with all data about a payment attached to the invokation
if (isDefined(pmt.assetId)) then throw("can hodl waves only at the moment") # waves-only threshold
else {
let currentKey = toBase58String(i.caller.bytes) # determining caller key
let currentAmount = match getInteger(this, currentKey) { # reading current user's balance from the account state
case a:Int => a
case _ => 0 # taking zero as a balance value if this is the first time user deposits money
}
let newAmount = currentAmount + pmt.amount # counting new balance as "old balance + payment value"
[IntegerEntry(currentKey, newAmount)] # updating user's balance in the account state
}
}
@Verifier(tx)
func verify() = false # this script can NOT be updated because of this verifier function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment