You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

582 line
25KB

  1. .. _stargate_dvm:
  2. DERO Virtual Machine
  3. ====================
  4. DERO Virtual Machine represents entire DERO Smart Contracts eco-system which runs on the DERO block chain.
  5. **Documentation**
  6. DVM is a decentralized platform that runs both public and private smart contracts: applications that run exactly as programmed without any possibility of downtime, censorship, fraud or third-party interference.Public Smart contracts are open versions. However, the DVM is being designed to support Private Smart Contracts where everything is hidden, eg parties, and information involved. Smart Contracts are nothing but rules which apply on transacting parties.
  7. Current version of DVM is an interpretor based system to avoid security vulneribilities, issues and compiler backdoors. This also allows easy audits of Smart Contracts for quality,bug-testing and security assurances. DVM supports a new language DVM-BASIC.
  8. DVM apps run on a from scratch custom built privacy supporting, encrypted blockchain, an enormously powerful shared global infrastructure that can move value around and represent the ownership of assets/property without leaking any information.No one knows who owns what and who transferred to whom.
  9. * This enables developers to create puzzles, games, voting, markets, store registries of debts or promises, move funds in accordance with instructions given long in the past (like a will or a futures contract) and many other ideas/things that have not been invented yet, all without a middleman or counterparty risk.
  10. * DVM-BASIC is a contract-oriented, high-level language for implementing smart contracts. It is influenced by GW-BASIC, Visual Basic and C and is designed to target the DERO Virtual Machine (DVM). It is very easy to program and very readable.
  11. * DVM runs Smart Contracts which are a collection of functions written in DVM-BASIC. These functions can be invoked over the blockchain to do something. SCs can act as libraries for other SCs.
  12. * DVM supports number of comments formats such as ‘, // , /* */ as good documentation is necessary.
  13. **Example Factorial Program**
  14. .. code-block:: php
  15. :linenos:
  16. ' This is a comment
  17. // This comment is supported
  18. /* this is multi-line comment */
  19. // printf is not supported in latest DVM. Please comment or remove printf from all old Smart Contracts.
  20. Function Factorial(s Uint64) Uint64 // this is a commment
  21. 10 DIM result,scopy as Uint64 /* this is also comment */
  22. 15 LET scopy = s
  23. 20 LET result = 1
  24. 30 LET result = result * s
  25. 40 LET s = s - 1
  26. 50 IF s >= 2 THEN GOTO 30
  27. //60 PRINTF "FACTORIAL of %d = %d" scopy result // printf is not supported in latest DVM.
  28. 70 RETURN result
  29. End Function
  30. **DVM are written in a DVM-BASIC custom BASIC style language with line numbers.**
  31. **DVM supports uint64 and string data-types.**
  32. **DVM interprets the smart-contract and processes the SC line-line**
  33. * uint64 supports almost all operators namely +,-,*,/,%
  34. * uint64 support following bitwise operators & ,|, ^, ! , >> , <<
  35. * uint64 supports following logical operators >, >= , <, <=, == , !=
  36. * string supports only + operator. string support concatenation with a uint64.
  37. * string supports ==, != logical operators.
  38. * All DVM variables are mandatory to define and are initialized to default values namely 0 and “”.
  39. A SC execution must return 0 to persist any changes made during execution. During execution, no panics should occur.
  40. DIM Statement
  41. -------------
  42. DIM stands for data in memory and is used to define variable names within a function
  43. syntax
  44. 10 DIM variable1 as type 20 DIM variable1,variable2 as type
  45. type can be any type supported by DVM
  46. Defining a varible initializes a variable to its ZERO value.
  47. Function statement
  48. ------------------
  49. Function statement is used to define a function. See eg, below for a function which adds 2 numbers
  50. .. code-block:: php
  51. :linenos:
  52. Function ADD(x uint64, y uint64) uint64
  53. 10 RETURN x + y
  54. End Function
  55. Function syntax is of 2 types
  56. 1. Function function_name( 0 or more arguments )
  57. 2. Function function_name( 0 or more arguments ) return type
  58. The rules for functions are as follows
  59. * All functions begin with Function keyword
  60. * Function name should be alpha-numeric. If the first letter of the function is Upper case alphabet, it can be invoked by blockchain and other smart-contracts. Otherwise the function can only be called by other functions within the smart contract.
  61. * Function may or may not have a return type
  62. * All functions must use RETURN to return from function or to return a value. RETURN is mandatory.
  63. * All functions must end with End Function. End Function is mandatory
  64. * A function can have a implicit parameter value of type uint64, which contains amount of DERO value sent with the transaction.
  65. Any error caused during processing will immediately stop execution and discard all changes that occur during SC execution.
  66. Any Entrypoint which returns uint64 value 0 is termed as success and will make transaction to commit all state changes.
  67. GOTO Statement
  68. --------------
  69. It is used to jump to any point within the function. It cannot cross function-boundary
  70. syntax GOTO line-number
  71. IF
  72. --
  73. If statement is used to evaluate expression and make decisions.It has following forms
  74. 1. IF expr1 condition expr2 THEN GOTO line number
  75. 2. IF expr1 condition expr2 THEN GOTO line number ELSE GOTO line number
  76. This is used to change execution flow based on conditions. Conditions can be as complex expressions
  77. LET Statement
  78. -------------
  79. LET is used to assign a value to a variable. value can be as complex as possible and can contain complex expression
  80. syntax
  81. .. code-block:: php
  82. line number LET variable_name = expression;
  83. expression can invoke other functions,eg
  84. 10 LET x = 2 + 3 + ADD(2,3)
  85. ANY assignments within DVM can only be done using LET
  86. Return Statement
  87. ----------------
  88. It is used to return from a function and can be used anywhere within a function
  89. syntax
  90. 1.RETURN ( return nil )
  91. 2.RETURN expression ( evaluates expression and returns value )
  92. Any return value must match with the type defined while declaring function
  93. Support Functions
  94. -----------------
  95. **LOAD(variable)**
  96. 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
  97. **Uint64 EXISTS(variable)**
  98. EXISTS return 1 if the variable is store in DB and 0 otherwise
  99. **STORE(key variable, value variable)**
  100. STORE stores key and value in the DB. All storage state of the SC is accessible only from the SC which created it.
  101. Uint64 RANDOM()
  102. **Uint64 RANDOM(limit Uin64)**
  103. 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
  104. **String SCID()**
  105. Returns SMART CONTRACT ID which is currently running
  106. **String BLID()**
  107. Returns current BLOCK ID which contains current execution-in-progress TXID
  108. **String TXID()**
  109. Returns current TXID which is execution-in-progress.
  110. **Uint64 BLOCK_HEIGHT()**
  111. Returns current chain height of BLID()
  112. **Uint64 BLOCK_TOPOHEIGHT()**
  113. Returns current topoheight of BLID()
  114. **String SIGNER()**
  115. Returns address of who signed this transaction
  116. **Uint64 IS_ADDRESS_VALID(p String)**
  117. Returns 1 if address is valid, 0 otherwise
  118. **String ADDRESS_RAW(p String)**
  119. Returns address in RAW form as 33 byte keys, stripping away textual/presentation form. 2 address should always be compared in RAW form
  120. **SEND_DERO_TO_ADDRESS(a String, amount Uint64)**
  121. Sends amount DERO from SC DERO balance to a address which should be raw form. address must in string form DERO/DETO form If the SC does not have enough balance, it will panic
  122. **ADD_VALUE(a String, amount Uint64)**
  123. Send specific number of token to specific account. If account is bring touched for the first time, it is done simply. If account is already initialized ( it already has some balance, but SC does not know how much). So, it gives additional balance homomorphically
  124. Lotter SC Guide
  125. ---------------
  126. **Dero Stargate DVM Smart Contracts guide to install and test various function of lottery Smart Contract.**
  127. **Download** DERO Stargate testnet `source <https://github.com/deroproject/derohe>`_ and `binaries. <https://github.com/deroproject/derohe/releases>`_
  128. .. code-block:: php
  129. ./derod-linux-amd64 --testnet;
  130. **Start DERO wallet in testnet.**
  131. .. code-block:: php
  132. dero-wallet-cli-linux-amd64 --rpc-server --wallet-file testnet.wallet --testnet;
  133. **Start Dero wallet second instance to test transfer/ownership functions etc.**
  134. .. code-block:: php
  135. dero-wallet-cli-linux-amd64 --wallet-file testnet2.wallet --testnet --rpc-server --rpc-bind=127.0.0.1:40403;
  136. **Dero testnet Explorer**
  137. .. code-block:: php
  138. ./explorer-linux-amd64 --rpc-server-address 127.0.0.1:30306 --http-address=0.0.0.0:8080;
  139. **DERO Stargate Testnet Explorer**
  140. https://testnetexplorer.dero.io/
  141. **Installing Smart Contract**
  142. Download Lottery.bas <https://git.dero.io/DeroProject/derosuite_stargate/src/master/cmd/dvm/lottery.bas>
  143. .. code-block:: php
  144. curl --request POST --data-binary @lottery.bas http://127.0.0.1:40403/install_sc;
  145. Download SC Code,check balance and variables from chain
  146. .. code-block:: php
  147. curl http://127.0.0.1:40402/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"getsc","params":{ "scid":"aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af" , "code":false, "keysstring":["deposit_count"]}}' -H 'Content-Type: application/json';
  148. **Examples of various lottery Smart Contract functions Eg: To play lottery**
  149. .. code-block:: php
  150. curl http://127.0.0.1:40403/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"transfer","params":{ "transfers":[{"amount":111111,"destination":"deto1qxqqen6lqmksmzmxmfqmxp2y8zvkldtcq8jhkzqflmyczepjw9dp46gc3cczu"}],"scid":"aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af","sc_rpc":[{"name":"entrypoint","datatype":"S","value":"Lottery"}] }}' -H 'Content-Type: application/json';
  151. **Eg: Withdraw balance only for smart contract owner**
  152. .. code-block:: php
  153. curl http://127.0.0.1:40403/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"transfer","params":{ "transfers":[{"amount":111111,"destination":"deto1qxqqen6lqmksmzmxmfqmxp2y8zvkldtcq8jhkzqflmyczepjw9dp46gc3cczu"}],"scid":"aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af", "sc_rpc":[{"name":"entrypoint","datatype":"S","value":"Withdraw"}, {"name":"amount","datatype":"U","value":2 }] }}' -H 'Content-Type: application/json';
  154. **Eg: To transfer ownership**
  155. .. code-block:: php
  156. curl http://127.0.0.1:40403/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"transfer","params":{ "transfers":[{"amount":111111,"destination":"deto1qxqqen6lqmksmzmxmfqmxp2y8zvkldtcq8jhkzqflmyczepjw9dp46gc3cczu"}],"scid":"aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af", "sc_rpc":[{"name":"entrypoint","datatype":"S","value":"TransferOwnership"}, {"name":"newowner","datatype":"S","value":"deto1qxsplx7vzgydacczw6vnrtfh3fxqcjevyxcvlvl82fs8uykjkmaxgfgulfha5" }] }}' -H 'Content-Type: application/json';
  157. **Eg: To claim ownership**
  158. .. code-block:: php
  159. curl http://127.0.0.1:40403/json_rpc -d ‘{“jsonrpc”:“2.0”,“id”:“0”,“method”:“transfer”,“params”:{ “transfers”:[{“amount”:111111,“destination”:“deto1qxqqen6lqmksmzmxmfqmxp2y8zvkldtcq8jhkzqflmyczepjw9dp46gc3cczu”}],“scid”:“aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af”, “sc_rpc”:[{“name”:“entrypoint”,“datatype”:“S”,“value”:“ClaimOwnership”}] }}’ -H ‘Content-Type: application/json’;
  160. Lottery.bas
  161. -------------
  162. .. code-block:: php
  163. :linenos:
  164. /* Lotter Smart Contract in DVM-BASIC
  165. This lottery smart contract will give lottery wins every xth try.
  166. */
  167. Function Lottery(value Uint64) Uint64
  168. 10 dim deposit_count,winner as Uint64
  169. 20 LET deposit_count = LOAD("deposit_count")+1
  170. 25 IF value == 0 THEN GOTO 110 // if deposit amount is 0, simply return
  171. 30 STORE("depositor_address" + (deposit_count-1), SIGNER()) // store address for later on payment
  172. 40 STORE("deposit_total", LOAD("deposit_total") + value )
  173. 50 STORE("deposit_count",deposit_count)
  174. 60 IF LOAD("lotteryeveryXdeposit") > deposit_count THEN GOTO 110 // we will wait till X players join in
  175. // we are here means all players have joined in, roll the DICE,
  176. 70 LET winner = RANDOM() % deposit_count // we have a winner
  177. 80 SEND_DERO_TO_ADDRESS(LOAD("depositor_address" + winner) , LOAD("lotterygiveback")*LOAD("deposit_total")/10000)
  178. // Re initialize for another round
  179. 90 STORE("deposit_count", 0) // initial players
  180. 100 STORE("deposit_total", 0) // total deposit of all players
  181. 110 RETURN 0
  182. End Function
  183. // this function is used to initialize parameters during install time
  184. Function Initialize() Uint64
  185. 10 STORE("owner", SIGNER()) // store in DB ["owner"] = address
  186. 20 STORE("lotteryeveryXdeposit", 2) // lottery will reward every X deposits
  187. // how much will lottery giveback in 1/10000 parts, granularity .01 %
  188. 30 STORE("lotterygiveback", 9900) // lottery will give reward 99% of deposits, 1 % is accumulated for owner to withdraw
  189. 33 STORE("deposit_count", 0) // initial players
  190. 34 STORE("deposit_total", 0) // total deposit of all players
  191. // 35 printf "Initialize executed"
  192. 40 RETURN 0
  193. End Function
  194. // Used to tune lottery parameters
  195. Function TuneLotteryParameters(input Uint64, lotteryeveryXdeposit Uint64, lotterygiveback Uint64) Uint64
  196. 10 dim key,stored_owner as String
  197. 20 dim value_uint64 as Uint64
  198. 30 IF LOAD("owner") == SIGNER() THEN GOTO 100 // check whether owner is real owner
  199. 40 RETURN 1
  200. 100 STORE("lotteryeveryXdeposit", lotteryeveryXdeposit) // lottery will reward every X deposits
  201. 130 STORE("lotterygiveback", value_uint64) // how much will lottery giveback in 1/10000 parts, granularity .01 %
  202. 140 RETURN 0 // return success
  203. End Function
  204. // This function is used to change owner
  205. // owner is an string form of address
  206. Function TransferOwnership(newowner String) Uint64
  207. 10 IF LOAD("owner") == SIGNER() THEN GOTO 30
  208. 20 RETURN 1
  209. 30 STORE("tmpowner",ADDRESS_RAW(newowner))
  210. 40 RETURN 0
  211. End Function
  212. // Until the new owner claims ownership, existing owner remains owner
  213. Function ClaimOwnership() Uint64
  214. 10 IF LOAD("tmpowner") == SIGNER() THEN GOTO 30
  215. 20 RETURN 1
  216. 30 STORE("owner",SIGNER()) // ownership claim successful
  217. 40 RETURN 0
  218. End Function
  219. // if signer is owner, withdraw any requested funds
  220. // if everthing is okay, they will be showing in signers wallet
  221. Function Withdraw( amount Uint64) Uint64
  222. 10 IF LOAD("owner") == SIGNER() THEN GOTO 30
  223. 20 RETURN 1
  224. 30 SEND_DERO_TO_ADDRESS(SIGNER(),amount)
  225. 40 RETURN 0
  226. End Function
  227. // if signer is owner, provide him rights to update code anytime
  228. // make sure update is always available to SC
  229. Function UpdateCode( code String) Uint64
  230. 10 IF LOAD("owner") == SIGNER() THEN GOTO 30
  231. 20 RETURN 1
  232. 30 UPDATE_SC_CODE(code)
  233. 40 RETURN 0
  234. End Function
  235. Token SC Guide
  236. --------------
  237. **Dero Stargate DVM Smart Contracts guide to install and test various function of private token Smart Contract.**
  238. Notes:
  239. 1. All wallet Addressess need to be registerd first with SC before they need to interact with. This condition will be removed in future.
  240. 2. Requirement of detoAnyRandomAddressFromExplorer during SC invocation will be removed in future.
  241. 3. burn is equal to deposit if SCID is defined else DERO/token will dissappear/burn from network forever. burn will renamed to something more meaningful. This can be proved with cryptographic proof.
  242. **Download** DERO Stargate testnet `source <https://github.com/deroproject/derohe>`_ and `binaries. <https://github.com/deroproject/derohe/releases>`_
  243. **Start Dero daemon in testnet mode**
  244. .. code-block:: php
  245. ./derod-linux-amd64 --testnet;
  246. **Start Dero wallet in testnet.**
  247. .. code-block:: php
  248. dero-wallet-cli-linux-amd64 --rpc-server --wallet-file testnet.wallet --testnet;
  249. **Start Dero wallet second instance to test transfer/ownership functions etc.**
  250. .. code-block:: php
  251. dero-wallet-cli-linux-amd64 --wallet-file testnet2.wallet --testnet --rpc-server --rpc-bind=127.0.0.1:40403;
  252. **Dero testnet Explorer, Not required but if you want to host your own explorer for privacy reasons.**
  253. .. code-block:: php
  254. ./explorer-linux-amd64 --http-address=0.0.0.0:8080 ;
  255. **Connect to explorer using browser on localhost:8080**
  256. **Dero Stargate Testnet Explorer**
  257. https://testnetexplorer.dero.io/
  258. **To send DERO to multiple users in one transaction**
  259. .. code-block:: php
  260. curl http://127.0.0.1:40403/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"transfer","params":{ "transfers":[{"amount":100000,"destination":"deto1ADDRESS1"},{"amount":300000,"destination":"deto1ADDRESS12}] }}' -H 'Content-Type: application/json';
  261. **DERO has 2 types of SCs, public and private.**
  262. 1.Public SCs are public with all data/code/exchanges are public.
  263. 2.Private SCs have their smart contract data public. But no one knows how many tokens a particular user own or how much is he sending or how much is he receiving. Best example is to understand private SCs as banks and private tokens as cash. Once cash is out from the bank, Bank doesn’t know “who has what amount or how is it being used/sent/received etc.”. This makes all private tokens completely private.
  264. **Installing Private Smart Contract.**
  265. Download token.bas <https://git.dero.io/DeroProject/derosuite_stargate/src/master/cmd/dvm/token.bas>
  266. .. code-block:: php
  267. curl --request POST --data-binary @token.bas http://127.0.0.1:40403/install_sc;
  268. **To check private token balance in wallet, type this command in wallet.**
  269. .. code-block:: php
  270. balance SCID;
  271. **Download SC Code,check SC balance and variables from chain**
  272. .. code-block:: php
  273. curl http://127.0.0.1:40402/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"getsc","params":{ "scid":"aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af" , "code":true}}' -H 'Content-Type: application/json';
  274. Examples of various private token Smart Contract functions Eg: To send private tokens from one wallet to another wallet, this does not involve SC Eg: this also showcases to send multiple assets( DERO and other tokens on DERO Network) within a single transaction
  275. .. code-block:: php
  276. curl http://127.0.0.1:40403/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"transfer","params":{ "transfers":[{"amount":111111,"destination":"deto1qxqqen6lqmksmzmxmfqmxp2y8zvkldtcq8jhkzqflmyczepjw9dp46gc3cczu"},{"amount":333333,"destination":"deto1qxqqen6lqmksmzmxmfqmxp2y8zvkldtcq8jhkzqflmyczepjw9dp46gc3cczu","scid": "aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af" }] }}' -H 'Content-Type: application/json';
  277. Eg: Convert DERO to tokens 1:1 swap, we are swapping 44 DERO atomic units to get some tokens
  278. .. code-block:: php
  279. curl http://127.0.0.1:40403/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"transfer","params":{"transfers":[{"amount":1,"destination":"deto1qxqqen6lqmksmzmxmfqmxp2y8zvkldtcq8jhkzqflmyczepjw9dp46gc3cczu", "burn":44}],"scid":"aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af", "sc_rpc":[{"name":"entrypoint","datatype":"S","value":"IssueTOKENX"}] }}' -H 'Content-Type: application/json';
  280. Convert tokens to DERO 1:1 swap, we are swapping 9 token atomic units to get 9 DERO atomic units This tx shows transferring tokens natively, no dero fees etc, this is under evaluation, **Currently these show as coinbase rewards **
  281. .. code-block:: php
  282. curl http://127.0.0.1:40403/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"transfer","params":{"transfers":[{"scid":"aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af", "amount":1,"destination":"deto1qxqqen6lqmksmzmxmfqmxp2y8zvkldtcq8jhkzqflmyczepjw9dp46gc3cczu", "burn":9}],"scid":"aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af", "sc_rpc":[{"name":"entrypoint","datatype":"S","value":"ConvertTOKENX"}] }}' -H 'Content-Type: application/json';
  283. Eg: To withdraw balance only for smart contract owner
  284. .. code-block:: php
  285. curl http://127.0.0.1:40403/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"transfer","params":{ "transfers":[{"amount":111111,"destination":"deto1qxqqen6lqmksmzmxmfqmxp2y8zvkldtcq8jhkzqflmyczepjw9dp46gc3cczu"}],"scid":"aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af", "sc_rpc":[{"name":"entrypoint","datatype":"S","value":"Withdraw"}, {"name":"amount","datatype":"U","value":2 }] }}' -H 'Content-Type: application/json';
  286. Eg: To transfer ownership of smart contract to new address/owner
  287. .. code-block:: php
  288. curl http://127.0.0.1:40403/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"transfer","params":{ "transfers":[{"amount":111111,"destination":"deto1qxqqen6lqmksmzmxmfqmxp2y8zvkldtcq8jhkzqflmyczepjw9dp46gc3cczu"}],"scid":"aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af", "sc_rpc":[{"name":"entrypoint","datatype":"S","value":"TransferOwnership"}, {"name":"newowner","datatype":"S","value":"deto1qxsplx7vzgydacczw6vnrtfh3fxqcjevyxcvlvl82fs8uykjkmaxgfgulfha5" }] }}' -H 'Content-Type: application/json';
  289. Eg: To claim ownership of smart contract
  290. .. code-block:: php
  291. curl http://127.0.0.1:40403/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"transfer","params":{ "transfers":[{"amount":111111,"destination":"deto1qxqqen6lqmksmzmxmfqmxp2y8zvkldtcq8jhkzqflmyczepjw9dp46gc3cczu"}],"scid":"aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af", "sc_rpc":[{"name":"entrypoint","datatype":"S","value":"ClaimOwnership"}] }}' -H 'Content-Type: application/json';
  292. Eg: To update smart contract code
  293. .. code-block:: php
  294. curl http://127.0.0.1:40403/json_rpc -d '{"jsonrpc":"2.0","id":"0","method":"transfer","params":{ "transfers":[{"amount":111111,"destination":"deto1qxqqen6lqmksmzmxmfqmxp2y8zvkldtcq8jhkzqflmyczepjw9dp46gc3cczu"}],"scid":"aacaa7bb2388d06e523e5bc0783e4e131738270641406c12978155ba033373af", "sc_rpc":[{"name":"entrypoint","datatype":"S","value":"UpdateCode"}, {"name":"code","datatype":"S","value":"new code should be placed here" }] }}' -H 'Content-Type: application/json';
  295. Token.bas
  296. --------------
  297. .. code-block:: php
  298. :linenos:
  299. /* Private Token Smart Contract Example in DVM-BASIC.
  300. DERO Smart Contracts Tokens privacy can be understood just like banks handle cash. Once cash is out from the bank, bank is not aware about it (who owns what value), until it is deposited back.
  301. Smart contract only maintains supply and other necessary items to keep it working.
  302. DERO Tokens can be tranfered to other wallets just like native DERO with Homomorphic Encryption and without involvement of issuing Smart Contracts.
  303. Token issuing Smart Contract cannot hold/freeze/control their tokens once they are issued and sent to any wallet.
  304. This token is Private. Use Function InitializePrivate() Uint64 to make any Smart Contract private.
  305. */
  306. // Issue tokens after depositing DERO (Convert DERO to TOKENX)
  307. Function IssueTOKENX() Uint64
  308. 10 ADD_VALUE(SIGNER(), DEROVALUE()) // Increment balance of user, without knowing original balance, this is done homomorphically
  309. 20 RETURN 0
  310. End Function
  311. // Convert TOKENX to DERO after depositing TOKENX. Smart Contract can give DERO, Only if DERO balance exists.
  312. Function ConvertTOKENX() Uint64
  313. 10 SEND_DERO_TO_ADDRESS(SIGNER(),TOKENVALUE()) // Increment balance of user, without knowing original balance, this is done using Homomorphic Encryption.
  314. 20 RETURN 0
  315. End Function
  316. // This function is used to initialize parameters during install time
  317. // InitializePrivate initializes a private SC
  318. Function InitializePrivate() Uint64
  319. 10 STORE("owner", SIGNER()) // Store in DB ["owner"] = address
  320. 20 ADD_VALUE(ADDRESS_RAW("deto1qxsplx7vzgydacczw6vnrtfh3fxqcjevyxcvlvl82fs8uykjkmaxgfgulfha5"), 1900000) // Gives initial encrypted balance.
  321. 30 ADD_VALUE(SIGNER(), 1600000) // Gives initial encrypted balance of 1600000.
  322. 40 RETURN 0
  323. End Function
  324. // This function is used to change owner
  325. // owner is an string form of address
  326. Function TransferOwnership(newowner String) Uint64
  327. 10 IF LOAD("owner") == SIGNER() THEN GOTO 30
  328. 20 RETURN 1
  329. 30 STORE("tmpowner",ADDRESS_RAW(newowner))
  330. 40 RETURN 0
  331. End Function
  332. // Until the new owner claims ownership, existing owner remains owner
  333. Function ClaimOwnership() Uint64
  334. 10 IF LOAD("tmpowner") == SIGNER() THEN GOTO 30
  335. 20 RETURN 1
  336. 30 STORE("owner",SIGNER()) // ownership claim successful
  337. 40 RETURN 0
  338. End Function
  339. // if signer is owner, withdraw any requested funds
  340. // if everthing is okay, they will be showing in signers wallet
  341. Function Withdraw( amount Uint64) Uint64
  342. 10 IF LOAD("owner") == SIGNER() THEN GOTO 30
  343. 20 RETURN 1
  344. 30 SEND_DERO_TO_ADDRESS(SIGNER(),amount)
  345. 40 RETURN 0
  346. End Function
  347. // if signer is owner, provide him rights to update code anytime
  348. // make sure update is always available to SC
  349. Function UpdateCode( code String) Uint64
  350. 10 IF LOAD("owner") == SIGNER() THEN GOTO 30
  351. 20 RETURN 1
  352. 30 UPDATE_SC_CODE(code)
  353. 40 RETURN 0
  354. End Function