diff --git a/contracts/money.go b/contracts/money.go new file mode 100644 index 0000000..73dc76b --- /dev/null +++ b/contracts/money.go @@ -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 +} diff --git a/contracts/nft.go b/contracts/nft.go index b7801eb..dea42d2 100644 --- a/contracts/nft.go +++ b/contracts/nft.go @@ -1,4 +1,4 @@ -package nft +package contracts import ( "fmt"