Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hafidkhoiruddin/992275be71f63bc5e9e8a9aac6339e2c to your computer and use it in GitHub Desktop.
Save hafidkhoiruddin/992275be71f63bc5e9e8a9aac6339e2c to your computer and use it in GitHub Desktop.
using Nethereum.Hex.HexTypes;
using Nethereum.JsonRpc.UnityClient;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using UnityEngine;
using UnityEngine.UI;
public class PlayerBalanceController : MonoBehaviour
{
public Text uiTextEtherBalance;
public InputField InputAccountText;
public InputField InputAccountPKtext;
public InputField InputPlayerName;
string PlayerInfo;
private string networkUrl = "https://kovan.infura.io";
string playerEthereumAccount;//0x4E77642b6C5d7c8ECDb809c60123E911aD1a2267 // akun tutorial
// 0xff5628FD13E08780a3A06A636ABd33aF19B12015 //dompetku
string playerEthereumAccountPK;//05be6ce86de17211a67d1fd83a94a5282a0f7e15d2e9887966161385787e82fc //akun tutorial
// BC9A75BECAE5C2DF124C551A59141F180CE59DAA8A7B19F9935151AAB8318B34//dompetku
private IEnumerator getAccountBalanceCoroutine;
private static PlayerBalanceController instance;
void Start()
{
PlayerInformation();
}
void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
public void OnRefreshButtonPressed()
{
GetAccountBalance();
SavePlayerInfo();
}
public void PlayerInformation(){ //memuat informasi player : nama, Account Address, Account private Key, Account Balance
PlayerInfo = PlayerPrefs.GetString("PlayerName");
playerEthereumAccount = PlayerPrefs.GetString("PlayerEthAccount");
playerEthereumAccountPK = PlayerPrefs.GetString("PlayerEthAccPK");
InputPlayerName.text = PlayerInfo;
InputAccountText.text = playerEthereumAccount;
InputAccountPKtext.text = playerEthereumAccountPK;
// playerEthereumAccount = InputAccountText.text;
// playerEthereumAccountPK = InputAccountPKtext.text;
getAccountBalanceCoroutine = GetAccountBalanceCoroutine();
}
public void SavePlayerInfo(){ //Dipanggil Di Button refresh
playerEthereumAccount = InputAccountText.text;
playerEthereumAccountPK = InputAccountPKtext.text;
PlayerInfo = InputPlayerName.text;
PlayerPrefs.SetString("PlayerName", PlayerInfo);
PlayerPrefs.SetString("PlayerEthAccount",playerEthereumAccount);
PlayerPrefs.SetString("PlayerEthAccPK", playerEthereumAccountPK);
}
public void GetAccountBalance(){
StopCoroutine(getAccountBalanceCoroutine);
getAccountBalanceCoroutine = GetAccountBalanceCoroutine();
StartCoroutine(getAccountBalanceCoroutine);
}
public IEnumerator GetAccountBalanceCoroutine(){
var getBalanceRequest = new EthGetBalanceUnityRequest(networkUrl);
yield return getBalanceRequest.SendRequest(InputAccountText.text, Nethereum.RPC.Eth.DTOs.BlockParameter.CreateLatest());
if(getBalanceRequest.Exception == null){
var balance = getBalanceRequest.Result.Value;
uiTextEtherBalance.text = Nethereum.Util.UnitConversion.Convert.FromWei(balance, 18).ToString("n8");
}
else
{
Debug.Log("RW: Get Account Balance gave an exception : " + getBalanceRequest.Exception.Message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment