Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to decode logs from the Envio indexer response? #1308

Open
chlenc opened this issue Mar 25, 2024 · 4 comments
Open

How to decode logs from the Envio indexer response? #1308

chlenc opened this issue Mar 25, 2024 · 4 comments

Comments

@chlenc
Copy link

chlenc commented Mar 25, 2024

Hi guys,I'm trying to use Envio indexer in the Spark app
Envio Allows you to send a POST request to "http://fuel.hypersync.xyz/query" and get a response with info

Request body

{
    "max_num_transactions": 1,
    "from_block": 0,
    "transactions": [
      {
        "owner": [
          "0x1ad65e67c522dc0355f17792417aeb3269b5adc84ba13549b94b98b2db34c1e0"
        ]
      }
    ],
    "field_selection": {
      "output": [
        "tx_id",
        "output_type",
        "to",
        "amount",
        "asset_id",
        "input_index",
        "balance_root",
        "state_root",
        "contract"
      ],
      "input": [
        "tx_id",
        "input_type",
        "utxo_id",
        "owner",
        "amount",
        "asset_id",
        "predicate_data",
        "contract",
        "sender",
        "recipient",
        "nonce",
        "data"
      ],
      "receipt": [
        "tx_id",
        "pc",
        "is",
        "to",
        "to_address",
        "amount",
        "param1",
        "param2",
        "val",
        "reason",
        "ra",
        "rb",
        "rc",
        "rd",
        "receipt_type",
        "result",
        "data",
        "sender",
        "recipient",
        "contract_id"
      ],
      "transaction": [
        "block_height",
        "id",
        "tx_type",
        "status",
        "reason",
        "script",
        "script_data"
      ]
    }
  }

Response

{
	"data": [
		{
			"receipts": [
				{
					"tx_id": "0xafd0bae4bbcc0ad76dfba85ca0271a519c0f138712acf1ccc982ab67bb6580d6",
					"pc": 11640,
					"is": 11640,
					"to": "0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f",
					"to_address": null,
					"amount": 0,
					"param1": 3918102790,
					"param2": 10448,
					"val": null,
					"reason": null,
					"ra": null,
					"rb": null,
					"rc": null,
					"rd": null,
					"receipt_type": 0,
					"result": null,
					"data": null,
					"sender": null,
					"recipient": null,
					"contract_id": null
				},
                                 ...
                         ],
			"inputs": [
				{
					"tx_id": "0x90fa080c8e1cec7021b893fbea86502c9767c5aa0470e03e7c77639050805109",
					"input_type": 1,
					"utxo_id": "0xd39184f2c179efa0b12a8d177412972d5cbe82c94914627215a59ea2ad48d4e1",
					"owner": null,
					"amount": null,
					"asset_id": null,
					"predicate_data": null,
					"contract": "0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f",
					"sender": null,
					"recipient": null,
					"nonce": null,
					"data": null
				},
                                 ...
                         ],
			"outputs": [
				{
					"tx_id": "0xafd0bae4bbcc0ad76dfba85ca0271a519c0f138712acf1ccc982ab67bb6580d6",
					"pc": 11640,
					"is": 11640,
					"to": "0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f",
					"to_address": null,
					"amount": 0,
					"param1": 3918102790,
					"param2": 10448,
					"val": null,
					"reason": null,
					"ra": null,
					"rb": null,
					"rc": null,
					"rd": null,
					"receipt_type": 0,
					"result": null,
					"data": null,
					"sender": null,
					"recipient": null,
					"contract_id": null
				},
				...
			],
			"transactions": [
				{
					"block_height": 7176398,
					"id": "0xd55192d58429b645511e09a2482f4c61542458e012d5a3908ac30fcb19b04890",
					"tx_type": 2,
					"status": 1,
					"reason": null,
					"script": null,
					"script_data": null
				},
				...
			],
	        ],
	"archive_height": 8008639,
	"next_block": 7210000,
	"total_execution_time": 178

Can you help me please to get my Events from here

pub struct MarketCreateEvent {
    asset_id: AssetId,
    asset_decimals: u32,
    timestamp: u64,
}

pub struct OrderChangeEvent {
    order_id: b256,
    trader: Address,
    base_token: AssetId,
    base_size_change: I64,
    base_price: u64,
    timestamp: u64,
}

pub struct TradeEvent {
    base_token: AssetId,
    order_matcher: Address,
    seller: Address,
    buyer: Address,
    trade_size: u64,
    trade_price: u64,
    timestamp: u64,
}

Additional info

Orderbook contract: 0x1ad65e67c522dc0355f17792417aeb3269b5adc84ba13549b94b98b2db34c1e0
Orderbook repo: https://github.com/compolabs/orderbook-contract
Envio docs: https://github.com/enviodev/hypersync-fuel-docs
Envio rust SDK(install apnproto and libcapnp-dev before use): https://docs.rs/skar-client-fuel/0.0.1/skar_client_fuel/struct.Client.html

@chlenc
Copy link
Author

chlenc commented Mar 25, 2024

It looks like somehow I need to create a LogDecoder instance given my contract and pass a list of receipts to decode_logs_with_type, or reuse a LogDecoder

use std::path::PathBuf;

use fuels::{
    test_helpers::{launch_custom_provider_and_get_wallets, WalletsConfig},
    tx::Receipt,
};
use orderbook::orderbook_utils::{OrderChangeEvent, Orderbook};
use src20_sdk::token_utils::{deploy_token_contract, Asset};

#[tokio::test]
async fn indexer_parse_test() {
    let path =
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/indexer_test/indexer_data.json");
    let addresses_json = std::fs::read_to_string(path).unwrap();
    let data: serde_json::Value = serde_json::from_str(&addresses_json).unwrap();
    let receipts_str = data["data"][0]["receipts"].as_str().unwrap();

    let wallets_config = WalletsConfig::new(Some(5), Some(1), Some(1_000_000_000));
    let wallets = launch_custom_provider_and_get_wallets(wallets_config, None, None)
        .await
        .unwrap();
    let admin = &wallets[0];

    let token_contract = deploy_token_contract(&admin).await;
    let btc = Asset::new(admin.clone(), token_contract.contract_id().into(), "BTC");
    let token_contract = deploy_token_contract(&admin).await;
    let usdc = Asset::new(admin.clone(), token_contract.contract_id().into(), "USDC");

    let orderbook = Orderbook::deploy(&admin, usdc.asset_id, usdc.decimals, 9).await;

    let res = orderbook
        ._create_market(btc.asset_id, btc.decimals as u32)
        .await
        .unwrap();

    let receipts: &[Receipt] = serde_json::from_str(&receipts_str).unwrap();
    let res = res
        .log_decoder
        .decode_logs_with_type::<OrderChangeEvent>(receipts);
}

@chlenc
Copy link
Author

chlenc commented Mar 26, 2024

Alexander from the indexer team told me we should be able to do something with the ProgramABI type and its logged_types field; they did something like that for V1 of the indexer — https://github.com/FuelLabs/fuel-indexer/blob/develop/packages/fuel-indexer-macros/src/indexer.rs#L48-L61

https://github.com/FuelLabs/fuel-indexer/blob/develop/packages/fuel-indexer-macros/src/indexer.rs

    let mut decoded_type_snippets = HashSet::new();
    let mut decoded_log_match_arms = HashSet::new();
    let mut decoded_type_fields = HashSet::new();
    let mut abi_dispatchers = Vec::new();

    let funcs = abi.clone().functions;
    let abi_types: Vec[TypeDeclaration](https://app.slack.com/client/T04L3KML1PZ/TypeDeclaration) = abi
        .clone()
        .types
        .iter()
        .map(|t| strip_callpath_from_type_field(t.clone()))
        .collect();
    let abi_log_types = abi.clone().logged_types.unwrap_or_default();
    let abi_msg_types = abi.clone().messages_types.unwrap_or_default();

@chlenc
Copy link
Author

chlenc commented Mar 26, 2024

Should I use this to decode the data?

https://docs.fuel.network/docs/fuels-rs/codec/decoding/

@chlenc
Copy link
Author

chlenc commented Mar 26, 2024

@chlenc chlenc changed the title How to decode logs from the Envio indexer responce? How to decode logs from the Envio indexer response? Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant