From 7ce8dbb9c859f199125332b4a83db26e8860b969 Mon Sep 17 00:00:00 2001 From: shashkevichfrida Date: Tue, 16 Jan 2024 20:45:30 +0300 Subject: [PATCH] refactor slot, roulette --- Exchanger/config.json | 1 - Exchanger/exchanger.nef | Bin 517 -> 0 bytes Exchanger/main.go | 111 ----------------------------- Exchanger/neo-go.yml | 16 ----- Roulette/Roulette.go | 29 ++++---- Roulette/config.json | 2 +- Roulette/roulette.nef | Bin 794 -> 854 bytes SlotMashine/config.json | 2 +- SlotMashine/slot.go | 8 ++- SlotMashine/slot.nef | Bin 778 -> 809 bytes ZaCoinRefill/config.json | 1 + {Exchanger => ZaCoinRefill}/go.mod | 4 +- {Exchanger => ZaCoinRefill}/go.sum | 0 ZaCoinRefill/zaCoinRefill.go | 68 ++++++++++++++++++ ZaCoinRefill/zaCoinRefill.nef | Bin 0 -> 536 bytes ZaCoinRefill/zaCoinRefill.yml | 11 +++ 16 files changed, 104 insertions(+), 149 deletions(-) delete mode 100755 Exchanger/config.json delete mode 100755 Exchanger/exchanger.nef delete mode 100644 Exchanger/main.go delete mode 100644 Exchanger/neo-go.yml create mode 100755 ZaCoinRefill/config.json rename {Exchanger => ZaCoinRefill}/go.mod (73%) rename {Exchanger => ZaCoinRefill}/go.sum (100%) create mode 100644 ZaCoinRefill/zaCoinRefill.go create mode 100755 ZaCoinRefill/zaCoinRefill.nef create mode 100644 ZaCoinRefill/zaCoinRefill.yml diff --git a/Exchanger/config.json b/Exchanger/config.json deleted file mode 100755 index a620b5f..0000000 --- a/Exchanger/config.json +++ /dev/null @@ -1 +0,0 @@ -{"name":"Exchanger","abi":{"methods":[{"name":"_initialize","offset":0,"parameters":[],"returntype":"Void","safe":false},{"name":"_deploy","offset":31,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"buyZaCoin","offset":126,"parameters":[{"name":"count","type":"Integer"},{"name":"player","type":"Hash160"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":297,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false}],"events":[{"name":"balanceAfter","parameters":[{"name":"int","type":"Integer"}]},{"name":"balanceUser","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file diff --git a/Exchanger/exchanger.nef b/Exchanger/exchanger.nef deleted file mode 100755 index 8b79bd6514ac53b88717757960d1060e5f56158a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 517 zcmeZsbu-RO&DTxO*EP^HG%(gPWFP}DoiBUTeSuBK%f0V$XdYv8Qz{tqJ@VAJON2EbI%AnA#-Eq_6dZ!003fb#}9;tZU zG*Dr=ZQ$w<&csxy#_Ukxb<+_j;8>6$aL((73hOB)7FPiV5e*d{dC$DE#GK3&g^a}F z42AqOg{nm7{LDOs9A|$^KUbK^!v&G>04f6-672ZQUZTYzoP)_xH;}_X z_*_AT;}7*WJ*(6-c~k;&5-U@S6f%ny^72a*N-|OviW75F6%vcV?Bv9poYW#K$J_r_ zloV!UR&a-yGcXiotx}Wb5pzq-%t=jAD9KlV1%*O-VlhN*Nsc)qLvgm?fl?kGXgD~g zm82Hs1~^V-%umb!%9iB`9w=w$;f6|w7N-{FvqPkTA;Ao`vH+y8kVn}W>Ou8l1*md` p;STr1<~< diff --git a/Exchanger/main.go b/Exchanger/main.go deleted file mode 100644 index a4ff4fb..0000000 --- a/Exchanger/main.go +++ /dev/null @@ -1,111 +0,0 @@ -package Exchanger - -import ( - "github.com/nspcc-dev/neo-go/pkg/interop" - "github.com/nspcc-dev/neo-go/pkg/interop/contract" - "github.com/nspcc-dev/neo-go/pkg/interop/runtime" - "github.com/nspcc-dev/neo-go/pkg/interop/storage" - "github.com/nspcc-dev/neo-go/pkg/interop/native/gas" - "github.com/nspcc-dev/neo-go/pkg/interop/lib/address" - //"github.com/nspcc-dev/neo-go/pkg/interop/util" -) - -const ( - zaCoinHashKey = "zaCoinHash" -) - -var walletAddress = address.ToHash160("NXbLSnHA8dNuMUPUSNNivx7XFucN1w5bRq") - -func _deploy(data interface{}, isUpdate bool) { - if isUpdate { - return - } - - args := data.(struct { - zaCoinHash interop.Hash160 - }) - - if len(args.zaCoinHash) != interop.Hash160Len { - panic("Invalid hash of zaCoin contract") - } - - ctx := storage.GetContext() - storage.Put(ctx, zaCoinHashKey, args.zaCoinHash) -} - -func BuyZaCoin(count int, player interop.Hash160) { - - playerOwner := runtime.GetScriptContainer().Sender - - if !runtime.CheckWitness(playerOwner) { - panic("Player is not the same as the caller") - } - contractHash := runtime.GetExecutingScriptHash() - - - transferredGas := gas.Transfer(playerOwner, contractHash, count, nil) - - if !transferredGas { - panic("Failed to transfer gas") - } - - balanceAfter := gas.BalanceOf(runtime.GetExecutingScriptHash()) - runtime.Notify("balanceAfter", balanceAfter) - balanceUser := gas.BalanceOf(playerOwner) - runtime.Notify("balanceUser", balanceUser) - //balanceBefore := gas.BalanceOf(runtime.GetExecutingScriptHash()) - //gasTransfer(player, count) - //balanceAfter := gas.BalanceOf(runtime.GetExecutingScriptHash()) - - //changePlayerBalance(ctx, playerOwner, count) - //if (balanceBefore - balanceAfter == count) { - // changePlayerBalance(ctx, playerOwner, count) - //} else { - // util.Abort() - //} -} - -func OnNEP17Payment(from interop.Hash160, amount int, data any) { - contractHash := runtime.GetExecutingScriptHash() - balance := gas.BalanceOf(contractHash) - runtime.Log("Contract's balance changed and has "+ string(balance)+" gas") -} - -func gasTransfer(playerOwner interop.Hash160, gasCount int) { - if !runtime.CheckWitness(playerOwner) { - panic("Player is not the same as the caller") - } - contractHash := runtime.GetExecutingScriptHash() - - - transferredGas := gas.Transfer(playerOwner, contractHash, gasCount, nil) - - if !transferredGas { - panic("Failed to transfer gas") - } -} - - -func changePlayerBalance(ctx storage.Context, playerOwner interop.Hash160, balanceChange int) { - zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) - playerContract := runtime.GetExecutingScriptHash() - - var from, to interop.Hash160 - var transferAmount int - if balanceChange > 0 { - // Transfer funds from contract to player owner - from = playerContract - to = playerOwner - transferAmount = balanceChange - } else { - // Transfer funds from player owner to contract - from = playerOwner - to = playerContract - transferAmount = -balanceChange // We flip sender/receiver, but keep amount positive - } - - transferred := contract.Call(zaCoinHash, "transfer", contract.All, from, to, transferAmount, "zacoin").(bool) - if !transferred { - panic("failed to transfer zaCoins") - } -} diff --git a/Exchanger/neo-go.yml b/Exchanger/neo-go.yml deleted file mode 100644 index 9946e3b..0000000 --- a/Exchanger/neo-go.yml +++ /dev/null @@ -1,16 +0,0 @@ -name: Exchanger -supportedstandards: [] -events: - - name: "balanceAfter" - parameters: - - name: "int" - type: "Integer" - - name: "balanceUser" - parameters: - - name: "int" - type: "Integer" -permissions: - - methods: '*' - events: - - "balanceAfter" - - "balanceUser" diff --git a/Roulette/Roulette.go b/Roulette/Roulette.go index 2191db7..81cdfc0 100644 --- a/Roulette/Roulette.go +++ b/Roulette/Roulette.go @@ -49,10 +49,10 @@ func PlayRoulette(bet int, selectedNumber int) { isWin := isWinner(selectedNumber) if isWin { winAmount := calculateWinAmount(bet, selectedNumber) - changePlayerBalance(zaCoinHash, playerContract, playerOwner, winAmount) + changePlayerBalance(playerContract, playerOwner, winAmount) runtime.Notify("gameResult", int(1)) } else { - changePlayerBalance(zaCoinHash, playerOwner, playerContract, bet) + changePlayerBalance(playerOwner, playerContract, bet) runtime.Notify("gameResult", int(0)) } playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) @@ -67,17 +67,16 @@ func isWinner(selectedNumber int) bool { } func calculateWinAmount(bet int, selectedNumber int) int { - coefficients := map[int]int{ - 1: 36, - 2: 18, - 3: 2, - } - - if coefficient, ok := coefficients[selectedNumber]; ok { - return bet * coefficient - } - - return 0 + coefs := []int{10, 20, 30, 2} + if selectedNumber == 36 { + return bet * coefs[0] + } else if selectedNumber == 18 { + return bet * coefs[1] + } else if selectedNumber == 2 { + return bet * coefs[2] + } else { + return bet * coefs[3] + } } func OnNEP17Payment(from interop.Hash160, amount int, data any) { @@ -90,7 +89,9 @@ func OnNEP17Payment(from interop.Hash160, amount int, data any) { } } -func changePlayerBalance(zaCoinHash interop.Hash160, sender interop.Hash160, recipient interop.Hash160, balanceChange int) { +func changePlayerBalance(sender interop.Hash160, recipient interop.Hash160, balanceChange int) { + ctx := storage.GetContext() + zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool) if !transferred { diff --git a/Roulette/config.json b/Roulette/config.json index 4bb5e69..aac602f 100644 --- a/Roulette/config.json +++ b/Roulette/config.json @@ -1 +1 @@ -{"name":"Roulette","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":535,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false},{"name":"playRoulette","offset":95,"parameters":[{"name":"bet","type":"Integer"},{"name":"selectedNumber","type":"Integer"}],"returntype":"Void","safe":false}],"events":[{"name":"rouletteNumber","parameters":[{"name":"int","type":"Integer"}]},{"name":"gameResult","parameters":[{"name":"int","type":"Integer"}]},{"name":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file +{"name":"Roulette","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":562,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false},{"name":"playRoulette","offset":95,"parameters":[{"name":"bet","type":"Integer"},{"name":"selectedNumber","type":"Integer"}],"returntype":"Void","safe":false}],"events":[{"name":"rouletteNumber","parameters":[{"name":"int","type":"Integer"}]},{"name":"gameResult","parameters":[{"name":"int","type":"Integer"}]},{"name":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file diff --git a/Roulette/roulette.nef b/Roulette/roulette.nef index fa72544260964a9f34dfbbb39ce6d4a72c2f4fd0..e4deddca74ea311a4081a6bb5c38a94f41274e51 100755 GIT binary patch delta 160 zcmbQmc8zU<7ZV%vMjuf|$(alc3}tz0h82|orZ<7?a`vpueD+||vdPtqvg|6ESrx&i zL6a9St}th05@L{J5Rno&P*BOBqQzB_A#iSigOXw;gAj-#2;zuTg4seKHb+H{ z#8fmng-KbyqOyuRMC3qmCJ#qRQDR2MQ~A jI7*5V^NQ0_i{vvy9FuCJgI297$e3)%tjLn9>+l%>;x;Hq diff --git a/SlotMashine/config.json b/SlotMashine/config.json index fdac4d4..8cb1d16 100755 --- a/SlotMashine/config.json +++ b/SlotMashine/config.json @@ -1 +1 @@ -{"name":"SlotMashine","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":519,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false},{"name":"rollSlot","offset":95,"parameters":[{"name":"bet","type":"Integer"}],"returntype":"Void","safe":false}],"events":[{"name":"SlotResult","parameters":[{"name":"array","type":"Array"}]},{"name":"gameResult","parameters":[{"name":"int","type":"Integer"}]},{"name":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file +{"name":"SlotMashine","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":517,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false},{"name":"rollSlot","offset":95,"parameters":[{"name":"bet","type":"Integer"}],"returntype":"Void","safe":false}],"events":[{"name":"SlotResult","parameters":[{"name":"array","type":"Array"}]},{"name":"gameResult","parameters":[{"name":"int","type":"Integer"}]},{"name":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file diff --git a/SlotMashine/slot.go b/SlotMashine/slot.go index 5a1c8f5..8506927 100644 --- a/SlotMashine/slot.go +++ b/SlotMashine/slot.go @@ -47,11 +47,11 @@ func RollSlot(bet int) { res := roll() if (res == 0){ - changePlayerBalance(zaCoinHash, playerOwner, playerContract, bet) + changePlayerBalance(playerOwner, playerContract, bet) runtime.Notify("gameResult", int(0)) } else { win := res * bet - changePlayerBalance(zaCoinHash, playerContract, playerOwner, win) + changePlayerBalance(playerContract, playerOwner, win) runtime.Notify("gameResult", int(1)) } playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) @@ -87,7 +87,9 @@ func OnNEP17Payment(from interop.Hash160, amount int, data any) { } } -func changePlayerBalance(zaCoinHash interop.Hash160, sender interop.Hash160, recipient interop.Hash160, balanceChange int) { +func changePlayerBalance(sender interop.Hash160, recipient interop.Hash160, balanceChange int) { + ctx := storage.GetContext() + zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool) if !transferred { diff --git a/SlotMashine/slot.nef b/SlotMashine/slot.nef index 76a4c1fc9ba5ba35e898cc2084279f132bd0e016..0c61d5455bd4255e1a364287d3d49ec37a26508b 100755 GIT binary patch delta 94 zcmV-k0HObi2B`*+N&(rCOXU!j0001XZV-~LDsFh7cL-`}ZwOPf zY5`gSlT-pDA9#6s3se-qb7>3+baG*Cb7p07A8S-WVtrdus;Y8oljQ;-1erUdY0R!5 AUH||9 delta 94 zcmV-k0HOb>28srdN&&i&OXU%m0001XZV zRI_LSS^*&f1bKRU3se-qad->}baG*Cb7p07A8AxUVtrdus;Y2klLP}H1eQ;No4Z0E AV*mgE diff --git a/ZaCoinRefill/config.json b/ZaCoinRefill/config.json new file mode 100755 index 0000000..6f4f5f8 --- /dev/null +++ b/ZaCoinRefill/config.json @@ -0,0 +1 @@ +{"name":"ZaCoinRefill","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"getZaCoin","offset":95,"parameters":[],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":244,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false}],"events":[{"name":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file diff --git a/Exchanger/go.mod b/ZaCoinRefill/go.mod similarity index 73% rename from Exchanger/go.mod rename to ZaCoinRefill/go.mod index 924c069..3e726d0 100644 --- a/Exchanger/go.mod +++ b/ZaCoinRefill/go.mod @@ -1,5 +1,5 @@ -module Exchanger +module ZaCoinRefill -go 1.21.5 +go 1.21.6 require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231020160724-c3955f87d1b5 diff --git a/Exchanger/go.sum b/ZaCoinRefill/go.sum similarity index 100% rename from Exchanger/go.sum rename to ZaCoinRefill/go.sum diff --git a/ZaCoinRefill/zaCoinRefill.go b/ZaCoinRefill/zaCoinRefill.go new file mode 100644 index 0000000..726febc --- /dev/null +++ b/ZaCoinRefill/zaCoinRefill.go @@ -0,0 +1,68 @@ +package ZaCoinRefill + +import ( + "github.com/nspcc-dev/neo-go/pkg/interop" + "github.com/nspcc-dev/neo-go/pkg/interop/contract" + "github.com/nspcc-dev/neo-go/pkg/interop/runtime" + "github.com/nspcc-dev/neo-go/pkg/interop/storage" +) + +const ( + zaCoinHashKey = "zaCoinHash" + minZaCoin = 5 + zaCoinForTransfer = 50 +) + + +func _deploy(data interface{}, isUpdate bool) { + if isUpdate { + return + } + + args := data.(struct { + zaCoinHash interop.Hash160 + }) + + if len(args.zaCoinHash) != interop.Hash160Len { + panic("Invalid hash of zaCoin contract") + } + + ctx := storage.GetContext() + storage.Put(ctx, zaCoinHashKey, args.zaCoinHash) +} + +func GetZaCoin() { + ctx := storage.GetContext() + playerOwner := runtime.GetScriptContainer().Sender + + zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) + playerBalance := contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) + if playerBalance < minZaCoin { + changePlayerBalance(ctx, playerOwner, zaCoinForTransfer) + } + + playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) + runtime.Notify("playerBalance", playerBalance) + +} + +func OnNEP17Payment(from interop.Hash160, amount int, data any) { + ctx := storage.GetContext() + zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) + + callingHash := runtime.GetCallingScriptHash() + if !callingHash.Equals(zaCoinHash) { + panic("only ZC is accepted") + } +} + + +func changePlayerBalance(ctx storage.Context, playerOwner interop.Hash160, transferAmount int) { + zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) + playerContract := runtime.GetExecutingScriptHash() + + transferred := contract.Call(zaCoinHash, "transfer", contract.All, playerContract, playerOwner, transferAmount, nil).(bool) + if !transferred { + panic("failed to transfer zaCoins") + } +} diff --git a/ZaCoinRefill/zaCoinRefill.nef b/ZaCoinRefill/zaCoinRefill.nef new file mode 100755 index 0000000000000000000000000000000000000000..8a516effd0f84c5fafd33d11bb9b952d2ff487dd GIT binary patch literal 536 zcmeZsbu-RO&DTxO*EP^HG%(gPWFQaxJ;4~x#8j!q>`>u#(@}-xwqrqtz&WoQDy*lJ zSX>1dL^M=*5OFvhb$-@Pa z@Bk_U8WQaI%wD3!A)K87EMMTL8^~cGe6BD9SzUnRq}UgRUN;SZRvQ#$3LYrQ;^9n6 z%t_2kPW4X{%n5N!s*Mivx~T}1S1isJ)#J&?WH1Ug-2n71P<1Ja>RdF{f(OcYcnfk8 zD^rV{AV%f|I8J5EPjm=pVTL#gw_^$&ErauHin5BcZ>X_l-cVxZR8h#jp(2@iLq#;( z>!t?KK^mD*dOA>6iJ4=Sngow