This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* First attemp to complete input data * Masking some errors for unsupported transactions * Removed commented code * Formated code * Supporting cairo v1 calldata structure * Supporting cairo v1 calldata structure * Support for some proxy contracts * Fixed keccak calculation and added support for binary abi * Removed debug information * mix format * fixed test
- Loading branch information
1 parent
3f18fa8
commit 83bcfb5
Showing
6 changed files
with
351 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
defmodule StarknetExplorer.Calldata do | ||
def from_plain_calldata([array_len | rest], nil) do | ||
{calls, [_calldata_length | calldata]} = | ||
List.foldl( | ||
Enum.to_list(1..felt_to_int(array_len)), | ||
{[], rest}, | ||
fn _, {acc_current, acc_rest} -> | ||
{new, new_rest} = get_call_header_v0(acc_rest) | ||
{[new | acc_current], new_rest} | ||
end | ||
) | ||
|
||
Enum.map( | ||
Enum.reverse(calls), | ||
fn call -> | ||
%{call | :calldata => Enum.slice(calldata, call.data_offset, call.data_len)} | ||
end | ||
) | ||
end | ||
|
||
# we assume contract_class_version 0.1.0 | ||
def from_plain_calldata([array_len | rest], _contract_class_version) do | ||
{calls, _} = | ||
List.foldl( | ||
Enum.to_list(1..felt_to_int(array_len)), | ||
{[], rest}, | ||
fn _, {acc_current, acc_rest} -> | ||
{new, new_rest} = get_call_header_v1(acc_rest) | ||
{[new | acc_current], new_rest} | ||
end | ||
) | ||
|
||
Enum.reverse(calls) | ||
end | ||
|
||
def get_call_header_v0([to, selector, data_offset, data_len | rest]) do | ||
{%{ | ||
:address => to, | ||
:selector => selector, | ||
:data_offset => felt_to_int(data_offset), | ||
:data_len => felt_to_int(data_len), | ||
:calldata => [] | ||
}, rest} | ||
end | ||
|
||
def get_call_header_v1([to, selector, data_len | rest]) do | ||
data_length = felt_to_int(data_len) | ||
{calldata, rest} = Enum.split(rest, data_length) | ||
|
||
{%{ | ||
:address => to, | ||
:selector => selector, | ||
:data_len => felt_to_int(data_len), | ||
:calldata => calldata | ||
}, rest} | ||
end | ||
|
||
def keccak(value) do | ||
<<_::6, result::250>> = ExKeccak.hash_256(value) | ||
("0x" <> Integer.to_string(result, 16)) |> String.downcase() | ||
end | ||
|
||
def as_fn_call(nil, _calldata) do | ||
nil | ||
end | ||
|
||
def as_fn_call(input, calldata) do | ||
%{:name => input["name"], :args => as_fn_inputs(input["inputs"], calldata)} | ||
end | ||
|
||
def as_fn_inputs(inputs, calldata) do | ||
{result, _} = | ||
List.foldl( | ||
inputs, | ||
{[], calldata}, | ||
fn input, {acc_current, acc_calldata} -> | ||
{fn_input, calldata_rest} = as_fn_input(input, acc_calldata) | ||
{[fn_input | acc_current], calldata_rest} | ||
end | ||
) | ||
|
||
Enum.reverse(result) | ||
end | ||
|
||
def as_fn_input(input, calldata) do | ||
{value, calldata_rest} = get_value_for_type(input["type"], calldata) | ||
{%{:name => input["name"], :type => input["type"], :value => value}, calldata_rest} | ||
end | ||
|
||
def get_value_for_type("Uint256", [value1, value2 | rest]) do | ||
{[value2, value1], rest} | ||
end | ||
|
||
def get_value_for_type(_, [value | rest]) do | ||
{value, rest} | ||
end | ||
|
||
def felt_to_int(<<"0x", hexa_value::binary>>) do | ||
{value, _} = Integer.parse(hexa_value, 16) | ||
value | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.