@@ -49,4 +49,188 @@ describe("coder.accounts", () => {
4949 assert . deepEqual ( coder . accounts . decode ( "MemberDAO" , encoded ) , memberDAO ) ;
5050 } ) ;
5151 } ) ;
52+
53+ test ( "Can encode and decode user-defined accounts, including those with more nested & multiple const generics" , ( ) => {
54+ const idl : Idl = {
55+ address : "EQoYLkj17hXm8yc9qLq5Cm7FgCqujRVHd2ZhdEfAmrMF" ,
56+ metadata : {
57+ name : "gen_idl" ,
58+ version : "0.1.0" ,
59+ spec : "0.1.0" ,
60+ description : "Created with Anchor" ,
61+ } ,
62+ instructions : [
63+ {
64+ name : "initialize" ,
65+ discriminator : [ 175 , 175 , 109 , 31 , 13 , 152 , 155 , 237 ] ,
66+ accounts : [
67+ {
68+ name : "my_acc" ,
69+ pda : {
70+ seeds : [
71+ {
72+ kind : "const" ,
73+ value : [
74+ 115 , 109 , 97 , 108 , 108 , 95 , 109 , 101 , 109 , 112 , 111 , 111 ,
75+ 108 ,
76+ ] ,
77+ } ,
78+ ] ,
79+ } ,
80+ } ,
81+ ] ,
82+ args : [ ] ,
83+ } ,
84+ ] ,
85+ accounts : [
86+ {
87+ name : "MyAcc" ,
88+ discriminator : [ 123 , 153 , 151 , 118 , 126 , 71 , 73 , 92 ] ,
89+ } ,
90+ ] ,
91+ types : [
92+ {
93+ name : "MaxHeap" ,
94+ serialization : "bytemuckunsafe" ,
95+ repr : {
96+ kind : "c" ,
97+ } ,
98+ generics : [
99+ {
100+ kind : "type" ,
101+ name : "T" ,
102+ } ,
103+ {
104+ kind : "const" ,
105+ name : "SIZE" ,
106+ type : "usize" ,
107+ } ,
108+ {
109+ kind : "const" ,
110+ name : "PADDING" ,
111+ type : "usize" ,
112+ } ,
113+ ] ,
114+ type : {
115+ kind : "struct" ,
116+ fields : [
117+ {
118+ name : "entries" ,
119+ type : {
120+ array : [
121+ {
122+ generic : "T" ,
123+ } ,
124+ {
125+ generic : "SIZE" ,
126+ } ,
127+ ] ,
128+ } ,
129+ } ,
130+ {
131+ name : "count" ,
132+ type : "u16" ,
133+ } ,
134+ {
135+ name : "padding" ,
136+ type : {
137+ array : [
138+ "u8" ,
139+ {
140+ generic : "PADDING" ,
141+ } ,
142+ ] ,
143+ } ,
144+ } ,
145+ ] ,
146+ } ,
147+ } ,
148+ {
149+ name : "MyStruct" ,
150+ serialization : "bytemuck" ,
151+ repr : {
152+ kind : "c" ,
153+ } ,
154+ type : {
155+ kind : "struct" ,
156+ fields : [
157+ {
158+ name : "a" ,
159+ type : "u16" ,
160+ } ,
161+ {
162+ name : "b" ,
163+ type : "u16" ,
164+ } ,
165+ ] ,
166+ } ,
167+ } ,
168+ {
169+ name : "MyAcc" ,
170+ serialization : "bytemuck" ,
171+ repr : {
172+ kind : "transparent" ,
173+ } ,
174+ type : {
175+ kind : "struct" ,
176+ fields : [
177+ {
178+ name : "inner" ,
179+ type : {
180+ defined : {
181+ name : "MaxHeap" ,
182+ generics : [
183+ {
184+ kind : "type" ,
185+ type : {
186+ defined : {
187+ name : "MyStruct" ,
188+ } ,
189+ } ,
190+ } ,
191+ {
192+ kind : "const" ,
193+ value : "3" ,
194+ } ,
195+ {
196+ kind : "const" ,
197+ value : "10" ,
198+ } ,
199+ ] ,
200+ } ,
201+ } ,
202+ } ,
203+ ] ,
204+ } ,
205+ } ,
206+ ] ,
207+ } ;
208+
209+ const coder = new BorshCoder ( idl ) ;
210+
211+ const myAcc = {
212+ inner : {
213+ entries : [
214+ {
215+ a : 1 ,
216+ b : 2 ,
217+ } ,
218+ {
219+ a : 3 ,
220+ b : 4 ,
221+ } ,
222+ {
223+ a : 5 ,
224+ b : 6 ,
225+ } ,
226+ ] ,
227+ count : 2 ,
228+ padding : new Array ( 10 ) . fill ( 0 ) ,
229+ } ,
230+ } ;
231+
232+ coder . accounts . encode ( "MyAcc" , myAcc ) . then ( ( encoded ) => {
233+ assert . deepEqual ( coder . accounts . decode ( "MyAcc" , encoded ) , myAcc ) ;
234+ } ) ;
235+ } ) ;
52236} ) ;
0 commit comments