Compare commits
2 commits
9c798b31a6
...
cba7093c18
Author | SHA1 | Date | |
---|---|---|---|
|
cba7093c18 | ||
|
3281f19594 |
2 changed files with 26 additions and 117 deletions
|
@ -2,132 +2,35 @@ package contracts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"context"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop"
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/gas"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
func DeductTokens(wallet interop.Hash160, amount int) bool {
|
||||||
walletKey = "walletKey"
|
balance := gas.BalanceOf(wallet)
|
||||||
)
|
if balance < amount {
|
||||||
|
runtime.Log(fmt.Sprintf("Insufficient balance for wallet: %x", wallet))
|
||||||
func _deploy(data interface{}, isUpdate bool) {
|
|
||||||
if isUpdate {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// передаем кошелек при деплое контрокта
|
|
||||||
|
|
||||||
args := data.(struct {
|
|
||||||
wallet interop.Hash160
|
|
||||||
})
|
|
||||||
|
|
||||||
if len(args.wallet) != interop.Hash160Len {
|
|
||||||
panic("Invalid hash of wallet")
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := storage.GetContext()
|
|
||||||
storage.Put(ctx, walletKey, args.wallet)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getBalance(wallet interop.Hash160) int {
|
|
||||||
rpcClient, err := rpcclient.New(context.Background(), "http://localhost:20332", rpcclient.Options{})
|
|
||||||
if err != nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
addressHash, err := util.Uint160DecodeStringLE(string(wallet))
|
|
||||||
if err != nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
balances, err := rpcClient.GetNEP11Balances(addressHash)
|
|
||||||
if err != nil {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return balances
|
|
||||||
}
|
|
||||||
|
|
||||||
func Deposit(walletAddr string, amount int) (string, error) {
|
|
||||||
// Create RPC client
|
|
||||||
rpcClient, err := rpcclient.New(context.Background(), "http://localhost:20332", rpcclient.Options{})
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to create RPC client: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
addressHash, err := util.Uint160DecodeStringLE(walletAddr)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("invalid wallet address: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
w, err := wallet.NewWallet("path_to_wallet_file") // Replace with actual wallet file path
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to load wallet: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
account, err := w.GetAccount("your_account_name") // Replace with your account name
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to get account: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
amountInCoins := util.Fixed8(amount) // Amount to transfer
|
|
||||||
tx, err := neotx.NewTransfer(account, addressHash, amountInCoins)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to create transfer transaction: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sign the transaction with the sender's private key
|
|
||||||
if err := tx.Sign(account.PrivateKey()); err != nil {
|
|
||||||
return "", fmt.Errorf("failed to sign transaction: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send the transaction to the blockchain
|
|
||||||
txID, err := rpcClient.SendRawTransaction(tx)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("failed to send transaction: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the transaction ID
|
|
||||||
return txID.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Transfer(from interop.Hash160, to interop.Hash160, amount int) bool {
|
|
||||||
if getBalance(from) < amount {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
Deposit(from, -amount)
|
contractHash := runtime.GetExecutingScriptHash()
|
||||||
Deposit(to, amount)
|
if !gas.Transfer(wallet, contractHash, amount, nil) {
|
||||||
|
runtime.Log(fmt.Sprintf("Failed to deduct %d tokens from wallet: %x", amount, wallet))
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo нужно сделать несколько функций
|
|
||||||
// функция списания комиссии за присоединение к комнате - эта комиссия начисляется на кошелек контракта в prize pool
|
|
||||||
// функция списания комиссии за ответ на вопрос - комиссия начисляется на кошелек контракта в prize pool
|
|
||||||
// функция распределения prize pool между победителями и хостом
|
|
||||||
// может быть еще какие нибудь, потом обсудим
|
|
||||||
|
|
||||||
func ChargeParticipationFee(wallet interop.Hash160, fee int) bool {
|
|
||||||
if getBalance(wallet) < fee {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
Deposit(wallet, -fee)
|
runtime.Log(fmt.Sprintf("Successfully deducted %d tokens from wallet: %x", amount, wallet))
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func DistributeRewards(host interop.Hash160, winners []interop.Hash160, reward int) bool {
|
func RewardTokens(wallet interop.Hash160, amount int) bool {
|
||||||
hostShare := reward * 50 / 100
|
contractHash := runtime.GetExecutingScriptHash()
|
||||||
Deposit(host, hostShare)
|
if !gas.Transfer(contractHash, wallet, amount, nil) {
|
||||||
|
runtime.Log(fmt.Sprintf("Failed to reward %d tokens to wallet: %x", amount, wallet))
|
||||||
winnerShare := reward * 50 / 100 / len(winners)
|
return false
|
||||||
for _, winner := range winners {
|
|
||||||
Deposit(winner, winnerShare)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runtime.Log(fmt.Sprintf("Successfully rewarded %d tokens to wallet: %x", amount, wallet))
|
||||||
return true
|
return true
|
||||||
}
|
}
|
|
@ -110,7 +110,8 @@ func CreateRoom(host interop.Hash160, RoundWinnersCount, GameWinnersCount int) s
|
||||||
Rounds: []Round{},
|
Rounds: []Round{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: Добавить списание токенов за создание комнаты
|
amount := 10
|
||||||
|
DeductTokens(host, amount);
|
||||||
|
|
||||||
setRoom(ctx, &room)
|
setRoom(ctx, &room)
|
||||||
return id
|
return id
|
||||||
|
@ -131,7 +132,8 @@ func JoinRoom(roomId string) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: Добавить списание токенов за вход в комнату
|
amount := 15
|
||||||
|
DeductTokens(wallet, amount)
|
||||||
|
|
||||||
var player = Player{
|
var player = Player{
|
||||||
Wallet: wallet,
|
Wallet: wallet,
|
||||||
|
@ -241,7 +243,8 @@ func SendAnswer(roomId string, text string) bool {
|
||||||
return false // Only player can send content, player must be active, room status must be answering
|
return false // Only player can send content, player must be active, room status must be answering
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: Добавить списание токенов за добавление ответа
|
amount := 10
|
||||||
|
DeductTokens(wallet, amount)
|
||||||
|
|
||||||
var round = room.Rounds[len(room.Rounds)-1]
|
var round = room.Rounds[len(room.Rounds)-1]
|
||||||
|
|
||||||
|
@ -501,6 +504,9 @@ func finishGame(ctx storage.Context, room *Room) bool {
|
||||||
sendMessageToPlayers("FinishGame", result)
|
sendMessageToPlayers("FinishGame", result)
|
||||||
|
|
||||||
// todo: Отправка награды победителям и хосту
|
// todo: Отправка награды победителям и хосту
|
||||||
|
for _, player := range winners {
|
||||||
|
RewardTokens(player.Wallet, 50)
|
||||||
|
}
|
||||||
|
|
||||||
room.Status = RoomStatusFinished
|
room.Status = RoomStatusFinished
|
||||||
setRoom(ctx, room)
|
setRoom(ctx, room)
|
||||||
|
|
Loading…
Add table
Reference in a new issue