From 1b3d6c32614f09e95c261f8fd55b0c1bb530c9d7 Mon Sep 17 00:00:00 2001 From: rishikpulhani Date: Tue, 24 Sep 2024 11:36:22 +0530 Subject: [PATCH] partially finished parse_full_abi_test.go --- athena_abi/abi.go | 16 +++ athena_abi/core.go | 42 ++++++- athena_abi/parse.go | 143 +++++++++++++++++------ athena_abi/parse_full_abi_test.go | 187 ++++++++++++++++++++++++++++++ athena_abi/parse_struct_test.go | 2 +- backup.txt | 90 ++++++++++++++ 6 files changed, 438 insertions(+), 42 deletions(-) create mode 100644 athena_abi/abi.go create mode 100644 athena_abi/parse_full_abi_test.go create mode 100644 backup.txt diff --git a/athena_abi/abi.go b/athena_abi/abi.go new file mode 100644 index 00000000..4d60821f --- /dev/null +++ b/athena_abi/abi.go @@ -0,0 +1,16 @@ +package athena_abi + +const FIRST_CLASS_HASH = "0x010455c752b86932ce552f2b0fe81a880746649b9aee7e0d842bf3f52378f9f8" +const FIRST_CLASS_ABI = `[{"members":[{"name":"index","offset":0,"type":"felt"},{"name":"values","offset":1,"type":"(felt,felt)"}],"name":"IndexAndValues","size":3,"type":"struct"},{"inputs":[{"name":"index","type":"felt"},{"name":"diffs_len","type":"felt"},{"name":"diffs","type":"felt*"}],"name":"advance_counter","outputs":[],"type":"function"},{"inputs":[{"name":"address","type":"felt"},{"name":"value","type":"felt"}],"name":"constructor","outputs":[],"type":"constructor"},{"inputs":[{"name":"index_and_x","type":"IndexAndValues"}],"name":"xor_counters","outputs":[],"type":"function"},{"inputs":[{"name":"address","type":"felt"},{"name":"index_and_x","type":"IndexAndValues"}],"name":"call_xor_counters","outputs":[],"type":"function"},{"inputs":[{"name":"index","type":"felt"}],"name":"add_signature_to_counters","outputs":[],"type":"function"},{"inputs":[{"name":"address","type":"felt"},{"name":"value","type":"felt"}],"name":"set_value","outputs":[],"type":"function"},{"inputs":[{"name":"address","type":"felt"}],"name":"get_value","outputs":[{"name":"res","type":"felt"}],"type":"function"},{"inputs":[],"name":"entry_point","outputs":[],"type":"function"},{"inputs":[],"name":"test_builtins","outputs":[{"name":"result","type":"felt"}],"type":"function"},{"inputs":[{"name":"to_address","type":"felt"}],"name":"send_message","outputs":[],"type":"function"},{"inputs":[{"name":"contract_address","type":"felt"},{"name":"function_selector","type":"felt"},{"name":"calldata_len","type":"felt"},{"name":"calldata","type":"felt*"}],"name":"test_call_contract","outputs":[],"type":"function"},{"inputs":[{"name":"contract_address","type":"felt"},{"name":"function_selector","type":"felt"},{"name":"calldata_len","type":"felt"},{"name":"calldata","type":"felt*"}],"name":"test_delegate_call","outputs":[],"type":"function"},{"inputs":[{"name":"from_address","type":"felt"},{"name":"amount","type":"felt"}],"name":"deposit","outputs":[],"type":"l1_handler"},{"inputs":[{"name":"expected_address","type":"felt"}],"name":"test_get_caller_address","outputs":[],"type":"function"},{"inputs":[{"name":"expected_address","type":"felt"}],"name":"test_get_sequencer_address","outputs":[],"type":"function"},{"inputs":[{"name":"expected_address","type":"felt"}],"name":"test_get_contract_address","outputs":[],"type":"function"},{"inputs":[{"name":"other_contract_address","type":"felt"},{"name":"address","type":"felt"}],"name":"test_call_storage_consistency","outputs":[],"type":"function"},{"inputs":[{"name":"other_contract_address","type":"felt"},{"name":"depth","type":"felt"}],"name":"test_re_entrance","outputs":[],"type":"function"},{"inputs":[{"name":"value","type":"felt"}],"name":"add_value","outputs":[],"type":"function"},{"inputs":[{"name":"self_address","type":"felt"},{"name":"value","type":"felt"}],"name":"recursive_add_value","outputs":[],"type":"function"},{"inputs":[{"name":"address","type":"felt"}],"name":"increase_value","outputs":[],"type":"function"},{"inputs":[{"name":"self_address","type":"felt"},{"name":"arr_len","type":"felt"},{"name":"arr","type":"felt*"}],"name":"test_call_with_array","outputs":[],"type":"function"}]` + +const STARKNET_USDC_CLASS_HASH = "0x05ffbcfeb50d200a0677c48a129a11245a3fc519d1d98d76882d1c9a1b19c6ed" +const STARKNET_USDC_ABI = `[{"type":"impl","name":"MintableToken","interface_name":"src::mintable_token_interface::IMintableToken"},{"type":"struct","name":"core::integer::u256","members":[{"name":"low","type":"core::integer::u128"},{"name":"high","type":"core::integer::u128"}]},{"type":"interface","name":"src::mintable_token_interface::IMintableToken","items":[{"type":"function","name":"permissioned_mint","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"permissioned_burn","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[],"state_mutability":"external"}]},{"type":"impl","name":"MintableTokenCamelImpl","interface_name":"src::mintable_token_interface::IMintableTokenCamel"},{"type":"interface","name":"src::mintable_token_interface::IMintableTokenCamel","items":[{"type":"function","name":"permissionedMint","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"permissionedBurn","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[],"state_mutability":"external"}]},{"type":"impl","name":"Replaceable","interface_name":"src::replaceability_interface::IReplaceable"},{"type":"struct","name":"core::array::Span::","members":[{"name":"snapshot","type":"@core::array::Array::"}]},{"type":"struct","name":"src::replaceability_interface::EICData","members":[{"name":"eic_hash","type":"core::starknet::class_hash::ClassHash"},{"name":"eic_init_data","type":"core::array::Span::"}]},{"type":"enum","name":"core::option::Option::","variants":[{"name":"Some","type":"src::replaceability_interface::EICData"},{"name":"None","type":"()"}]},{"type":"enum","name":"core::bool","variants":[{"name":"False","type":"()"},{"name":"True","type":"()"}]},{"type":"struct","name":"src::replaceability_interface::ImplementationData","members":[{"name":"impl_hash","type":"core::starknet::class_hash::ClassHash"},{"name":"eic_data","type":"core::option::Option::"},{"name":"final","type":"core::bool"}]},{"type":"interface","name":"src::replaceability_interface::IReplaceable","items":[{"type":"function","name":"get_upgrade_delay","inputs":[],"outputs":[{"type":"core::integer::u64"}],"state_mutability":"view"},{"type":"function","name":"get_impl_activation_time","inputs":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData"}],"outputs":[{"type":"core::integer::u64"}],"state_mutability":"view"},{"type":"function","name":"add_new_implementation","inputs":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"remove_implementation","inputs":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"replace_to","inputs":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData"}],"outputs":[],"state_mutability":"external"}]},{"type":"impl","name":"AccessControlImplExternal","interface_name":"src::access_control_interface::IAccessControl"},{"type":"interface","name":"src::access_control_interface::IAccessControl","items":[{"type":"function","name":"has_role","inputs":[{"name":"role","type":"core::felt252"},{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[{"type":"core::bool"}],"state_mutability":"view"},{"type":"function","name":"get_role_admin","inputs":[{"name":"role","type":"core::felt252"}],"outputs":[{"type":"core::felt252"}],"state_mutability":"view"}]},{"type":"impl","name":"RolesImpl","interface_name":"src::roles_interface::IMinimalRoles"},{"type":"interface","name":"src::roles_interface::IMinimalRoles","items":[{"type":"function","name":"is_governance_admin","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[{"type":"core::bool"}],"state_mutability":"view"},{"type":"function","name":"is_upgrade_governor","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[{"type":"core::bool"}],"state_mutability":"view"},{"type":"function","name":"register_governance_admin","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"remove_governance_admin","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"register_upgrade_governor","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"remove_upgrade_governor","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"renounce","inputs":[{"name":"role","type":"core::felt252"}],"outputs":[],"state_mutability":"external"}]},{"type":"impl","name":"ERC20Impl","interface_name":"openzeppelin::token::erc20::interface::IERC20"},{"type":"interface","name":"openzeppelin::token::erc20::interface::IERC20","items":[{"type":"function","name":"name","inputs":[],"outputs":[{"type":"core::felt252"}],"state_mutability":"view"},{"type":"function","name":"symbol","inputs":[],"outputs":[{"type":"core::felt252"}],"state_mutability":"view"},{"type":"function","name":"decimals","inputs":[],"outputs":[{"type":"core::integer::u8"}],"state_mutability":"view"},{"type":"function","name":"total_supply","inputs":[],"outputs":[{"type":"core::integer::u256"}],"state_mutability":"view"},{"type":"function","name":"balance_of","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[{"type":"core::integer::u256"}],"state_mutability":"view"},{"type":"function","name":"allowance","inputs":[{"name":"owner","type":"core::starknet::contract_address::ContractAddress"},{"name":"spender","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[{"type":"core::integer::u256"}],"state_mutability":"view"},{"type":"function","name":"transfer","inputs":[{"name":"recipient","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"},{"type":"function","name":"transfer_from","inputs":[{"name":"sender","type":"core::starknet::contract_address::ContractAddress"},{"name":"recipient","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"},{"type":"function","name":"approve","inputs":[{"name":"spender","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"}]},{"type":"impl","name":"ERC20CamelOnlyImpl","interface_name":"openzeppelin::token::erc20::interface::IERC20CamelOnly"},{"type":"interface","name":"openzeppelin::token::erc20::interface::IERC20CamelOnly","items":[{"type":"function","name":"totalSupply","inputs":[],"outputs":[{"type":"core::integer::u256"}],"state_mutability":"view"},{"type":"function","name":"balanceOf","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[{"type":"core::integer::u256"}],"state_mutability":"view"},{"type":"function","name":"transferFrom","inputs":[{"name":"sender","type":"core::starknet::contract_address::ContractAddress"},{"name":"recipient","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"}]},{"type":"constructor","name":"constructor","inputs":[{"name":"name","type":"core::felt252"},{"name":"symbol","type":"core::felt252"},{"name":"decimals","type":"core::integer::u8"},{"name":"initial_supply","type":"core::integer::u256"},{"name":"recipient","type":"core::starknet::contract_address::ContractAddress"},{"name":"permitted_minter","type":"core::starknet::contract_address::ContractAddress"},{"name":"provisional_governance_admin","type":"core::starknet::contract_address::ContractAddress"},{"name":"upgrade_delay","type":"core::integer::u64"}]},{"type":"function","name":"increase_allowance","inputs":[{"name":"spender","type":"core::starknet::contract_address::ContractAddress"},{"name":"added_value","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"},{"type":"function","name":"decrease_allowance","inputs":[{"name":"spender","type":"core::starknet::contract_address::ContractAddress"},{"name":"subtracted_value","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"},{"type":"function","name":"increaseAllowance","inputs":[{"name":"spender","type":"core::starknet::contract_address::ContractAddress"},{"name":"addedValue","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"},{"type":"function","name":"decreaseAllowance","inputs":[{"name":"spender","type":"core::starknet::contract_address::ContractAddress"},{"name":"subtractedValue","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"},{"type":"event","name":"openzeppelin::token::erc20_v070::erc20::ERC20::Transfer","kind":"struct","members":[{"name":"from","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"to","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"value","type":"core::integer::u256","kind":"data"}]},{"type":"event","name":"openzeppelin::token::erc20_v070::erc20::ERC20::Approval","kind":"struct","members":[{"name":"owner","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"spender","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"value","type":"core::integer::u256","kind":"data"}]},{"type":"event","name":"src::replaceability_interface::ImplementationAdded","kind":"struct","members":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData","kind":"data"}]},{"type":"event","name":"src::replaceability_interface::ImplementationRemoved","kind":"struct","members":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData","kind":"data"}]},{"type":"event","name":"src::replaceability_interface::ImplementationReplaced","kind":"struct","members":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData","kind":"data"}]},{"type":"event","name":"src::replaceability_interface::ImplementationFinalized","kind":"struct","members":[{"name":"impl_hash","type":"core::starknet::class_hash::ClassHash","kind":"data"}]},{"type":"event","name":"src::access_control_interface::RoleGranted","kind":"struct","members":[{"name":"role","type":"core::felt252","kind":"data"},{"name":"account","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"sender","type":"core::starknet::contract_address::ContractAddress","kind":"data"}]},{"type":"event","name":"src::access_control_interface::RoleRevoked","kind":"struct","members":[{"name":"role","type":"core::felt252","kind":"data"},{"name":"account","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"sender","type":"core::starknet::contract_address::ContractAddress","kind":"data"}]},{"type":"event","name":"src::access_control_interface::RoleAdminChanged","kind":"struct","members":[{"name":"role","type":"core::felt252","kind":"data"},{"name":"previous_admin_role","type":"core::felt252","kind":"data"},{"name":"new_admin_role","type":"core::felt252","kind":"data"}]},{"type":"event","name":"src::roles_interface::GovernanceAdminAdded","kind":"struct","members":[{"name":"added_account","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"added_by","type":"core::starknet::contract_address::ContractAddress","kind":"data"}]},{"type":"event","name":"src::roles_interface::GovernanceAdminRemoved","kind":"struct","members":[{"name":"removed_account","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"removed_by","type":"core::starknet::contract_address::ContractAddress","kind":"data"}]},{"type":"event","name":"src::roles_interface::UpgradeGovernorAdded","kind":"struct","members":[{"name":"added_account","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"added_by","type":"core::starknet::contract_address::ContractAddress","kind":"data"}]},{"type":"event","name":"src::roles_interface::UpgradeGovernorRemoved","kind":"struct","members":[{"name":"removed_account","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"removed_by","type":"core::starknet::contract_address::ContractAddress","kind":"data"}]},{"type":"event","name":"openzeppelin::token::erc20_v070::erc20::ERC20::Event","kind":"enum","variants":[{"name":"Transfer","type":"openzeppelin::token::erc20_v070::erc20::ERC20::Transfer","kind":"nested"},{"name":"Approval","type":"openzeppelin::token::erc20_v070::erc20::ERC20::Approval","kind":"nested"},{"name":"ImplementationAdded","type":"src::replaceability_interface::ImplementationAdded","kind":"nested"},{"name":"ImplementationRemoved","type":"src::replaceability_interface::ImplementationRemoved","kind":"nested"},{"name":"ImplementationReplaced","type":"src::replaceability_interface::ImplementationReplaced","kind":"nested"},{"name":"ImplementationFinalized","type":"src::replaceability_interface::ImplementationFinalized","kind":"nested"},{"name":"RoleGranted","type":"src::access_control_interface::RoleGranted","kind":"nested"},{"name":"RoleRevoked","type":"src::access_control_interface::RoleRevoked","kind":"nested"},{"name":"RoleAdminChanged","type":"src::access_control_interface::RoleAdminChanged","kind":"nested"},{"name":"GovernanceAdminAdded","type":"src::roles_interface::GovernanceAdminAdded","kind":"nested"},{"name":"GovernanceAdminRemoved","type":"src::roles_interface::GovernanceAdminRemoved","kind":"nested"},{"name":"UpgradeGovernorAdded","type":"src::roles_interface::UpgradeGovernorAdded","kind":"nested"},{"name":"UpgradeGovernorRemoved","type":"src::roles_interface::UpgradeGovernorRemoved","kind":"nested"}]}]` + +const STARKNET_ETH_CLASS_HASH = "0x05ffbcfeb50d200a0677c48a129a11245a3fc519d1d98d76882d1c9a1b19c6ed" +const STARKNET_ETH_ABI = `[{"type":"impl","name":"MintableToken","interface_name":"src::mintable_token_interface::IMintableToken"},{"type":"struct","name":"core::integer::u256","members":[{"name":"low","type":"core::integer::u128"},{"name":"high","type":"core::integer::u128"}]},{"type":"interface","name":"src::mintable_token_interface::IMintableToken","items":[{"type":"function","name":"permissioned_mint","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"permissioned_burn","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[],"state_mutability":"external"}]},{"type":"impl","name":"MintableTokenCamelImpl","interface_name":"src::mintable_token_interface::IMintableTokenCamel"},{"type":"interface","name":"src::mintable_token_interface::IMintableTokenCamel","items":[{"type":"function","name":"permissionedMint","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"permissionedBurn","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[],"state_mutability":"external"}]},{"type":"impl","name":"Replaceable","interface_name":"src::replaceability_interface::IReplaceable"},{"type":"struct","name":"core::array::Span::","members":[{"name":"snapshot","type":"@core::array::Array::"}]},{"type":"struct","name":"src::replaceability_interface::EICData","members":[{"name":"eic_hash","type":"core::starknet::class_hash::ClassHash"},{"name":"eic_init_data","type":"core::array::Span::"}]},{"type":"enum","name":"core::option::Option::","variants":[{"name":"Some","type":"src::replaceability_interface::EICData"},{"name":"None","type":"()"}]},{"type":"enum","name":"core::bool","variants":[{"name":"False","type":"()"},{"name":"True","type":"()"}]},{"type":"struct","name":"src::replaceability_interface::ImplementationData","members":[{"name":"impl_hash","type":"core::starknet::class_hash::ClassHash"},{"name":"eic_data","type":"core::option::Option::"},{"name":"final","type":"core::bool"}]},{"type":"interface","name":"src::replaceability_interface::IReplaceable","items":[{"type":"function","name":"get_upgrade_delay","inputs":[],"outputs":[{"type":"core::integer::u64"}],"state_mutability":"view"},{"type":"function","name":"get_impl_activation_time","inputs":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData"}],"outputs":[{"type":"core::integer::u64"}],"state_mutability":"view"},{"type":"function","name":"add_new_implementation","inputs":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"remove_implementation","inputs":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"replace_to","inputs":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData"}],"outputs":[],"state_mutability":"external"}]},{"type":"impl","name":"AccessControlImplExternal","interface_name":"src::access_control_interface::IAccessControl"},{"type":"interface","name":"src::access_control_interface::IAccessControl","items":[{"type":"function","name":"has_role","inputs":[{"name":"role","type":"core::felt252"},{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[{"type":"core::bool"}],"state_mutability":"view"},{"type":"function","name":"get_role_admin","inputs":[{"name":"role","type":"core::felt252"}],"outputs":[{"type":"core::felt252"}],"state_mutability":"view"}]},{"type":"impl","name":"RolesImpl","interface_name":"src::roles_interface::IMinimalRoles"},{"type":"interface","name":"src::roles_interface::IMinimalRoles","items":[{"type":"function","name":"is_governance_admin","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[{"type":"core::bool"}],"state_mutability":"view"},{"type":"function","name":"is_upgrade_governor","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[{"type":"core::bool"}],"state_mutability":"view"},{"type":"function","name":"register_governance_admin","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"remove_governance_admin","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"register_upgrade_governor","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"remove_upgrade_governor","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"renounce","inputs":[{"name":"role","type":"core::felt252"}],"outputs":[],"state_mutability":"external"}]},{"type":"impl","name":"ERC20Impl","interface_name":"openzeppelin::token::erc20::interface::IERC20"},{"type":"interface","name":"openzeppelin::token::erc20::interface::IERC20","items":[{"type":"function","name":"name","inputs":[],"outputs":[{"type":"core::felt252"}],"state_mutability":"view"},{"type":"function","name":"symbol","inputs":[],"outputs":[{"type":"core::felt252"}],"state_mutability":"view"},{"type":"function","name":"decimals","inputs":[],"outputs":[{"type":"core::integer::u8"}],"state_mutability":"view"},{"type":"function","name":"total_supply","inputs":[],"outputs":[{"type":"core::integer::u256"}],"state_mutability":"view"},{"type":"function","name":"balance_of","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[{"type":"core::integer::u256"}],"state_mutability":"view"},{"type":"function","name":"allowance","inputs":[{"name":"owner","type":"core::starknet::contract_address::ContractAddress"},{"name":"spender","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[{"type":"core::integer::u256"}],"state_mutability":"view"},{"type":"function","name":"transfer","inputs":[{"name":"recipient","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"},{"type":"function","name":"transfer_from","inputs":[{"name":"sender","type":"core::starknet::contract_address::ContractAddress"},{"name":"recipient","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"},{"type":"function","name":"approve","inputs":[{"name":"spender","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"}]},{"type":"impl","name":"ERC20CamelOnlyImpl","interface_name":"openzeppelin::token::erc20::interface::IERC20CamelOnly"},{"type":"interface","name":"openzeppelin::token::erc20::interface::IERC20CamelOnly","items":[{"type":"function","name":"totalSupply","inputs":[],"outputs":[{"type":"core::integer::u256"}],"state_mutability":"view"},{"type":"function","name":"balanceOf","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"}],"outputs":[{"type":"core::integer::u256"}],"state_mutability":"view"},{"type":"function","name":"transferFrom","inputs":[{"name":"sender","type":"core::starknet::contract_address::ContractAddress"},{"name":"recipient","type":"core::starknet::contract_address::ContractAddress"},{"name":"amount","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"}]},{"type":"constructor","name":"constructor","inputs":[{"name":"name","type":"core::felt252"},{"name":"symbol","type":"core::felt252"},{"name":"decimals","type":"core::integer::u8"},{"name":"initial_supply","type":"core::integer::u256"},{"name":"recipient","type":"core::starknet::contract_address::ContractAddress"},{"name":"permitted_minter","type":"core::starknet::contract_address::ContractAddress"},{"name":"provisional_governance_admin","type":"core::starknet::contract_address::ContractAddress"},{"name":"upgrade_delay","type":"core::integer::u64"}]},{"type":"function","name":"increase_allowance","inputs":[{"name":"spender","type":"core::starknet::contract_address::ContractAddress"},{"name":"added_value","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"},{"type":"function","name":"decrease_allowance","inputs":[{"name":"spender","type":"core::starknet::contract_address::ContractAddress"},{"name":"subtracted_value","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"},{"type":"function","name":"increaseAllowance","inputs":[{"name":"spender","type":"core::starknet::contract_address::ContractAddress"},{"name":"addedValue","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"},{"type":"function","name":"decreaseAllowance","inputs":[{"name":"spender","type":"core::starknet::contract_address::ContractAddress"},{"name":"subtractedValue","type":"core::integer::u256"}],"outputs":[{"type":"core::bool"}],"state_mutability":"external"},{"type":"event","name":"openzeppelin::token::erc20_v070::erc20::ERC20::Transfer","kind":"struct","members":[{"name":"from","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"to","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"value","type":"core::integer::u256","kind":"data"}]},{"type":"event","name":"openzeppelin::token::erc20_v070::erc20::ERC20::Approval","kind":"struct","members":[{"name":"owner","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"spender","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"value","type":"core::integer::u256","kind":"data"}]},{"type":"event","name":"src::replaceability_interface::ImplementationAdded","kind":"struct","members":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData","kind":"data"}]},{"type":"event","name":"src::replaceability_interface::ImplementationRemoved","kind":"struct","members":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData","kind":"data"}]},{"type":"event","name":"src::replaceability_interface::ImplementationReplaced","kind":"struct","members":[{"name":"implementation_data","type":"src::replaceability_interface::ImplementationData","kind":"data"}]},{"type":"event","name":"src::replaceability_interface::ImplementationFinalized","kind":"struct","members":[{"name":"impl_hash","type":"core::starknet::class_hash::ClassHash","kind":"data"}]},{"type":"event","name":"src::access_control_interface::RoleGranted","kind":"struct","members":[{"name":"role","type":"core::felt252","kind":"data"},{"name":"account","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"sender","type":"core::starknet::contract_address::ContractAddress","kind":"data"}]},{"type":"event","name":"src::access_control_interface::RoleRevoked","kind":"struct","members":[{"name":"role","type":"core::felt252","kind":"data"},{"name":"account","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"sender","type":"core::starknet::contract_address::ContractAddress","kind":"data"}]},{"type":"event","name":"src::access_control_interface::RoleAdminChanged","kind":"struct","members":[{"name":"role","type":"core::felt252","kind":"data"},{"name":"previous_admin_role","type":"core::felt252","kind":"data"},{"name":"new_admin_role","type":"core::felt252","kind":"data"}]},{"type":"event","name":"src::roles_interface::GovernanceAdminAdded","kind":"struct","members":[{"name":"added_account","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"added_by","type":"core::starknet::contract_address::ContractAddress","kind":"data"}]},{"type":"event","name":"src::roles_interface::GovernanceAdminRemoved","kind":"struct","members":[{"name":"removed_account","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"removed_by","type":"core::starknet::contract_address::ContractAddress","kind":"data"}]},{"type":"event","name":"src::roles_interface::UpgradeGovernorAdded","kind":"struct","members":[{"name":"added_account","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"added_by","type":"core::starknet::contract_address::ContractAddress","kind":"data"}]},{"type":"event","name":"src::roles_interface::UpgradeGovernorRemoved","kind":"struct","members":[{"name":"removed_account","type":"core::starknet::contract_address::ContractAddress","kind":"data"},{"name":"removed_by","type":"core::starknet::contract_address::ContractAddress","kind":"data"}]},{"type":"event","name":"openzeppelin::token::erc20_v070::erc20::ERC20::Event","kind":"enum","variants":[{"name":"Transfer","type":"openzeppelin::token::erc20_v070::erc20::ERC20::Transfer","kind":"nested"},{"name":"Approval","type":"openzeppelin::token::erc20_v070::erc20::ERC20::Approval","kind":"nested"},{"name":"ImplementationAdded","type":"src::replaceability_interface::ImplementationAdded","kind":"nested"},{"name":"ImplementationRemoved","type":"src::replaceability_interface::ImplementationRemoved","kind":"nested"},{"name":"ImplementationReplaced","type":"src::replaceability_interface::ImplementationReplaced","kind":"nested"},{"name":"ImplementationFinalized","type":"src::replaceability_interface::ImplementationFinalized","kind":"nested"},{"name":"RoleGranted","type":"src::access_control_interface::RoleGranted","kind":"nested"},{"name":"RoleRevoked","type":"src::access_control_interface::RoleRevoked","kind":"nested"},{"name":"RoleAdminChanged","type":"src::access_control_interface::RoleAdminChanged","kind":"nested"},{"name":"GovernanceAdminAdded","type":"src::roles_interface::GovernanceAdminAdded","kind":"nested"},{"name":"GovernanceAdminRemoved","type":"src::roles_interface::GovernanceAdminRemoved","kind":"nested"},{"name":"UpgradeGovernorAdded","type":"src::roles_interface::UpgradeGovernorAdded","kind":"nested"},{"name":"UpgradeGovernorRemoved","type":"src::roles_interface::UpgradeGovernorRemoved","kind":"nested"}]}]` + +const NO_STRUCT_CLASS_HASH = "0x03c5ace9d7b61e976247182246cf11ff0df039ecee1e0e37296b73bcb207b77d" +const NO_STRUCT_ABI_DEFINITION = `[{"type":"function","name":"get_account_token_balance","inputs":[{"name":"account","type":"core::starknet::contract_address::ContractAddress"},{"name":"token_type","type":"core::felt252"}],"outputs":[{"type":"core::integer::u128"}],"state_mutability":"view"},{"type":"function","name":"get_pool_token_balance","inputs":[{"name":"token_type","type":"core::felt252"}],"outputs":[{"type":"core::integer::u128"}],"state_mutability":"view"},{"type":"function","name":"set_pool_token_balance","inputs":[{"name":"token_type","type":"core::felt252"},{"name":"balance","type":"core::integer::u128"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"add_demo_token","inputs":[{"name":"token_a_amount","type":"core::integer::u128"},{"name":"token_b_amount","type":"core::integer::u128"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"init_pool","inputs":[{"name":"token_a","type":"core::integer::u128"},{"name":"token_b","type":"core::integer::u128"}],"outputs":[],"state_mutability":"external"},{"type":"function","name":"swap","inputs":[{"name":"token_from","type":"core::felt252"},{"name":"amount_from","type":"core::integer::u128"}],"outputs":[],"state_mutability":"external"}]` + +const VERSION_0_CLASS_HASH = "0x0305ee7a74fc480b0ec54bb306ab32d23da2b6c9b307423d23370eb5351220c3" +const VERSION_0_ABI_DEFINITION = `[{"members":[{"name":"low","offset":0,"type":"felt"},{"name":"high","offset":1,"type":"felt"}],"name":"Uint256","size":2,"type":"struct"},{"data":[{"name":"role","type":"felt"},{"name":"account","type":"felt"}],"keys":[],"name":"RoleGranted","type":"event"},{"data":[{"name":"role","type":"felt"},{"name":"account","type":"felt"}],"keys":[],"name":"RoleRevoked","type":"event"},{"inputs":[{"name":"owner","type":"felt"}],"name":"initialize","outputs":[],"type":"function"},{"inputs":[{"name":"implementation","type":"felt"}],"name":"upgrade","outputs":[],"type":"function"},{"inputs":[],"name":"MINTER_ROLE","outputs":[{"name":"role","type":"felt"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"name":"owner","type":"felt"}],"stateMutability":"view","type":"function"},{"inputs":[{"name":"role","type":"felt"},{"name":"index","type":"felt"}],"name":"getRoleMember","outputs":[{"name":"account","type":"felt"}],"stateMutability":"view","type":"function"},{"inputs":[{"name":"role","type":"felt"}],"name":"getRoleMemberCount","outputs":[{"name":"count","type":"felt"}],"stateMutability":"view","type":"function"},{"inputs":[{"name":"role","type":"felt"},{"name":"account","type":"felt"}],"name":"hasRole","outputs":[{"name":"has_role","type":"felt"}],"stateMutability":"view","type":"function"},{"inputs":[{"name":"artist_name","type":"Uint256"}],"name":"artistExists","outputs":[{"name":"res","type":"felt"}],"stateMutability":"view","type":"function"},{"inputs":[{"name":"account","type":"felt"}],"name":"addMinter","outputs":[],"type":"function"},{"inputs":[{"name":"account","type":"felt"}],"name":"revokeMinter","outputs":[],"type":"function"},{"inputs":[{"name":"artist_name","type":"Uint256"}],"name":"createArtist","outputs":[],"type":"function"},{"inputs":[{"name":"new_owner","type":"felt"}],"name":"transferOwnership","outputs":[{"name":"new_owner","type":"felt"}],"type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"type":"function"}]` diff --git a/athena_abi/core.go b/athena_abi/core.go index a0cda58e..b6058a5b 100644 --- a/athena_abi/core.go +++ b/athena_abi/core.go @@ -2,6 +2,7 @@ package athena_abi import ( "errors" + "fmt" "log" ) @@ -34,7 +35,11 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class groupedAbi := GroupAbiByType(abiJson) // Parse defined types (structs and enums) + fmt.Println("hello") + fmt.Println("grouped abi", groupedAbi["type_def"]) definedTypes, err := ParseEnumsAndStructs(groupedAbi["type_def"]) + fmt.Println("defined types Map contents:", definedTypes) + fmt.Println("is there error", err) if err != nil { sortedDefs, errDef := TopoSortTypeDefs(groupedAbi["type_def"]) if errDef == nil { @@ -50,6 +55,7 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class // Parse interfaces var definedInterfaces []AbiInterface + fmt.Println("now parsing interfaces") for _, iface := range groupedAbi["interface"] { functions := []AbiFunction{} for _, funcData := range iface["items"].([]interface{}) { @@ -67,6 +73,7 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class // Parse functions functions := make(map[string]AbiFunction) + fmt.Println("now parsing functions") for _, functionData := range groupedAbi["function"] { funcName := functionData["name"].(string) abiFunc, errParsingFunctions := ParseAbiFunction(functionData, definedTypes) @@ -85,15 +92,28 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class // Parse events parsedAbiEvents := []AbiEvent{} + fmt.Println("now parsing events") for _, eventData := range groupedAbi["event"] { + fmt.Println("eventdata is ", eventData) parsedEvent, errParsingEvent := ParseAbiEvent(eventData, definedTypes) + fmt.Println("parsed event is ", parsedEvent) + fmt.Println("the err is ", errParsingEvent) + if errParsingEvent != nil { return nil, errParseEvents } - parsedAbiEvents = append(parsedAbiEvents, *parsedEvent) + //parsedAbiEvents = append(parsedAbiEvents, *parsedEvent) + if parsedEvent != nil { + parsedAbiEvents = append(parsedAbiEvents, *parsedEvent) + } else { + // Handle the nil case if necessary + //return nil, errors.New("parsed event is nil") + continue + } } events := make(map[string]AbiEvent) + fmt.Println("parsedabievents are ", parsedAbiEvents) for _, event := range parsedAbiEvents { if event.name != "" { events[event.name] = event @@ -102,6 +122,7 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class // Parse constructor var constructor []AbiParameter + fmt.Println("now parsing constructor") if len(groupedAbi["constructor"]) == 1 { for _, paramData := range groupedAbi["constructor"][0]["inputs"].([]interface{}) { param := paramData.(map[string]interface{}) @@ -120,6 +141,7 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class // Parse L1 handler var l1Handler *AbiFunction + fmt.Println("now parsing l1handler") if len(groupedAbi["l1_handler"]) == 1 { handler, errorParsingFunction := ParseAbiFunction(groupedAbi["l1_handler"][0], definedTypes) if errorParsingFunction != nil { @@ -132,9 +154,11 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class // Parse implemented interfaces implementedInterfaces := make(map[string]AbiInterface) + fmt.Println("now parsing implemented interfaces") implArray, ok := groupedAbi["impl"] if !ok { - return nil, errParseImplementedInterfaces + //return nil, errParseImplementedInterfaces + //this if block is not required } for _, implData := range implArray { implMap := implData @@ -146,7 +170,19 @@ func StarknetAbiFromJSON(abiJson []map[string]interface{}, abiName string, class } } } - + fmt.Println("abi name : ", abiName) + fmt.Println("classhash is : ", classHash) + fmt.Println() + fmt.Println("functions is : ", functions) + fmt.Println() + fmt.Println("events is : ", events) + fmt.Println() + fmt.Println("constructor is : ", constructor) + fmt.Println() + fmt.Println("l1handles is ", l1Handler) + fmt.Println() + fmt.Println("implemented interfaces is : ", implementedInterfaces) + fmt.Println() // Return the populated StarknetAbi struct return &StarknetABI{ ABIName: &abiName, diff --git a/athena_abi/parse.go b/athena_abi/parse.go index 83b0e8a3..46c12f67 100644 --- a/athena_abi/parse.go +++ b/athena_abi/parse.go @@ -2,6 +2,7 @@ package athena_abi import ( "fmt" + "reflect" "strings" ) @@ -20,8 +21,10 @@ func GroupAbiByType(abiJson []map[string]interface{}) map[AbiMemberType][]map[st // Convert the string to AbiMemberType if typeStr == "struct" || typeStr == "enum" { + fmt.Println("the types string value is ", typeStr) grouped["type_def"] = append(grouped["type_def"], entry) } else { + fmt.Println("the types string value is ", typeStr) grouped[AbiMemberType(typeStr)] = append(grouped[AbiMemberType(typeStr)], entry) } @@ -148,6 +151,8 @@ func ParseEnumsAndStructs(abiStructs []map[string]interface{}) (map[string]inter switch abiStruct["type"] { case "struct": + fmt.Println("a") + fmt.Println("abistruct is", abiStruct) res, err := parseStruct(abiStruct, outputTypes) if err != nil { return nil, err @@ -155,6 +160,7 @@ func ParseEnumsAndStructs(abiStructs []map[string]interface{}) (map[string]inter outputTypes[typeName] = res case "enum": + fmt.Println("d") res, err := parseEnum(abiStruct, outputTypes) if err != nil { return nil, err @@ -182,7 +188,10 @@ func parseStruct(abiStruct map[string]interface{}, typeContext map[string]interf } // Parse the member type + fmt.Println("b") + fmt.Println("thetypes are ", member["type"]) res, err := parseType(member["type"].(string), typeContext) + fmt.Println("res is ", res) if err != nil { return StarknetStruct{}, err } @@ -246,7 +255,9 @@ func parseType(abiType string, customTypes map[string]interface{}) (StarknetType } if strings.HasPrefix(abiType, "(") { + fmt.Println("the abitype for parsetuple input is", abiType) res, err := ParseTuple(abiType, customTypes) + fmt.Println("the result afternparsing the tuple is ", res) if err != nil { return nil, err } @@ -255,8 +266,11 @@ func parseType(abiType string, customTypes map[string]interface{}) (StarknetType /*if strings.HasPrefix(abiType, "(") && strings.HasSuffix(abiType, ")") { return ParseTuple(abiType, customTypes) }*/ - + fmt.Println("abitype is", abiType) parts := strings.Split(abiType, "::")[1:] + fmt.Println("c") + fmt.Println(parts) + switch { case len(parts) == 1 && parts[0] == "felt252": return Felt, nil @@ -272,7 +286,8 @@ func parseType(abiType string, customTypes map[string]interface{}) (StarknetType return Bytes31, nil case len(parts) == 3 && parts[0] == "starknet" && parts[1] == "storage_access" && parts[2] == "StorageAddress": return StorageAddress, nil - case len(parts) >= 2 && parts[0] == "array" && parts[1] == "Array" || parts[1] == "Span": + case len(parts) >= 2 && (parts[0] == "array" && parts[1] == "Array" || parts[1] == "Span"): + //case len(parts) >= 2 && parts[0] res, err := parseType(extractInnerType(abiType), customTypes) if err != nil { return nil, err @@ -298,25 +313,28 @@ func parseType(abiType string, customTypes map[string]interface{}) (StarknetType } return intType, nil default: + fmt.Println("the lenght of abitype in the default for v1 is ", len(abiType)) + abiType := strings.TrimSpace(abiType) if val, exists := customTypes[abiType]; exists { return val.(StarknetType), nil - } - if abiType == "felt" { + } else if abiType == "felt" { + fmt.Println("this was run correctly") return Felt, nil - } - if abiType == "Uint256" { + } else if abiType == "Uint256" { return U256, nil - } - if strings.HasSuffix(abiType, "*") { + } else if strings.HasSuffix(abiType, "*") { res, err := parseType(strings.TrimSuffix(abiType, "*"), customTypes) if err != nil { return nil, err } return StarknetArray{res}, nil + } else { + fmt.Println("hey hi this was executed and the value if abitype is ", abiType) + return nil, &InvalidAbiError{ + Msg: "Invalid ABI type: " + abiType, + } } - return nil, &InvalidAbiError{ - Msg: "Invalid ABI type: " + abiType, - } + } } @@ -338,64 +356,51 @@ func isNamedTuple(typeStr string) int { // customTypes is a map from string to StarknetStruct or StarknetEnum func ParseTuple(abiType string, customTypes map[string]interface{}) (StarknetTuple, error) { trimmed := strings.TrimSpace(abiType) + fmt.Println("trrimmed tuopls is", trimmed) strippedTuple := strings.TrimSpace(trimmed[1 : len(trimmed)-1]) - + fmt.Println("stripped tuples is", strippedTuple) outputTypes := []StarknetType{} parenthesisCache := []string{} typeCache := []string{} for _, typeString := range strings.Split(strippedTuple, ",") { - tupleOpen := strings.Count(typeString, "(") tupleClose := strings.Count(typeString, ")") - if tupleOpen > 0 { - for i := 0; i < tupleOpen; i++ { parenthesisCache = append(parenthesisCache, "(") } - } - if len(parenthesisCache) > 0 { typeCache = append(typeCache, typeString) - } else { if isNamedTuple(typeString) > 0 { res, err := parseType(typeString[isNamedTuple(typeString)+1:], customTypes) - if err != nil { - return StarknetTuple{}, err - } outputTypes = append(outputTypes, res) - } else { res, err := parseType(typeString, customTypes) - if err != nil { - return StarknetTuple{}, err } + fmt.Print("the res in tuple parsing is", res) outputTypes = append(outputTypes, res) - } } - if tupleClose > 0 { parenthesisCache = parenthesisCache[:len(parenthesisCache)-tupleClose] - if len(parenthesisCache) == 0 { res, err := ParseTuple(strings.Join(typeCache, ","), customTypes) - if err != nil { - + fmt.Println("error reported") return StarknetTuple{}, err } outputTypes = append(outputTypes, res) } } } + fmt.Println("the output in tuple parsing is ", outputTypes) return StarknetTuple{Members: outputTypes}, nil } @@ -449,11 +454,16 @@ func ParseAbiTypes(types []string, customTypes map[string]interface{}) ([]Starkn func ParseAbiFunction(abiFunction map[string]interface{}, customTypes map[string]interface{}) (*AbiFunction, error) { names := []string{} types := []string{} - for _, abiInput := range abiFunction["inputs"].([]map[string]interface{}) { + /*for _, abiInput := range abiFunction["inputs"].([]map[string]interface{}) { names = append(names, abiInput["name"].(string)) + }*/ + for _, abiInput := range abiFunction["inputs"].([]interface{}) { + inputMap := abiInput.(map[string]interface{}) // Assert each element as map[string]interface{} + names = append(names, inputMap["name"].(string)) } - for _, abiInput := range abiFunction["inputs"].([]map[string]interface{}) { - types = append(types, abiInput["type"].(string)) + for _, abiInput := range abiFunction["inputs"].([]interface{}) { + inputMap := abiInput.(map[string]interface{}) + types = append(types, inputMap["type"].(string)) } parsedInputs, err := parseAbiParameters( names, @@ -464,8 +474,12 @@ func ParseAbiFunction(abiFunction map[string]interface{}, customTypes map[string return nil, err } - for _, abiOutput := range abiFunction["outputs"].([]map[string]interface{}) { + /*for _, abiOutput := range abiFunction["outputs"].([]map[string]interface{}) { types = append(types, abiOutput["type"].(string)) + }*/ + for _, abiOutput := range abiFunction["outputs"].([]interface{}) { + outputMap := abiOutput.(map[string]interface{}) + types = append(types, outputMap["type"].(string)) } parsedOutputs, err := ParseAbiTypes( @@ -485,10 +499,18 @@ func ParseAbiFunction(abiFunction map[string]interface{}, customTypes map[string func ParseAbiEvent(abiEvent map[string]interface{}, customTypes map[string]interface{}) (*AbiEvent, error) { eventParameters := []map[string]interface{}{} + fmt.Print("the abievent is", abiEvent) if value, exists := abiEvent["kind"]; exists { if value == "struct" { - eventParameters = abiEvent["members"].([]map[string]interface{}) + //eventParameters = abiEvent["members"].([]map[string]interface{}) + eventMembers := abiEvent["members"].([]interface{}) // Assert as []interface{} + eventParameters := make([]map[string]interface{}, len(eventMembers)) + + for i, member := range eventMembers { + eventParameters[i] = member.(map[string]interface{}) // Assert each element as map[string]interface{} + } } else { + fmt.Print("in parsig abi event the else1 nil was there") return nil, nil } } else if inputs, ok := abiEvent["inputs"].([]map[string]interface{}); ok { @@ -499,28 +521,73 @@ func ParseAbiEvent(abiEvent map[string]interface{}, customTypes map[string]inter } eventParameters = append(eventParameters, eventParameter) } - } else if data, ok := abiEvent["data"].([]map[string]interface{}); ok { - for _, e := range data { + } else if data, ok := abiEvent["data"].([]interface{}); ok { + fmt.Println(ok) + var result []map[string]interface{} + for _, item := range data { + fmt.Println("item is of type ", reflect.TypeOf(item)) + fmt.Println("result is of type ", reflect.TypeOf(result)) + // Assert the type of item + if m, ok := item.(map[string]interface{}); ok { + result = append(result, m) + } else { + // Handle the case where the item is not of the expected type + fmt.Println("Item is not of type map[string]interface{}:", item) + } + } + for _, e := range result { + fmt.Println(e) eventParameter := map[string]interface{}{"kind": "data"} for k, v := range e { eventParameter[k] = v } eventParameters = append(eventParameters, eventParameter) } - for _, e := range abiEvent["keys"].([]map[string]interface{}) { + /*for _, e := range abiEvent["keys"].([]map[string]interface{}) { eventParameter := map[string]interface{}{"kind": "key"} for k, v := range e { eventParameter[k] = v } eventParameters = append(eventParameters, eventParameter) + }*/ + if keys, ok := abiEvent["keys"].([]interface{}); ok { + var keyres []map[string]interface{} + fmt.Println("keys is of type ", reflect.TypeOf(abiEvent["keys"])) + fmt.Println("result is of type ", reflect.TypeOf(result)) + for _, key := range keys { + fmt.Println("item is of type ", reflect.TypeOf(key)) + fmt.Println("result is of type ", reflect.TypeOf(keyres)) + // Assert the type of item + if m, ok := key.(map[string]interface{}); ok { + keyres = append(keyres, m) + } else { + // Handle the case where the item is not of the expected type + fmt.Println("Item is not of type map[string]interface{}:", key) + } + } + for _, e := range keyres { + fmt.Println(e) + eventParameter := map[string]interface{}{"kind": "key"} + for k, v := range e { + eventParameter[k] = v + } + eventParameters = append(eventParameters, eventParameter) + } } + } else { + fmt.Println("the type is ", reflect.TypeOf(abiEvent["data"])) + fmt.Println("the data is ", abiEvent["data"]) + data, ok := abiEvent["data"].([]map[string]interface{}) + fmt.Println("data is", data) + fmt.Println("ok is ", ok) + fmt.Print("in parsig abi event the else2 nil was there") return nil, nil } types := []string{} names := []string{} - + fmt.Println("hey hey hey the value of eventparameters is ", eventParameters) for _, eventParameter := range eventParameters { types = append(types, eventParameter["type"].(string)) names = append(names, eventParameter["name"].(string)) diff --git a/athena_abi/parse_full_abi_test.go b/athena_abi/parse_full_abi_test.go new file mode 100644 index 00000000..3b998e70 --- /dev/null +++ b/athena_abi/parse_full_abi_test.go @@ -0,0 +1,187 @@ +package athena_abi + +import ( + "encoding/hex" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFunctionSignatures(t *testing.T) { + + transfer := NewAbiFunction("transfer", []AbiParameter{ + {Name: "recipient", Type: ContractAddress}, + {Name: "amount", Type: U256}, + }, []StarknetType{Bool}, "") + + assert.Equal(t, "Function(recipient:ContractAddress,amount:U256) -> (Bool)", transfer.idStr()) + + expectedSignature := "0083afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e" + assert.Equal(t, expectedSignature, hex.EncodeToString(transfer.signature)) +} + +func TestEventSignatures(t *testing.T) { + transfer := NewAbiEvent( + "Transfer", + []string{"from", "to", "amount"}, + map[string]StarknetType{ + "from": ContractAddress, + "to": ContractAddress, + "amount": U256, + }, + make(map[string]StarknetType), + "", + ) + idStr, err := transfer.idStr() + assert.NoError(t, err, "Error getting id string") + + assert.Equal(t, "Event(from:ContractAddress,to:ContractAddress,amount:U256)", idStr, "Unexpected id string") + + expectedSignature := "0099cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9" + assert.Equal(t, expectedSignature, hex.EncodeToString(transfer.signature), "Unexpected signature") +} + +func TestKeyEventSignature(t *testing.T) { + transfer := NewAbiEvent( + "Transfer", + []string{"from", "to", "amount"}, + map[string]StarknetType{ + "amount": U256, + }, + map[string]StarknetType{ + "from": ContractAddress, + "to": ContractAddress, + }, + "", + ) + + expectedIDStr := "Event(:ContractAddress,:ContractAddress,amount:U256)" + actualIDStr, _ := transfer.idStr() + + assert.Equal(t, expectedIDStr, actualIDStr, "ID strings should match") + + expectedSignatureHex := "0099cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9" + actualSignatureHex := hex.EncodeToString(transfer.signature) + + assert.Equal(t, expectedSignatureHex, actualSignatureHex, "Signatures should match") +} + +func TestLoadEthAbi(t *testing.T) { + // Load the ABI using the load_abi function + ethAbi, err := loadAbi("starknet_eth", 2) + assert.NoError(t, err, "Loading ABI should not return an error") + + // Convert the hexadecimal string to a byte slice + classHash, err := hex.DecodeString("05ffbcfeb50d200a0677c48a129a11245a3fc519d1d98d76882d1c9a1b19c6ed") + assert.NoError(t, err, "Decoding class hash from hex should not return an error") + + // Call the StarknetAbi from JSON method + ethDecoder, err := StarknetAbiFromJSON(ethAbi, "starknet_eth", classHash) + assert.NoError(t, err, "Decoding ABI from JSON should not return an error") + + // Additional assertions can be made here based on the expected structure of ethDecoder + // For example, check the number of functions or events parsed, etc. + assert.NotNil(t, ethDecoder, "EthDecoder should not be nil") + assert.Equal(t, "starknet_eth", *ethDecoder.ABIName, "ABI name should match") +} + +/*func TestLoadWildcardArraySyntax(t *testing.T) { + // Load the ABI + wildcardAbi, err := loadAbi("complex_array", 1) + assert.NoError(t, err, "Failed to load ABI") + + // Convert the hex string to bytes + classHash, err := hex.DecodeString("0031da92cf5f54bcb81b447e219e2b791b23f3052d12b6c9abd04ff2e5626576") + assert.NoError(t, err, "Failed to decode hex string") + + // Create the decoder using the from JSON function + decoder, err := StarknetAbiFromJSON(wildcardAbi, "complex_array", classHash) + assert.NoError(t, err, "Failed to create Starknet ABI from JSON") + + // Add assertions to verify the properties of `decoder` + // Example assertions (adjust according to expected values): + assert.NotNil(t, decoder, "Decoder should not be nil") + //assert.Equal(t, expectedValue, decoder.SomeProperty, "Unexpected value for SomeProperty") +} + + +func TestLoadWildcardArraySyntax(t *testing.T) { + wildcardAbi,err := loadAbi("complex_array", 1) + assert.NoError(t, err, "Loading ABI should not return an error") + // Decode the hex string directly + decodedClassHash, err := hex.DecodeString("0031da92cf5f54bcb81b447e219e2b791b23f3052d12b6c9abd04ff2e5626576") + assert.NoError(t, err, "Failed to decode hex string") + + decoder, err := StarknetAbiFromJSON( + wildcardAbi, + "complex_array", + decodedClassHash, + ) + assert.NoError(t, err, "Failed to decode ABI") + + parsedEvent := decoder.Events["log_storage_cells"] + + // Assert the length of parsed event data + assert.Len(t, parsedEvent.data, 1, "Expected parsed event data length to be 1") + + // Assert the storage_cells data matches the expected structure + assert.Equal(t, parsedEvent.data["storage_cells"], StarknetArray( + StarknetStruct{ + Name: "StorageCell", + Members: []AbiParameter{ + {Name: "key", Type: StarknetCoreType.Felt}, + {Name: "value", Type: StarknetCoreType.Felt}, + }, + }, + ), "Expected storage_cells data structure to match") + + // Assert the event name + assert.Equal(t, parsedEvent.Name, "log_storage_cells", "Expected event name to be 'log_storage_cells'") +} +*/ + +func TestLoadWildcardArraySyntax(t *testing.T) { + // Load the ABI (you'll need to implement this function) + wildcardAbi, err := loadAbi("complex_array", 1) + assert.NoError(t, err, "Loading ABI should not return an error") + + classHash, _ := hex.DecodeString("0031da92cf5f54bcb81b447e219e2b791b23f3052d12b6c9abd04ff2e5626576") + //fmt.Println("wildcard",wildcardAbi) + decoder, err := StarknetAbiFromJSON(wildcardAbi, "complex_array", classHash) + assert.NoError(t, err, "there should not be error") + fmt.Println("decoder is ", decoder) + fmt.Println("the err is ", err) + parsedEvent, ok := decoder.Events["log_storage_cells"] + fmt.Println("parsedevent is ", parsedEvent) + assert.True(t, ok, "Event 'log_storage_cells' should exist") + + assert.Equal(t, 1, len(parsedEvent.data), "Parsed event should have 1 data field") + + storageCellsType, ok := parsedEvent.data["storage_cells"] + assert.True(t, ok, "Event should have 'storage_cells' field") + + arrayType, ok := storageCellsType.(StarknetArray) + assert.True(t, ok, "storage_cells should be a StarknetArray") + + structType, ok := arrayType.InnerType.(StarknetStruct) + assert.True(t, ok, "Array element should be a StarknetStruct") + assert.Equal(t, "StorageCell", structType.Name) + + assert.Equal(t, 2, len(structType.Members), "StorageCell struct should have 2 members") + fmt.Println("hello hello the val is ", structType.Members) + assert.Equal(t, "key", structType.Members[0].Name) + assert.Equal(t, StarknetCoreType(Felt), structType.Members[0].Type) + + assert.Equal(t, "value", structType.Members[1].Name) + assert.Equal(t, StarknetCoreType(Felt), structType.Members[1].Type) + + assert.Equal(t, "log_storage_cells", parsedEvent.name) + + // Test the idStr() methods + expectedIdStr := "[{key:Felt,value:Felt}]" + assert.Equal(t, expectedIdStr, arrayType.idStr(), "Array idStr should match expected") + + expectedStructIdStr := "{key:Felt,value:Felt}" + assert.Equal(t, expectedStructIdStr, structType.idStr(), "Struct idStr should match expected") +} diff --git a/athena_abi/parse_struct_test.go b/athena_abi/parse_struct_test.go index 4a6e2a5d..f16b24b7 100644 --- a/athena_abi/parse_struct_test.go +++ b/athena_abi/parse_struct_test.go @@ -36,7 +36,7 @@ func loadAbi(abiName string, abiVersion int) ([]map[string]interface{}, error) { if err != nil { return nil, fmt.Errorf("failed to decode ABI JSON: %w", err) } - + //fmt.Println("Map contents:", abiJson) return abiJson, nil } func init() { diff --git a/backup.txt b/backup.txt new file mode 100644 index 00000000..addf39d7 --- /dev/null +++ b/backup.txt @@ -0,0 +1,90 @@ +package athena_abi +import ( + "fmt" + "reflect" +) +// The function takes in a list of type definitions (dict) and returns a dict of sets (map[string]bool) +func BuildTypeGraph(typeDefs []map[string]interface{}) map[string]map[string]bool { + fmt.Println() + fmt.Println("typdefs os ",typeDefs) + fmt.Println() + fmt.Println("the types of typdefs is ",reflect.TypeOf(typeDefs)) + fmt.Println() + outputGraph := make(map[string]map[string]bool) + for _, typeDef := range typeDefs { + referencedTypes := []string{} + fmt.Println("the typedef is ",typeDef) + fmt.Println() + fmt.Println("the types of typdef is ",reflect.TypeOf(typeDef)) + fmt.Println() + if typeDef["type"] == "struct" { + /*for _, member := range typeDef["members"].([]map[string]interface{}) { + referencedTypes = append(referencedTypes, member["type"].(string)) + }*/ + fmt.Println("the type of typedef.member",reflect.TypeOf(typeDef["members"])) + //fmt.Println("the typedef is ",typeDef["member"]) + members, ok := typeDef["members"].([]map[string]interface {}) + fmt.Println() + fmt.Println("ok is ",ok) + /*if !ok { + + panic("Expected members to be a slice of interface{}") + }*/ + + // Loop over each element in the slice + for _, memberInterface := range members { + // Assert that each element is a map[string]interface{} + member, ok := memberInterface.(map[string]interface{}) + if !ok { + panic("Expected each member to be a map[string]interface{}") + } + + // Now you can safely access member["type"].(string) + referencedTypes = append(referencedTypes, member["type"].(string)) + } + + } else { + /*for _, variant := range typeDef["variants"].([]map[string]interface{}) { + referencedTypes = append(referencedTypes, variant["type"].(string)) + }*/ + // Assert that typeDef["variants"] is a slice of interfaces ([]interface{}) + variants, ok := typeDef["variants"].([]interface{}) + if !ok { + panic("Expected variants to be a slice of interface{}") + } + + // Loop over each element in the slice + for _, variantInterface := range variants { + // Assert that each element is a map[string]interface{} + variant, ok := variantInterface.(map[string]interface{}) + if !ok { + panic("Expected each variant to be a map[string]interface{}") + } + + // Now you can safely access variant["type"].(string) + referencedTypes = append(referencedTypes, variant["type"].(string)) + } + + } + + refTypes := make(map[string]bool) + + for _, typeStr := range referencedTypes { + if _, ok := StarknetCoreTypes[typeStr]; ok { + continue + } + + if _, ok := StarknetCoreTypes[extractInnerType(typeStr)]; ok { + if strings.HasPrefix(typeStr, "core::array") || strings.HasPrefix(typeStr, "@core::array") { + continue + } + } + + refTypes[typeStr] = true + } + + outputGraph[typeDef["name"].(string)] = refTypes + } + + return outputGraph +}