Skip to content

Instantly share code, notes, and snippets.

@chadoh
Created September 21, 2020 23:40
Show Gist options
  • Save chadoh/1befecc13a4fe0c78fe34e26c32f3448 to your computer and use it in GitHub Desktop.
Save chadoh/1befecc13a4fe0c78fe34e26c32f3448 to your computer and use it in GitHub Desktop.
super rough pseudocode, brainstorming how to duplicate some lengthy metadata as few times as possible in a NEAR smart contract
struct Token {
id: u64,
metadata_id: u64,
}
let metadata_vec = vec![];
let metadata_map = HashMap::new(); // String to metadata_vec index of this String
let creator_id_to_metadata_ids = HashMap<String, Vec<u64>>::new();
fn fetch_token(id: u64) -> Token {
Token(id: 1, metadata_id: 1)
}
fn get_token_and_metadata(id: u64) -> (Token, String) {
let token = fetch_token(id);
let metadata_string = metadata_vec[token.metadata_id]
(token, metadata_string)
}
fn find_or_create_metadata(creator_id: String, img_url: String) -> u64 {
let metdata_string = format!("{}~{}", creator_id, img_url)
match metadata_map.get(metadata_string) {
Some(metadata_index) -> metadata_index,
None -> {
let array_index = metadata_vec.length;
metadata_vec.push(metadata_string);
metadata_map.insert(metadata_string, array_index);
match creator_id_to_metadata_ids.get(creator_id) {
Some(array_of_metadata_ids) -> {
array_of_metadata_ids.push(array_index);
},
None -> {
creator_id_to_metadata_ids.insert(creator_id, vec![array_index])
}
}
array_index
}
}
}
fn create_token(creator_id: String, img_url: String) -> (Token, String) {
let metadata_id = find_or_create_metadata(creator_id, img_url);
}
fn get_all_metadata_for_creator(creator_id: String) -> Vec<String> {
creator_id_to_metadata_ids[creator_id].map(n => {
metadata_vec[n]
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment