Browse Source

DERO-HE DVM guide updates 2

master
Captain 9 months ago
parent
commit
6fb9ed4076
3 changed files with 0 additions and 199 deletions
  1. +0
    -83
      lottery.bas
  2. +0
    -54
      lottery_SC_guide.md
  3. +0
    -62
      support_functions

+ 0
- 83
lottery.bas View File

@@ -1,83 +0,0 @@
/* Lotter Smart Contract in DVM-BASIC
This lottery smart contract will give lottery wins every xth try.
*/



Function Lottery(value Uint64) Uint64
10 dim deposit_count,winner as Uint64
20 LET deposit_count = LOAD("deposit_count")+1
25 IF value == 0 THEN GOTO 110 // if deposit amount is 0, simply return
30 STORE("depositor_address" + (deposit_count-1), SIGNER()) // store address for later on payment
40 STORE("deposit_total", LOAD("deposit_total") + value )
50 STORE("deposit_count",deposit_count)
60 IF LOAD("lotteryeveryXdeposit") > deposit_count THEN GOTO 110 // we will wait till X players join in
// we are here means all players have joined in, roll the DICE,
70 LET winner = RANDOM() % deposit_count // we have a winner
80 SEND_DERO_TO_ADDRESS(LOAD("depositor_address" + winner) , LOAD("lotterygiveback")*LOAD("deposit_total")/10000)
// re initialize for another round
90 STORE("deposit_count", 0) // initial players
100 STORE("deposit_total", 0) // total deposit of all players
110 RETURN 0
End Function

// this function is used to initialize parameters during install time
Function Initialize() Uint64
10 STORE("owner", SIGNER()) // store in DB ["owner"] = address
20 STORE("lotteryeveryXdeposit", 2) // lottery will reward every X deposits
// how much will lottery giveback in 1/10000 parts, granularity .01 %
30 STORE("lotterygiveback", 9900) // lottery will give reward 99% of deposits, 1 % is accumulated for owner to withdraw
33 STORE("deposit_count", 0) // initial players
34 STORE("deposit_total", 0) // total deposit of all players
35 printf "Initialize executed"
40 RETURN 0
End Function
// used to tune lottery parameters
Function TuneLotteryParameters(input Uint64, lotteryeveryXdeposit Uint64, lotterygiveback Uint64) Uint64
10 dim key,stored_owner as String
20 dim value_uint64 as Uint64
30 IF ADDRESS_RAW(LOAD("owner")) == ADDRESS_RAW(SIGNER()) THEN GOTO 100 // check whether owner is real owner
40 RETURN 1
100 STORE("lotteryeveryXdeposit", lotteryeveryXdeposit) // lottery will reward every X deposits
130 STORE("lotterygiveback", value_uint64) // how much will lottery giveback in 1/10000 parts, granularity .01 %
140 RETURN 0 // return success
End Function

// this function is used to change owner
// owner is an string form of address
Function TransferOwnership(newowner String) Uint64
10 IF ADDRESS_RAW(LOAD("owner")) == ADDRESS_RAW(SIGNER()) THEN GOTO 30
20 RETURN 1
30 STORE("tmpowner",newowner)
40 RETURN 0
End Function
// until the new owner claims ownership, existing owner remains owner
Function ClaimOwnership() Uint64
10 IF ADDRESS_RAW(LOAD("tmpowner")) == ADDRESS_RAW(SIGNER()) THEN GOTO 30
20 RETURN 1
30 STORE("owner",SIGNER()) // ownership claim successful
40 RETURN 0
End Function
// if signer is owner, withdraw any requested funds
// if everthing is okay, thety will be showing in signers wallet
Function Withdraw( amount Uint64) Uint64
10 IF ADDRESS_RAW(LOAD("owner")) == ADDRESS_RAW(SIGNER()) THEN GOTO 30
20 RETURN 1
30 SEND_DERO_TO_ADDRESS(SIGNER(),amount)
40 RETURN 0
End Function



+ 0
- 54
lottery_SC_guide.md View File

@@ -1,54 +0,0 @@
## Dero Stargate DVM Smart Contracts guide to install and test various function of lottery Smart Contract.

**Download** Dero Stargate testnet [source](https://git.dero.io/DeroProject/derosuite_stargate) and [binaries](https://git.dero.io/DeroProject/Dero_Stargate_testnet_binaries).

**Start Dero daemon in testnet mode.**
```
./derod-linux-amd64 --testnet
```

**Start Dero wallet in testnet.**
```
dero-wallet-cli-linux-amd64 --rpc-server --wallet-file testnet.wallet --testnet
```

**Start Dero wallet second instance to test transfer/ownership functions etc.**
```
dero-wallet-cli-linux-amd64 --wallet-file testnet2.wallet --testnet --rpc-server --rpc-bind=127.0.0.1:30308
```

**Dero testnet Explorer**
```
./explorer-linux-amd64 --rpc-server-address 127.0.0.1:30306 --http-address=0.0.0.0:8080
```

**Dero Stargate Testnet Explorer**
[https://testnetexplorer.dero.io/ ](https://testnetexplorer.dero.io/)


**Installing Smart Contract.**
[Download lottery.bas](https://git.dero.io/DeroProject/derosuite_stargate/src/master/cmd/dvm/lottery.bas)
```
curl --request POST --data-binary @lottery.bas http://127.0.0.1:30309/install_sc
```

**Examples of various lottery Smart Contract functions**
**Eg: To play lottery**
```
curl -X POST http://127.0.0.1:30309/json_rpc -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":"0","method":"transfer_split","params":{"mixin":5,"get_tx_key": true , "sc_tx":{"entrypoint":"Lottery","scid":"ab82caa18753efa0f76e7266af7fdd7f11e0ada5e135bd63f1cd823f5e2c2fdc" , "value":4000000000000 } }}'
```

**Eg: Withdraw balance**
```
curl -X POST http://127.0.0.1:30309/json_rpc -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":"0","method":"transfer_split","params":{"mixin":5,"get_tx_key": true , "sc_tx":{"entrypoint":"Withdraw","scid":"55aaf55b5203cd10f473a9bcf641f305885235e176270ae5e38ba6fa05dbf2b0", "params":{ "amount":"1" } } }}'
```

**Eg: To transfer ownership**
```
curl -X POST http://127.0.0.1:30309/json_rpc -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":"0","method":"transfer_split","params":{"mixin":5,"get_tx_key": true , "sc_tx":{"entrypoint":"TransferOwnership","scid":"55aaf55b5203cd10f473a9bcf641f305885235e176270ae5e38ba6fa05dbf2b0", "params":{ "newowner":"dETohFmTunwF58wAs5Jn3d1N1oaJqwRxvarNJudUh95nVbZyigTja1W8Ljzp3j8VhxbB9gv3TBs4W5gnFa24cehz2cM6nUg84s" } } }}'
```

**Eg: To claim ownership**
```
curl -X POST http://127.0.0.1:30308/json_rpc -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":"0","method":"transfer_split","params":{"mixin":5,"get_tx_key": true , "sc_tx":{"entrypoint":"ClaimOwnership","scid":"55aaf55b5203cd10f473a9bcf641f305885235e176270ae5e38ba6fa05dbf2b0" } }}'
```

+ 0
- 62
support_functions View File

@@ -1,62 +0,0 @@
Support Functions are inbuilt functions which provide some functionality or expose internals for speed and technical reasons.


LOAD(variable)
==============

LOAD loads a variable which was previously stored in the blockchain using STORE function. Return type will be Uint64/String depending on what is stored.
It will panic if the value does NOT exists

Uint64 EXISTS(variable)
=======================
EXISTS return 1 if the variable is store in DB and 0 otherwise

STORE(key variable, value variable)
===================================
STORE stores key and value in the DB. All storage state of the SC is accessible only from the SC which created it.

Uint64 RANDOM()
Uint64 RANDOM(limit Uin64)
============================
RANDOM returns a random using a PRNG seeded on BLID,SCID,TXID. First form gives a uint64, second form returns
random number in the range 0 - (limit), 0 is inclusive, limit is exclusive

String SCID()
==============
Returns SMART CONTRACT ID which is currently running

String BLID()
==============
Returns current BLOCK ID which contains current execution-in-progress TXID

String TXID()
=============
Returns current TXID which is execution-in-progress.

Uint64 BLOCK_HEIGHT()
=====================
Returns current chain height of BLID()

Uint64 BLOCK_TOPOHEIGHT()
===========================
Returns current topoheight of BLID()

String SIGNER()
=================
Returns address of who signed this transaction

Uint64 IS_ADDRESS_VALID(p String)
=================================
Returns 1 is address is valid, 0 otherwise


String ADDRESS_RAW(p String)
============================
Returns address in RAW form as 64 byte keys, stripping away textual/presentation form. 2 address should always be compared in RAW form

SEND_DERO_TO_ADDRESS(a String, amount Uint64)
==============================================
Sends amount DERO from SC DERO balance to a address. address must in string form DERO/DETO form
If the SC does not have enough balance, it will panic



Loading…
Cancel
Save