diff --git a/Craps/config.json b/Craps/config.json index 7e12d09..bdb2011 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":311,"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"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file +{"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} diff --git a/Craps/craps.go b/Craps/craps.go index fd2c48e..85e6479 100644 --- a/Craps/craps.go +++ b/Craps/craps.go @@ -16,7 +16,6 @@ func _deploy(data interface{}, isUpdate bool) { return } - // Parse hash of forint contract from incoming data args := data.(struct { zaCoinHash interop.Hash160 }) @@ -33,12 +32,25 @@ func _deploy(data interface{}, isUpdate bool) { func PlayCraps(bet int, firstSum int, secondSum int) { ctx := storage.GetContext() playerOwner := runtime.GetScriptContainer().Sender + + if bet <= 0 { + panic("Invalid bet amount") + } + + zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) + playerBalance := contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) + if playerBalance < bet { + panic("Insufficient funds") + } + isWin := isWinner(firstSum, secondSum) if (isWin){ changePlayerBalance(ctx, playerOwner, bet) } else { changePlayerBalance(ctx, playerOwner, -bet) } + playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) + runtime.Notify("playerBalance", playerBalance) } func isWinner(firstSum int, secondSum int) bool { @@ -49,6 +61,7 @@ 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 @@ -75,15 +88,13 @@ func changePlayerBalance(ctx storage.Context, playerOwner interop.Hash160, balan 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 + transferAmount = -balanceChange } transferred := contract.Call(zaCoinHash, "transfer", contract.All, from, to, transferAmount, nil).(bool) diff --git a/Craps/craps.yml b/Craps/craps.yml index de93ea8..ec88586 100644 --- a/Craps/craps.yml +++ b/Craps/craps.yml @@ -9,7 +9,13 @@ events: parameters: - name: "int" type: "Integer" + - name: "playerBalance" + parameters: + - name: "int" + type: "Integer" permissions: - methods: '*' events: - "Crup number" + - "Random number" + - "playerBalance" diff --git a/Exchanger/config.json b/Exchanger/config.json new file mode 100755 index 0000000..b76b7b6 --- /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"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":161,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false}],"events":[]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null} \ No newline at end of file diff --git a/Exchanger/exchanger.go b/Exchanger/exchanger.go new file mode 100644 index 0000000..fd5e578 --- /dev/null +++ b/Exchanger/exchanger.go @@ -0,0 +1,103 @@ +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) { + ctx := storage.GetContext() + playerOwner := runtime.GetScriptContainer().Sender + + //balanceBefore := gas.BalanceOf(runtime.GetExecutingScriptHash()) + gasTransfer(playerOwner, 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) { + zacoinStr := "zacoin" + ctx := storage.GetContext() + zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) + + callingHash := runtime.GetCallingScriptHash() + if data == zacoinStr { + if !callingHash.Equals(zaCoinHash) { + panic("only ZC is accepted") + } + } else { + if !callingHash.Equals(gas.Hash) { + panic("only GAS is accepted") + } + } +} + +func gasTransfer(playerOwner interop.Hash160, gasCount int) { + 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/exchanger.nef b/Exchanger/exchanger.nef new file mode 100755 index 0000000..8d32c2c Binary files /dev/null and b/Exchanger/exchanger.nef differ diff --git a/Exchanger/exchanger.yml b/Exchanger/exchanger.yml new file mode 100644 index 0000000..9f0abe8 --- /dev/null +++ b/Exchanger/exchanger.yml @@ -0,0 +1,5 @@ +name: Exchanger +supportedstandards: [] +events: +permissions: + - methods: "*" diff --git a/Exchanger/go.mod b/Exchanger/go.mod new file mode 100644 index 0000000..924c069 --- /dev/null +++ b/Exchanger/go.mod @@ -0,0 +1,5 @@ +module Exchanger + +go 1.21.5 + +require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231020160724-c3955f87d1b5 diff --git a/Exchanger/go.sum b/Exchanger/go.sum new file mode 100644 index 0000000..05f981f --- /dev/null +++ b/Exchanger/go.sum @@ -0,0 +1,2 @@ +github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231020160724-c3955f87d1b5 h1:09CpI5uwsxb1EeFPIKQRwwWlfCmDD/Dwwh01lPiQScM= +github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231020160724-c3955f87d1b5/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag= diff --git a/Roulette/Roulette.go b/Roulette/Roulette.go index 09d5f39..9a46ef6 100644 --- a/Roulette/Roulette.go +++ b/Roulette/Roulette.go @@ -21,7 +21,7 @@ func _deploy(data interface{}, isUpdate bool) { }) if len(args.zaCoinHash) != interop.Hash160Len { - panic("Invalid hash of zaCoin contract") + panic("invalid hash of zaCoin contract") } ctx := storage.GetContext() @@ -38,8 +38,8 @@ func PlayRoulette(bet int, selectedNumber int) { zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) playerBalance := contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) if playerBalance < bet { - panic("Insufficient funds lol, expected > " + string(rune(bet)) + "but balance " + string(rune(playerBalance))) - } + panic("Insufficient funds") + } if selectedNumber < 1 || selectedNumber > 36 { panic("Illegal number selected for roulette") @@ -47,12 +47,13 @@ func PlayRoulette(bet int, selectedNumber int) { isWin := isWinner(selectedNumber) if isWin { - panic("You win!") winAmount := calculateWinAmount(bet, selectedNumber) changePlayerBalance(ctx, playerOwner, winAmount) } else { changePlayerBalance(ctx, playerOwner, -bet) } + playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) + runtime.Notify("playerBalance", playerBalance) } func isWinner(selectedNumber int) bool { diff --git a/Roulette/config.json b/Roulette/config.json index 6c012d5..2eac8fe 100644 --- a/Roulette/config.json +++ b/Roulette/config.json @@ -1 +1,2 @@ -{"name":"Roulette","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":408,"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"}]}]},"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":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} + diff --git a/Roulette/roulette.yml b/Roulette/roulette.yml index bab6da4..a9ec3e6 100644 --- a/Roulette/roulette.yml +++ b/Roulette/roulette.yml @@ -1,11 +1,16 @@ -name: Roulette + name: Roulette supportedstandards: [] events: - name: "rouletteNumber" parameters: - name: "int" type: "Integer" + - name: "playerBalance" + parameters: + - name: "int" + type: "Integer" permissions: - methods: '*' events: - "rouletteNumber" + - "playerBalance" diff --git a/SlotMashine/config.json b/SlotMashine/config.json index 9080a29..7964377 100755 --- a/SlotMashine/config.json +++ b/SlotMashine/config.json @@ -1,2 +1,2 @@ -{"name":"SlotMashine","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":548,"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":"wheelNumber","parameters":[{"name":"int","type":"Integer"}]},{"name":"value","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":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} diff --git a/SlotMashine/config.yml b/SlotMashine/config.yml index 6d77a1b..659266b 100644 --- a/SlotMashine/config.yml +++ b/SlotMashine/config.yml @@ -1,16 +1,16 @@ name: SlotMashine supportedstandards: [] events: - - name: "wheelNumber" + - name: SlotResult parameters: - - name: "int" - type: "Integer" - - name: "value" + - name: array + type: Array + - name: playerBalance parameters: - - name: "int" - type: "Integer" + - name: int + type: Integer permissions: - methods: '*' events: - - "wheelNumber" - - "value" + - SlotResult + - playerBalance diff --git a/SlotMashine/slot.go b/SlotMashine/slot.go index d9062aa..2089b02 100644 --- a/SlotMashine/slot.go +++ b/SlotMashine/slot.go @@ -17,13 +17,12 @@ func _deploy(data interface{}, isUpdate bool) { return } - // Parse hash of forint contract from incoming data args := data.(struct { zaCoinHash interop.Hash160 }) if len(args.zaCoinHash) != interop.Hash160Len { - panic("invalid hash of zaCoin contract") + panic("Invalid hash of zaCoin contract") } @@ -34,6 +33,17 @@ func _deploy(data interface{}, isUpdate bool) { func RollSlot(bet int) { ctx := storage.GetContext() playerOwner := runtime.GetScriptContainer().Sender + + if bet <= 0 { + panic("Invalid bet amount") + } + zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) + playerBalance := contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) + + if playerBalance < bet { + panic("Insufficient funds") + } + res := roll() if (res == 0){ changePlayerBalance(ctx, playerOwner, -bet) @@ -41,24 +51,22 @@ func RollSlot(bet int) { win := res * bet changePlayerBalance(ctx, playerOwner, win) } + playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) + runtime.Notify("playerBalance", playerBalance) } func roll() int { + var result []int + for i:=0; i<3; i++ { + wheel := (runtime.GetRandom() % 8) + 1 + result = append(result, wheel) + runtime.Log("WheelNumber=" + string(i + 1) +", value="+string(wheel)) + } + runtime.Notify("SlotResult", result) - firstWheel := (runtime.GetRandom() % 8) + 1 - runtime.Notify("wheelNumber", 1) - runtime.Notify("value", firstWheel) - secondWheel := (runtime.GetRandom() % 8) + 1 - runtime.Notify("wheelNumber", 2) - runtime.Notify("value", secondWheel) - - thirdWheel := (runtime.GetRandom() % 8) + 1 - runtime.Notify("wheelNumber", 3) - runtime.Notify("value", thirdWheel) - - if (firstWheel == secondWheel && firstWheel == thirdWheel){ - return firstWheel + if (result[0] == result[1] && result[0] == result[2]){ + return result[0] } else { return 0 } @@ -72,7 +80,7 @@ func OnNEP17Payment(from interop.Hash160, amount int, data any) { callingHash := runtime.GetCallingScriptHash() if !callingHash.Equals(zaCoinHash) { - panic("only ZC is accepted") + panic("Only ZC is accepted") } } @@ -83,15 +91,13 @@ func changePlayerBalance(ctx storage.Context, playerOwner interop.Hash160, balan 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 + transferAmount = -balanceChange } transferred := contract.Call(zaCoinHash, "transfer", contract.All, from, to, transferAmount, nil).(bool) diff --git a/wallets/walletAleksey.json b/wallets/walletAleksey.json new file mode 100644 index 0000000..00de6ae --- /dev/null +++ b/wallets/walletAleksey.json @@ -0,0 +1,22 @@ +{ + "name": "n3UserWallet", + "chain": "neo3", + "version": "1.0", + "scrypt": { + "cost": 16384, + "blockSize": 8, + "parallel": 8, + "size": 64 + }, + "accounts": [ + { + "address": "NQCLAHuu4umnR99KB5m7U8ppJFtWqhw6DS", + "label": "test", + "isDefault": false, + "lock": false, + "key": "6PYKvmjnpLfNTbmx1YiHmZK6zYik7NSaqDZAeWcaVbcdxULYnXkKWW183H", + "contract": {}, + "extra": null + } + ] +}