Skip to content

Commit 13baaa9

Browse files
committed
save
1 parent 53ae152 commit 13baaa9

File tree

10 files changed

+368
-292
lines changed

10 files changed

+368
-292
lines changed

Web3/Assets/EasyEthereum/Scripts/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ public class Constants {
33
public static readonly bool debug = true;
44
public static readonly string NODEDEFAULT_ETH_MAINNET = "http://ec2-44-200-18-204.compute-1.amazonaws.com:8545";
55
public static readonly string NODEDEFAULT_ETH_ROPSTEN = "http://ec2-44-203-28-1.compute-1.amazonaws.com:8545";
6+
public static readonly string ADDR_UNISWAPV2 = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D";
67
}
78
}

Web3/Assets/EasyEthereum/Scripts/Contracts/ERC20.cs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,57 @@ namespace EasyWeb3 {
1111
public class ERC20 : Contract {
1212
private string ERC20ABI = "[{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_from\",\"type\":\"address\"},{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"balance\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"},{\"name\":\"_spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"fallback\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"spender\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"}]";
1313

14-
public ERC20(string _contract, ChainName _n, ChainId _i) : base(_contract,_n,_i) {
14+
public ERC20(string _contract, ChainId _i) : base(_contract,ChainName.ETH,_i) {
1515

1616
}
1717

18+
public async Task<bool> Load() {
19+
await GetDecimals();
20+
await GetTotalSupply();
21+
await GetName();
22+
await GetSymbol();
23+
await GetOwner();
24+
return true;
25+
}
26+
1827
public async Task<BigInteger> GetTotalSupply() {
19-
try {
20-
var _input = new CallInput(FunctionHash("totalSupply()"), m_Contract);
21-
var _result = await m_Web3.Eth.Transactions.Call.SendRequestAsync(_input);
22-
return (new HexBigInteger(_result)).Value;
23-
} catch (System.Exception _e) {
24-
LogWarning("[GetTotalSupply] Error: "+_e);
25-
return 0;
26-
}
28+
var _out = await CallFunction("totalSupply()", new string[]{"int"});
29+
TotalSupply = (BigInteger)_out[0];
30+
return TotalSupply;
31+
}
32+
33+
public async Task<BigInteger> GetDecimals() {
34+
var _out = await CallFunction("decimals()", new string[]{"int"});
35+
Decimals = (BigInteger)_out[0];
36+
return Decimals;
37+
}
38+
39+
public async Task<string> GetName() {
40+
var _out = await CallFunction("name()", new string[]{"string"});
41+
Name = (string)_out[0];
42+
return Name;
43+
}
44+
45+
public async Task<string> GetSymbol() {
46+
var _out = await CallFunction("symbol()", new string[]{"string"});
47+
Symbol = (string)_out[0];
48+
return Symbol;
49+
}
50+
51+
public async Task<string> GetOwner() {
52+
var _out = await CallFunction("owner()", new string[]{"address"});
53+
Owner = (string)_out[0];
54+
return Owner;
55+
}
56+
57+
public async Task<BigInteger> GetBalanceOf(string _addr) {
58+
var _out = await CallFunction("balanceOf(address)", new string[]{"int"}, new string[]{_addr});
59+
return (BigInteger)_out[0];
60+
}
61+
62+
public async Task<BigInteger> GetAllowance(string _owner, string _spender) {
63+
var _out = await CallFunction("allowance(address,address)", new string[]{"int"}, new string[]{_owner,_spender});
64+
return (BigInteger)_out[0];
2765
}
2866
}
2967
}

Web3/Assets/EasyEthereum/Scripts/EasyWeb3Controller.cs

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections;
2-
using System.Collections.Generic;
32
using System;
43
using System.Numerics;
54
using System.Linq;
@@ -29,9 +28,94 @@ private void Start() {
2928

3029
#region Private Functions
3130
private async void Load() {
32-
ERC20 _token = new ERC20("0x1A2933fbA0c6e959c9A2D2c933f3f8AD4aa9f06e", ChainName.ETH, ChainId.ETH_MAINNET);
33-
BigInteger _totalSupply = await _token.GetTotalSupply();
34-
Debug.Log("total supply: "+_totalSupply);
31+
// ERC20 _token = new ERC20("0x1A2933fbA0c6e959c9A2D2c933f3f8AD4aa9f06e", ChainId.ETH_MAINNET);
32+
ERC20 _token = new ERC20("0x4C9B1DB55c8A89cb0312C8fD84B0cB650E648618", ChainId.ETH_ROPSTEN);
33+
// method 1
34+
// Debug.Log("\nMethod 1 ERC20 Data Extraction");
35+
await _token.Load();
36+
Debug.Log("decimals: "+_token.Decimals);
37+
Debug.Log("name: "+_token.Name);
38+
Debug.Log("symbol: "+_token.Symbol);
39+
Debug.Log("owner: "+_token.Owner);
40+
Debug.Log("total supply: "+_token.ValueFromDecimals(_token.TotalSupply));
41+
42+
// // method 2
43+
// Debug.Log("\nMethod 2 ERC20 Data Extraction");
44+
// BigInteger _decimals = await _token.GetDecimals();
45+
// BigInteger _totalSupply = await _token.GetTotalSupply();
46+
// string _name = await _token.GetName();
47+
// string _symbol = await _token.GetSymbol();
48+
// string _owner = await _token.GetOwner();
49+
// Debug.Log("decimals: "+_decimals);
50+
// Debug.Log("name: "+_name);
51+
// Debug.Log("symbol: "+_symbol);
52+
// Debug.Log("owner: "+_owner);
53+
// Debug.Log("total supply: "+_token.ValueFromDecimals(_totalSupply));
54+
55+
// // standard calls
56+
// Debug.Log("\nStandard ERC20 Data");
57+
// BigInteger _balance = await _token.GetBalanceOf("0x34221445c2dd9fd3f41a8a8bfa7d49ec898e0ef4");
58+
// BigInteger _allowance = await _token.GetAllowance("0xbd0dbb9fddc73b6ebffc7c09cfae1b19d6dece40", Constants.ADDR_UNISWAPV2);
59+
// Debug.Log("balance: "+_token.ValueFromDecimals(_balance));
60+
// Debug.Log("allowance: "+_token.ValueFromDecimals(_allowance));
61+
62+
// var _out = await _token.CallFunction("isSniper(address)", new string[]{"bool"}, new string[]{"0x34221445c2dd9fd3f41a8a8bfa7d49ec898e0ef4"});
63+
// bool _isSniper = (bool)_out[0];
64+
// Debug.Log(_isSniper);
65+
66+
// var _out = await _token.CallFunction("totalSupply()", new string[]{"int"});
67+
// BigInteger _supply = (BigInteger)_out[0];
68+
// Debug.Log(_out[0]);
69+
70+
// Debug.Log("TEST: getUint256()");
71+
// var _out = await _token.CallFunction("getUint256()", new string[]{"uint256"});
72+
// Debug.Log("returned: "+_out[0]);
73+
74+
// Debug.Log("TEST: getUint128()");
75+
// _out = await _token.CallFunction("getUint128()", new string[]{"uint128"});
76+
// Debug.Log("returned: "+_out[0]);
77+
78+
// Debug.Log("TEST: getUint64()");
79+
// _out = await _token.CallFunction("getUint64()", new string[]{"uint64"});
80+
// Debug.Log("returned: "+_out[0]);
81+
82+
// Debug.Log("TEST: getUint32()");
83+
// _out = await _token.CallFunction("getUint32()", new string[]{"uint32"});
84+
// Debug.Log("returned: "+_out[0]);
85+
86+
// Debug.Log("TEST: getUint16()");
87+
// _out = await _token.CallFunction("getUint16()", new string[]{"uint16"});
88+
// Debug.Log("returned: "+_out[0]);
89+
90+
// Debug.Log("TEST: getUint8()");
91+
// _out = await _token.CallFunction("getUint8()", new string[]{"uint8"});
92+
// Debug.Log("returned: "+_out[0]);
93+
94+
// Debug.Log("TEST: getUint()");
95+
// _out = await _token.CallFunction("getUint()", new string[]{"uint"});
96+
// Debug.Log("returned: "+_out[0]);
97+
98+
// Debug.Log("TEST: getDataStruct()");
99+
// var _out = await _token.CallFunction("getDataStruct()", new string[]{"uint256","bool","address","string"});
100+
// Debug.Log("returned: "+_out[0]+" | "+_out[1]+" | "+_out[2]+" | "+_out[3]);
101+
102+
// Debug.Log("TEST: getPrimitiveMultiReturnData()");
103+
// var _out = await _token.CallFunction("getPrimitiveMultiReturnData()", new string[]{"uint256","string","bool","address"});
104+
// Debug.Log("returned length: "+_out.Count);
105+
// Debug.Log(_out[0]);
106+
// Debug.Log(_out[1]);
107+
// Debug.Log(_out[2]);
108+
// Debug.Log(_out[3]);
109+
110+
Debug.Log("TEST: getStructMultiReturnData()");
111+
var _out = await _token.CallFunction("getStructMultiReturnData()", new string[]{"uint256","string","bool","address","struct","uint256","string","bool","address"});
112+
foreach(object _o in _out) {
113+
Debug.Log(_o);
114+
}
115+
116+
// Debug.Log((new HexBigInteger("0x0000000000000000000000000000000000000000000000000000000000000060")).Value);
117+
// string _data = "0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000464617665000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003";
118+
// Debug.Log(_data.Substring(192));
35119
}
36120

37121
private async void GetChainId() {

0 commit comments

Comments
 (0)