Nov yog txoj cai kom nkag siab qhov xwm txheej no:
# In this example multiple accounts can deposit their funds and safely take them back. No one can interfere with this.
# An inner state is maintained as mapping `address=>waves`.
{-# STDLIB_VERSION 3 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}
@Callable(i)
func deposit() = {
let pmt = extract(i.payment)
if (isDefined(pmt.assetId))
then throw("works with waves only")
else {
let currentKey = toBase58String(i.caller.bytes)
let currentAmount = match getInteger(this, currentKey) {
case a:Int => a
case _ => 0
}
let newAmount = currentAmount + pmt.amount
WriteSet([DataEntry(currentKey, newAmount)])
}
}
@Callable(i)
func withdraw(amount: Int) = {
let currentKey = toBase58String(i.caller.bytes)
let currentAmount = match getInteger(this, currentKey) {
case a:Int => a
case _ => 0
}
let newAmount = currentAmount - amount
if (amount < 0)
then throw("Can't withdraw negative amount")
else if (newAmount < 0)
then throw("Not enough balance")
else ScriptResult(
WriteSet([DataEntry(currentKey, newAmount)]),
TransferSet([ScriptTransfer(i.caller, amount, unit)])
)
}
@Verifier(tx)
func verify() = false
Cia peb saib xeem ntawv. Tsim los ntawm JavaScript's Mocha mocha. Nws muaj "Ua ntej" ua haujlwm thiab peb qhov kev xeem:
"Ua ntej" nyiaj ntau tus account los ntawm MassTransferTransaction, sau cov ntawv thiab xa mus rau blockchain.
"Can deposit" xa ib qho InvokeScriptTransaction mus rau lub network, ua kom cov deposit() muaj nuj nqi rau txhua tus ntawm ob tug account.
"Tsis tuaj yeem thim ntau dua li tau tso tseg" cov kev sim uas tsis muaj leej twg tuaj yeem nyiag lwm tus tokens.
"Tau tso nyiaj" cov tshev uas rho tawm tau ua tiav lawm.
Khiav kev xeem los ntawm Surfboard thiab txheeb xyuas cov txiaj ntsig hauv Waves Explorer
Txhawm rau khiav qhov kev xeem, khiav
surfboard test
Yog tias koj muaj ntau cov ntawv sau (piv txwv li, koj xav tau cov ntawv xa tawm cais), koj tuaj yeem khiav
surfboard test my-scenario.js
Surfboard yuav sau cov ntawv xeem hauv ./test/ nplaub tshev thiab khiav cov ntawv sau rau ntawm node uas tau teeb tsa hauv surfboard.config.json. Tom qab ob peb feeb koj yuav pom tej yam zoo li no:
wallet test suite
Generating accounts with nonce: ce8d86ee
Account generated: foofoofoofoofoofoofoofoofoofoofoo#ce8d86ee - 3M763WgwDhmry95XzafZedf7WoBf5ixMwhX
Account generated: barbarbarbarbarbarbarbarbarbar#ce8d86ee - 3MAi9KhwnaAk5HSHmYPjLRdpCAnsSFpoY2v
Account generated: wallet#ce8d86ee - 3M5r6XYMZPUsRhxbwYf1ypaTB6MNs2Yo1Gb
Accounts successfully funded
Script has been set
√ Can deposit (4385ms)
√ Cannot withdraw more than was deposited
√ Can withdraw (108ms)
3 passing (15s)
Hooray! Kev xeem dhau lawm. Tam sim no cia peb saib seb yuav ua li cas thaum siv Waves Explorer: saib cov blocks lossis muab tshuaj txhuam ib qho ntawm cov chaw nyob saum toj no rau hauv kev tshawb fawb (piv txwv li, qhov sib thooj. wallet#. Nyob ntawd koj tuaj yeem nrhiav keeb kwm kev hloov pauv, dApp xwm txheej, cov ntaub ntawv binary decompiled.
Waves Explorer. Ib daim ntawv thov uas nyuam qhuav tau siv.
2. Yog tias koj xav pom JSON versions ntawm kev lag luam thiab yuav ua li cas lawv tau ua tiav los ntawm node, khiav qhov kev sim nrog -v (txhais tau tias 'verbose'):