Skip to content

Commit

Permalink
- fix: fix rust project not being able to run due to borrower check
Browse files Browse the repository at this point in the history
- fix: fix typings generations to parse the object values correctly
  • Loading branch information
joshstevens19 committed Jul 21, 2024
1 parent b19ac41 commit 9d2475f
Show file tree
Hide file tree
Showing 18 changed files with 669 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ start_graphql:
start_indexer_uniswap_v3_factory:
RUSTFLAGS='-C target-cpu=native' cargo run --release --features jemalloc -- start --path /Users/joshstevens/code/rindexer/examples/uniswap_v3_factory all
codegen:
RUSTFLAGS='-C target-cpu=native' cargo run --release --features jemalloc -- codegen --path /Users/joshstevens/code/rust typings
RUSTFLAGS='-C target-cpu=native' cargo run --release --features jemalloc -- codegen --path /Users/joshstevens/code/kami typings
codegen_typings:
cargo run -- codegen typings
codegen_indexer:
Expand Down
6 changes: 3 additions & 3 deletions core/src/generator/events_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
database::postgres::generate::{
generate_column_names_only_with_base_properties, generate_event_table_full_name,
},
helpers::{camel_to_snake, get_full_path},
helpers::{camel_to_snake, camel_to_snake_advanced, get_full_path},
manifest::{
contract::{Contract, ContractDetails},
storage::{CsvDetails, Storage},
Expand All @@ -24,7 +24,7 @@ pub fn abigen_contract_name(contract: &Contract) -> String {
}

fn abigen_contract_mod_name(contract: &Contract) -> String {
camel_to_snake(&abigen_contract_name(contract))
camel_to_snake_advanced(&abigen_contract_name(contract), true)
}

pub fn abigen_contract_file_name(contract: &Contract) -> String {
Expand Down Expand Up @@ -799,7 +799,7 @@ pub fn generate_event_handlers(
if !static_bytes.is_empty() {
".into()"
} else {
""
".clone()"
}
} else {
""
Expand Down
26 changes: 23 additions & 3 deletions core/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ pub use file::{
use rand::{distributions::Alphanumeric, Rng};

pub fn camel_to_snake(s: &str) -> String {
camel_to_snake_advanced(s, false)
}

pub fn camel_to_snake_advanced(s: &str, numbers_attach_to_last_word: bool) -> String {
let mut snake_case = String::new();
let mut previous_was_uppercase = false;
let mut previous_was_digit = false;

for (i, c) in s.chars().enumerate() {
if c.is_alphanumeric() || c == '_' {
if c.is_uppercase() {
// Insert an underscore if it's not the first character and the previous character
// wasn't uppercase
if i > 0 &&
(!previous_was_uppercase ||
(i + 1 < s.len() &&
Expand All @@ -40,9 +43,22 @@ pub fn camel_to_snake(s: &str) -> String {
}
snake_case.push(c.to_ascii_lowercase());
previous_was_uppercase = true;
previous_was_digit = false;
} else if c.is_ascii_digit() {
if !numbers_attach_to_last_word &&
i > 0 &&
!previous_was_digit &&
!snake_case.ends_with('_')
{
snake_case.push('_');
}
snake_case.push(c);
previous_was_uppercase = false;
previous_was_digit = true;
} else {
snake_case.push(c);
previous_was_uppercase = false;
previous_was_digit = false;
}
}
}
Expand Down Expand Up @@ -109,6 +125,10 @@ mod tests {
assert_eq!(camel_to_snake("Camel"), "camel");
assert_eq!(camel_to_snake("camel"), "camel");
assert_eq!(camel_to_snake("collectNFTId"), "collect_nft_id");
assert_eq!(camel_to_snake("ERC20"), "erc20");
assert_eq!(camel_to_snake("ERC20"), "erc_20");
assert_eq!(camel_to_snake("arg1"), "arg_1");

assert_eq!(camel_to_snake_advanced("ERC20", false), "erc_20");
assert_eq!(camel_to_snake_advanced("ERC20", true), "erc20");
}
}
8 changes: 5 additions & 3 deletions core/src/indexer/fetch_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,11 @@ fn retry_with_block_range(
) -> Option<RetryWithBlockRangeResult> {
let error_message = &error.message;
// some providers put the data in the data field
let error_data = match &error.data {
Some(data) => &data.to_string(),
None => &String::from(""),
let error_data_binding = error.data.as_ref().map(|data| data.to_string());
let empty_string = String::from("");
let error_data = match &error_data_binding {
Some(data) => data,
None => &empty_string,
};

fn compile_regex(pattern: &str) -> Result<Regex, regex::Error> {
Expand Down
4 changes: 4 additions & 0 deletions documentation/docs/pages/docs/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@

### Bug fixes
-------------------------------------------------

- fix: fixing the query of implemntation ABI for proxy contracts
- fix: add request timeouts to adapt to different verifier's rate limits
- fix: make chain_id u64 instead of u32 - https://github.com/joshstevens19/rindexer/issues/53
- fix: fix rust project not being able to run due to borrower check
- fix: fix typings generations to parse the object values correctly

### Breaking changes
-------------------------------------------------

Expand Down
33 changes: 33 additions & 0 deletions rindexer_rust_playground/abis/world.abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"type": "event",
"name": "ComponentValueSet",
"inputs": [
{
"name": "arg0",
"type": "uint256",
"indexed": true,
"internalType": "uint256"
},
{
"name": "arg1",
"type": "address",
"indexed": true,
"internalType": "address"
},
{
"name": "arg2",
"type": "uint256",
"indexed": true,
"internalType": "uint256"
},
{
"name": "arg3",
"type": "bytes",
"indexed": false,
"internalType": "bytes"
}
],
"anonymous": false
}
]
14 changes: 13 additions & 1 deletion rindexer_rust_playground/rindexer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ networks:
- name: ethereum
chain_id: 1
rpc: https://mainnet.gateway.tenderly.co
- name: yominet
chain_id: 5264468217
rpc: https://yominet.rpc.caldera.xyz/http
storage:
postgres:
enabled: true
Expand All @@ -28,4 +31,13 @@ contracts:
network: ethereum
#start_block: 56399431
abi: ./abis/erc20-abi.json
generate_csv: true
generate_csv: true
- name: World
details:
- network: yominet
address: 0x441e13a25caecad50028e7623a39b91a507bca02
start_block: '1077466'
end_block: '1650276'
abi: ./abis/world.abi.json
include_events:
- ComponentValueSet
1 change: 1 addition & 0 deletions rindexer_rust_playground/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async fn main() {
let result = start_rindexer(StartDetails {
manifest_path: &manifest_path,
indexing_details: if enable_indexer {
// EventCallbackRegistry { events: vec![] }
Some(IndexingDetails { registry: register_all_handlers(&manifest_path).await })
} else {
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ use std::path::PathBuf;
use rindexer::event::callback_registry::EventCallbackRegistry;

use super::rindexer_playground::{
erc20_filter::erc20_filter_handlers, rocket_pool_eth::rocket_pool_eth_handlers,
erc_20_filter::erc_20_filter_handlers, rocket_pool_eth::rocket_pool_eth_handlers,
world::world_handlers,
};

pub async fn register_all_handlers(manifest_path: &PathBuf) -> EventCallbackRegistry {
let mut registry = EventCallbackRegistry::new();
rocket_pool_eth_handlers(manifest_path, &mut registry).await;
erc20_filter_handlers(manifest_path, &mut registry).await;
erc_20_filter_handlers(manifest_path, &mut registry).await;
world_handlers(manifest_path, &mut registry).await;
registry
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rindexer::{
EthereumSqlTypeWrapper, PgType, RindexerColorize,
};

use super::super::super::typings::rindexer_playground::events::erc20_filter::{
use super::super::super::typings::rindexer_playground::events::erc_20_filter::{
no_extensions, ERC20FilterEventType, TransferEvent,
};

Expand Down Expand Up @@ -66,7 +66,7 @@ async fn transfer_handler(manifest_path: &PathBuf, registry: &mut EventCallbackR
let result = context
.database
.bulk_insert_via_copy(
"rindexer_playground_erc20_filter.transfer",
"rindexer_playground_erc_20_filter.transfer",
&[
"contract_address".to_string(),
"from".to_string(),
Expand Down Expand Up @@ -100,7 +100,7 @@ async fn transfer_handler(manifest_path: &PathBuf, registry: &mut EventCallbackR
let result = context
.database
.bulk_insert(
"rindexer_playground_erc20_filter.transfer",
"rindexer_playground_erc_20_filter.transfer",
&[
"contract_address".to_string(),
"from".to_string(),
Expand Down Expand Up @@ -140,6 +140,6 @@ async fn transfer_handler(manifest_path: &PathBuf, registry: &mut EventCallbackR
)
.register(manifest_path, registry);
}
pub async fn erc20_filter_handlers(manifest_path: &PathBuf, registry: &mut EventCallbackRegistry) {
pub async fn erc_20_filter_handlers(manifest_path: &PathBuf, registry: &mut EventCallbackRegistry) {
transfer_handler(manifest_path, registry).await;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(dead_code, unused)]
pub mod erc20_filter;
pub mod erc_20_filter;
pub mod rocket_pool_eth;
pub mod world;
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use std::{path::PathBuf, sync::Arc};

use rindexer::{
event::callback_registry::EventCallbackRegistry, rindexer_error, rindexer_info,
EthereumSqlTypeWrapper, PgType, RindexerColorize,
};

use super::super::super::typings::rindexer_playground::events::world::{
no_extensions, ComponentValueSetEvent, WorldEventType,
};

async fn component_value_set_handler(
manifest_path: &PathBuf,
registry: &mut EventCallbackRegistry,
) {
WorldEventType::ComponentValueSet(
ComponentValueSetEvent::handler(|results, context| async move {
if results.is_empty() {
return Ok(());
}



let mut postgres_bulk_data: Vec<Vec<EthereumSqlTypeWrapper>> = vec![];
let mut csv_bulk_data: Vec<Vec<String>> = vec![];
for result in results.iter() {
csv_bulk_data.push(vec![format!("{:?}", result.tx_information.address),result.event_data.arg_0.to_string(),format!("{:?}", result.event_data.arg_1,),result.event_data.arg_2.to_string(),result.event_data.arg_3.iter().map(|byte| format!("{:02x}", byte)).collect::<Vec<_>>().join(""),format!("{:?}", result.tx_information.transaction_hash),result.tx_information.block_number.to_string(),result.tx_information.block_hash.to_string(),result.tx_information.network.to_string(),result.tx_information.transaction_index.to_string(),result.tx_information.log_index.to_string()]);
let data = vec![EthereumSqlTypeWrapper::Address(result.tx_information.address),EthereumSqlTypeWrapper::U256(result.event_data.arg_0),EthereumSqlTypeWrapper::Address(result.event_data.arg_1),EthereumSqlTypeWrapper::U256(result.event_data.arg_2),EthereumSqlTypeWrapper::Bytes(result.event_data.arg_3.clone()),EthereumSqlTypeWrapper::H256(result.tx_information.transaction_hash),EthereumSqlTypeWrapper::U64(result.tx_information.block_number),EthereumSqlTypeWrapper::H256(result.tx_information.block_hash),EthereumSqlTypeWrapper::String(result.tx_information.network.to_string()),EthereumSqlTypeWrapper::U64(result.tx_information.transaction_index),EthereumSqlTypeWrapper::U256(result.tx_information.log_index)];;
postgres_bulk_data.push(data);
}

if !csv_bulk_data.is_empty() {
let csv_result = context.csv.append_bulk(csv_bulk_data).await;
if let Err(e) = csv_result {
rindexer_error!("WorldEventType::ComponentValueSet inserting csv data: {:?}", e);
return Err(e.to_string());
}
}

if postgres_bulk_data.is_empty() {
return Ok(());
}

if postgres_bulk_data.len() > 100 {
let result = context
.database
.bulk_insert_via_copy(
"rindexer_playground_world.component_value_set",
&["contract_address".to_string(), "arg_0".to_string(), "arg_1".to_string(), "arg_2".to_string(), "arg_3".to_string(), "tx_hash".to_string(), "block_number".to_string(), "block_hash".to_string(), "network".to_string(), "tx_index".to_string(), "log_index".to_string()],
&postgres_bulk_data
.first()
.ok_or("No first element in bulk data, impossible")?
.iter()
.map(|param| param.to_type())
.collect::<Vec<PgType>>(),
&postgres_bulk_data,
)
.await;

if let Err(e) = result {
rindexer_error!("WorldEventType::ComponentValueSet inserting bulk data via COPY: {:?}", e);
return Err(e.to_string());
}
} else {
let result = context
.database
.bulk_insert(
"rindexer_playground_world.component_value_set",
&["contract_address".to_string(), "arg_0".to_string(), "arg_1".to_string(), "arg_2".to_string(), "arg_3".to_string(), "tx_hash".to_string(), "block_number".to_string(), "block_hash".to_string(), "network".to_string(), "tx_index".to_string(), "log_index".to_string()],
&postgres_bulk_data,
)
.await;

if let Err(e) = result {
rindexer_error!("WorldEventType::ComponentValueSet inserting bulk data via INSERT: {:?}", e);
return Err(e.to_string());
}
}


rindexer_info!(
"World::ComponentValueSet - {} - {} events",
"INDEXED".green(),
results.len(),
);

Ok(())
},
no_extensions(),
)
.await,
)
.register(manifest_path, registry);
}
pub async fn world_handlers(manifest_path: &PathBuf, registry: &mut EventCallbackRegistry) {
component_value_set_handler(manifest_path, registry).await;
}
18 changes: 18 additions & 0 deletions rindexer_rust_playground/src/rindexer_lib/typings/networks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ lazy_static! {
None
)
.expect("Error creating provider");
static ref YOMINET_PROVIDER: Arc<JsonRpcCachedProvider> = create_client(
&public_read_env_value("https://yominet.rpc.caldera.xyz/http")
.unwrap_or("https://yominet.rpc.caldera.xyz/http".to_string()),
None
)
.expect("Error creating provider");
}
pub fn get_ethereum_provider_cache() -> Arc<JsonRpcCachedProvider> {
Arc::clone(&ETHEREUM_PROVIDER)
Expand All @@ -27,9 +33,21 @@ pub fn get_ethereum_provider() -> Arc<Provider<RetryClient<Http>>> {
ETHEREUM_PROVIDER.get_inner_provider()
}

pub fn get_yominet_provider_cache() -> Arc<JsonRpcCachedProvider> {
Arc::clone(&YOMINET_PROVIDER)
}

pub fn get_yominet_provider() -> Arc<Provider<RetryClient<Http>>> {
YOMINET_PROVIDER.get_inner_provider()
}

pub fn get_provider_cache_for_network(network: &str) -> Arc<JsonRpcCachedProvider> {
if network == "ethereum" {
return get_ethereum_provider_cache();
}

if network == "yominet" {
return get_yominet_provider_cache();
}
panic!("Network not supported")
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use super::super::super::super::typings::networks::get_provider_cache_for_networ
///
/// This file was auto generated by rindexer - https://github.com/joshstevens19/rindexer.
/// Any manual changes to this file will be overwritten.
use super::erc20_filter_abi_gen::rindexer_erc20_filter_gen::{self, RindexerERC20FilterGen};
use super::erc_20_filter_abi_gen::rindexer_erc20_filter_gen::{self, RindexerERC20FilterGen};

pub type ApprovalData = rindexer_erc20_filter_gen::ApprovalFilter;

Expand Down Expand Up @@ -180,7 +180,7 @@ where
Transfer(TransferEvent<TExtensions>),
}

pub fn erc20_filter_contract(
pub fn erc_20_filter_contract(
network: &str,
address: Address,
) -> RindexerERC20FilterGen<Arc<Provider<RetryClient<Http>>>> {
Expand Down
Loading

0 comments on commit 9d2475f

Please sign in to comment.