
Hello aliyense!
В Mu gawoli tidawona mwatsatanetsatane momwe mungapangire ndikugwira ntchito ndi dApp (decentralized application) mkati .
Tiyeni tiyese pang'ono chovundukulacho .
Khwerero 3: Yesani akaunti ya dApp

Ndizovuta ziti zomwe zimakuvutitsani nthawi yomweyo ndi Alice? dApp Akaunti?
Poyamba:
Boob ndi Cooper akhoza kutumiza ndalama mwangozi ku adilesi ya dApp pogwiritsa ntchito nthawi zonse tumizani zotuluka ndipo motero sindingathe kuzipezanso.Kachiwiri:
Sitimaletsa Alice kuchotsa ndalama popanda chilolezo cha Boob ndi/kapena Cooper. Monga mukuwonera, zochitika zonse zochokera kwa Alice zidzachitidwa, monga zikuwonetsedwa ndi batani lotsimikizira.
Tiyeni tikonze 2 poletsa Alice tumizani zochita. Kutumiza script yokonzedwa:


Tikuyesera kuchotsa ndalama za Alice's dApp 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 takonza mfundo 2!
Khwerero 4: Pangani DAO ndikuvota
Tsoka ilo, chilankhulo cha RIDE sichinapereke kuthekera kogwira ntchito ndi zosonkhanitsira (dictionaries of dictionaries, 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 kugwirizanitsa, zingwe zimatha kugawidwa ndi ma index.
Monga chitsanzo choyesera, tiyeni tisonkhanitse ndikusanthula chingwe ndikuwona momwe izi zimakhudzira zotsatira za malondawo.
Tidamaliza ndi mfundo yoti Alice sanathe kusaina Transfer transaction, chifukwa lusoli lidatsekedwa mu @verifier pakuchita kwamtunduwu.
Tiyeni tiyesetse ndi zingwe kenako kuthetsa izi.
kukwera Zingwe
Kugulitsako ndikothekanso, tikudziwa momwe tingagwiritsire ntchito zingwe.


Chifukwa chake, tili ndi zonse zomwe timafunikira kuti tilembe zovuta. DAO dApp.
Kusintha kwa Data
Kusinthana ndi Data:
"Kukula kwakukulu kwa kiyi ndi zilembo 100, ndipo fungulo limatha kukhala ndi ma code a Unicode mosagwirizana kuphatikiza mipata ndi zizindikiro zina zosasindikizidwa. Zingwe zimakhala ndi malire a ma byte 32,768 ndipo kuchuluka kwazomwe zingatheke potengera deta ndi 100. Ponseponse, kukula kwake kopitilira muyeso kwa data ku Shakespeare kuli pafupifupi 40kb kutalika kwa 1 "Romeo ndi Juliet."
Timapanga DAO ndi zinthu zotsatirazi:
Kuti oyambitsa alandire ndalama poyimba foni GetFunds () Thandizo la osachepera awiri omwe atenga nawo mbali - osunga ndalama a DAO - akufunika. Zotsatira zidzakhala zotheka kukhala ndendende ndi kuchuluka komwe kwasonyezedwa kuvota Eni ake a DAO.
Tiyeni tipange makiyi amitundu itatu ndikuwonjezera malingaliro oti mugwire ntchito ndi masikelo muzinthu 2 zatsopano, kuvota 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)
Dziwani kuti mu Vote tidafunikira kusintha magawo angapo nthawi imodzi:
WriteSet([DataEntry(key1, value1), DataEntry(key2, value2)]),WriteSet imatilola kupanga zolemba zingapo nthawi imodzi invokeScript zochita.
Izi ndi momwe zimawonekera mu sitolo yamtengo wapatali ya DAO dApp Bob ndi Cooper atadzaza. ia- deposits:

Ntchito yathu yosungitsa ndalama yasintha pang'ono:

Tsopano ikubwera nthawi yofunika kwambiri muzochitika 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: []}))
M'malo osungiramo zinthu, tikuwona zolemba zonse zofunika za adilesi ya Neli:

Cooper adavoteranso polojekiti ya Neli.

Tiyeni tiwone ntchito code getFundsNeli atenge 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!
Takambirana momwe tingapangire DAO muchilankhulo Mtengo wa RIDE4DAPPS.
M'magawo otsatirawa, tiwona mozama zakusintha ma code ndi kuyezetsa milandu.
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
