Skip to content

Instantly share code, notes, and snippets.

@sccheruku
Created May 17, 2022 16:30
Show Gist options
  • Save sccheruku/78105c3181918046473bd3a1524b2b3a to your computer and use it in GitHub Desktop.
Save sccheruku/78105c3181918046473bd3a1524b2b3a to your computer and use it in GitHub Desktop.
near protocol contract built using templates and building blocks
// To conserve gas, efficient serialization is achieved through Borsh (http://borsh.io/)
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::{LookupMap, UnorderedMap};
use near_sdk::json_types::U128;
use near_sdk::serde_json::json;
use near_sdk::{assert_one_yocto, env, near_bindgen, setup_alloc, AccountId, Balance};
use std::collections::HashMap;
use near_contract_standards::fungible_token::FungibleToken;
use near_contract_standards::fungible_token::metadata::{
FungibleTokenMetadata, FungibleTokenMetadataProvider, FT_METADATA_SPEC,
};
setup_alloc!();
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct ComposedSmartContract {
token: FungibleToken,
// Using String, String for simplicity, in production scenario it will be more mapped to a struct.
catalog: UnorderedMap<String, String>,
}
impl Default for ComposedSmartContract {
fn default() -> Self {
Self {
token: FungibleToken::new(),
ad_campaign_impressions: UnorderedMap::new(StorageKey::AdCampaignImpression),
}
}
}
near_contract_standards::impl_fungible_token_core!(Contract, token, on_tokens_burned);
near_contract_standards::impl_fungible_token_storage!(Contract, token, on_account_closed);
// Shared templates build and improved by the community
near_template_standards::impl_non_transferrable_token!(Contract, token, call_back_fn);
near_template_standards::impl_pagination!(Contract, catalog, call_back_fn);
// Domain specific templates
kirana_network_template_standards::impl_perishable_catalog!(Contract, catalog, call_back_fn);
kirana_network_template_standards::impl_escrowed_sales!(Contract, catalog, call_back_fn);
// Platform specific templates ???
kirana_network_template_standards::impl_event_logs!(Contract, catalog, LIST_OF_METHODS);
#[near_bindgen]
impl ComposedSmartContract {
// Example code generated by `impl_pagination`
pub fn list_products(self, take: u8, skip: u32) -> Vec<AdCampaign> {
// ... generated implementation. Provided by impl_pagination macro
}
fn list_products_by_category(self, take: u8, skip: u32) -> Vec<AdCampaign> {
// ... generated implementation. Provided by impl_pagination macro
}
pub fn get_product(self, id: String) -> String {
// ...generated implementation
}
pub fn create_order(self, id: Struct) -> String {
// ...generated implementation
}
/// from impl_event_logs, a set of events will be logged using the Events Format.
/// https://nomicon.io/Standards/EventsFormat
/// EVENT_JSON:{
// "standard": "nepXXX", // TODO: Can we use "knXXX" instead ?
// "version": "1.0.0",
// "event": "abc_is_triggered"
// }
/// kirana network will be watching the network for these events, downstream event driven processing can be handled.
}
#[cfg(test)]
mod tests {
use super::*;
use near_sdk::MockedBlockchain;
use near_sdk::{testing_env, VMContext};
/** Autogenerated tests omitted for brevity */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment