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

Binary file not shown.

View file

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

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

Binary file not shown.

View file

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

View file

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

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: []
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"

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:
- 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

View file

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

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"