refactor craps, rps

This commit is contained in:
shashkevichfrida 2024-01-16 16:59:15 +03:00
parent 7ad38b66b3
commit cc4f3a1df4
27 changed files with 216 additions and 205 deletions

View file

@ -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}

View file

@ -32,6 +32,7 @@ func _deploy(data interface{}, isUpdate bool) {
func PlayCraps(bet int, firstSum int, secondSum int) { func PlayCraps(bet int, firstSum int, secondSum int) {
ctx := storage.GetContext() ctx := storage.GetContext()
playerOwner := runtime.GetScriptContainer().Sender playerOwner := runtime.GetScriptContainer().Sender
playerContract := runtime.GetExecutingScriptHash()
if bet <= 0 { if bet <= 0 {
panic("Invalid bet amount") panic("Invalid bet amount")
@ -43,11 +44,14 @@ func PlayCraps(bet int, firstSum int, secondSum int) {
panic("Insufficient funds") panic("Insufficient funds")
} }
isWin := isWinner(firstSum, secondSum) isWin := isWinner(firstSum, secondSum)
if (isWin){ if (isWin){
changePlayerBalance(ctx, playerOwner, bet) changePlayerBalance(playerContract, playerOwner, bet)
runtime.Notify("gameResult", int(1))
} else { } else {
changePlayerBalance(ctx, playerOwner, -bet) changePlayerBalance(playerOwner, playerContract, bet)
runtime.Notify("gameResult", int(0))
} }
playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int)
runtime.Notify("playerBalance", playerBalance) runtime.Notify("playerBalance", playerBalance)
@ -61,7 +65,6 @@ func isWinner(firstSum int, secondSum int) bool {
sum := 0 sum := 0
for i:=0; i<2; i++ { for i:=0; i<2; i++ {
crap := (runtime.GetRandom() % 6) + 1 crap := (runtime.GetRandom() % 6) + 1
runtime.Log("Crup number " + string(i+1) + " Rundom number " + string(crap))
runtime.Notify("Crup number", i+1) runtime.Notify("Crup number", i+1)
runtime.Notify("Random number", crap) runtime.Notify("Random number", crap)
sum += 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) { func changePlayerBalance(sender interop.Hash160, recipient interop.Hash160, balanceChange int) {
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) ctx := storage.GetContext()
playerContract := runtime.GetExecutingScriptHash() zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
var from, to interop.Hash160 transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool)
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 { if !transferred {
panic("failed to transfer zaCoins") panic("failed to transfer zaCoins")
} }

Binary file not shown.

View file

@ -9,6 +9,10 @@ events:
parameters: parameters:
- name: "int" - name: "int"
type: "Integer" type: "Integer"
- name: "gameResult"
parameters:
- name: "int"
type: "Integer"
- name: "playerBalance" - name: "playerBalance"
parameters: parameters:
- name: "int" - name: "int"
@ -19,3 +23,4 @@ permissions:
- "Crup number" - "Crup number"
- "Random number" - "Random number"
- "playerBalance" - "playerBalance"
- "gameResult"

1
Exchanger/config.json Executable file
View file

@ -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}

BIN
Exchanger/exchanger.nef Executable file

Binary file not shown.

111
Exchanger/main.go Normal file
View file

@ -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")
}
}

16
Exchanger/neo-go.yml Normal file
View file

@ -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"

View file

@ -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} {"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}

View file

@ -11,11 +11,6 @@ const (
zaCoinHashKey = "zaCoinHash" zaCoinHashKey = "zaCoinHash"
) )
type Result struct {
win bool
tie bool
lose bool
}
func _deploy(data interface{}, isUpdate bool) { func _deploy(data interface{}, isUpdate bool) {
if isUpdate { if isUpdate {
@ -34,10 +29,11 @@ func _deploy(data interface{}, isUpdate bool) {
storage.Put(ctx, zaCoinHashKey, args.zaCoinHash) storage.Put(ctx, zaCoinHashKey, args.zaCoinHash)
} }
func PlayRPS(playerChoice string, bet int) { func PlayRPS(playerChoice int, bet int) {
ctx := storage.GetContext() ctx := storage.GetContext()
playerOwner := runtime.GetScriptContainer().Sender playerOwner := runtime.GetScriptContainer().Sender
playerContract := runtime.GetExecutingScriptHash()
if bet <= 0 { if bet <= 0 {
panic("Invalid bet amount") panic("Invalid bet amount")
@ -48,57 +44,49 @@ func PlayRPS(playerChoice string, bet int) {
panic("Insufficient funds") panic("Insufficient funds")
} }
if playerChoice != "rock" && playerChoice != "paper" && playerChoice != "scissors" { if playerChoice <= 0 || playerChoice > 3 {
panic("invalid player choice") panic("invalid player choice")
} }
computerChoice := (runtime.GetRandom() % 3) + 1 computerChoice := (runtime.GetRandom() % 3) + 1
runtime.Notify("computerChoice", computerChoice)
var computerChoiceString string result := isWinner(playerChoice, computerChoice)
switch computerChoice {
case 0:
computerChoiceString = "rock"
case 1:
computerChoiceString = "paper"
case 2:
computerChoiceString = "scissors"
}
runtime.Notify("computerChoice", computerChoiceString) if result == 1 {
changePlayerBalance(playerContract, playerOwner, bet)
result := isWinner(playerChoice, computerChoiceString) runtime.Notify("gameResult", result)
} else if result == 0 {
if result.tie { changePlayerBalance(playerOwner, playerContract, bet)
runtime.Log("game tied: player chose " + playerChoice + ", computer chose " + computerChoiceString) runtime.Notify("gameResult", result)
} else if result.win { } else {
changePlayerBalance(ctx, playerOwner, bet) runtime.Log("game tied")
} else { runtime.Notify("gameResult", result)
runtime.Log("player lost: player chose " + playerChoice + ", computer chose " + computerChoiceString)
} }
playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) 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 { if playerChoice == computerChoice {
return Result{tie: true} return 2
} }
if playerChoice == "rock" && computerChoice == "scissors" { if playerChoice == 1 && computerChoice == 3 {
return Result{win: true} return 1
} }
if playerChoice == "scissors" && computerChoice == "paper" { if playerChoice == 3 && computerChoice == 2 {
return Result{win: true} return 1
} }
if playerChoice == "paper" && computerChoice == "rock" { if playerChoice == 2 && computerChoice == 1 {
return Result{win: true} return 1
} }
return Result{lose: true} return 0
} }
func OnNEP17Payment(from interop.Hash160, amount int, data any) { 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) { func changePlayerBalance(sender interop.Hash160, recipient interop.Hash160, balanceChange int) {
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160) ctx := storage.GetContext()
playerContract := runtime.GetExecutingScriptHash() zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
var from, to interop.Hash160 transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool)
var transferAmount int if !transferred {
if balanceChange > 0 { panic("failed to transfer zaCoins")
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")
}
} }

Binary file not shown.

View file

@ -3,8 +3,12 @@ supportedstandards: []
events: events:
- name: computerChoice - name: computerChoice
parameters: parameters:
- name: str - name: int
type: String type: Integer
- name: gameResult
parameters:
- name: int
type: Integer
- name: playerBalance - name: playerBalance
parameters: parameters:
- name: int - name: int
@ -14,3 +18,4 @@ permissions:
events: events:
- computerChoice - computerChoice
- playerBalance - playerBalance
- gameResult

View file

@ -31,6 +31,7 @@ func _deploy(data interface{}, isUpdate bool) {
func PlayRoulette(bet int, selectedNumber int) { func PlayRoulette(bet int, selectedNumber int) {
ctx := storage.GetContext() ctx := storage.GetContext()
playerOwner := runtime.GetScriptContainer().Sender playerOwner := runtime.GetScriptContainer().Sender
playerContract := runtime.GetExecutingScriptHash()
if bet <= 0 { if bet <= 0 {
panic("Invalid bet amount") panic("Invalid bet amount")
@ -48,9 +49,11 @@ func PlayRoulette(bet int, selectedNumber int) {
isWin := isWinner(selectedNumber) isWin := isWinner(selectedNumber)
if isWin { if isWin {
winAmount := calculateWinAmount(bet, selectedNumber) winAmount := calculateWinAmount(bet, selectedNumber)
changePlayerBalance(ctx, playerOwner, winAmount) changePlayerBalance(zaCoinHash, playerContract, playerOwner, winAmount)
runtime.Notify("gameResult", int(1))
} else { } else {
changePlayerBalance(ctx, playerOwner, -bet) changePlayerBalance(zaCoinHash, playerOwner, playerContract, bet)
runtime.Notify("gameResult", int(0))
} }
playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int) playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int)
runtime.Notify("playerBalance", playerBalance) 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) { func changePlayerBalance(zaCoinHash interop.Hash160, sender interop.Hash160, recipient interop.Hash160, balanceChange int) {
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
playerContract := runtime.GetExecutingScriptHash()
var from, to interop.Hash160 transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool)
var transferAmount int if !transferred {
if balanceChange > 0 { panic("failed to transfer zaCoins")
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")
}
} }

View file

@ -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}

Binary file not shown.

View file

@ -1,10 +1,14 @@
name: Roulette name: Roulette
supportedstandards: [] supportedstandards: []
events: events:
- name: "rouletteNumber" - name: "rouletteNumber"
parameters: parameters:
- name: "int" - name: "int"
type: "Integer" type: "Integer"
- name: "gameResult"
parameters:
- name: "int"
type: "Integer"
- name: "playerBalance" - name: "playerBalance"
parameters: parameters:
- name: "int" - name: "int"
@ -13,4 +17,5 @@ permissions:
- methods: '*' - methods: '*'
events: events:
- "rouletteNumber" - "rouletteNumber"
- "gameResult"
- "playerBalance" - "playerBalance"

View file

@ -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}

View file

@ -5,6 +5,10 @@ events:
parameters: parameters:
- name: array - name: array
type: Array type: Array
- name: gameResult
parameters:
- name: int
type: Integer
- name: playerBalance - name: playerBalance
parameters: parameters:
- name: int - name: int
@ -13,4 +17,5 @@ permissions:
- methods: '*' - methods: '*'
events: events:
- SlotResult - SlotResult
- gameResult
- playerBalance - playerBalance

View file

@ -33,6 +33,7 @@ func _deploy(data interface{}, isUpdate bool) {
func RollSlot(bet int) { func RollSlot(bet int) {
ctx := storage.GetContext() ctx := storage.GetContext()
playerOwner := runtime.GetScriptContainer().Sender playerOwner := runtime.GetScriptContainer().Sender
playerContract := runtime.GetExecutingScriptHash()
if bet <= 0 { if bet <= 0 {
panic("Invalid bet amount") panic("Invalid bet amount")
@ -46,10 +47,12 @@ func RollSlot(bet int) {
res := roll() res := roll()
if (res == 0){ if (res == 0){
changePlayerBalance(ctx, playerOwner, -bet) changePlayerBalance(zaCoinHash, playerOwner, playerContract, bet)
runtime.Notify("gameResult", int(0))
} else { } else {
win := res * bet 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) playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int)
runtime.Notify("playerBalance", playerBalance) 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) { func changePlayerBalance(zaCoinHash interop.Hash160, sender interop.Hash160, recipient interop.Hash160, balanceChange int) {
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
playerContract := runtime.GetExecutingScriptHash()
var from, to interop.Hash160 transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool)
var transferAmount int if !transferred {
if balanceChange > 0 { panic("failed to transfer zaCoins")
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")
}
} }

Binary file not shown.

View file

@ -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}

Binary file not shown.

View file

@ -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")
}
}

Binary file not shown.

View file

@ -1,11 +0,0 @@
name: Exchanger
supportedstandards: []
events:
- name: "playerBalance"
parameters:
- name: "int"
type: "Integer"
permissions:
- methods: "*"
events:
- "playerBalance"