Dero Stargate testnet for Smart Contracts.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

84 lignes
3.2KB

  1. /* Lotter Smart Contract in DVM-BASIC
  2. This lottery smart contract will give lottery wins every xth try.
  3. */
  4. Function Lottery(value Uint64) Uint64
  5. 10 dim deposit_count,winner as Uint64
  6. 20 LET deposit_count = LOAD("deposit_count")+1
  7. 25 IF value == 0 THEN GOTO 110 // if deposit amount is 0, simply return
  8. 30 STORE("depositor_address" + (deposit_count-1), SIGNER()) // store address for later on payment
  9. 40 STORE("deposit_total", LOAD("deposit_total") + value )
  10. 50 STORE("deposit_count",deposit_count)
  11. 60 IF LOAD("lotteryeveryXdeposit") > deposit_count THEN GOTO 110 // we will wait till X players join in
  12. // we are here means all players have joined in, roll the DICE,
  13. 70 LET winner = RANDOM() % deposit_count // we have a winner
  14. 80 SEND_DERO_TO_ADDRESS(LOAD("depositor_address" + winner) , LOAD("lotterygiveback")*LOAD("deposit_total")/10000)
  15. // re initialize for another round
  16. 90 STORE("deposit_count", 0) // initial players
  17. 100 STORE("deposit_total", 0) // total deposit of all players
  18. 110 RETURN 0
  19. End Function
  20. // this function is used to initialize parameters during install time
  21. Function Initialize() Uint64
  22. 10 STORE("owner", SIGNER()) // store in DB ["owner"] = address
  23. 20 STORE("lotteryeveryXdeposit", 2) // lottery will reward every X deposits
  24. // how much will lottery giveback in 1/10000 parts, granularity .01 %
  25. 30 STORE("lotterygiveback", 9900) // lottery will give reward 99% of deposits, 1 % is accumulated for owner to withdraw
  26. 33 STORE("deposit_count", 0) // initial players
  27. 34 STORE("deposit_total", 0) // total deposit of all players
  28. 35 printf "Initialize executed"
  29. 40 RETURN 0
  30. End Function
  31. // used to tune lottery parameters
  32. Function TuneLotteryParameters(input Uint64, lotteryeveryXdeposit Uint64, lotterygiveback Uint64) Uint64
  33. 10 dim key,stored_owner as String
  34. 20 dim value_uint64 as Uint64
  35. 30 IF ADDRESS_RAW(LOAD("owner")) == ADDRESS_RAW(SIGNER()) THEN GOTO 100 // check whether owner is real owner
  36. 40 RETURN 1
  37. 100 STORE("lotteryeveryXdeposit", lotteryeveryXdeposit) // lottery will reward every X deposits
  38. 130 STORE("lotterygiveback", value_uint64) // how much will lottery giveback in 1/10000 parts, granularity .01 %
  39. 140 RETURN 0 // return success
  40. End Function
  41. // this function is used to change owner
  42. // owner is an string form of address
  43. Function TransferOwnership(newowner String) Uint64
  44. 10 IF ADDRESS_RAW(LOAD("owner")) == ADDRESS_RAW(SIGNER()) THEN GOTO 30
  45. 20 RETURN 1
  46. 30 STORE("tmpowner",newowner)
  47. 40 RETURN 0
  48. End Function
  49. // until the new owner claims ownership, existing owner remains owner
  50. Function ClaimOwnership() Uint64
  51. 10 IF ADDRESS_RAW(LOAD("tmpowner")) == ADDRESS_RAW(SIGNER()) THEN GOTO 30
  52. 20 RETURN 1
  53. 30 STORE("owner",SIGNER()) // ownership claim successful
  54. 40 RETURN 0
  55. End Function
  56. // if signer is owner, withdraw any requested funds
  57. // if everthing is okay, thety will be showing in signers wallet
  58. Function Withdraw( amount Uint64) Uint64
  59. 10 IF ADDRESS_RAW(LOAD("owner")) == ADDRESS_RAW(SIGNER()) THEN GOTO 30
  60. 20 RETURN 1
  61. 30 SEND_DERO_TO_ADDRESS(SIGNER(),amount)
  62. 40 RETURN 0
  63. End Function