Cumu custruisce, implementà è pruvà l'App Waves RIDE

Bonghjornu! In questu articulu vi mustraraghju cumu scrive è eseguisce una dApp regulare nantu à un node Waves. Fighjemu i strumenti necessarii, i metudi è un esempiu di sviluppu.

Cumu custruisce, implementà è pruvà l'App Waves RIDE

U schema di sviluppu per dApps è appiicazioni regulare hè quasi u listessu:

  • Codice di scrittura
  • Scrizzione di teste automatizati
  • Lanciatu di l'applicazione
  • Testing

Ferramenti

1. docker per eseguisce u nodu è Waves Explorer

Se ùn vulete micca inizià un node, pudete saltà stu passu. Dopu tuttu, ci hè una rete di prova è sperimentale. Ma senza implementà u vostru propiu nodu, u prucessu di prova pò trascinà.

  • Averete sempre bisognu di novi cunti cù tokens di prova. U faucet di a rete di prova trasferisce 10 WAVES ogni 10 minuti.
  • U tempu mediu di bloccu in a reta di teste hè 1 minutu, in u node - 15 seconde. Questu hè soprattuttu notevuli quandu una transazzione richiede parechje cunfirmazioni.
  • A caching aggressiva hè pussibule nantu à i nodi di teste publichi.
  • Puderanu ancu esse indisponibili temporaneamente per via di mantenimentu.

Da avà, assumeraghju chì avete travagliatu cù u vostru propiu node.

2. Surfboard Command Line Tool

  • Scaricate è installate Node.js cù ppa, homebrew o exe quì: https://nodejs.org/en/download/.
  • Installa Surfboard, un strumentu chì vi permette di eseguisce teste nantu à un node esistenti.

npm install -g @waves/surfboard

3. Visual Studio Code plugin

Stu passu hè facultativu sè ùn site micca un fan di IDE è preferite editori di testu. Tutti i strumenti necessarii sò utilità di linea di cumanda. Se utilizate vim, fate attenzione à u plugin vim-ride.

Scaricate è installate Visual Studio Code: https://code.visualstudio.com/

Aprite VS Code è installate u plugin waves-ride:

Cumu custruisce, implementà è pruvà l'App Waves RIDE

Estensione di navigatore Waves Keeper: https://wavesplatform.com/products-keeper

Fattu!

Avviate u node è Waves Explorer

1. Cumincià u node:

docker run -d -p 6869:6869 wavesplatform/waves-private-node

Assicuratevi chì u node hè lanciatu via l'API REST in http://localhost:6869:

Cumu custruisce, implementà è pruvà l'App Waves RIDE
Swagger REST API per u node

2. Avviate una istanza di Waves Explorer:

docker run -d -e API_NODE_URL=http://localhost:6869 -e NODE_LIST=http://localhost:6869 -p 3000:8080 wavesplatform/explorer

Aprite un navigatore è andate à http://localhost:3000. Puderete vede quantu rapidamente hè custruitu un circuitu di nodi lucali vacanti.

Cumu custruisce, implementà è pruvà l'App Waves RIDE
Waves Explorer mostra una istanza di nodu locale

Strutture RIDE è Strumenta Surfboard

Crea un repertoriu viotu è eseguite u cumandamentu in questu

surfboard init

U cumandimu inizializza un repertoriu cù a struttura di u prugettu, l'applicazioni "hello world" è e teste. Sè avete apertu stu cartulare cù VS Code, vi vede:

Cumu custruisce, implementà è pruvà l'App Waves RIDE
Surfboard.config.json

  • Sottu u cartulare ./ride/ truverete un unicu schedariu wallet.ride - u cartulare induve u codice dApp hè situatu. Avemu da analizà brevemente dApps in u prossimu bloccu.
  • Sottu u cartulare ./test/ truverete un schedariu *.js. I testi sò guardati quì.
  • ./surfboard.config.json - schedariu di cunfigurazione per l'esecuzione di teste.

Envs hè una sezione impurtante. Ogni ambiente hè cunfiguratu cusì:

  • REST API endpoint di u node chì serà utilizatu per lancià a dApp è CHAIN_ID di a reta.
  • Una frasa secreta per un cuntu cù tokens chì saranu e fonti di i vostri tokens di prova.

Comu pudete vede, surfboard.config.json sustene parechje ambienti per automaticamente. U predeterminatu hè l'ambiente lucale (a chjave defaultEnv hè un paràmetru mudificabile).

Applicazione demo di wallet

Questa sezione ùn hè micca una riferenza à a lingua RIDE. Piuttostu, un sguardu à l'applicazione chì implementemu è testemu per capisce megliu ciò chì succede in a blockchain.

Fighjemu una semplice applicazione Wallet-demo. Qualchese pò mandà tokens à un indirizzu dApp. Pudete solu ritirà i vostri WAVES. Dui funzioni @Callable sò dispunibuli via InvokeScriptTransaction:

  • deposit()chì richiede un pagamentu attaccatu in WAVES
  • withdraw(amount: Int)chì rende tokens

In tuttu u ciclu di vita dApp, a struttura (indirizzu → quantità) serà mantinuta:

Action
Statu risultatu

nizziali
Vacanti

Alice deposita 5 ONDE
alice-indirizzu → 500000000

Bob deposita 2 ONDE

alice-indirizzu → 500000000
indirizzu bob → 200000000

Bob ritira 7 ONDE
NEGATA !

Alice ritira 4 ONDE
alice-indirizzu → 100000000
indirizzu bob → 200000000

Eccu u codice per capisce cumplettamente a situazione:

# 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

U codice di mostra pò ancu esse truvatu à GitHub.

U plugin VSCode supporta a compilazione cuntinuu mentre edità un schedariu. Dunque, pudete sempre monitorà l'errori in a tabulazione PROBLEMI.

Cumu custruisce, implementà è pruvà l'App Waves RIDE
Se vulete usà un editore di testu diversu quandu compile u schedariu, utilizate

surfboard compile ride/wallet.ride

Questu pruducerà una seria di codice RIDE compilatu in base64.

Test script per 'wallet.ride'

Fighjemu u schedariu di prova. Impulsatu da u framework Mocha di JavaScript. Ci hè una funzione "Prima" è trè teste:

  • "Prima" finanzia parechji cunti via MassTransferTransaction, compile u script è implementa à u blockchain.
  • "Pudete depositu" manda un InvokeScriptTransaction à a reta, attivendu a funzione di depositu () per ognunu di i dui cunti.
  • "Ùn pò micca ritirare più di ciò chì era dipositu" teste chì nimu pò arrubbari i tokens di l'altri.
  • "Pudete deposità" cuntrolli chì i ritirati sò trattati currettamente.

Eseguite teste da Surfboard è analizà i risultati in Waves Explorer

Per fà a prova, corre

surfboard test

Sì avete parechje script (per esempiu, avete bisognu di un script di implementazione separata), pudete eseguisce

surfboard test my-scenario.js

Surfboard raccoglierà i schedarii di teste in u cartulare ./test/ è eseguisce u script in u node chì hè cunfiguratu in surfboard.config.json. Dopu qualchì seconde, vi vede qualcosa cum'è questu:

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)

Eura! I testi passati. Avà fighjemu un ochju à ciò chì succede quandu si usa Waves Explorer: fighjate i blocchi o incollà unu di l'indirizzi sopra in a ricerca (per esempiu, u currispundente). wallet#. Ci si pò truvà a storia transazzione, statutu dApp, u schedariu binariu decompiled.

Cumu custruisce, implementà è pruvà l'App Waves RIDE
Waves Explorer. Una applicazione chì hè stata appena implementata.

Certi cunsiglii di surfboard:

1. Per pruvà in l'ambiente di testnet, utilizate:

surfboard test --env=testnet

Get tokens di prova

2. Se vulete vede e versioni JSON di e transazzione è cumu si trattanu da u node, eseguite a prova cù -v (significa "verbose"):

surfboard test -v

Utilizà app cù Waves Keeper

1. Configurate Waves Keeper per travaglià: http://localhost:6869

Cumu custruisce, implementà è pruvà l'App Waves RIDE
Configurazione di Waves Keeper per travaglià cù un node locale

2. Import frasa secreta cù tokens per a reta? Per simplicità, utilizate a sumente iniziale di u vostru node: waves private node seed with waves tokens. Indirizzu: 3M4qwDomRabJKLZxuXhwfqLApQkU592nWxF.

3. Pudete eseguisce una applicazione di una sola pagina senza server cù npm. O vai à quellu esistenti: chrome-ext.wvservices.com/dapp-wallet.html

4. Inserite l'indirizzu di a billetera da a prova (sottolineata sopra) in a casella di testu di l'indirizzu dApp

5. Inserite una piccula quantità in u campu "Depositu" è cliccate u buttone:

Cumu custruisce, implementà è pruvà l'App Waves RIDE
Waves Keeper dumanda u permessu di firmà una InvokeScriptTransaction cù pagamentu di 10 WAVES.

6. Cunfirmà a transazzione:

Cumu custruisce, implementà è pruvà l'App Waves RIDE
A transazzione hè creata è trasmessa à a reta. Avà pudete vede u so ID

7. Monitorà a transazzione cù Waves Explorer. Inserite ID in u campu di ricerca

Cumu custruisce, implementà è pruvà l'App Waves RIDE

Conclusioni è infurmazioni supplementari

Avemu guardatu l'arnesi per u sviluppu, a prova, l'implementazione è l'usu di dApps simplici nantu à a piattaforma Waves:

  • lingua RIDE
  • Editor di codice VS
  • Waves Explorer
  • Tabuleta
  • Custode di l'onda

Ligami per quelli chì volenu cuntinuà à amparà RIDE:

Più esempi
IDE in linea cù esempi
Documentazione Waves
Chat di sviluppatore in Telegram
Onde è RIDE nantu à stackoverflow
NEW! Corsi in linea nantu à a creazione di dApps nantu à a piattaforma Waves

Cuntinuà à immerse in u tema RIDE è crea a vostra prima dApp!

TL; DR: bit.ly/2YCFnwY

Source: www.habr.com

Add a comment