-
Notifications
You must be signed in to change notification settings - Fork 66
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
Add hello world example #360
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
707a556
Add hello world example
2e02ad4
Merge branch 'master' of github.com:FuelLabs/fuel-indexer into rashad…
a845263
Update wasm
d4cb8b5
Update scripts
a6fabb5
Update docs
1611fb5
update hello readme
17b9ea8
Merge branch 'master' of github.com:FuelLabs/fuel-indexer into rashad…
dfa4877
update docs + schema
3bef6b9
Update docs
379f703
Merge branch 'master' into rashad/add-hello-world-example-1
ra0x3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Hello World | ||
|
||
A "Hello World" type of program for the Fuel Indexer service. | ||
|
||
```rust | ||
//! A rudimentary block explorer implementation demonstrating how blocks, transactions, | ||
//! contracts, and accounts can be persisted into the database. | ||
//! | ||
//! Build this example's WASM module using the following command. Note that a | ||
//! wasm32-unknown-unknown target will be required. | ||
//! | ||
//! ```bash | ||
//! cargo build -p hello-index --release --target wasm32-unknown-unknown | ||
//! ``` | ||
//! | ||
//! Start a local test Fuel node | ||
//! | ||
//! ```bash | ||
//! cargo run --bin fuel-node | ||
//! ``` | ||
//! | ||
//! With your database backend set up, now start your fuel-indexer binary using the | ||
//! assets from this example: | ||
//! | ||
//! ```bash | ||
//! cargo run --bin fuel-indexer -- --manifest examples/hello-world/hello-index.manifest.yaml | ||
//! ``` | ||
//! | ||
//! Now trigger an event. | ||
//! | ||
//! ```bash | ||
//! cargo run --bin hello-bin | ||
//! ``` | ||
|
||
extern crate alloc; | ||
use fuel_indexer_macros::indexer; | ||
use fuel_indexer_plugin::{types::Bytes32, utils::sha256_digest}; | ||
|
||
// A utility function used to convert an arbitrarily sized string into Bytes32 | ||
// using the first 32 bytes of the String. This might be provided by a standard-ish | ||
// library in the future. | ||
fn bytes32(data: &str) -> Bytes32 { | ||
let data = sha256_digest(&data); | ||
let mut buff = [0u8; 32]; | ||
buff.copy_from_slice(&data.as_bytes()[..32]); | ||
Bytes32::from(buff) | ||
} | ||
|
||
// A utility function used to convert an arbitrarily sized string into u64 | ||
// using the first 8 bytes of the String. This might be provided by a standard-ish | ||
// library in the future. | ||
fn u64_id(data: &str) -> u64 { | ||
let mut buff = [0u8; 8]; | ||
buff.copy_from_slice(&data.as_bytes()[..8]); | ||
u64::from_le_bytes(buff) | ||
} | ||
|
||
#[indexer(manifest = "examples/hello-world/hello-index.manifest.yaml")] | ||
mod hello_world_index { | ||
fn index_logged_greeting(event: Greeting, block: BlockData) { | ||
// Since all events require a u64 ID field, let's derive an ID using the | ||
// name of the person in the Greeting | ||
let greeter_id = u64_id(&event.person.name.to_string()); | ||
|
||
// Here we 'get or create' a Salutation based on the ID of the event | ||
// emiited in the LogData receipt of our smart contract | ||
let greeting = match Salutation::load(event.id) { | ||
Some(mut g) => { | ||
// If we found an event, let's use block height as a proxy for time | ||
g.last_seen = block.height; | ||
g | ||
} | ||
None => { | ||
// If we did not already have this Saluation stored in the database. Here we | ||
// show how you can use the String255 type to store strings with length <= 255 | ||
let message = | ||
format!("{} 👋, my name is {}", &event.greeting, &event.person.name); | ||
|
||
Salutation { | ||
id: event.id, | ||
message_hash: bytes32(&message), | ||
message, | ||
greeter: greeter_id, | ||
first_seen: block.height, | ||
last_seen: block.height, | ||
} | ||
} | ||
}; | ||
|
||
// Here we do the same with Greeter that we did for Saluation -- if we have an event | ||
// already saved in the database, load it and update it. If we do not have this Greeter | ||
// in the database then create one | ||
let greeter = match Greeter::load(greeter_id) { | ||
Some(mut g) => { | ||
g.last_seen = block.height; | ||
g | ||
} | ||
None => Greeter { | ||
id: greeter_id, | ||
first_seen: block.height, | ||
name: event.person.name.to_string(), | ||
last_seen: block.height, | ||
}, | ||
}; | ||
|
||
// Both entity saves will occur in the same transaction | ||
greeting.save(); | ||
greeter.save(); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
# Examples | ||
|
||
- [Hello World](./hello-world.md) | ||
- A "Hello World" type of program for the Fuel Indexer service. | ||
- [Block Explorer](./block-explorer.md) | ||
- An extremely basic block explorer implementation that shows how blocks, transactions, contracts, and accounts can be persisted into the database. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: "emitted"