This Rust project implements a canister for managing suppliers, clients, and orders within the context of the Internet Computer (IC). The canister exposes functions for querying and updating data related to clients, suppliers, and orders. It utilizes the ic_cdk
framework, and the data is stored in a thread-local storage structure (StableBTreeMap
) managed by a MemoryManager
.
- Rust
- Internet Computer SDK
- IC CDK
-
Clone the repository:
git clone https://github.com/Collins-Ruto/supply-chain.git cd supply-chain
The core data structures are defined as Rust structs:
#[derive(CandidType, etc)]
struct Client {
id: u64,
name: String,
// etc
}
#[derive(CandidType, etc)]
struct Supplier {
// fields
}
#[derive(CandidType, etc)]
struct Order {
// fields
}
Each record contains relevant fields like IDs, names and timestamps.
Memory is allocated using a MemoryManager
from the ic-stable-structures
crate:
static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> = // initialized
This manages allocating VirtualMemory
for storages.
Unique IDs are generated using a thread-local IdCell
:
static ID_COUNTER: RefCell<IdCell> = // initialized
The counter is incremented when adding new records.
Records are stored in thread-local StableBTreeMap
s:
static CLIENT_STORAGE: RefCell<StableBTreeMap<u64, Client>> = // initialized
static SUPPLIER_STORAGE: RefCell<StableBTreeMap<u64, Supplier>> = // initialized
static ORDERS: RefCell<StableBTreeMap<u64, Order>> = // initialized
This provides fast random access to records.
The Storable
and BoundedStorable
traits are implemented for serialization and bounding record sizes during storage.
Query and update functions are defined using attributes:
#[ic_cdk::query]
fn get_client(id: u64) -> Result<Client, Error> {
// ...
}
#[ic_cdk::update]
fn add_order(payload: OrderPayload) -> Option<Order> {
// ...
}
This exposes a Candid API.
add_client(payload: ClientPayload) -> Option<Client>
Creates a new client based on the provided payload and adds it to the client storage. Returns the created client if successful.
add_supplier(payload: SupplierPayload) -> Option<Supplier>
Creates a new supplier based on the provided payload and adds it to the supplier storage. Returns the created supplier if successful.
add_order(payload: OrderPayload) -> Option<Order>
Creates a new order based on the provided payload and adds it to the order storage. Returns the created order if successful.
add_order_supplier(id: u64, supplier_id: u64) -> Result<Order, Error>
Associates a supplier with an existing order identified by the given ID. Returns the updated order if successful, otherwise returns a NotFound error if the order is not found.
complete_order(id: u64) -> Result<Order, Error>
Marks an order as complete based on the provided ID. Returns the completed order if successful, otherwise returns a NotFound error if the order is not found.
update_order(id: u64, payload: OrderPayload) -> Option<Order>
Updates the information of an existing order identified by the given ID with the provided payload. Returns the updated order if successful.
delete_order(id: u64) -> Result<Order, Error>
Deletes an order based on the provided ID. Returns the deleted order if successful, otherwise returns a NotFound error if the order is not found.
Private helper functions handle data access:
fn _get_client(id: &u64) -> Option<Client> {
// lookup client
}
fn _insert_client(client: &Client) {
// insert into storage
}
The Error
enum captures errors from record lookups.
This provides a full-featured in-memory database with types and interfaces to manage orders, clients, and suppliers through a thread-safe Rust implementation.
This canister provides functionality for managing clients, suppliers, and orders through a set of query and update functions. See the Functions section for details on available operations.
If you want to start working on your project right away, you might want to try the following commands:
cd supply_chain/
dfx help
dfx canister --help
If you want to test your project locally, you can use the following commands:
# Starts the replica, running in the background
dfx start --background
# Deploys your canisters to the replica and generates your candid interface
dfx deploy
Once the job completes, your application will be available at https://localhost:4943?canisterId={asset_canister_id}
.
If you have made changes to your backend canister, you can generate a new candid interface with
npm run generate
at any time. This is recommended before starting the frontend development server, and will be run automatically any time you run dfx deploy
.
If you are making frontend changes, you can start a development server with
npm start
Which will start a server at https://localhost:8080
, proxying API requests to the replica at port 4943.
To learn more before you start working with supply_chain, see the following documentation available online: