Kawm sau Waves ntse daim ntawv cog lus ntawm RIDE thiab RIDE4DAPPS. Part 2 (DAO - Decentralized Autonomous Organization)

Kawm sau Waves ntse daim ntawv cog lus ntawm RIDE thiab RIDE4DAPPS. Part 2 (DAO - Decentralized Autonomous Organization)

Nyob zoo txhua leej txhua tus!

Π’ thawj ib feem peb saib hauv kev nthuav dav ntawm yuav ua li cas tsim thiab ua haujlwm nrog dApp (daim ntawv thov decentralized) hauv Wave RIDE IDE.

Cia peb sim lub disassembled ib me ntsis tam sim no Piv txwv.

Kauj Ruam 3. Ntsuas tus account dApp

Kawm sau Waves ntse daim ntawv cog lus ntawm RIDE thiab RIDE4DAPPS. Part 2 (DAO - Decentralized Autonomous Organization)

Cov teeb meem dab tsi tam sim ntawd dhia tawm ntawm koj nrog Alice? dApp ua Tus lej nyiaj?
Ua ntej:
Boob thiab Cooper yuav yuam kev xa nyiaj mus rau dApp chaw nyob siv li niaj zaus hloov kev hloov pauv thiab yog li yuav tsis tuaj yeem nkag mus rau lawv rov qab.

Thib ob:
Peb tsis txwv Alice hauv txhua txoj kev los ntawm kev rho nyiaj yam tsis tau txais kev pom zoo los ntawm Boob thiab / lossis Cooper. Txij li thaum, ua tib zoo saib xyuas, tag nrho cov kev lag luam los ntawm Alice yuav raug tua.

Cia peb kho 2nd los ntawm kev txwv Alice hloov kev lag luam. Cia peb siv cov ntawv kho kom raug:
Kawm sau Waves ntse daim ntawv cog lus ntawm RIDE thiab RIDE4DAPPS. Part 2 (DAO - Decentralized Autonomous Organization)
οΏΌ
Peb tab tom sim thim nyiaj npib los ntawm dApp Alice thiab nws kos npe. Peb tau txais qhov yuam kev:
Kawm sau Waves ntse daim ntawv cog lus ntawm RIDE thiab RIDE4DAPPS. Part 2 (DAO - Decentralized Autonomous Organization)

Peb sim thim los ntawm kev rho tawm:

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

Tsab ntawv ua haujlwm thiab peb xam pom qhov 2nd point!

Theem 4. Tsim DAO nrog kev pov npav

Hmoov tsis zoo, cov lus RIDE tseem tsis tau muab lub peev xwm ua haujlwm nrog cov khoom sau (dictionary dictionaries, iterators, reducers, etc.). Txawm li cas los xij, rau txhua qhov haujlwm nrog cov khoom sib dhos qhov tseem ceeb-nqi peb tuaj yeem tsim lub kaw lus rau kev ua haujlwm nrog cov hlua, raws li cov yuam sij thiab lawv decryption.

Cov hlua yog yooj yim heev rau concatenate; cov hlua tuaj yeem sib cais los ntawm qhov ntsuas.
Cia peb sau thiab txheeb xyuas ib txoj hlua ua piv txwv kuaj thiab xyuas seb qhov no cuam tshuam li cas rau qhov txiaj ntsig ntawm kev sib pauv.
Peb tau txiav txim siab ntawm qhov tseeb tias Alice tsis tuaj yeem kos npe rau Kev Hloov pauv hloov pauv, txij li lub peev xwm no tau raug thaiv hauv @verifier rau hom kev hloov pauv no.

Cia peb xyaum nrog cov hlua thiab tom qab ntawd daws qhov no.

RIDE Strings

Kev hloov pauv yog ua tau dua, peb paub yuav ua li cas ua haujlwm nrog cov hlua.
Kawm sau Waves ntse daim ntawv cog lus ntawm RIDE thiab RIDE4DAPPS. Part 2 (DAO - Decentralized Autonomous Organization)

οΏΌ
Nyob rau hauv tag nrho, peb muaj txhua yam tsim nyog los sau complex logic DAO dApp.

Cov ntaub ntawv ua lag luam

Cov ntaub ntawv pauv:
"Qhov loj tshaj plaws rau tus yuam sij yog 100 tus cim, thiab tus yuam sij tuaj yeem muaj cov ntsiab lus ntawm Unicode tsis raug cai nrog rau qhov chaw thiab lwm cov cim tsis tuaj yeem luam tawm. Txoj hlua qhov tseem ceeb muaj qhov txwv ntawm 32,768 bytes thiab qhov siab tshaj plaws ntawm cov khoom tau nkag hauv cov ntaub ntawv hloov pauv yog 100. Zuag qhia tag nrho, qhov siab tshaj plaws ntawm cov ntaub ntawv hloov pauv yog nyob ib puag ncig 140kb - rau kev siv, yuav luag raws nraim qhov ntev ntawm Shakespeare qhov kev ua si 'Romeo thiab Juliet. '."

Peb tsim DAO nrog rau cov xwm txheej hauv qab no:
Txhawm rau pib ua lag luam kom tau txais nyiaj los ntawm kev hu xov tooj getFunds() kev txhawb nqa ntawm tsawg kawg 2 tus neeg koom - DAO cov tub ua lag luam - yuav tsum tau. Tshem tawm nws yuav ua tau raws nraim li qhov tag nrho qhia rau pov npav DAO cov tswv.

Cia peb ua 3 yam ntawm cov yuam sij thiab ntxiv cov laj thawj rau kev ua haujlwm nrog kev sib npaug hauv 2 txoj haujlwm tshiab pov npav thiab getFunds:
xx… xx_ia = investor, tshuav nyiaj li cas (pov npav, tso nyiaj, rho tawm)
xx… xx_sv = startups, tus naj npawb ntawm votes (vote, getFunds)
xx… xx_sf ib = startups, tus naj npawb ntawm votes (vote, getFunds)
xx…xx = pej xeem chaw nyob (35 cim)

Thov nco ntsoov tias hauv Vote peb yuav tsum hloov kho ntau qhov chaw ib zaug:

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

WriteSet tso cai rau peb ua ntau cov ntaub ntawv ib zaug hauv ib qho invokeScript kev lag luam.

Qhov no yog qhov nws zoo li hauv qhov tseem ceeb-tus nqi khaws cia ntawm DAO dApp, tom qab Bob thiab Cooper rov ntxiv ia- deposits:
Kawm sau Waves ntse daim ntawv cog lus ntawm RIDE thiab RIDE4DAPPS. Part 2 (DAO - Decentralized Autonomous Organization)

Peb txoj haujlwm tso nyiaj tau hloov me ntsis:
Kawm sau Waves ntse daim ntawv cog lus ntawm RIDE thiab RIDE4DAPPS. Part 2 (DAO - Decentralized Autonomous Organization)

Tam sim no los txog lub sijhawm tseem ceeb tshaj plaws hauv DAO cov haujlwm - pov ntawv rau tej yaam num yuav tau txais nyiaj txiag.

Bob pov npav rau 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: []}))

Kawm sau Waves ntse daim ntawv cog lus ntawm RIDE thiab RIDE4DAPPS. Part 2 (DAO - Decentralized Autonomous Organization)

Hauv cov ntaub ntawv khaws cia peb pom txhua qhov tsim nyog nkag rau Neli qhov chaw nyob:
Kawm sau Waves ntse daim ntawv cog lus ntawm RIDE thiab RIDE4DAPPS. Part 2 (DAO - Decentralized Autonomous Organization)
Cooper kuj tau pov npav rau Neli qhov project.
Kawm sau Waves ntse daim ntawv cog lus ntawm RIDE thiab RIDE4DAPPS. Part 2 (DAO - Decentralized Autonomous Organization)

Cia peb saib ntawm qhov ua haujlwm code getFunds. Neli yuav tsum sau tsawg kawg ntawm 2 daim ntawv pov npav kom muaj peev xwm thim nyiaj los ntawm DAO.
Kawm sau Waves ntse daim ntawv cog lus ntawm RIDE thiab RIDE4DAPPS. Part 2 (DAO - Decentralized Autonomous Organization)

Neli yuav thim ib nrab ntawm cov nyiaj uas tau muab rau nws:

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

Kawm sau Waves ntse daim ntawv cog lus ntawm RIDE thiab RIDE4DAPPS. Part 2 (DAO - Decentralized Autonomous Organization)

Nws ua tiav, uas yog, DAO ua haujlwm!

Peb tau saib cov txheej txheem tsim DAO ua lus RIDE4 DAPPS.
Hauv seem hauv qab no peb yuav ua tib zoo saib xyuas cov cai refactoring thiab kuaj cov ntaub ntawv.

Tag nrho version ntawm code nyob rau hauv Wave 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
    }
}

Thawj tshooj
Code ntawm GitHub
Wave RIDE IDE
Tshaj tawm ntawm qhov kev pab cuam

Tau qhov twg los: www.hab.com

Ntxiv ib saib