Skip to content

Commit 479a247

Browse files
committed
save
1 parent d5bd531 commit 479a247

File tree

2 files changed

+184
-98
lines changed

2 files changed

+184
-98
lines changed

Web3/Assets/EasyEthereum/Scripts/EasyWeb3Controller.cs

Lines changed: 96 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -53,84 +53,92 @@ private async void Load() {
5353
// Debug.Log("total supply: "+_token.ValueFromDecimals(_totalSupply));
5454

5555
// // 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));
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));
6161

6262
// var _out = await _token.CallFunction("isSniper(address)", new string[]{"bool"}, new string[]{"0x34221445c2dd9fd3f41a8a8bfa7d49ec898e0ef4"});
6363
// bool _isSniper = (bool)_out[0];
6464
// Debug.Log(_isSniper);
6565

66-
// var _out = await _token.CallFunction("totalSupply()", new string[]{"int"});
67-
// BigInteger _supply = (BigInteger)_out[0];
68-
// Debug.Log(_out[0]);
66+
var _out = await _token.CallFunction("totalSupply()", new string[]{"uint"});
67+
BigInteger _supply = (BigInteger)_out[0];
68+
Debug.Log(_out[0]);
6969

70-
// Debug.Log("TEST: getUint256()");
71-
// var _out = await _token.CallFunction("getUint256()", new string[]{"uint"});
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[]{"struct(uint,string,bool,address)"});
100-
// foreach(object _o in _out) {
101-
// Debug.Log(_o);
102-
// }
70+
Debug.Log("TEST: getUint256()");
71+
_out = await _token.CallFunction("getUint256()", new string[]{"uint"});
72+
Debug.Log("returned: "+_out[0]);
73+
74+
Debug.Log("TEST: getUint128()");
75+
_out = await _token.CallFunction("getUint128()", new string[]{"uint"});
76+
Debug.Log("returned: "+_out[0]);
77+
78+
Debug.Log("TEST: getUint64()");
79+
_out = await _token.CallFunction("getUint64()", new string[]{"uint"});
80+
Debug.Log("returned: "+_out[0]);
81+
82+
Debug.Log("TEST: getUint32()");
83+
_out = await _token.CallFunction("getUint32()", new string[]{"uint"});
84+
Debug.Log("returned: "+_out[0]);
85+
86+
Debug.Log("TEST: getUint16()");
87+
_out = await _token.CallFunction("getUint16()", new string[]{"uint"});
88+
Debug.Log("returned: "+_out[0]);
89+
90+
Debug.Log("TEST: getUint8()");
91+
_out = await _token.CallFunction("getUint8()", new string[]{"uint"});
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+
_out = await _token.CallFunction("getDataStruct()", new string[]{"struct(uint,string,bool,address)"});
100+
foreach(object _o in _out) {
101+
Debug.Log(_o);
102+
}
103103

104-
// Debug.Log("TEST: getPrimitiveMultiReturnData()");
105-
// var _out = await _token.CallFunction("getPrimitiveMultiReturnData()", new string[]{"uint","string","bool","address"});
106-
// Debug.Log("returned length: "+_out.Count);
107-
// foreach(object _o in _out) {
108-
// Debug.Log(_o);
109-
// }
110-
111-
// Debug.Log("TEST: getStructMultiReturnData()");
112-
// var _out = await _token.CallFunction("getStructMultiReturnData()", new string[]{"uint","string","bool","address","struct(uint,string,bool,address)"});
113-
// foreach(object _o in _out) {
114-
// Debug.Log(_o);
115-
// }
116-
117-
// Debug.Log("TEST: getStructArr()");
118-
// var _out = await _token.CallFunction("getStructArr(DataStruct[] memory _arr)", new string[]{"struct[](uint,string,bool,address)"}, new string[]{});
119-
// foreach(object _o in _out) {
120-
// Debug.Log(_o);
121-
// }
122-
123-
// Debug.Log("TEST: getUintArr()");
124-
// var _out = await _token.CallFunction("getUintArr(uint256[])", new string[]{"uint[]"}, new string[]{"uint[](12415,23431,3555523)"});
125-
// BigInteger[] _intarr = (BigInteger[])_out[0];
126-
// foreach(BigInteger _int in _intarr) {
127-
// Debug.Log(_int);
128-
// }
104+
Debug.Log("TEST: getPrimitiveMultiReturnData()");
105+
_out = await _token.CallFunction("getPrimitiveMultiReturnData()", new string[]{"uint","string","bool","address"});
106+
Debug.Log("returned length: "+_out.Count);
107+
foreach(object _o in _out) {
108+
Debug.Log(_o);
109+
}
110+
111+
Debug.Log("TEST: getStructMultiReturnData()");
112+
_out = await _token.CallFunction("getStructMultiReturnData()", new string[]{"uint","string","bool","address","struct(uint,string,bool,address)"});
113+
foreach(object _o in _out) {
114+
Debug.Log(_o);
115+
}
116+
117+
Debug.Log("TEST: getUintArr()");
118+
_out = await _token.CallFunction("getUintArr(uint256[])", new string[]{"uint[]"}, new string[]{"uint[](12415,23431,3555523)"});
119+
BigInteger[] _intarr = (BigInteger[])_out[0];
120+
foreach(BigInteger _int in _intarr) {
121+
Debug.Log(_int);
122+
}
129123

130124
Debug.Log("TEST: getStrArr()");
131-
var _out = await _token.CallFunction("getStrArr(string[])", new string[]{"string[]"}, new string[]{"string[](abcqest,123,341agf3)"});
132-
BigInteger[] _strarr = (BigInteger[])_out[0];
133-
foreach(BigInteger _s in _strarr) {
125+
_out = await _token.CallFunction("getStrArr(string[])", new string[]{"string[]"}, new string[]{"string[](abcqest,123,341agf3)"});
126+
string[] _strarr = (string[])_out[0];
127+
foreach(string _s in _strarr) {
128+
Debug.Log(_s);
129+
}
130+
131+
Debug.Log("TEST: getBoolArr()");
132+
_out = await _token.CallFunction("getBoolArr(bool[])", new string[]{"bool[]"}, new string[]{"bool[](0,1,1)"});
133+
bool[] _boolarr = (bool[])_out[0];
134+
foreach(bool _s in _boolarr) {
135+
Debug.Log(_s);
136+
}
137+
138+
Debug.Log("TEST: getAddrArr()");
139+
_out = await _token.CallFunction("getAddrArr(address[])", new string[]{"address[]"}, new string[]{"address[](0x4C9B1DB55c8A89cb0312C8fD84B0cB650E648618,0x1A2933fbA0c6e959c9A2D2c933f3f8AD4aa9f06e)"});
140+
string[] _addrarr = (string[])_out[0];
141+
foreach(string _s in _addrarr) {
134142
Debug.Log(_s);
135143
}
136144
}
@@ -143,4 +151,22 @@ private async void GetChainId() {
143151

144152
#endregion
145153
}
146-
}
154+
}
155+
/*
156+
0000000000000000000000000000000000000000000000000000000000000020
157+
0000000000000000000000000000000000000000000000000000000000000002 (arrlen)
158+
0000000000000000000000000000000000000000000000000000000000000040 (arrptr) 64 <- count from this line
159+
0000000000000000000000000000000000000000000000000000000000000100 (arrptr2) 256
160+
000000000000000000000000000000000000000000000000000000000000007b (int)
161+
0000000000000000000000000000000000000000000000000000000000000080 (strptr)
162+
0000000000000000000000000000000000000000000000000000000000000001 (bool)
163+
000000000000000000000000250e75b9f33940506d1cf31fab63cfaa5ad98c95 (address)
164+
0000000000000000000000000000000000000000000000000000000000000009 (strlen)
165+
737472696e677661720000000000000000000000000000000000000000000000 (string)
166+
000000000000000000000000000000000000000000000000000000000000007b (int)
167+
0000000000000000000000000000000000000000000000000000000000000080 (strptr)
168+
0000000000000000000000000000000000000000000000000000000000000001 (bool)
169+
000000000000000000000000250e75b9f33940506d1cf31fab63cfaa5ad98c95 (address)
170+
0000000000000000000000000000000000000000000000000000000000000009 (strlen)
171+
737472696e677661720000000000000000000000000000000000000000000000 (string)
172+
*/

Web3/Assets/EasyEthereum/Scripts/Model.cs

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,34 +89,52 @@ public string Encode(string _signature, string[] _types, string[] _values) {
8989
foreach(string _in in _types) {
9090
switch (_in) {
9191
case "": break;
92+
case "string":
93+
_ret += Web3Utils.StringToHexBigInteger(_values[i].Length.ToString());
94+
_ret += Web3Utils.StringToHexString(_values[i]);
95+
break;
9296
case "address":
93-
_ret += Web3Utils.HexAddressToString(_values[i]);
97+
_ret += Web3Utils.AddressToHexString(_values[i]);
9498
break;
95-
default:
96-
if (_in.Contains("uint256[]") || _in.Contains("uint128[]") || _in.Contains("uint64[]") || _in.Contains("uint32[]") || _in.Contains("uint16[]") || _in.Contains("uint8[]") || _in.Contains("uint[]")) {
97-
string[] _ints = GetInputParams(_values[i]);
98-
_ret += Web3Utils.StringToHexBigInteger(((_ret.Length - 10 + 64)/2).ToString()); // ptr
99-
_ret += Web3Utils.StringToHexBigInteger(_ints.Length.ToString()); // len
100-
foreach (string _intstr in _ints) {
101-
_ret += Web3Utils.StringToHexBigInteger(_intstr);
102-
}
103-
} else if (_in.Contains("string[]")) {
104-
string[] _strings = GetInputParams(_values[i]);
105-
_ret += Web3Utils.StringToHexBigInteger(((_ret.Length - 10 + 64)/2).ToString()); // ptr
106-
_ret += Web3Utils.StringToHexBigInteger(_strings.Length.ToString()); // len
107-
int _tmpLen = _strings.Length*64;
108-
for (int k = 0; k < _strings.Length; k++) {
109-
_ret += Web3Utils.StringToHexBigInteger((((_tmpLen + (k * 128)) / 2).ToString())); // ptr
110-
}
111-
foreach (string _s in _strings) {
112-
_ret += Web3Utils.StringToHexBigInteger(_s.Length.ToString());
113-
_ret += Web3Utils.StringToHexString(_s);
114-
}
115-
} else {
116-
Debug.LogWarning("Could not encode function ["+_signature+"]. Unsupported input type found in signature ("+_in+").");
117-
return _ret;
99+
case "uint256[]":
100+
case "uint128[]":
101+
case "uint64[]":
102+
case "uint32[]":
103+
case "uint16[]":
104+
case "uint8[]":
105+
case "bool[]":
106+
string[] _ints = GetInputParams(_values[i]);
107+
_ret += Web3Utils.StringToHexBigInteger(((_ret.Length - 10 + 64)/2).ToString()); // ptr
108+
_ret += Web3Utils.StringToHexBigInteger(_ints.Length.ToString()); // len
109+
foreach (string _intstr in _ints) {
110+
_ret += Web3Utils.StringToHexBigInteger(_intstr);
118111
}
119112
break;
113+
case "string[]":
114+
case "byte[]":
115+
string[] _strings = GetInputParams(_values[i]);
116+
_ret += Web3Utils.StringToHexBigInteger(((_ret.Length - 10 + 64)/2).ToString()); // ptr
117+
_ret += Web3Utils.StringToHexBigInteger(_strings.Length.ToString()); // len
118+
int _tmpLen = _strings.Length*64;
119+
for (int k = 0; k < _strings.Length; k++) {
120+
_ret += Web3Utils.StringToHexBigInteger((((_tmpLen + (k * 128)) / 2).ToString())); // ptr
121+
}
122+
foreach (string _s in _strings) {
123+
_ret += Web3Utils.StringToHexBigInteger(_s.Length.ToString());
124+
_ret += Web3Utils.StringToHexString(_s);
125+
}
126+
break;
127+
case "address[]":
128+
string[] _addrs = GetInputParams(_values[i]);
129+
_ret += Web3Utils.StringToHexBigInteger(((_ret.Length - 10 + 64)/2).ToString()); // ptr
130+
_ret += Web3Utils.StringToHexBigInteger(_addrs.Length.ToString()); // len
131+
foreach (string _s in _addrs) {
132+
_ret += Web3Utils.AddressToHexString(_s);
133+
}
134+
break;
135+
default:
136+
Debug.LogWarning("Could not encode function ["+_signature+"]. Unsupported input type found in signature ("+_in+").");
137+
return _ret;
120138
}
121139
i++;
122140
}
@@ -138,42 +156,81 @@ private string[] GetInputParams(string _values) {
138156
}
139157
public class Decoder {
140158
public List<object> Decode(string _hex, string[] _outputs, ref int _len) {
141-
Debug.Log("decode: "+_hex);
159+
// Debug.Log("decode: "+_hex);
142160
List<object> _ret = new List<object>();
143161
BigInteger _ptr = 0;
144162
int _cursor = 0;
145163
List<int[]> _history = new List<int[]>();
146164
for (int k = 0; k < _outputs.Length; k++) {
147165
string _out = _outputs[k];
148-
Debug.Log("Checking output: "+_out+" where cursor = "+_cursor);
166+
// Debug.Log("Checking output: "+_out+" where cursor = "+_cursor);
149167
if (DidAnalyze(_history, ref _cursor)) {
150-
Debug.Log("Already analyzed here. Moving cursor to "+_cursor);
168+
// Debug.Log("Already analyzed here. Moving cursor to "+_cursor);
151169
k--;
152170
continue;
153171
}
154172

155173
string _data = "";
174+
BigInteger _arrlen = 0;
156175
switch (_out) {
157176
case "bool":
158177
_data = _hex.Substring(_cursor,64);
159178
_ret.Add((new HexBigInteger(_data)).Value == 1 ? true : false);
160179
_cursor += 64;
161180
break;
181+
case "bool[]":
182+
_ptr = GetPointerOrLength(_hex, _cursor) * 2;
183+
_arrlen = GetPointerOrLength(_hex, (int)_ptr);
184+
bool[] _boolarr = new bool[(int)_arrlen];
185+
for (int i = 0; i < _boolarr.Length; i++) {
186+
_data = _hex.Substring((int)_ptr + 64*(i+1), 64);
187+
_boolarr[i] = (new HexBigInteger(_data)).Value == 1 ? true : false;
188+
}
189+
_ret.Add(_boolarr);
190+
AddHistory(ref _history, _ptr, 64*_boolarr.Length+64);
191+
_cursor += 64;
192+
break;
193+
case "byte": // dynamic
162194
case "string": // dynamic
163195
_ptr = GetPointerOrLength(_hex, _cursor) * 2;
164196
_data = _hex.Substring((int)_ptr,128);
165197
_ret.Add(Web3Utils.HexToString(_data));
166198
AddHistory(ref _history, _ptr, 128);
167199
_cursor += 64;
168200
break;
201+
case "byte[]":
202+
case "string[]":
203+
_ptr = GetPointerOrLength(_hex, _cursor) * 2;
204+
_arrlen = GetPointerOrLength(_hex, (int)_ptr);
205+
string[] _strarr = new string[(int)_arrlen];
206+
for (int i = 0; i < _strarr.Length; i++) {
207+
_data = _hex.Substring((int)_ptr + 128*(i+1)+128, 128);
208+
_strarr[i] = Web3Utils.HexToString(_data);
209+
}
210+
_ret.Add(_strarr);
211+
AddHistory(ref _history, _ptr, 128*_strarr.Length+64);
212+
_cursor += 64;
213+
break;
169214
case "address":
170215
_data = _hex.Substring(_cursor,64);
171216
_ret.Add(Web3Utils.HexAddressToString(_data));
172217
_cursor += 64;
173218
break;
219+
case "address[]":
220+
_ptr = GetPointerOrLength(_hex, _cursor) * 2;
221+
_arrlen = GetPointerOrLength(_hex, (int)_ptr);
222+
string[] _addrarr = new string[(int)_arrlen];
223+
for (int i = 0; i < _addrarr.Length; i++) {
224+
_data = _hex.Substring((int)_ptr + 64*(i+1), 64);
225+
_addrarr[i] = Web3Utils.HexAddressToString(_data);
226+
}
227+
_ret.Add(_addrarr);
228+
AddHistory(ref _history, _ptr, 64*_addrarr.Length+64);
229+
_cursor += 64;
230+
break;
174231
case "uint[]":
175232
_ptr = GetPointerOrLength(_hex, _cursor) * 2;
176-
BigInteger _arrlen = GetPointerOrLength(_hex, (int)_ptr);
233+
_arrlen = GetPointerOrLength(_hex, (int)_ptr);
177234
BigInteger[] _intarr = new BigInteger[(int)_arrlen];
178235
for (int i = 0; i < _intarr.Length; i++) {
179236
_data = _hex.Substring((int)_ptr + 64*(i+1), 64);
@@ -341,6 +398,9 @@ public static string HexToString(string _hex) {
341398
public static string HexAddressToString(string _addr) {
342399
return "0x"+_addr.Replace("000000000000000000000000", "");
343400
}
401+
public static string AddressToHexString(string _addr) {
402+
return "000000000000000000000000"+_addr.Replace("0x","");
403+
}
344404
public static string StringToHexBigInteger(string _intstr) {
345405
BigInteger _int = 0;
346406
BigInteger.TryParse(_intstr, out _int);

0 commit comments

Comments
 (0)