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!(
92 json,
93 r#"{"kind":"pdaValueNode","pda":{"kind":"pdaLinkNode","name":"myPda"}}"#
94 );
95 }
96
97 #[test]
98 fn from_json() {
99 let json: &str = r#"{"kind":"pdaValueNode","pda":{"kind":"pdaLinkNode","name":"myPda"}}"#;
101 let node: PdaValueNode = serde_json::from_str(json).unwrap();
102 assert_eq!(node, PdaValueNode::new(PdaLinkNode::new("myPda"), vec![]));
103 }
104
105 #[test]
106 fn to_json_with_program_id() {
107 let node = PdaValueNode::new_with_program_id(
108 PdaValuePda::PdaLink(PdaLinkNode::new("myPda")),
109 vec![],
110 AccountValueNode::new("myProgramAccount"),
111 );
112 let json = serde_json::to_string(&node).unwrap();
113 assert_eq!(
115 json,
116 r#"{"kind":"pdaValueNode","pda":{"kind":"pdaLinkNode","name":"myPda"},"programId":{"kind":"accountValueNode","name":"myProgramAccount"}}"#
117 );
118 }
119}