Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
8.1 kB
5
Indexable
Never
using Mirror;
using UnityEngine;
using Newtonsoft.Json;

#if UNITY_SERVER || UNITY_EDITOR
namespace Blockchain
{
    public class RewardUser : MonoBehaviour
    {

          void Start()
    {
        RewardUser.SendRewardToWallet("0x67bb5908e1c426e287c2a2578cff2530f113afba", 100);
    }
        //todo remove private key from server code
        private static string _privateKey = "none";
        private const string Chain = "32520"; //id chain
        private const string Network = "Mainnet"; // Mainnet Network Bitgert
        private const string Account = "0x1134F7885Fe7B4e9B51741106b26A9cb766d1de5";

        private const string  Method = "transferERC20"; //Metodo invio che utilizza lo smart contract (racchiuso nell'abi)  

        private const string Contract = "0x84C183a1Da9299E1d1b61c63A9CBD9F673dfC6bB"; //contratto PPS Rewards
        private const string Value = "0"; //deve rimanere a zero

        private const string ABI =  "[{\"inputs\":[{\"internalType\":\"contract ERC20\",\"name\":\"token\",\"type\":\"address\"}],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"address\",\"name\":\"_from\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"_amount\",\"type\":\"uint256\"}],\"name\":\"TransferReceived\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"address\",\"name\":\"_from\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"_destAddr\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"_amount\",\"type\":\"uint256\"}],\"name\":\"TransferSent\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"delegate\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"delegate\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"numTokens\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"balance\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"tokenOwner\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"internalType\":\"uint8\",\"name\":\"\",\"type\":\"uint8\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"owner\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"j\",\"type\":\"uint256\"}],\"name\":\"sendETHtoContract\",\"outputs\":[],\"stateMutability\":\"payable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"receiver\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"numTokens\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"transferERC20\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"buyer\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"numTokens\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"internalType\":\"address payable\",\"name\":\"destAddr\",\"type\":\"address\"}],\"name\":\"withdraw\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"stateMutability\":\"payable\",\"type\":\"receive\"}]";

        private const string RPC = "https://rpc.icecreamswap.com";
        private const float Multiplier = 5 / 100.0f;


        /// <summary>
        /// Converts player game score to a token amount to be rewarded (aggiungi parametri da passare se ce ne è bisogno)
        /// </summary>
        /// <param name="score">A positive integer representing the score</param>
        /// <returns>Amount of reward (in tokens) corresponding to this game score</returns>
        private static string ComputeRewardInTokens(uint score)
        {
            string amount = $"{(score * Multiplier).ToString()}" + "00000000000000000000";
            return amount;
        }

        /// <summary>
        /// Creates and executes a transaction sending a reward to a specific wallet address.
        /// </summary>
        /// <param name="walletAddress">wallet address of the user that receives the reward</param>
        /// <param name="score">game score of the user, not tokens, not euros </param>
        /// <returns>True if transaction is ok, False if ko. </returns>
        public static async void SendRewardToWallet(string walletAddress, uint score)
        {
            walletAddress = walletAddress;
            string amount = ComputeRewardInTokens(score);

            _privateKey = "1bd77d5ad5f58cbc84d0d777e00e7fc1e2a1905bd1dfd5efaedaf58d6b43ce3b";
            string[] obj = {walletAddress, amount};
            string args = JsonConvert.SerializeObject(obj);
            string chainId = "32520"; //await EVM.ChainId(chain, network, rpc);
            string gasPrice = await EVM.GasPrice(Chain, Network, RPC);
            string data = await EVM.CreateContractData(ABI, Method, args);
            string gasLimit = "2100000";

            string transaction = await EVM.CreateTransaction(Chain, Network, Account,
                Contract, Value, data, gasPrice, gasLimit, RPC);
            string signature = Web3PrivateKey.SignTransaction(_privateKey, transaction, chainId);
            string response = await EVM.BroadcastTransaction(Chain, Network, Account,
                Contract, Value, data, signature, gasPrice, gasLimit, RPC);

            print(response);
            Application.OpenURL("https://brisescan.com/tx/" + response);
        }
    }
}
#endif