-
Notifications
You must be signed in to change notification settings - Fork 731
/
Copy pathwrapped_asset.rs
97 lines (79 loc) · 2.57 KB
/
wrapped_asset.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use anchor_lang::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct MetadataUri {
wormhole_chain_id: u16,
canonical_address: String,
native_decimals: u8,
}
#[derive(Debug, AnchorSerialize, AnchorDeserialize, Clone, PartialEq, Eq, InitSpace)]
pub struct LegacyWrappedAsset {
pub token_chain: u16,
pub token_address: [u8; 32],
pub native_decimals: u8,
}
impl core_bridge_program::sdk::legacy::LegacyAccount for LegacyWrappedAsset {
const DISCRIMINATOR: &'static [u8] = &[];
fn program_id() -> Pubkey {
crate::ID
}
}
impl LegacyWrappedAsset {
pub const SEED_PREFIX: &'static [u8] = b"meta";
pub fn to_uri(&self) -> String {
let mut uri = serde_json::to_string_pretty(&MetadataUri {
wormhole_chain_id: self.token_chain,
canonical_address: format!("0x{}", hex::encode(self.token_address)),
native_decimals: self.native_decimals,
})
.expect("serialization should not fail");
// Unlikely to happen, but truncate the URI if it's too long.
uri.truncate(mpl_token_metadata::state::MAX_URI_LENGTH);
uri
}
}
#[derive(Debug, AnchorSerialize, AnchorDeserialize, Clone, PartialEq, Eq, InitSpace)]
pub struct WrappedAsset {
pub legacy: LegacyWrappedAsset,
pub last_updated_sequence: u64,
}
impl std::ops::Deref for WrappedAsset {
type Target = LegacyWrappedAsset;
fn deref(&self) -> &Self::Target {
&self.legacy
}
}
impl WrappedAsset {
pub const SEED_PREFIX: &'static [u8] = LegacyWrappedAsset::SEED_PREFIX;
}
impl core_bridge_program::sdk::legacy::LegacyAccount for WrappedAsset {
const DISCRIMINATOR: &'static [u8] = LegacyWrappedAsset::DISCRIMINATOR;
fn program_id() -> Pubkey {
crate::ID
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn to_uri() {
let asset = WrappedAsset {
legacy: LegacyWrappedAsset {
token_chain: 420,
token_address: [
222, 173, 190, 239, 222, 173, 190, 239, 222, 173, 190, 239, 222, 173, 190, 239,
222, 173, 190, 239, 222, 173, 190, 239, 222, 173, 190, 239, 222, 173, 190, 239,
],
native_decimals: 18,
},
last_updated_sequence: 69,
};
let expected = r#"{
"wormholeChainId": 420,
"canonicalAddress": "0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
"nativeDecimals": 18
}"#;
assert_eq!(asset.to_uri(), expected);
}
}