Compare commits
2 commits
2fea01fc86
...
fe96a4d7d4
Author | SHA1 | Date | |
---|---|---|---|
|
fe96a4d7d4 | ||
|
f94fed3108 |
2 changed files with 134 additions and 1 deletions
133
contracts/money.go
Normal file
133
contracts/money.go
Normal file
|
@ -0,0 +1,133 @@
|
|||
package contracts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"context"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop"
|
||||
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
||||
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
)
|
||||
|
||||
const (
|
||||
walletKey = "walletKey"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Deposit(from, -amount)
|
||||
Deposit(to, amount)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// todo нужно сделать несколько функций
|
||||
// функция списания комиссии за присоединение к комнате - эта комиссия начисляется на кошелек контракта в prize pool
|
||||
// функция списания комиссии за ответ на вопрос - комиссия начисляется на кошелек контракта в prize pool
|
||||
// функция распределения prize pool между победителями и хостом
|
||||
// может быть еще какие нибудь, потом обсудим
|
||||
|
||||
func ChargeParticipationFee(wallet interop.Hash160, fee int) bool {
|
||||
if getBalance(wallet) < fee {
|
||||
return false
|
||||
}
|
||||
|
||||
Deposit(wallet, -fee)
|
||||
return true
|
||||
}
|
||||
|
||||
func DistributeRewards(host interop.Hash160, winners []interop.Hash160, reward int) bool {
|
||||
hostShare := reward * 50 / 100
|
||||
Deposit(host, hostShare)
|
||||
|
||||
winnerShare := reward * 50 / 100 / len(winners)
|
||||
for _, winner := range winners {
|
||||
Deposit(winner, winnerShare)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package nft
|
||||
package contracts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
|
Loading…
Add table
Reference in a new issue