refactor slot, roulette

This commit is contained in:
shashkevichfrida 2024-01-16 20:45:30 +03:00
parent cc4f3a1df4
commit 7ce8dbb9c8
16 changed files with 104 additions and 149 deletions

View file

@ -1 +0,0 @@
{"name":"Exchanger","abi":{"methods":[{"name":"_initialize","offset":0,"parameters":[],"returntype":"Void","safe":false},{"name":"_deploy","offset":31,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"buyZaCoin","offset":126,"parameters":[{"name":"count","type":"Integer"},{"name":"player","type":"Hash160"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":297,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false}],"events":[{"name":"balanceAfter","parameters":[{"name":"int","type":"Integer"}]},{"name":"balanceUser","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null}

Binary file not shown.

View file

@ -1,111 +0,0 @@
package Exchanger
import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
"github.com/nspcc-dev/neo-go/pkg/interop/native/gas"
"github.com/nspcc-dev/neo-go/pkg/interop/lib/address"
//"github.com/nspcc-dev/neo-go/pkg/interop/util"
)
const (
zaCoinHashKey = "zaCoinHash"
)
var walletAddress = address.ToHash160("NXbLSnHA8dNuMUPUSNNivx7XFucN1w5bRq")
func _deploy(data interface{}, isUpdate bool) {
if isUpdate {
return
}
args := data.(struct {
zaCoinHash interop.Hash160
})
if len(args.zaCoinHash) != interop.Hash160Len {
panic("Invalid hash of zaCoin contract")
}
ctx := storage.GetContext()
storage.Put(ctx, zaCoinHashKey, args.zaCoinHash)
}
func BuyZaCoin(count int, player interop.Hash160) {
playerOwner := runtime.GetScriptContainer().Sender
if !runtime.CheckWitness(playerOwner) {
panic("Player is not the same as the caller")
}
contractHash := runtime.GetExecutingScriptHash()
transferredGas := gas.Transfer(playerOwner, contractHash, count, nil)
if !transferredGas {
panic("Failed to transfer gas")
}
balanceAfter := gas.BalanceOf(runtime.GetExecutingScriptHash())
runtime.Notify("balanceAfter", balanceAfter)
balanceUser := gas.BalanceOf(playerOwner)
runtime.Notify("balanceUser", balanceUser)
//balanceBefore := gas.BalanceOf(runtime.GetExecutingScriptHash())
//gasTransfer(player, count)
//balanceAfter := gas.BalanceOf(runtime.GetExecutingScriptHash())
//changePlayerBalance(ctx, playerOwner, count)
//if (balanceBefore - balanceAfter == count) {
// changePlayerBalance(ctx, playerOwner, count)
//} else {
// util.Abort()
//}
}
func OnNEP17Payment(from interop.Hash160, amount int, data any) {
contractHash := runtime.GetExecutingScriptHash()
balance := gas.BalanceOf(contractHash)
runtime.Log("Contract's balance changed and has "+ string(balance)+" gas")
}
func gasTransfer(playerOwner interop.Hash160, gasCount int) {
if !runtime.CheckWitness(playerOwner) {
panic("Player is not the same as the caller")
}
contractHash := runtime.GetExecutingScriptHash()
transferredGas := gas.Transfer(playerOwner, contractHash, gasCount, nil)
if !transferredGas {
panic("Failed to transfer gas")
}
}
func changePlayerBalance(ctx storage.Context, playerOwner interop.Hash160, balanceChange int) {
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
playerContract := runtime.GetExecutingScriptHash()
var from, to interop.Hash160
var transferAmount int
if balanceChange > 0 {
// Transfer funds from contract to player owner
from = playerContract
to = playerOwner
transferAmount = balanceChange
} else {
// Transfer funds from player owner to contract
from = playerOwner
to = playerContract
transferAmount = -balanceChange // We flip sender/receiver, but keep amount positive
}
transferred := contract.Call(zaCoinHash, "transfer", contract.All, from, to, transferAmount, "zacoin").(bool)
if !transferred {
panic("failed to transfer zaCoins")
}
}

View file

@ -1,16 +0,0 @@
name: Exchanger
supportedstandards: []
events:
- name: "balanceAfter"
parameters:
- name: "int"
type: "Integer"
- name: "balanceUser"
parameters:
- name: "int"
type: "Integer"
permissions:
- methods: '*'
events:
- "balanceAfter"
- "balanceUser"

View file

@ -49,10 +49,10 @@ func PlayRoulette(bet int, selectedNumber int) {
isWin := isWinner(selectedNumber)
if isWin {
winAmount := calculateWinAmount(bet, selectedNumber)
changePlayerBalance(zaCoinHash, playerContract, playerOwner, winAmount)
changePlayerBalance(playerContract, playerOwner, winAmount)
runtime.Notify("gameResult", int(1))
} else {
changePlayerBalance(zaCoinHash, playerOwner, playerContract, bet)
changePlayerBalance(playerOwner, playerContract, bet)
runtime.Notify("gameResult", int(0))
}
playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int)
@ -67,17 +67,16 @@ func isWinner(selectedNumber int) bool {
}
func calculateWinAmount(bet int, selectedNumber int) int {
coefficients := map[int]int{
1: 36,
2: 18,
3: 2,
}
if coefficient, ok := coefficients[selectedNumber]; ok {
return bet * coefficient
}
return 0
coefs := []int{10, 20, 30, 2}
if selectedNumber == 36 {
return bet * coefs[0]
} else if selectedNumber == 18 {
return bet * coefs[1]
} else if selectedNumber == 2 {
return bet * coefs[2]
} else {
return bet * coefs[3]
}
}
func OnNEP17Payment(from interop.Hash160, amount int, data any) {
@ -90,7 +89,9 @@ func OnNEP17Payment(from interop.Hash160, amount int, data any) {
}
}
func changePlayerBalance(zaCoinHash interop.Hash160, sender interop.Hash160, recipient interop.Hash160, balanceChange int) {
func changePlayerBalance(sender interop.Hash160, recipient interop.Hash160, balanceChange int) {
ctx := storage.GetContext()
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool)
if !transferred {

View file

@ -1 +1 @@
{"name":"Roulette","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":535,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false},{"name":"playRoulette","offset":95,"parameters":[{"name":"bet","type":"Integer"},{"name":"selectedNumber","type":"Integer"}],"returntype":"Void","safe":false}],"events":[{"name":"rouletteNumber","parameters":[{"name":"int","type":"Integer"}]},{"name":"gameResult","parameters":[{"name":"int","type":"Integer"}]},{"name":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null}
{"name":"Roulette","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":562,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false},{"name":"playRoulette","offset":95,"parameters":[{"name":"bet","type":"Integer"},{"name":"selectedNumber","type":"Integer"}],"returntype":"Void","safe":false}],"events":[{"name":"rouletteNumber","parameters":[{"name":"int","type":"Integer"}]},{"name":"gameResult","parameters":[{"name":"int","type":"Integer"}]},{"name":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null}

Binary file not shown.

View file

@ -1 +1 @@
{"name":"SlotMashine","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":519,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false},{"name":"rollSlot","offset":95,"parameters":[{"name":"bet","type":"Integer"}],"returntype":"Void","safe":false}],"events":[{"name":"SlotResult","parameters":[{"name":"array","type":"Array"}]},{"name":"gameResult","parameters":[{"name":"int","type":"Integer"}]},{"name":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null}
{"name":"SlotMashine","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":517,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false},{"name":"rollSlot","offset":95,"parameters":[{"name":"bet","type":"Integer"}],"returntype":"Void","safe":false}],"events":[{"name":"SlotResult","parameters":[{"name":"array","type":"Array"}]},{"name":"gameResult","parameters":[{"name":"int","type":"Integer"}]},{"name":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null}

View file

@ -47,11 +47,11 @@ func RollSlot(bet int) {
res := roll()
if (res == 0){
changePlayerBalance(zaCoinHash, playerOwner, playerContract, bet)
changePlayerBalance(playerOwner, playerContract, bet)
runtime.Notify("gameResult", int(0))
} else {
win := res * bet
changePlayerBalance(zaCoinHash, playerContract, playerOwner, win)
changePlayerBalance(playerContract, playerOwner, win)
runtime.Notify("gameResult", int(1))
}
playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int)
@ -87,7 +87,9 @@ func OnNEP17Payment(from interop.Hash160, amount int, data any) {
}
}
func changePlayerBalance(zaCoinHash interop.Hash160, sender interop.Hash160, recipient interop.Hash160, balanceChange int) {
func changePlayerBalance(sender interop.Hash160, recipient interop.Hash160, balanceChange int) {
ctx := storage.GetContext()
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
transferred := contract.Call(zaCoinHash, "transfer", contract.All, sender, recipient, balanceChange, nil).(bool)
if !transferred {

Binary file not shown.

1
ZaCoinRefill/config.json Executable file
View file

@ -0,0 +1 @@
{"name":"ZaCoinRefill","abi":{"methods":[{"name":"_deploy","offset":0,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"getZaCoin","offset":95,"parameters":[],"returntype":"Void","safe":false},{"name":"onNEP17Payment","offset":244,"parameters":[{"name":"from","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Void","safe":false}],"events":[{"name":"playerBalance","parameters":[{"name":"int","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":"*"}],"supportedstandards":[],"trusts":[],"extra":null}

View file

@ -1,5 +1,5 @@
module Exchanger
module ZaCoinRefill
go 1.21.5
go 1.21.6
require github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20231020160724-c3955f87d1b5

View file

@ -0,0 +1,68 @@
package ZaCoinRefill
import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)
const (
zaCoinHashKey = "zaCoinHash"
minZaCoin = 5
zaCoinForTransfer = 50
)
func _deploy(data interface{}, isUpdate bool) {
if isUpdate {
return
}
args := data.(struct {
zaCoinHash interop.Hash160
})
if len(args.zaCoinHash) != interop.Hash160Len {
panic("Invalid hash of zaCoin contract")
}
ctx := storage.GetContext()
storage.Put(ctx, zaCoinHashKey, args.zaCoinHash)
}
func GetZaCoin() {
ctx := storage.GetContext()
playerOwner := runtime.GetScriptContainer().Sender
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
playerBalance := contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int)
if playerBalance < minZaCoin {
changePlayerBalance(ctx, playerOwner, zaCoinForTransfer)
}
playerBalance = contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner).(int)
runtime.Notify("playerBalance", playerBalance)
}
func OnNEP17Payment(from interop.Hash160, amount int, data any) {
ctx := storage.GetContext()
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
callingHash := runtime.GetCallingScriptHash()
if !callingHash.Equals(zaCoinHash) {
panic("only ZC is accepted")
}
}
func changePlayerBalance(ctx storage.Context, playerOwner interop.Hash160, transferAmount int) {
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
playerContract := runtime.GetExecutingScriptHash()
transferred := contract.Call(zaCoinHash, "transfer", contract.All, playerContract, playerOwner, transferAmount, nil).(bool)
if !transferred {
panic("failed to transfer zaCoins")
}
}

BIN
ZaCoinRefill/zaCoinRefill.nef Executable file

Binary file not shown.

View file

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