Hello aliyense!
В
Tiyeni tiyese pang'ono chophwanyikacho
Gawo 3. Kuyesa akaunti ya dApp
Ndi mavuto ati omwe amakugwerani nthawi yomweyo ndi Alice? dApp Akaunti?
Poyamba:
Boob ndi Cooper atha kutumiza ndalama mwangozi ku adilesi ya dApp pogwiritsa ntchito nthawi zonse tumizani zotuluka ndipo motero sindingathe kuzipezanso.Kachiwiri:
Sitimaletsa Alice mwanjira iliyonse kuchotsa ndalama popanda chilolezo cha Boob ndi/kapena Cooper. Popeza, tcherani khutu kutsimikizira, zochitika zonse za Alice zidzachitidwa.
Tiyeni tikonze 2 poletsa Alice tumizani zochita. Tiyeni titumizire script yokonzedwa:

Tikuyesera kuchotsa ndalama za dApp Alice ndi siginecha yake. Tikupeza cholakwika:
Tiyeni tiyesere kuchoka kudzera mu withdraw:
broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"withdraw",args:[{type:"integer", value: 1000000}]}, payment: []}))
Zolemba zimagwira ntchito ndipo tapeza mfundo ya 2!
Gawo 4. Pangani DAO ndikuvota
Tsoka ilo, chilankhulo cha RIDE sichinaperekebe kuthekera kogwira ntchito ndi zosonkhanitsira (dictionary dictionary, iterators, reducers, etc.). Komabe, ntchito iliyonse yokhala ndi zosonkhanitsira lathyathyathya chinsinsi-mtengo titha kupanga dongosolo logwirira ntchito ndi zingwe, molingana ndi makiyi ndi kumasulira kwawo.
Zingwe ndizosavuta kulumikiza; zingwe zimatha kulekanitsidwa ndi ma index.
Tiyeni tisonkhanitse ndikusintha chingwe ngati chitsanzo choyesera ndikuwona momwe izi zimakhudzira zotsatira zamalondawo.
Tinakhazikika pa mfundo yakuti Alice sakanatha kusaina Transfer transaction, chifukwa lusoli linali loletsedwa mu @verifier pamtundu wamtunduwu.
Tiyeni tiyesetse ndi zingwe kenako kuthetsa izi.
kukwera Zingwe
Kugulitsako ndikothekanso, tikudziwa momwe tingagwiritsire ntchito zingwe.

Pazonse, tili ndi zonse zofunika kuti tilembe zomveka zovuta DAO dApp.
Kusintha kwa Data
Kusinthana ndi Data:
"Kukula kwakukulu kwa kiyi ndi zilembo 100, ndipo kiyi imatha kukhala ndi ma code a Unicode osagwirizana, kuphatikiza mipata ndi zizindikiro zina zosasindikizidwa. Zingwe zamtengo wapatali zimakhala ndi malire a 32,768 byte ndipo chiwerengero chachikulu cha zolemba zomwe zingatheke pakugulitsa deta ndi 100. Ponseponse, kukula kwakukulu kwa kugulitsa kwa deta kuli pafupi ndi 140kb - kutanthauza, pafupifupi ndendende kutalika kwa sewero la Shakespeare 'Romeo ndi Juliet. '."
Timapanga DAO ndi zinthu zotsatirazi:
Kuti oyambitsa alandire ndalama poyimba foni getFunds () thandizo la osachepera 2 otenga nawo mbali - osunga ndalama a DAO - akufunika. Zotsatira zidzatheka ndendende monga momwe zasonyezedwera kuvota Eni ake a DAO.
Tiyeni tipange mitundu itatu ya makiyi ndikuwonjezera malingaliro ogwirira ntchito ndi masikelo mu mavoti 3 atsopano a mavoti ndi getFunds:
xx…xx_ndi = osunga ndalama, ndalama zomwe zilipo (vote, deposit, withdrawal)
xx…xx_sv = zoyambira, kuchuluka kwa mavoti (vote, getFunds)
xx…xx_sf = zoyambira, kuchuluka kwa mavoti (vote, getFunds)
xx…xx = adilesi ya anthu onse (zilembo 35)
Chonde dziwani kuti mu Vote tidafunikira kusintha magawo angapo nthawi imodzi:
WriteSet([DataEntry(key1, value1), DataEntry(key2, value2)]),
WriteSet imatithandiza kupanga zolemba zingapo nthawi imodzi invokeScript zochita.
Izi ndi momwe zimawonekera pakusungidwa kwamtengo wapatali kwa DAO dApp, Bob ndi Cooper atadzaza. ia- deposits:
Ntchito yathu yosungitsa ndalama yasintha pang'ono:
Tsopano pakubwera nthawi yofunika kwambiri muzochita za DAO - kuvota kuti ma projekiti azipereka ndalama.
Bob amavotera Neli's 500000 wavelets project:
broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"vote",args:[{type:"integer", value: 500000}, {type:"string", value: "3MrXEKJr9nDLNyVZ1d12Mq4jjeUYwxNjMsH"}]}, payment: []}))
Mu data store tikuwona zonse zofunikira pa adilesi ya Neli:
Cooper adavoteranso polojekiti ya Neli.
Tiyeni tiwone ntchito code getFunds. Neli akuyenera kutolera mavoti osachepera 2 kuti athe kutenga ndalama ku DAO.
Neli atenga theka la ndalama zomwe adamupatsa:
broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"getFunds",args:[{type:"integer", value: 500000}]}, payment: []}))
Amachita bwino, ndiye kuti, DAO imagwira ntchito!
Tidayang'ana njira yopangira DAO muchilankhulo Mtengo wa RIDE4DAPPS.
M'magawo otsatirawa tiwona mozama za kukonzanso kachidindo komanso kuyesa kwamilandu.
Mtundu wonse wa code mu
# 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