Merge branch 'test' into dev

This commit is contained in:
shashkevichfrida 2024-01-09 16:13:11 +03:00
commit 84f8fad0a1
16 changed files with 207 additions and 39 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":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}
{"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}

View file

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

View file

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

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"}],"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}

103
Exchanger/exchanger.go Normal file
View file

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

BIN
Exchanger/exchanger.nef Executable file

Binary file not shown.

5
Exchanger/exchanger.yml Normal file
View file

@ -0,0 +1,5 @@
name: Exchanger
supportedstandards: []
events:
permissions:
- methods: "*"

5
Exchanger/go.mod Normal file
View file

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

2
Exchanger/go.sum Normal file
View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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