Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
pending sdk update
Browse files Browse the repository at this point in the history
  • Loading branch information
Rashad Alston authored and Rashad Alston committed Aug 15, 2023
1 parent 76429ab commit 83a25cd
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 225 deletions.
253 changes: 69 additions & 184 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Use the new resolver to prevent dev-deps and build-deps from enabling debugging or test features in production.
resolver = "2"
members = [
# "examples/fuel-explorer/fuel-explorer",
# "examples/hello-world-native/hello-indexer-native",
# "examples/hello-world/hello-indexer",
"examples/fuel-explorer/fuel-explorer",
"examples/hello-world-native/hello-indexer-native",
"examples/hello-world/hello-indexer",
"examples/hello-world/hello-world-data",
"examples/hello-world/hello-world-node",
"packages/fuel-indexer",
Expand Down Expand Up @@ -86,7 +86,7 @@ fuel-indexer-utils = { version = "0.19.4", path = "./packages/fuel-indexer-utils
fuel-tx = { version = "0.35", default-features = false }
fuel-types = { version = "0.35", default-features = false, features = ["serde"] }
fuel-vm = { version = "0.35", default-features = false }
fuels = { version = "0.45", path = "/Users/rashad/dev/repos/fuels-rs/packages/fuels", default-features = false }
fuels = { version = "0.45.1", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = { version = "1.0", default-features = false }
thiserror = "1.0"
Expand Down
18 changes: 3 additions & 15 deletions examples/fuel-explorer/fuel-explorer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,7 @@ impl From<Bytes32> for ContractIdFragment {
fn from(hash: Bytes32) -> Self {
// Since the ID will just be derived from the hash, rather than using `::new()`,
// just manaully derived the ID, then use `::get_or_create()`.
Self {
id: id(hash),
hash,
}
.get_or_create()
Self { id: id(hash), hash }.get_or_create()
}
}

Expand All @@ -170,23 +166,15 @@ impl From<Bytes32> for BlockIdFragment {
fn from(hash: Bytes32) -> Self {
// Since the ID will just be derived from the hash, rather than using `::new()`,
// just manaully derived the ID, then use `::get_or_create()`.
Self {
id: id(hash),
hash,
}
.get_or_create()
Self { id: id(hash), hash }.get_or_create()
}
}

impl From<Bytes32> for TransactionIdFragment {
fn from(hash: Bytes32) -> Self {
// Since the ID will just be derived from the hash, rather than using `::new()`,
// just manaully derived the ID, then use `::get_or_create()`.
Self {
id: id(hash),
hash,
}
.get_or_create()
Self { id: id(hash), hash }.get_or_create()
}
}

Expand Down
11 changes: 2 additions & 9 deletions examples/hello-world-native/hello-indexer-native/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ mod hello_world_native {

async fn index_logged_greeting(event: Greeting, block: BlockData) {
info!("Handling {:?}.", event);
// We're using the `::new()` method to create a Greeter, which automatically
// generates an ID for the entity. Then, we use `::get_or_create()` to
// load the corresponding record from the database, if present.
let greeter = Greeter::new(
event.person.name.to_right_trimmed_str().into(),
block.height.into(),
Expand All @@ -42,27 +39,23 @@ mod hello_world_native {
.get_or_create()
.await;

// Here we show how you can use the Charfield type to store strings with
// length <= 255. The fuel-indexer-utils package contains a number of helpful
// functions for byte conversion, string manipulation, etc.
let message = format!(
"{} 👋, my name is {}",
event.greeting.to_right_trimmed_str(),
event.person.name.to_right_trimmed_str(),
);
let message_hash = first32_bytes_to_bytes32(&message);
let message_hash = bytes32(&message);

let salutation = Salutation::new(
message_hash,
message,
greeter.id,
greeter.id.clone(),
block.height.into(),
block.height.into(),
)
.get_or_create()
.await;

// Finally, we save the entities to the database.
greeter.save().await;
salutation.save().await;
}
Expand Down
11 changes: 2 additions & 9 deletions examples/hello-world/hello-indexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ use fuel_indexer_utils::prelude::*;
mod hello_world_indexer {

fn index_logged_greeting(event: Greeting, block: BlockData) {
// We're using the `::new()` method to create a Greeter, which automatically
// generates an ID for the entity. Then, we use `::get_or_create()` to
// load the corresponding record from the database, if present.
let greeter = Greeter::new(
event.person.name.to_right_trimmed_str().into(),
block.height,
Expand All @@ -44,26 +41,22 @@ mod hello_world_indexer {
)
.get_or_create();

// Here we show how you can use the Charfield type to store strings with
// length <= 255. The fuel-indexer-utils package contains a number of helpful
// functions for byte conversion, string manipulation, etc.
let message = format!(
"{} 👋, my name is {}",
event.greeting.to_right_trimmed_str(),
event.person.name.to_right_trimmed_str(),
);
let message_hash = first32_bytes_to_bytes32(&message);
let message_hash = bytes32(&message);

let salutation = Salutation::new(
message_hash,
message,
greeter.id,
greeter.id.clone(),
block.height,
block.height,
)
.get_or_create();

// Finally, we save the entities to the database.
greeter.save();
salutation.save();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/fuel-indexer-macros/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ impl From<ObjectDecoder> for TokenStream {
unsafe {
match &db {
Some(d) => {
match d.lock().await.get_object(Self::TYPE_ID, id).await {
match d.lock().await.get_object(Self::TYPE_ID, id.to_string()).await {
Some(bytes) => {
let columns: Vec<FtColumn> = bincode::deserialize(&bytes).expect("Failed to deserialize Vec<FtColumn> for Entity::load.");
let obj = Self::from_row(columns);
Expand Down
4 changes: 2 additions & 2 deletions packages/fuel-indexer-tests/tests/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ async fn generate_schema_then_load_schema_from_wasm_module(database_url: &str) {
assert!(db.schema().contains_key(&key));
}

let object_id = 4;
let object_id = SizedAsciiString::new("0000000000000000000000000000000000000000000000000000000000000000".to_string()).unwrap();
let columns = vec![
FtColumn::ID(Some(object_id)),
FtColumn::Address(Some(fuel_indexer_types::fuel::Address::from([0x04; 32]))),
Expand Down Expand Up @@ -173,5 +173,5 @@ async fn generate_schema_then_load_schema_from_wasm_module(database_url: &str) {

assert_eq!(obj, bytes);

assert_eq!(db.get_object(thing1_ty_id, 90).await, None);
assert_eq!(db.get_object(thing1_ty_id, SizedAsciiString::new("0000000000000000000000000000000000000000000000000000000000000001".to_string()).unwrap()).await, None);
}
11 changes: 10 additions & 1 deletion packages/fuel-indexer-utils/src/utilities.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
/// Utility functions for Fuel indexers.
use fuel_indexer_plugin::prelude::sha256_digest;
use fuel_indexer_plugin::types::SizedAsciiString;
use fuel_indexer_plugin::types::{Bytes32, SizedAsciiString};

/// Returns the SHA256 hex digest of the input as a `SizedAsciiString`.
pub fn id(data: impl AsRef<[u8]>) -> SizedAsciiString<64> {
SizedAsciiString::<64>::new(sha256_digest(&data)).unwrap()
}

/// Converts a given input into a `Bytes32` object by taking the first 32 bytes
/// of the input's SHA256 hex digest.
pub fn bytes32(data: impl AsRef<[u8]>) -> Bytes32 {
let digest = sha256_digest(&data);
let mut result = [0u8; 32];
result.copy_from_slice(&digest.as_bytes()[0..32]);
Bytes32::from(result)
}

0 comments on commit 83a25cd

Please sign in to comment.