Kọ ẹkọ lati kọ awọn adehun smart Waves lori RIDE ati RIDE4DAPPS. Apa 2 (DAO - Ajo Adase Alailowaya)

Kọ ẹkọ lati kọ awọn adehun smart Waves lori RIDE ati RIDE4DAPPS. Apa 2 (DAO - Ajo Adase Alailowaya)

Kaabo gbogbo eniyan!

В akọkọ apakan ti a wo ni awọn alaye ni bi o ṣe le ṣẹda ati ṣiṣẹ pẹlu dApp (ohun elo ti a ko pin) ni igbi RIDE IDE.

Jẹ ki a ṣe idanwo eyi ti a ti tuka diẹ diẹ bayi apẹẹrẹ.

Ipele 3. Idanwo akọọlẹ dApp

Kọ ẹkọ lati kọ awọn adehun smart Waves lori RIDE ati RIDE4DAPPS. Apa 2 (DAO - Ajo Adase Alailowaya)

Awọn iṣoro wo ni lẹsẹkẹsẹ fo si ọ pẹlu Alice? ohun elo Àkọọlẹ?
Ni ibere:
Boob ati Cooper le fi owo ranṣẹ lairotẹlẹ si adiresi dApp ni lilo deede gbigbe awọn iṣowo ati nitorinaa kii yoo ni anfani lati wọle si wọn pada.

Keji:
A ko ni ihamọ Alice ni eyikeyi ọna lati yọkuro awọn owo laisi ifọwọsi ti Boob ati/tabi Cooper. Niwon, san ifojusi lati mọ daju, gbogbo awọn iṣowo lati Alice yoo wa ni pipa.

Jẹ ki a ṣatunṣe 2nd nipa idinamọ Alice gbigbe lẹkọ. Jẹ ki a gbe iwe afọwọkọ ti a ṣe atunṣe jade:
Kọ ẹkọ lati kọ awọn adehun smart Waves lori RIDE ati RIDE4DAPPS. Apa 2 (DAO - Ajo Adase Alailowaya)
Iduro
A n gbiyanju lati yọkuro awọn owó lati dApp Alice ati ibuwọlu rẹ. A gba aṣiṣe:
Kọ ẹkọ lati kọ awọn adehun smart Waves lori RIDE ati RIDE4DAPPS. Apa 2 (DAO - Ajo Adase Alailowaya)

Jẹ ki a gbiyanju lati yọkuro nipasẹ yiyọ kuro:

broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"withdraw",args:[{type:"integer", value: 1000000}]}, payment: []}))

Awọn akosile ṣiṣẹ ati awọn ti a ro ero 2nd ojuami!

Ipele 4. Ṣẹda DAO pẹlu idibo

Laanu, ede RIDE ko sibẹsibẹ pese agbara lati ṣiṣẹ pẹlu awọn ikojọpọ (awọn iwe-itumọ iwe-itumọ, awọn olutọpa, awọn olupilẹṣẹ, ati bẹbẹ lọ). Sibẹsibẹ, fun eyikeyi mosi pẹlu alapin collections bọtini-iye a le ṣe apẹrẹ eto kan fun ṣiṣẹ pẹlu awọn okun, ni ibamu pẹlu awọn bọtini ati idinku wọn.

Awọn okun jẹ rọrun pupọ lati ṣajọpọ; awọn okun le pin nipasẹ awọn atọka.
Jẹ ki a gba ati ṣe itupalẹ okun kan bi apẹẹrẹ idanwo ati ṣayẹwo bii eyi ṣe ni ipa lori abajade ti idunadura naa.
A yanju lori otitọ pe Alice ko le fowo si iṣowo Gbigbe, nitori agbara yii ti dina ni @verifier fun iru iṣowo yii.

Jẹ ki a ṣe adaṣe pẹlu awọn gbolohun ọrọ lẹhinna yanju eyi.

Awọn okun gigun

Iṣowo naa ṣee ṣe lẹẹkansi, a mọ bi a ṣe le ṣiṣẹ pẹlu awọn okun.
Kọ ẹkọ lati kọ awọn adehun smart Waves lori RIDE ati RIDE4DAPPS. Apa 2 (DAO - Ajo Adase Alailowaya)

Iduro
Ni lapapọ, a ni ohun gbogbo pataki lati kọ eka kannaa DAO dApp.

Data lẹkọ

Awọn iṣowo data:
“Iwọn ti o pọ julọ fun bọtini jẹ awọn ohun kikọ 100, ati pe bọtini kan le ni awọn aaye koodu Unicode lainidii pẹlu awọn alafo ati awọn aami miiran ti kii ṣe titẹ. Awọn iye okun ni opin ti awọn baiti 32,768 ati nọmba ti o pọju ti awọn titẹ sii ti o ṣeeṣe ni iṣowo data jẹ 100. Iwoye, iwọn ti o pọju ti idunadura data wa ni ayika 140kb - fun itọkasi, o fẹrẹ jẹ deede ipari ti ere Shakespeare 'Romeo ati Juliet '."

A ṣẹda DAO pẹlu awọn ipo wọnyi:
Ni ibere fun ibẹrẹ kan lati gba igbeowosile nipasẹ pipe getFunds() atilẹyin ti o kere ju awọn olukopa 2 - awọn oludokoowo DAO - nilo. Yiyọ kuro o yoo ṣee ṣe gangan bi lapapọ ti itọkasi lori idibo DAO onihun.

Jẹ ki a ṣe awọn oriṣi awọn bọtini 3 ki o ṣafikun ọgbọn-ọrọ fun ṣiṣẹ pẹlu awọn iwọntunwọnsi ni ibo awọn iṣẹ tuntun 2 ati gbaFunds:
xx… xx_ia = afowopaowo, wa iwontunwonsi (idibo, idogo, yiyọ kuro)
xx… xx_sv = startups, nọmba ti ibo (idibo, getFunds)
xx… xx_sf = startups, nọmba ti ibo (idibo, getFunds)
xx…xx = adirẹsi gbogbo eniyan (awọn ami 35)

Jọwọ ṣe akiyesi pe ni Idibo a nilo lati ṣe imudojuiwọn awọn aaye pupọ ni ẹẹkan:

WriteSet([DataEntry(key1, value1), DataEntry(key2, value2)]),

WriteSet gba wa laaye lati ṣe ọpọlọpọ awọn igbasilẹ ni ẹẹkan laarin ọkan invokeScript lẹkọ.

Eyi ni ohun ti o dabi ninu ibi ipamọ iye-bọtini ti DAO dApp, lẹhin ti Bob ati Cooper ti kun ia-awọn ohun idogo:
Kọ ẹkọ lati kọ awọn adehun smart Waves lori RIDE ati RIDE4DAPPS. Apa 2 (DAO - Ajo Adase Alailowaya)

Iṣẹ idogo wa ti yipada diẹ:
Kọ ẹkọ lati kọ awọn adehun smart Waves lori RIDE ati RIDE4DAPPS. Apa 2 (DAO - Ajo Adase Alailowaya)

Bayi wa akoko pataki julọ ninu awọn iṣẹ ti DAO - dibo fun ise agbese lati wa ni inawo.

Bob dibo fun iṣẹ akanṣe 500000 ti Neli:

broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"vote",args:[{type:"integer", value: 500000}, {type:"string", value: "3MrXEKJr9nDLNyVZ1d12Mq4jjeUYwxNjMsH"}]}, payment: []}))

Kọ ẹkọ lati kọ awọn adehun smart Waves lori RIDE ati RIDE4DAPPS. Apa 2 (DAO - Ajo Adase Alailowaya)

Ninu ile itaja data a rii gbogbo awọn titẹ sii pataki fun adirẹsi Neli:
Kọ ẹkọ lati kọ awọn adehun smart Waves lori RIDE ati RIDE4DAPPS. Apa 2 (DAO - Ajo Adase Alailowaya)
Cooper tun dibo fun iṣẹ akanṣe Neli.
Kọ ẹkọ lati kọ awọn adehun smart Waves lori RIDE ati RIDE4DAPPS. Apa 2 (DAO - Ajo Adase Alailowaya)

Jẹ ki a wo koodu iṣẹ naa gba awọn owo. Neli gbọdọ gba o kere ju awọn ibo 2 lati ni anfani lati yọ owo kuro lati DAO.
Kọ ẹkọ lati kọ awọn adehun smart Waves lori RIDE ati RIDE4DAPPS. Apa 2 (DAO - Ajo Adase Alailowaya)

Neli yoo yọ idaji iye ti a fi le e lọwọ:

broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"getFunds",args:[{type:"integer", value: 500000}]}, payment: []}))

Kọ ẹkọ lati kọ awọn adehun smart Waves lori RIDE ati RIDE4DAPPS. Apa 2 (DAO - Ajo Adase Alailowaya)

O ṣaṣeyọri, iyẹn ni, DAO ṣiṣẹ!

A wo ilana ti ṣiṣẹda DAO ni ede naa RIDE4DAPPS.
Ni awọn apakan atẹle a yoo wo isunmọ si isọdọtun koodu ati idanwo ọran.

Ẹya kikun ti koodu ni Waves RIDE IDE:

# 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
    }
}

Apa akọkọ
Koodu lori GitHub
igbi RIDE IDE
Ikede eto fifunni

orisun: www.habr.com

Fi ọrọìwòye kun