Hello a h-uile duine!
В
Feuch an dèan sinn deuchainn beagan air an fhear a chaidh a chuir às a chèile
Ìre 3. A 'dèanamh deuchainn air a' chunntas dApp
Dè na duilgheadasan a bhios a’ leum a-mach ort sa bhad le Alice? dApp Cunntas?
An toiseach:
Faodaidh Boob agus Cooper airgead a chuir gu seòladh dApp gun fhiosta a’ cleachdadh gu cunbhalach gluasad gnothaichean agus mar sin cha bhith e comasach dhaibh faighinn air ais.San dàrna àite:
Cha bhith sinn a’ cuingealachadh ann an dòigh sam bith air Alice airgead a tharraing air ais gun chead bho Boob agus/no Cooper. Leis, thoir aire do dhearbhadh, thèid a h-uile gnothach bho Alice a chuir gu bàs.
Feuch an socraich sinn 2na le bhith a’ toirmeasg Alice gluasad gnothaichean. Leig leinn an sgriobt ceartaichte a chleachdadh:

Tha sinn a’ feuchainn ri buinn a tharraing air ais bho dApp Alice agus an ainm-sgrìobhte aice. Gheibh sinn mearachd:
Feuchaidh sinn ri tarraing air ais tro tharraing air ais:
broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"withdraw",args:[{type:"integer", value: 1000000}]}, payment: []}))
Tha an sgriobt ag obair agus fhuair sinn a-mach an 2na puing!
Ceum 4. Cruthaich DAO le bhòtadh
Gu mì-fhortanach, chan eil an cànan RIDE fhathast a’ toirt comas obrachadh le cruinneachaidhean (faclairean faclair, iterators, reducers, msaa). Ach, airson gnìomhachd sam bith le cruinneachaidhean còmhnard prìomh-luach is urrainn dhuinn siostam a dhealbhadh airson obrachadh le teudan, a rèir sin le iuchraichean agus an dì-chrioptachadh.
Tha sreangan gu math furasta an co-cheangail; faodar sreangan a sgaradh le clàran-amais.
Cruinnichidh agus rannsaich sinn sreang mar eisimpleir deuchainn agus thoir sùil air mar a bheir seo buaidh air toradh a’ ghnothaich.
Shocraich sinn nach b’ urrainn dha Alice ainm a chuir ris a’ ghnothach gluasaid, leis gun deach an comas seo a bhacadh ann an @verifier airson an seòrsa malairt seo.
Feuch an cleachd sinn le teudan agus an uairsin fuasgladh fhaighinn air seo.
Sreathan RIDE
Tha an gnothach comasach a-rithist, tha fios againn mar a dh'obraicheas sinn le sreangan.

Gu h-iomlan, tha a h-uile dad a tha riatanach againn airson loidsig iom-fhillte a sgrìobhadh DAO dApp.
Gnìomhan Dàta
Gnìomhan dàta:
“Is e 100 caractar am meud as motha airson iuchair, agus faodaidh puingean còd Unicode neo-riaghailteach a bhith ann an iuchair a’ toirt a-steach beàrnan agus samhlaidhean eile nach gabh an clò-bhualadh. Tha crìoch de 32,768 bytes aig luachan sreang agus is e 100 an àireamh as motha de chuir a-steach ann an malairt dàta. Gu h-iomlan, tha meud as motha malairt dàta timcheall air 140kb - airson fiosrachadh, cha mhòr dìreach fad dealbh-chluich Shakespeare 'Romeo and Juliet '."
Bidh sinn a’ cruthachadh DAO leis na cumhaichean a leanas:
Gus am faigh neach-tòiseachaidh maoineachadh le bhith a 'gairm faigh airgead() tha feum air taic bho co-dhiù com-pàirtichean 2 - luchd-tasgaidh DAO -. Thoir air falbh bidh e comasach dìreach cho mòr ris an àireamh iomlan a tha air a chomharrachadh air bhòtadh Luchd-seilbh DAO.
Dèanamaid 3 seòrsaichean iuchraichean agus cuiridh sinn loidsig airson obrachadh le cothromachadh ann an 2 ghnìomh ùr bhòt agus getFunds:
xx... xx_ia = luchd-tasgaidh, cothromachadh ri fhaighinn (bhòt, tasgadh, tarraing air ais)
xx... xx_sv = startups, àireamh bhòtaichean (bhòt, getFunds)
xx... xx_sf = startups, àireamh bhòtaichean (bhòt, getFunds)
xx…xx = seòladh poblach (35 caractaran)
Thoir an aire gum feumadh sinn sa Bhòt grunn raointean ùrachadh aig an aon àm:
WriteSet([DataEntry(key1, value1), DataEntry(key2, value2)]),
Leigidh WriteSet leinn grunn chlàran a dhèanamh aig an aon àm taobh a-staigh aon invokeScript gnothaichean.
Seo mar a tha e coltach ann an stòradh prìomh luach an DAO dApp, às deidh dha Bob agus Cooper ath-lìonadh ia- tasgaidhean:
Tha ar gnìomh tasgaidh air atharrachadh beagan:
A-nis thig an àm as cudromaiche ann an gnìomhachd an DAO - bhòt airson pròiseactan a bhith air am maoineachadh.
Bidh Bob a’ bhòtadh airson a’ phròiseact 500000 tonn aig Neli:
broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"vote",args:[{type:"integer", value: 500000}, {type:"string", value: "3MrXEKJr9nDLNyVZ1d12Mq4jjeUYwxNjMsH"}]}, payment: []}))
Anns a’ bhùth dàta chì sinn na h-inntrigidhean riatanach airson seòladh Neli:
Bhòt Cooper cuideachd airson pròiseact Neli.
Bheir sinn sùil air a’ chòd gnìomh faighFunds. Feumaidh Neli co-dhiù 2 bhòt a chruinneachadh airson a bhith comasach air airgead a tharraing air ais bhon DAO.
Tha Neli a’ dol a tharraing air ais leth den t-suim a chaidh a thoirt dhi:
broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"getFunds",args:[{type:"integer", value: 500000}]}, payment: []}))
Bidh i a’ soirbheachadh, is e sin, tha DAO ag obair!
Thug sinn sùil air a’ phròiseas airson DAO a chruthachadh sa chànan RIDE4DAPPS.
Anns na pàirtean a leanas bheir sinn sùil nas mionaidiche air ath-leasachadh còd agus deuchainn cùise.
An tionndadh slàn dhen chòd ann an
# In this example multiple accounts can deposit their funds to DAO and safely take them back, no one can interfere with this.
# DAO participants can also vote for particular addresses and let them withdraw invested funds then quorum has reached.
# An inner state is maintained as mapping `address=>waves`.
# https://medium.com/waves-lab/waves-announces-funding-for-ride-for-dapps-developers-f724095fdbe1
# You can try this contract by following commands in the IDE (ide.wavesplatform.com)
# Run commands as listed below
# From account #0:
# deploy()
# From account #1: deposit funds
# broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"deposit",args:[]}, payment: [{amount: 100000000, asset:null }]}))
# From account #2: deposit funds
# broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"deposit",args:[]}, payment: [{amount: 100000000, asset:null }]}))
# From account #1: vote for startup
# broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"vote",args:[{type:"integer", value: 500000}, {type:"string", value: "3MrXEKJr9nDLNyVZ1d12Mq4jjeUYwxNjMsH"}]}, payment: []}))
# From account #2: vote for startup
# broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"vote",args:[{type:"integer", value: 500000}, {type:"string", value: "3MrXEKJr9nDLNyVZ1d12Mq4jjeUYwxNjMsH"}]}, payment: []}))
# From account #3: get invested funds
# broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"getFunds",args:[{type:"integer", value: 500000}]}, payment: []}))
{-# STDLIB_VERSION 3 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}
@Callable(i)
func deposit() = {
let pmt = extract(i.payment)
if (isDefined(pmt.assetId)) then throw("can hodl waves only at the moment")
else {
let currentKey = toBase58String(i.caller.bytes)
let xxxInvestorBalance = currentKey + "_" + "ib"
let currentAmount = match getInteger(this, xxxInvestorBalance) {
case a:Int => a
case _ => 0
}
let newAmount = currentAmount + pmt.amount
WriteSet([DataEntry(xxxInvestorBalance, newAmount)])
}
}
@Callable(i)
func withdraw(amount: Int) = {
let currentKey = toBase58String(i.caller.bytes)
let xxxInvestorBalance = currentKey + "_" + "ib"
let currentAmount = match getInteger(this, xxxInvestorBalance) {
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(xxxInvestorBalance, newAmount)]),
TransferSet([ScriptTransfer(i.caller, amount, unit)])
)
}
@Callable(i)
func getFunds(amount: Int) = {
let quorum = 2
let currentKey = toBase58String(i.caller.bytes)
let xxxStartupFund = currentKey + "_" + "sf"
let xxxStartupVotes = currentKey + "_" + "sv"
let currentAmount = match getInteger(this, xxxStartupFund) {
case a:Int => a
case _ => 0
}
let totalVotes = match getInteger(this, xxxStartupVotes) {
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 if (totalVotes < quorum)
then throw("Not enough votes. At least 2 votes required!")
else ScriptResult(
WriteSet([
DataEntry(xxxStartupFund, newAmount)
]),
TransferSet([ScriptTransfer(i.caller, amount, unit)])
)
}
@Callable(i)
func vote(amount: Int, address: String) = {
let currentKey = toBase58String(i.caller.bytes)
let xxxInvestorBalance = currentKey + "_" + "ib"
let xxxStartupFund = address + "_" + "sf"
let xxxStartupVotes = address + "_" + "sv"
let currentAmount = match getInteger(this, xxxInvestorBalance) {
case a:Int => a
case _ => 0
}
let currentVotes = match getInteger(this, xxxStartupVotes) {
case a:Int => a
case _ => 0
}
let currentFund = match getInteger(this, xxxStartupFund) {
case a:Int => a
case _ => 0
}
if (amount <= 0)
then throw("Can't withdraw negative amount")
else if (amount > currentAmount)
then throw("Not enough balance")
else ScriptResult(
WriteSet([
DataEntry(xxxInvestorBalance, currentAmount - amount),
DataEntry(xxxStartupVotes, currentVotes + 1),
DataEntry(xxxStartupFund, currentFund + amount)
]),
TransferSet([ScriptTransfer(i.caller, amount, unit)])
)
}
@Verifier(tx)
func verify() = {
match tx {
case t: TransferTransaction =>false
case _ => true
}
}
Source: www.habr.com