ProHoster > Blog > Tsamaiso > Mokhoa oa ho aha, ho kenya le ho lekola maqhubu a RIDE dApp
Mokhoa oa ho aha, ho kenya le ho lekola maqhubu a RIDE dApp
Lumela! Sehloohong sena ke tla u bontša mokhoa oa ho ngola le ho tsamaisa dApp e tloaelehileng ho node ea Waves. A re shebeng lisebelisoa tse hlokahalang, mekhoa le mohlala oa tsoelo-pele.
Sekema sa nts'etsopele ea dApps le lits'ebetso tse tloaelehileng se batla se tšoana:
Ho ngola khoutu
Ho ngola tlhahlobo e iketsang
Ho qala ts'ebeliso
Teko
Lisebelisoa
1. docker ho tsamaisa node le Waves Explorer
Haeba u sa batle ho qala node, u ka tlola mohato ona. Ntle le moo, ho na le marang-rang a liteko le liteko. Empa ntle le ho sebelisa node ea hau, ts'ebetso ea tlhahlobo e ka 'na ea tsoela pele.
U tla lula u hloka li-account tse ncha tse nang le li-tokens tsa liteko. Teko ea marang-rang ea liteko e fetisetsa WAVES tse 10 metsotsong e meng le e meng e 10.
Karolelano ea nako ea ho thibela marang-rang ea teko ke metsotso e 1, ka node - metsotsoana e 15. Sena se bonahala haholo ha khoebo e hloka litiisetso tse ngata.
Caching e matla e ka khoneha libakeng tsa tlhahlobo ea sechaba.
Li ka 'na tsa se be teng ka nakoana ka lebaka la tlhokomelo.
Ho tloha joale ho ea pele ke tla nka hore u sebetsa ka node ea hau.
Kenya Surfboard, sesebelisoa se u lumellang ho etsa liteko sebakeng se seng se le teng.
npm install -g @waves/surfboard
3. Visual Studio Code plugin
Mohato ona ke oa boikhethelo haeba ha u morati oa li-IDE 'me u khetha bahlophisi ba mongolo. Lisebelisoa tsohle tse hlokahalang ke lisebelisoa tsa line line. Haeba u sebelisa vim, ela hloko plugin vim-kalla.
Khoasolla le ho kenya Khoutu ea Visual Studio: https://code.visualstudio.com/
Bula Khoutu ea VS 'me u kenye plugin ea wave-ride:
docker run -d -e API_NODE_URL=http://localhost:6869 -e NODE_LIST=http://localhost:6869 -p 3000:8080 wavesplatform/explorer
Bula sebatli 'me u ee ho http://localhost:3000. U tla bona hore na potoloho ea node ea sebaka se se nang letho e hahiloe kapele hakae.
Waves Explorer e bonts'a mohlala oa node ea lehae
Sebopeho sa RIDE le sesebelisoa sa Surfboard
Etsa buka e se nang letho 'me u tsamaise taelo ho eona
surfboard init
Taelo e qala bukana e nang le sebopeho sa morero, "hello world" lits'ebetso le liteko. Haeba u bula foldara ena ka VS Code, u tla bona:
Surfboard.config.json
Tlas'a ./ride/ foldara u tla fumana faele e le 'ngoe wallet.ride - bukana moo khoutu ea dApp e leng teng. Re tla sekaseka dApps ka bokhuts'oane sebakeng se latelang.
Tlas'a ./test/ foldara u tla fumana *.js faele. Liteko li bolokiloe mona.
./surfboard.config.json - faele ea tlhophiso bakeng sa liteko tse sebetsang.
Envs ke karolo ea bohlokoa. Tikoloho e 'ngoe le e' ngoe e hlophisitsoe ka tsela ena:
REST API pheletso ea node e tla sebelisoa ho qala dApp le CHAIN_ID ea marang-rang.
Polelo ea lekunutu bakeng sa ak'haonte e nang le li-tokens tse tla ba mehloli ea matšoao a hau a tlhahlobo.
Joalokaha u bona, surfboard.config.json e ts'ehetsa libaka tse ngata ka mokhoa o ikhethileng. Ntho e sa fetoheng ke tikoloho ea lehae (senotlolo sa defaultEnv ke parameter e fetohang).
Sesebelisoa sa Wallet-demo
Karolo ena ha e bue ka puo ea RIDE. Sebakeng seo, sheba ts'ebeliso eo re e sebelisang le ho e leka ho utloisisa hantle se etsahalang ho blockchain.
Ha re shebeng ts'ebeliso e bonolo ea Wallet-demo. Mang kapa mang a ka romella li-tokens atereseng ea dApp. U ka hula MAFUTA a hau feela. Mesebetsi e 'meli ea @Callable e fumaneha ka InvokeScriptTransaction:
deposit()e hlokang tefo e khomaretsoeng ho WAVES
withdraw(amount: Int)e kgutlisang matshwao
Ho pholletsa le bophelo ba dApp, sebopeho (aterese → chelete) se tla bolokoa:
Action
Boemo ba sephetho
lekgetlo la ntlha
Ha e na letho
Alice o beha 5 WAVES
alice-aterese → 500000000
Bob o beha 2 WAVES
alice-aterese → 500000000
bob-aterese → 200000000
Bob o hula MAqhubu a 7
E LATOA!
Alice o hula MAqhubu a 4
alice-aterese → 100000000
bob-aterese → 200000000
Mona ke khoutu ea ho utloisisa boemo ka botlalo:
# 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
Mohlala oa khoutu o ka boela oa fumanoa ho GitHub.
Plugin ea VSCode e ts'ehetsa ho kopanya ho tsoelang pele ha u ntse u hlophisa faele. Ka hona, o ka lula o beha leihlo liphoso ho tab ea PROBLEMS.
Haeba u batla ho sebelisa mohlophisi oa mongolo o fapaneng ha u hlophisa faele, sebelisa
surfboard compile ride/wallet.ride
Sena se tla hlahisa letoto la khoutu ea base64 e hlophisitsoeng ea RIDE.
Mongolo oa teko oa 'wallet.ride'
Ha re shebeng faele ea teko. E tsamaisoa ke moralo oa Mocha oa JavaScript. Ho na le ts'ebetso ea "Pele" le liteko tse tharo:
"Pele" chelete ea li-account tse ngata ka MassTransferTransaction, e bokella script ebe e e fetisetsa ho blockchain.
"Can deposit" e romela InvokeScriptTransaction ho marang-rang, e kenya tšebetsong deposit() tšebetsong bakeng sa e 'ngoe le e 'ngoe ea li-account tse peli.
"Ha ke khone ho ntša chelete e ngata ho feta e neng e behiloe" liteko tseo ho seng motho ea ka utsoang li-tokens tsa batho ba bang.
"Na u ka beha" e hlahloba hore ho tlosoa ha chelete ho sebetsoa ka nepo.
Etsa liteko ho tsoa ho Surfboard 'me u hlahlobe liphetho ho Waves Explorer
Ho etsa tlhahlobo, matha
surfboard test
Haeba u na le lingoloa tse ngata (mohlala, u hloka sengoloa se arohaneng sa deployment), u ka matha
surfboard test my-scenario.js
Surfboard e tla bokella lifaele tsa tlhahlobo ho ./test/ foldareng ebe e tsamaisa mongolo sebakeng se hlophisitsoeng ho surfboard.config.json. Ka mor'a metsotsoana e seng mekae u tla bona ntho e kang ena:
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! Liteko li fetile. Joale ha re shebeng se etsahalang ha u sebelisa Waves Explorer: sheba li-blocks kapa u manamise e 'ngoe ea liaterese tse kaholimo ho batlisisa (mohlala, tse tsamaellanang. wallet#. Ha u le moo u ka fumana nalane ea transaction, boemo ba dApp, faele ea binary e senyehileng.
Waves Explorer. Sesebelisoa se sa tsoa kenngoa.
Malebela a mang a Surfboard:
1. Ho etsa liteko tikolohong ea testnet, sebelisa:
2. Kenya poleloana ea lekunutu ka li-tokens tsa marang-rang? Ho nolofatsa, sebelisa peo ea pele ea node ea hau: waves private node seed with waves tokens. Aterese: 3M4qwDomRabJKLZxuXhwfqLApQkU592nWxF.