auction/Exchanger/exchanger.go

58 lines
1.4 KiB
Go
Raw Normal View History

2024-01-07 14:43:07 +00:00
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"
)
func _deploy(data interface{}, isUpdate bool) {
if isUpdate {
return
}
args := data.(struct {
zaCoinHash interop.Hash160
})
if len(args.zaCoinHash) != interop.Hash160Len {
2024-01-07 17:12:36 +00:00
panic("invalid hash of zaCoin contract")
}
2024-01-07 14:43:07 +00:00
ctx := storage.GetContext()
storage.Put(ctx, zaCoinHashKey, args.zaCoinHash)
}
2024-01-07 17:12:36 +00:00
func BuyZaCoin(gasCount int) bool {
2024-01-07 14:43:07 +00:00
playerOwner := runtime.GetScriptContainer().Sender
playerBalance := gas.BalanceOf(playerOwner)
if playerBalance < gasCount {
panic("Insufficient funds")
}
contractHash := runtime.GetExecutingScriptHash()
transferredGas := gas.Transfer(playerOwner, contractHash, gasCount, nil).(bool)
2024-01-07 17:12:36 +00:00
2024-01-07 14:43:07 +00:00
if !transferredGas {
2024-01-07 17:12:36 +00:00
panic("failed to transfer gas")
}
resultAmountZaCoin := gasCount * 57
transferredZaCoin := contract.Call(zaCoinHash, "transfer", contract.All, contractHash, playerBalance, resultAmountZaCoin, nil).(bool)
2024-01-07 14:43:07 +00:00
if !transferredZaCoin {
panic("failed to transfer zaCoins")
2024-01-07 17:12:36 +00:00
} else {
runtime.Log("Gas balance: ", gas.BalanceOf(playerOwner))
runtime.Log("ZaCoin balance: ", contract.Call(zaCoinHash, "balanceOf", contract.ReadStates, playerOwner))
2024-01-07 14:43:07 +00:00
}
2024-01-07 17:12:36 +00:00
return transferredZaCoin
2024-01-07 14:43:07 +00:00
}