fix: transfer token

This commit is contained in:
shashkevichfrida 2024-01-08 17:42:36 +03:00
parent 070d2de721
commit 021c155273
7 changed files with 60 additions and 35 deletions

View file

@ -1 +1 @@
{"name":"Exchanger","abi":{"methods":[{"name":"_initialize","offset":0,"parameters":[],"returntype":"Void","safe":false},{"name":"_deploy","offset":33,"parameters":[{"name":"data","type":"Any"},{"name":"isUpdate","type":"Boolean"}],"returntype":"Void","safe":false},{"name":"buyZaCoin","offset":128,"parameters":[{"name":"gasCount","type":"Integer"}],"returntype":"Boolean","safe":false},{"name":"onNEP17Payment","offset":350,"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}
{"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}

View file

@ -7,6 +7,7 @@ import (
"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 (
@ -25,50 +26,76 @@ 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()
storage.Put(ctx, zaCoinHashKey, args.zaCoinHash)
}
func BuyZaCoin(gasCount int) bool {
func BuyZaCoin(count int) {
ctx := storage.GetContext()
zaCoinHash := storage.Get(ctx, zaCoinHashKey).(interop.Hash160)
playerOwner := runtime.GetScriptContainer().Sender
playerBalance := gas.BalanceOf(playerOwner)
//balanceBefore := gas.BalanceOf(runtime.GetExecutingScriptHash())
gasTransfer(playerOwner, count)
//balanceAfter := gas.BalanceOf(runtime.GetExecutingScriptHash())
if playerBalance < gasCount {
panic("Insufficient funds")
}
//contractHash := runtime.GetExecutingScriptHash()
transferredGas := gas.Transfer(playerOwner, walletAddress, gasCount, nil)
if !transferredGas {
panic("failed to transfer gas")
}
resultAmountZaCoin := gasCount * 57
transferredZaCoin := contract.Call(zaCoinHash, "transfer", contract.All, walletAddress, playerBalance, resultAmountZaCoin, nil).(bool)
if !transferredZaCoin {
panic("Failed to transfer zaCoins")
} else {
runtime.Log("Gas balance: "+ string(gas.BalanceOf(playerOwner)))
//runtime.Log("ZaCoin balance: " + string(contract.Call(zaCoinHash) + "balanceOf" +string(contract.ReadStates) + string(playerOwner)))
}
return transferredZaCoin
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 !callingHash.Equals(zaCoinHash) {
panic("Only ZC is accepted")
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")
}
}

Binary file not shown.

View file

@ -1,6 +1,5 @@
name: Exchanger
sourceurl: http://example.com/
safemethods: []
supportedstandards: []
events:
permissions:
- methods: '*'
- methods: "*"

View file

@ -1 +1 @@
{"name":"ZaCoin","abi":{"methods":[{"name":"balanceOf","offset":583,"parameters":[{"name":"holder","type":"Hash160"}],"returntype":"Integer","safe":true},{"name":"decimals","offset":559,"parameters":[],"returntype":"Integer","safe":true},{"name":"isUsableAddress","offset":254,"parameters":[{"name":"addr","type":"ByteArray"}],"returntype":"Boolean","safe":false},{"name":"mint","offset":633,"parameters":[{"name":"to","type":"Hash160"}],"returntype":"Void","safe":false},{"name":"symbol","offset":554,"parameters":[],"returntype":"String","safe":true},{"name":"totalSupply","offset":564,"parameters":[],"returntype":"Integer","safe":true},{"name":"transfer","offset":606,"parameters":[{"name":"from","type":"Hash160"},{"name":"to","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Boolean","safe":false}],"events":[{"name":"Transfer","parameters":[{"name":"from","type":"Hash160"},{"name":"to","type":"Hash160"},{"name":"amount","type":"Integer"}]}]},"features":{},"groups":[{"pubkey":"027171df30177d401c638fb2ddc14f9dbda323291e363ba4f7c3b19a8b44c8ba0a","signature":"2YQ2Jy/KZJlg9lQyguFyOpKfY4L2HYvwHkb6XkiALhVlPFVUKXvKMF5I2u9dnIFCLvI0W9h/D/46edTwPCpLjg=="}],"permissions":[{"contract":"*","methods":["onNEP17Payment"]}],"supportedstandards":["NEP-17"],"trusts":[],"extra":null}
{"name":"ZaCoin","abi":{"methods":[{"name":"balanceOf","offset":583,"parameters":[{"name":"holder","type":"Hash160"}],"returntype":"Integer","safe":true},{"name":"decimals","offset":559,"parameters":[],"returntype":"Integer","safe":true},{"name":"isUsableAddress","offset":254,"parameters":[{"name":"addr","type":"ByteArray"}],"returntype":"Boolean","safe":false},{"name":"mint","offset":633,"parameters":[{"name":"to","type":"Hash160"}],"returntype":"Void","safe":false},{"name":"symbol","offset":554,"parameters":[],"returntype":"String","safe":true},{"name":"totalSupply","offset":564,"parameters":[],"returntype":"Integer","safe":true},{"name":"transfer","offset":606,"parameters":[{"name":"from","type":"Hash160"},{"name":"to","type":"Hash160"},{"name":"amount","type":"Integer"},{"name":"data","type":"Any"}],"returntype":"Boolean","safe":false}],"events":[{"name":"Transfer","parameters":[{"name":"from","type":"Hash160"},{"name":"to","type":"Hash160"},{"name":"amount","type":"Integer"}]}]},"features":{},"groups":[],"permissions":[{"contract":"*","methods":["onNEP17Payment"]}],"supportedstandards":["NEP-17"],"trusts":[],"extra":null}

Binary file not shown.

View file

@ -7,7 +7,6 @@ import (
)
func getToken() Token {
// Owner of the wallet is wallets/game-wallet.json нету еще
owner := address.ToHash160("NXbLSnHA8dNuMUPUSNNivx7XFucN1w5bRq")
token := Token{
Name: "ZaCoin",
@ -53,6 +52,6 @@ func Mint(to interop.Hash160) {
ctx := storage.GetContext()
minted := getToken().Mint(ctx, to)
if !minted {
panic("failed to mint initial supply")
panic("Failed to mint initial supply")
}
}