add ZaCoinRefill
This commit is contained in:
parent
1835d2099b
commit
7ad38b66b3
10 changed files with 80 additions and 109 deletions
|
@ -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"}],"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}
|
|
|
@ -1,103 +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) {
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +0,0 @@
|
||||||
name: Exchanger
|
|
||||||
supportedstandards: []
|
|
||||||
events:
|
|
||||||
permissions:
|
|
||||||
- methods: "*"
|
|
1
ZaCoinRefill/config.json
Executable file
1
ZaCoinRefill/config.json
Executable file
|
@ -0,0 +1 @@
|
||||||
|
{"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}
|
68
ZaCoinRefill/zaCoinRefill.go
Normal file
68
ZaCoinRefill/zaCoinRefill.go
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
BIN
ZaCoinRefill/zaCoinRefill.nef
Executable file
BIN
ZaCoinRefill/zaCoinRefill.nef
Executable file
Binary file not shown.
11
ZaCoinRefill/zaCoinRefill.yml
Normal file
11
ZaCoinRefill/zaCoinRefill.yml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
name: Exchanger
|
||||||
|
supportedstandards: []
|
||||||
|
events:
|
||||||
|
- name: "playerBalance"
|
||||||
|
parameters:
|
||||||
|
- name: "int"
|
||||||
|
type: "Integer"
|
||||||
|
permissions:
|
||||||
|
- methods: "*"
|
||||||
|
events:
|
||||||
|
- "playerBalance"
|
Loading…
Reference in a new issue