హలో అందరికీ!
В
ఇప్పుడు విడదీయబడిన వాటిని కొద్దిగా పరీక్షిద్దాం
దశ 3. dApp ఖాతాను పరీక్షిస్తోంది
ఏ సమస్యలు వెంటనే ఆలిస్తో స్వరాలకు వస్తాయి dApp ఖాతా?
ముందుగా:
బూబ్ మరియు కూపర్ అనుకోకుండా నార్మల్ని ఉపయోగించి dApp చిరునామాకు నిధులను పంపవచ్చు బదిలీ లావాదేవీలు మరియు తద్వారా వాటిని తిరిగి యాక్సెస్ చేయలేరు.రెండవది:
బూబ్ మరియు/లేదా కూపర్ సమ్మతి లేకుండా నిధులను ఉపసంహరించుకోకుండా ఆలిస్ను మేము ఏ విధంగానూ నిరోధించము. ధృవీకరించడానికి శ్రద్ధ వహించండి, ఆలిస్ నుండి అన్ని లావాదేవీలు అమలు చేయబడతాయి.
ఆలిస్ని నిషేధించడం ద్వారా 2ని సరిచేద్దాం బదిలీ లావాదేవీలు. సరిదిద్దబడిన స్క్రిప్ట్ని అమలు చేయండి:

మేము dApp ఆలిస్ మరియు ఆమె సంతకంతో నాణేలను ఉపసంహరించుకోవడానికి ప్రయత్నిస్తున్నాము. మాకు లోపం వస్తుంది:
ఉపసంహరణ ద్వారా ఉపసంహరించుకోవడానికి ప్రయత్నిస్తున్నారు:
broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"withdraw",args:[{type:"integer", value: 1000000}]}, payment: []}))
స్క్రిప్ట్ పని చేస్తుంది మరియు మేము 2వ పాయింట్ని కనుగొన్నాము!
స్టేజ్ 4. ఓటింగ్తో DAOని సృష్టించండి
దురదృష్టవశాత్తూ, RIDE భాష ఇంకా సేకరణలతో పని చేసే సామర్థ్యాన్ని అందించలేదు (నిఘంటువులు, నిఘంటువులు, పునరావృత్తులు, తగ్గింపులు మొదలైనవి). అయితే, ఫ్లాట్ సేకరణలపై ఏదైనా కార్యకలాపాలకు కీ-విలువ కీలు మరియు వాటి డిక్రిప్షన్తో వరుసగా స్ట్రింగ్లతో పని చేయడానికి మేము సిస్టమ్ను రూపొందించవచ్చు.
తీగలను కలపడం చాలా సులభం, తీగలను సూచికల ద్వారా వేరు చేయవచ్చు.
స్ట్రింగ్ని టెస్ట్ కేస్గా సేకరించి అన్వయించండి మరియు ఇది లావాదేవీ ఫలితాన్ని ఎలా ప్రభావితం చేస్తుందో తనిఖీ చేద్దాం.
ఈ రకమైన లావాదేవీకి @verifierలో ఈ అవకాశం బ్లాక్ చేయబడినందున, ఆలిస్ బదిలీ లావాదేవీపై సంతకం చేయలేకపోవడాన్ని మేము నిర్ధారించాము.
స్ట్రింగ్స్తో ప్రాక్టీస్ చేసి, దీనిని పరిష్కరిద్దాం.
రైడ్ స్ట్రింగ్స్
లావాదేవీ మళ్లీ సాధ్యమవుతుంది, తీగలతో ఎలా పని చేయాలో మాకు తెలుసు.

మొత్తంగా, మీరు సంక్లిష్ట తర్కాన్ని వ్రాయడానికి కావలసినవన్నీ మా వద్ద ఉన్నాయి DAO dApp.
డేటా లావాదేవీలు
డేటా లావాదేవీలు:
“కీకి గరిష్ట పరిమాణం 100 అక్షరాలు మరియు ఒక కీ ఖాళీలు మరియు ఇతర ముద్రించలేని చిహ్నాలతో సహా ఏకపక్ష యూనికోడ్ కోడ్ పాయింట్లను కలిగి ఉంటుంది. స్ట్రింగ్ విలువలు 32,768 బైట్ల పరిమితిని కలిగి ఉంటాయి మరియు డేటా లావాదేవీలో సాధ్యమయ్యే నమోదుల గరిష్ట సంఖ్య 100. మొత్తంమీద, డేటా లావాదేవీ యొక్క గరిష్ట పరిమాణం దాదాపు 140kb - సూచన కోసం, దాదాపు ఖచ్చితంగా షేక్స్పియర్ నాటకం 'రోమియో అండ్ జూలియట్ యొక్క పొడవు. '."
మేము క్రింది షరతులతో DAOని సృష్టిస్తాము:
స్టార్టప్కి కాల్ చేయడం ద్వారా నిధులు పొందేందుకు getFunds() కనీసం 2 పాల్గొనేవారి మద్దతు - DAO పెట్టుబడిదారులు అవసరం. వెనక్కి సూచించిన మొత్తంలో ఇది ఖచ్చితంగా సాధ్యమవుతుంది ఓటింగ్ DAO యజమానులు.
3 రకాల కీలను తయారు చేద్దాం మరియు 2 కొత్త ఫంక్షన్లలో బ్యాలెన్స్లతో పని చేయడానికి లాజిక్ని జోడిద్దాము ఓటు మరియు getFunds:
xx…xx_ia = పెట్టుబడిదారులు, అందుబాటులో ఉన్న బ్యాలెన్స్ (ఓటు, డిపాజిట్, ఉపసంహరణ)
xx…xx_ఎస్ వి = స్టార్టప్లు, ఓట్ల సంఖ్య (ఓటు, గెట్ ఫండ్లు)
xx…xx_sf = స్టార్టప్లు, ఓట్ల సంఖ్య (ఓటు, గెట్ ఫండ్లు)
xx…xx = పబ్లిక్ చిరునామా (35 అక్షరాలు)
మేము ఒకేసారి అనేక ఫీల్డ్లను అప్డేట్ చేయాల్సిన అవసరం ఉందని ఓటులో గమనించండి:
WriteSet([DataEntry(key1, value1), DataEntry(key2, value2)]),
WriteSet ఒకదానిలో ఒకేసారి అనేక రికార్డులను చేయడానికి అనుమతిస్తుంది ఇన్వోక్స్క్రిప్ట్ లావాదేవీలు.
బాబ్ మరియు కూపర్ తిరిగి నింపిన తర్వాత DAO dApp యొక్క కీ-వాల్యూ స్టోర్లో ఇది ఇలా కనిపిస్తుంది ia- డిపాజిట్లు:
మా డిపాజిట్ ఫంక్షన్ కొద్దిగా మారింది:
DAO యొక్క కార్యకలాపాలలో ఇప్పుడు అత్యంత ముఖ్యమైన క్షణం వస్తుంది - ఓటు నిధుల కోసం ప్రాజెక్టుల కోసం.
బాబ్ 500000 వేవ్లెట్లపై నెలి ప్రాజెక్ట్కు ఓట్లు వేశారు:
broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"vote",args:[{type:"integer", value: 500000}, {type:"string", value: "3MrXEKJr9nDLNyVZ1d12Mq4jjeUYwxNjMsH"}]}, payment: []}))
డేటా స్టోర్లో, నెలి చిరునామాకు అవసరమైన అన్ని నమోదులను మేము చూస్తాము:
కూపర్ నెలి ప్రాజెక్ట్కు కూడా ఓటు వేశారు.
ఫంక్షన్ కోడ్ని పరిశీలిద్దాం getFunds. DAO నుండి నిధులను ఉపసంహరించుకోవడానికి నెలి తప్పనిసరిగా కనీసం 2 ఓట్లను సేకరించాలి.
నెలి తనకు అప్పగించిన మొత్తంలో సగం ఉపసంహరించుకోబోతోంది:
broadcast(invokeScript({dappAddress: address(env.accounts[1]), call:{function:"getFunds",args:[{type:"integer", value: 500000}]}, payment: []}))
ఆమె విజయం సాధించింది, అంటే DAO పనిచేస్తుంది!
మేము భాషలో DAOని సృష్టించే ప్రక్రియను సమీక్షించాము RIDE4DAPPS.
కింది భాగాలలో, మేము కోడ్ రీఫ్యాక్టరింగ్ మరియు కేస్ టెస్టింగ్ను నిశితంగా పరిశీలిస్తాము.
పూర్తి కోడ్
# 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
}
}
మూలం: www.habr.com