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 8b79bd6..0000000 Binary files a/Exchanger/exchanger.nef and /dev/null differ 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 fa72544..e4deddc 100755 Binary files a/Roulette/roulette.nef and b/Roulette/roulette.nef differ 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 76a4c1f..0c61d54 100755 Binary files a/SlotMashine/slot.nef and b/SlotMashine/slot.nef differ 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 0000000..8a516ef Binary files /dev/null and b/ZaCoinRefill/zaCoinRefill.nef differ diff --git a/ZaCoinRefill/zaCoinRefill.yml b/ZaCoinRefill/zaCoinRefill.yml new file mode 100644 index 0000000..43a9c9a --- /dev/null +++ b/ZaCoinRefill/zaCoinRefill.yml @@ -0,0 +1,11 @@ +name: ZaCoinRefill +supportedstandards: [] +events: + - name: "playerBalance" + parameters: + - name: "int" + type: "Integer" +permissions: + - methods: "*" + events: + - "playerBalance"