codama_nodes/contextual_value_nodes/
pda_value_node.rs1use crate::{PdaSeedValueNode, PdaValueNode, PdaValuePda, PdaValueProgramId};
2
3impl PdaValueNode {
4 pub fn new<T>(pda: T, seeds: Vec<PdaSeedValueNode>) -> Self
5 where
6 T: Into<PdaValuePda>,
7 {
8 Self {
9 pda: Box::new(pda.into()),
10 seeds,
11 program_id: Box::new(None),
12 }
13 }
14
15 pub fn new_with_program_id<T, U>(pda: T, seeds: Vec<PdaSeedValueNode>, program_id: U) -> Self
16 where
17 T: Into<PdaValuePda>,
18 U: Into<PdaValueProgramId>,
19 {
20 Self {
21 pda: Box::new(pda.into()),
22 seeds,
23 program_id: Box::new(Some(program_id.into())),
24 }
25 }
26}
27
28#[cfg(test)]
29mod tests {
30 use super::*;
31 use crate::{
32 AccountValueNode, NumberTypeNode, NumberValueNode, PdaLinkNode, PdaNode,
33 PublicKeyValueNode, VariablePdaSeedNode, U32,
34 };
35
36 #[test]
37 fn new_linked() {
38 let node = PdaValueNode::new(
39 PdaLinkNode::new("masterEdition"),
40 vec![
41 PdaSeedValueNode::new(
42 "mint",
43 PublicKeyValueNode::new("33QJ9VtGKRS7wstQiwuigk1cBVYEPp3XBCC1g9WkDFEE"),
44 ),
45 PdaSeedValueNode::new("edition", NumberValueNode::new(42)),
46 ],
47 );
48 assert_eq!(
49 *node.pda,
50 PdaValuePda::PdaLink(PdaLinkNode::new("masterEdition"))
51 );
52 assert_eq!(
53 node.seeds,
54 vec![
55 PdaSeedValueNode::new(
56 "mint",
57 PublicKeyValueNode::new("33QJ9VtGKRS7wstQiwuigk1cBVYEPp3XBCC1g9WkDFEE")
58 ),
59 PdaSeedValueNode::new("edition", NumberValueNode::new(42)),
60 ]
61 );
62 }
63
64 #[test]
65 fn new_nested() {
66 let node = PdaValueNode::new(
67 PdaNode::new(
68 "counter",
69 vec![VariablePdaSeedNode::new("value", NumberTypeNode::le(U32)).into()],
70 ),
71 vec![PdaSeedValueNode::new("value", NumberValueNode::new(42))],
72 );
73 assert_eq!(
74 *node.pda,
75 PdaValuePda::Pda(PdaNode::new(
76 "counter",
77 vec![VariablePdaSeedNode::new("value", NumberTypeNode::le(U32)).into()],
78 ))
79 );
80 assert_eq!(
81 node.seeds,
82 vec![PdaSeedValueNode::new("value", NumberValueNode::new(42)),]
83 );
84 }
85
86 #[test]
87 fn to_json() {
88 let node = PdaValueNode::new(PdaLinkNode::new("myPda"), vec![]);
89 let json = serde_json::to_string(&node).unwrap();
90 assert_eq!(
91 json,
92 r#"{"kind":"pdaValueNode","pda":{"kind":"pdaLinkNode","name":"myPda"},"seeds":[]}"#
93 );
94 }
95
96 #[test]
97 fn from_json() {
98 let json: &str =
99 r#"{"kind":"pdaValueNode","pda":{"kind":"pdaLinkNode","name":"myPda"},"seeds":[]}"#;
100 let node: PdaValueNode = serde_json::from_str(json).unwrap();
101 assert_eq!(node, PdaValueNode::new(PdaLinkNode::new("myPda"), vec![]));
102 }
103
104 #[test]
105 fn to_json_with_program_id() {
106 let node = PdaValueNode::new_with_program_id(
107 PdaValuePda::PdaLink(PdaLinkNode::new("myPda")),
108 vec![],
109 AccountValueNode::new("myProgramAccount"),
110 );
111 let json = serde_json::to_string(&node).unwrap();
112 assert_eq!(
113 json,
114 r#"{"kind":"pdaValueNode","pda":{"kind":"pdaLinkNode","name":"myPda"},"seeds":[],"programId":{"kind":"accountValueNode","name":"myProgramAccount"}}"#
115 );
116 }
117}