diff --git a/Craps/config.json b/Craps/config.json index bdb2011..760fef3 100755 --- a/Craps/config.json +++ b/Craps/config.json @@ -1 +1 @@ -{"name":"Craps","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":543,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false},{"name":"playCraps","offset":95,"parameters":[{"name":"bet","type":"Integer"},{"name":"firstSum","type":"Integer"},{"name":"secondSum","type":"Integer"}],"returntype":"Void","safe":false}],"events":[{"name":"Crup number","parameters":[{"name":"int","type":"Integer"}]},{"name":"Random number","parameters":[{"name":"int","type":"Integer"}]},{"name":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} +{"name":"Craps","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":540,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false},{"name":"playCraps","offset":95,"parameters":[{"name":"bet","type":"Integer"},{"name":"firstSum","type":"Integer"},{"name":"secondSum","type":"Integer"}],"returntype":"Void","safe":false}],"events":[{"name":"Crup number","parameters":[{"name":"int","type":"Integer"}]},{"name":"Random number","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/Craps/craps.go b/Craps/craps.go index 85e6479..632a250 100644 --- a/Craps/craps.go +++ b/Craps/craps.go @@ -32,6 +32,7 @@ func _deploy(data interface{}, isUpdate bool) { func PlayCraps(bet int, firstSum int, secondSum int) { ctx := storage.GetContext() playerOwner := runtime.GetScriptContainer().Sender + playerContract := runtime.GetExecutingScriptHash() if bet <= 0 { panic("Invalid bet amount") @@ -43,11 +44,14 @@ func PlayCraps(bet int, firstSum int, secondSum int) { panic("Insufficient funds") } + isWin := isWinner(firstSum, secondSum) if (isWin){ - changePlayerBalance(ctx, playerOwner, bet) + changePlayerBalance(playerContract, playerOwner, bet) + runtime.Notify("gameResult", int(1)) } else { - changePlayerBalance(ctx, playerOwner, -bet) + changePlayerBalance(playerOwner, playerContract, bet) + runtime.Notify("gameResult", int(0)) } playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) runtime.Notify("playerBalance", playerBalance) @@ -61,7 +65,6 @@ func isWinner(firstSum int, secondSum int) bool { sum := 0 for i:=0; i<2; i++ { crap := (runtime.GetRandom() % 6) + 1 - runtime.Log("Crup number " + string(i+1) + " Rundom number " + string(crap)) runtime.Notify("Crup number", i+1) runtime.Notify("Random number", crap) sum += crap @@ -81,23 +84,11 @@ func OnNEP17Payment(from interop.Hash160, amount int, data any) { } } -func changePlayerBalance(ctx storage.Context, playerOwner interop.Hash160, balanceChange int) { - zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) - playerContract := runtime.GetExecutingScriptHash() +func changePlayerBalance(sender interop.Hash160, recipient interop.Hash160, balanceChange int) { + ctx := storage.GetContext() + zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) - var from, to interop.Hash160 - var transferAmount int - if balanceChange > 0 { - from = playerContract - to = playerOwner - transferAmount = balanceChange - } else { - from = playerOwner - to = playerContract - transferAmount = -balanceChange - } - - transferred := contract.Call(zaCoinHash, "transfer", contract.All, from, to, transferAmount, nil).(bool) + transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool) if !transferred { panic("failed to transfer zaCoins") } diff --git a/Craps/craps.nef b/Craps/craps.nef index 8f5090d..b04d7ce 100755 Binary files a/Craps/craps.nef and b/Craps/craps.nef differ diff --git a/Craps/craps.yml b/Craps/craps.yml index ec88586..adc6f9c 100644 --- a/Craps/craps.yml +++ b/Craps/craps.yml @@ -9,6 +9,10 @@ events: parameters: - name: "int" type: "Integer" + - name: "gameResult" + parameters: + - name: "int" + type: "Integer" - name: "playerBalance" parameters: - name: "int" @@ -19,3 +23,4 @@ permissions: - "Crup number" - "Random number" - "playerBalance" + - "gameResult" diff --git a/Exchanger/config.json b/Exchanger/config.json new file mode 100755 index 0000000..a620b5f --- /dev/null +++ b/Exchanger/config.json @@ -0,0 +1 @@ +{"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 new file mode 100755 index 0000000..8b79bd6 Binary files /dev/null and b/Exchanger/exchanger.nef differ diff --git a/ZaCoinRefill/go.mod b/Exchanger/go.mod similarity index 100% rename from ZaCoinRefill/go.mod rename to Exchanger/go.mod diff --git a/ZaCoinRefill/go.sum b/Exchanger/go.sum similarity index 100% rename from ZaCoinRefill/go.sum rename to Exchanger/go.sum diff --git a/Exchanger/main.go b/Exchanger/main.go new file mode 100644 index 0000000..a4ff4fb --- /dev/null +++ b/Exchanger/main.go @@ -0,0 +1,111 @@ +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 new file mode 100644 index 0000000..9946e3b --- /dev/null +++ b/Exchanger/neo-go.yml @@ -0,0 +1,16 @@ +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/RPS/config.json b/RPS/config.json index c744d05..a7533bc 100644 --- a/RPS/config.json +++ b/RPS/config.json @@ -1 +1 @@ -{"name":"rps","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":687,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false},{"name":"playRPS","offset":95,"parameters":[{"name":"playerChoice","type":"String"},{"name":"bet","type":"Integer"}],"returntype":"Void","safe":false}],"events":[{"name":"computerChoice","parameters":[{"name":"str","type":"String"}]},{"name":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file +{"name":"rps","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":538,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false},{"name":"playRPS","offset":95,"parameters":[{"name":"playerChoice","type":"Integer"},{"name":"bet","type":"Integer"}],"returntype":"Void","safe":false}],"events":[{"name":"computerChoice","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/RPS/rps.go b/RPS/rps.go index 44dc221..4726039 100644 --- a/RPS/rps.go +++ b/RPS/rps.go @@ -11,11 +11,6 @@ const ( zaCoinHashKey = "zaCoinHash" ) -type Result struct { - win bool - tie bool - lose bool -} func _deploy(data interface{}, isUpdate bool) { if isUpdate { @@ -34,10 +29,11 @@ func _deploy(data interface{}, isUpdate bool) { storage.Put(ctx, zaCoinHashKey, args.zaCoinHash) } -func PlayRPS(playerChoice string, bet int) { +func PlayRPS(playerChoice int, bet int) { ctx := storage.GetContext() playerOwner := runtime.GetScriptContainer().Sender + playerContract := runtime.GetExecutingScriptHash() if bet <= 0 { panic("Invalid bet amount") @@ -48,57 +44,49 @@ func PlayRPS(playerChoice string, bet int) { panic("Insufficient funds") } - if playerChoice != "rock" && playerChoice != "paper" && playerChoice != "scissors" { + if playerChoice <= 0 || playerChoice > 3 { panic("invalid player choice") } computerChoice := (runtime.GetRandom() % 3) + 1 + runtime.Notify("computerChoice", computerChoice) - var computerChoiceString string - switch computerChoice { - case 0: - computerChoiceString = "rock" - case 1: - computerChoiceString = "paper" - case 2: - computerChoiceString = "scissors" - } + result := isWinner(playerChoice, computerChoice) - runtime.Notify("computerChoice", computerChoiceString) - - result := isWinner(playerChoice, computerChoiceString) - - if result.tie { - runtime.Log("game tied: player chose " + playerChoice + ", computer chose " + computerChoiceString) - } else if result.win { - changePlayerBalance(ctx, playerOwner, bet) - } else { - runtime.Log("player lost: player chose " + playerChoice + ", computer chose " + computerChoiceString) + if result == 1 { + changePlayerBalance(playerContract, playerOwner, bet) + runtime.Notify("gameResult", result) + } else if result == 0 { + changePlayerBalance(playerOwner, playerContract, bet) + runtime.Notify("gameResult", result) + } else { + runtime.Log("game tied") + runtime.Notify("gameResult", result) } playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) - runtime.Notify("playerBalance", playerBalance) + runtime.Notify("playerBalance", playerBalance) } -func isWinner(playerChoice, computerChoice string) Result { +func isWinner(playerChoice int, computerChoice int) int { if playerChoice == computerChoice { - return Result{tie: true} + return 2 } - if playerChoice == "rock" && computerChoice == "scissors" { - return Result{win: true} + if playerChoice == 1 && computerChoice == 3 { + return 1 } - if playerChoice == "scissors" && computerChoice == "paper" { - return Result{win: true} + if playerChoice == 3 && computerChoice == 2 { + return 1 } - if playerChoice == "paper" && computerChoice == "rock" { - return Result{win: true} + if playerChoice == 2 && computerChoice == 1 { + return 1 } - return Result{lose: true} + return 0 } func OnNEP17Payment(from interop.Hash160, amount int, data any) { @@ -111,24 +99,12 @@ func OnNEP17Payment(from interop.Hash160, amount int, data any) { } } -func changePlayerBalance(ctx storage.Context, playerOwner interop.Hash160, balanceChange int) { - zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) - playerContract := runtime.GetExecutingScriptHash() +func changePlayerBalance(sender interop.Hash160, recipient interop.Hash160, balanceChange int) { + ctx := storage.GetContext() + zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) - var from, to interop.Hash160 - var transferAmount int - if balanceChange > 0 { - from = playerContract - to = playerOwner - transferAmount = balanceChange - } else { - from = playerOwner - to = playerContract - transferAmount = -balanceChange - } - - transferred := contract.Call(zaCoinHash, "transfer", contract.All, from, to, transferAmount, nil).(bool) - if !transferred { - panic("failed to transfer zaCoins") - } + transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool) + if !transferred { + panic("failed to transfer zaCoins") + } } diff --git a/RPS/rps.nef b/RPS/rps.nef index 9773565..c21168c 100644 Binary files a/RPS/rps.nef and b/RPS/rps.nef differ diff --git a/RPS/rps.yml b/RPS/rps.yml index 2da2783..4fc80fa 100644 --- a/RPS/rps.yml +++ b/RPS/rps.yml @@ -3,8 +3,12 @@ supportedstandards: [] events: - name: computerChoice parameters: - - name: str - type: String + - name: int + type: Integer + - name: gameResult + parameters: + - name: int + type: Integer - name: playerBalance parameters: - name: int @@ -14,3 +18,4 @@ permissions: events: - computerChoice - playerBalance + - gameResult diff --git a/Roulette/Roulette.go b/Roulette/Roulette.go index 9a46ef6..2191db7 100644 --- a/Roulette/Roulette.go +++ b/Roulette/Roulette.go @@ -31,6 +31,7 @@ func _deploy(data interface{}, isUpdate bool) { func PlayRoulette(bet int, selectedNumber int) { ctx := storage.GetContext() playerOwner := runtime.GetScriptContainer().Sender + playerContract := runtime.GetExecutingScriptHash() if bet <= 0 { panic("Invalid bet amount") @@ -48,9 +49,11 @@ func PlayRoulette(bet int, selectedNumber int) { isWin := isWinner(selectedNumber) if isWin { winAmount := calculateWinAmount(bet, selectedNumber) - changePlayerBalance(ctx, playerOwner, winAmount) + changePlayerBalance(zaCoinHash, playerContract, playerOwner, winAmount) + runtime.Notify("gameResult", int(1)) } else { - changePlayerBalance(ctx, playerOwner, -bet) + changePlayerBalance(zaCoinHash, playerOwner, playerContract, bet) + runtime.Notify("gameResult", int(0)) } playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) runtime.Notify("playerBalance", playerBalance) @@ -87,24 +90,10 @@ func OnNEP17Payment(from interop.Hash160, amount int, data any) { } } -func changePlayerBalance(ctx storage.Context, playerOwner interop.Hash160, balanceChange int) { - zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) - playerContract := runtime.GetExecutingScriptHash() +func changePlayerBalance(zaCoinHash interop.Hash160, sender interop.Hash160, recipient interop.Hash160, balanceChange int) { - var from, to interop.Hash160 - var transferAmount int - if balanceChange > 0 { - from = playerContract - to = playerOwner - transferAmount = balanceChange - } else { - from = playerOwner - to = playerContract - transferAmount = -balanceChange - } - - transferred := contract.Call(zaCoinHash, "transfer", contract.All, from, to, transferAmount, nil).(bool) - if !transferred { - panic("failed to transfer zaCoins") - } + transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool) + if !transferred { + panic("failed to transfer zaCoins") + } } diff --git a/Roulette/config.json b/Roulette/config.json index 2eac8fe..4bb5e69 100644 --- a/Roulette/config.json +++ b/Roulette/config.json @@ -1,2 +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":473,"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":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} - +{"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 diff --git a/Roulette/roulette.nef b/Roulette/roulette.nef index c411147..fa72544 100755 Binary files a/Roulette/roulette.nef and b/Roulette/roulette.nef differ diff --git a/Roulette/roulette.yml b/Roulette/roulette.yml index a9ec3e6..14c886e 100644 --- a/Roulette/roulette.yml +++ b/Roulette/roulette.yml @@ -1,10 +1,14 @@ - name: Roulette +name: Roulette supportedstandards: [] events: - name: "rouletteNumber" parameters: - name: "int" type: "Integer" + - name: "gameResult" + parameters: + - name: "int" + type: "Integer" - name: "playerBalance" parameters: - name: "int" @@ -13,4 +17,5 @@ permissions: - methods: '*' events: - "rouletteNumber" + - "gameResult" - "playerBalance" diff --git a/SlotMashine/config.json b/SlotMashine/config.json index 7964377..fdac4d4 100755 --- a/SlotMashine/config.json +++ b/SlotMashine/config.json @@ -1,2 +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":457,"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":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} - +{"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 diff --git a/SlotMashine/config.yml b/SlotMashine/config.yml index 659266b..c75f258 100644 --- a/SlotMashine/config.yml +++ b/SlotMashine/config.yml @@ -5,6 +5,10 @@ events: parameters: - name: array type: Array + - name: gameResult + parameters: + - name: int + type: Integer - name: playerBalance parameters: - name: int @@ -13,4 +17,5 @@ permissions: - methods: '*' events: - SlotResult + - gameResult - playerBalance diff --git a/SlotMashine/slot.go b/SlotMashine/slot.go index 2089b02..5a1c8f5 100644 --- a/SlotMashine/slot.go +++ b/SlotMashine/slot.go @@ -33,6 +33,7 @@ func _deploy(data interface{}, isUpdate bool) { func RollSlot(bet int) { ctx := storage.GetContext() playerOwner := runtime.GetScriptContainer().Sender + playerContract := runtime.GetExecutingScriptHash() if bet <= 0 { panic("Invalid bet amount") @@ -46,10 +47,12 @@ func RollSlot(bet int) { res := roll() if (res == 0){ - changePlayerBalance(ctx, playerOwner, -bet) + changePlayerBalance(zaCoinHash, playerOwner, playerContract, bet) + runtime.Notify("gameResult", int(0)) } else { win := res * bet - changePlayerBalance(ctx, playerOwner, win) + changePlayerBalance(zaCoinHash, playerContract, playerOwner, win) + runtime.Notify("gameResult", int(1)) } playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) runtime.Notify("playerBalance", playerBalance) @@ -84,24 +87,10 @@ func OnNEP17Payment(from interop.Hash160, amount int, data any) { } } -func changePlayerBalance(ctx storage.Context, playerOwner interop.Hash160, balanceChange int) { - zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) - playerContract := runtime.GetExecutingScriptHash() +func changePlayerBalance(zaCoinHash interop.Hash160, sender interop.Hash160, recipient interop.Hash160, balanceChange int) { - var from, to interop.Hash160 - var transferAmount int - if balanceChange > 0 { - from = playerContract - to = playerOwner - transferAmount = balanceChange - } else { - from = playerOwner - to = playerContract - transferAmount = -balanceChange - } - - transferred := contract.Call(zaCoinHash, "transfer", contract.All, from, to, transferAmount, nil).(bool) - if !transferred { - panic("failed to transfer zaCoins") - } + transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool) + if !transferred { + panic("failed to transfer zaCoins") + } } diff --git a/SlotMashine/slot.nef b/SlotMashine/slot.nef index e8c8c38..76a4c1f 100755 Binary files a/SlotMashine/slot.nef and b/SlotMashine/slot.nef differ diff --git a/ZaCoinRefill/config.json b/ZaCoinRefill/config.json deleted file mode 100755 index 5ab9704..0000000 --- a/ZaCoinRefill/config.json +++ /dev/null @@ -1 +0,0 @@ -{"name":"Exchanger","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/ZaCoinRefill/exchanger.nef b/ZaCoinRefill/exchanger.nef deleted file mode 100755 index 8d32c2c..0000000 Binary files a/ZaCoinRefill/exchanger.nef and /dev/null differ diff --git a/ZaCoinRefill/zaCoinRefill.go b/ZaCoinRefill/zaCoinRefill.go deleted file mode 100644 index 56c0eda..0000000 --- a/ZaCoinRefill/zaCoinRefill.go +++ /dev/null @@ -1,68 +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" -) - -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 deleted file mode 100755 index 8a516ef..0000000 Binary files a/ZaCoinRefill/zaCoinRefill.nef and /dev/null differ diff --git a/ZaCoinRefill/zaCoinRefill.yml b/ZaCoinRefill/zaCoinRefill.yml deleted file mode 100644 index 6aeebf4..0000000 --- a/ZaCoinRefill/zaCoinRefill.yml +++ /dev/null @@ -1,11 +0,0 @@ -name: Exchanger -supportedstandards: [] -events: - - name: "playerBalance" - parameters: - - name: "int" - type: "Integer" -permissions: - - methods: "*" - events: - - "playerBalance"