1
1
import { AnchorProvider , Wallet } from '@coral-xyz/anchor'
2
2
import { Connection , Keypair , PublicKey } from '@solana/web3.js'
3
+ import { BN } from 'bn.js'
3
4
import { getPythProgramKeyForCluster , pythOracleProgram , PythOracleCoder } from '../index'
4
5
5
6
test ( 'Anchor' , ( done ) => {
@@ -12,7 +13,7 @@ test('Anchor', (done) => {
12
13
const pythOracle = pythOracleProgram ( getPythProgramKeyForCluster ( 'mainnet-beta' ) , provider )
13
14
pythOracle . methods
14
15
. initMapping ( )
15
- . accounts ( { fundingAccount : new PublicKey ( 0 ) , freshMappingAccount : new PublicKey ( 1 ) } )
16
+ . accounts ( { fundingAccount : PublicKey . unique ( ) , freshMappingAccount : PublicKey . unique ( ) } )
16
17
. instruction ( )
17
18
. then ( ( instruction ) => {
18
19
expect ( instruction . data ) . toStrictEqual ( Buffer . from ( [ 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) )
@@ -22,7 +23,7 @@ test('Anchor', (done) => {
22
23
} )
23
24
pythOracle . methods
24
25
. addMapping ( )
25
- . accounts ( { fundingAccount : new PublicKey ( 0 ) , curMapping : new PublicKey ( 1 ) , nextMapping : new PublicKey ( 2 ) } )
26
+ . accounts ( { fundingAccount : PublicKey . unique ( ) , curMapping : PublicKey . unique ( ) , nextMapping : PublicKey . unique ( ) } )
26
27
. instruction ( )
27
28
. then ( ( instruction ) => {
28
29
expect ( instruction . data ) . toStrictEqual ( Buffer . from ( [ 2 , 0 , 0 , 0 , 1 , 0 , 0 , 0 ] ) )
@@ -31,14 +32,114 @@ test('Anchor', (done) => {
31
32
expect ( decoded ?. data ) . toStrictEqual ( { } )
32
33
} )
33
34
pythOracle . methods
34
- . addMapping ( )
35
- . accounts ( { fundingAccount : new PublicKey ( 0 ) , curMapping : new PublicKey ( 1 ) , nextMapping : new PublicKey ( 2 ) } )
35
+ . updProduct ( )
36
+ . accounts ( { fundingAccount : PublicKey . unique ( ) , productAccount : PublicKey . unique ( ) } )
37
+ . instruction ( )
38
+ . then ( ( instruction ) => {
39
+ expect ( instruction . data ) . toStrictEqual ( Buffer . from ( [ 2 , 0 , 0 , 0 , 3 , 0 , 0 , 0 ] ) )
40
+ const decoded = ( pythOracle . coder as PythOracleCoder ) . instruction . decode ( instruction . data )
41
+ expect ( decoded ?. name ) . toBe ( 'updProduct' )
42
+ expect ( decoded ?. data ) . toStrictEqual ( { } )
43
+ } ) ;
44
+
45
+ pythOracle . methods
46
+ . addPrice ( 1 , 1 )
47
+ . accounts ( { fundingAccount : PublicKey . unique ( ) , productAccount : PublicKey . unique ( ) } )
36
48
. instruction ( )
37
49
. then ( ( instruction ) => {
38
- expect ( instruction . data ) . toStrictEqual ( Buffer . from ( [ 2 , 0 , 0 , 0 , 1 , 0 , 0 , 0 ] ) )
50
+ expect ( instruction . data ) . toStrictEqual ( Buffer . from ( [ 2 , 0 , 0 , 0 , 4 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 ] ) )
39
51
const decoded = ( pythOracle . coder as PythOracleCoder ) . instruction . decode ( instruction . data )
40
- expect ( decoded ?. name ) . toBe ( 'addMapping' )
41
- expect ( decoded ?. data ) . toStrictEqual ( { } )
42
- } )
52
+ expect ( decoded ?. name ) . toBe ( 'addPrice' )
53
+ expect ( decoded ?. data ) . toStrictEqual ( { expo : 1 , pType : 1 } )
54
+ } ) ;
55
+
56
+ pythOracle . methods
57
+ . addPublisher ( new PublicKey ( 5 ) )
58
+ . accounts ( { fundingAccount : PublicKey . unique ( ) , priceAccount : PublicKey . unique ( ) } )
59
+ . instruction ( )
60
+ . then ( ( instruction ) => {
61
+ expect ( instruction . data ) . toStrictEqual ( Buffer . from ( [ 2 , 0 , 0 , 0 , 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 5 ]
62
+ ) )
63
+ const decoded = ( pythOracle . coder as PythOracleCoder ) . instruction . decode ( instruction . data )
64
+ expect ( decoded ?. name ) . toBe ( 'addPublisher' )
65
+ expect ( decoded ?. data . pub . equals ( new PublicKey ( 5 ) ) ) . toBeTruthy ( )
66
+ } ) ;
67
+
68
+ pythOracle . methods
69
+ . updPrice ( 1 , 0 , new BN ( 42 ) , new BN ( 9 ) , new BN ( 1 ) )
70
+ . accounts ( { fundingAccount : PublicKey . unique ( ) , priceAccount : PublicKey . unique ( ) } )
71
+ . instruction ( )
72
+ . then ( ( instruction ) => {
73
+ expect ( instruction . data ) . toStrictEqual ( Buffer . from ( [ 2 , 0 , 0 , 0 , 7 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 42 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 9 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
74
+ ) )
75
+ const decoded = ( pythOracle . coder as PythOracleCoder ) . instruction . decode ( instruction . data )
76
+ expect ( decoded ?. name ) . toBe ( 'updPrice' )
77
+ expect ( decoded ?. data . status === 1 ) . toBeTruthy ( )
78
+ expect ( decoded ?. data . price . eq ( new BN ( 42 ) ) ) . toBeTruthy ( )
79
+ expect ( decoded ?. data . conf . eq ( new BN ( 9 ) ) ) . toBeTruthy ( )
80
+ expect ( decoded ?. data . pubSlot . eq ( new BN ( 1 ) ) ) . toBeTruthy ( )
81
+ } ) ;
82
+
83
+ pythOracle . methods
84
+ . updPriceNoFailOnError ( 1 , 0 , new BN ( 42 ) , new BN ( 9 ) , new BN ( 1 ) )
85
+ . accounts ( { fundingAccount : PublicKey . unique ( ) , priceAccount : PublicKey . unique ( ) } )
86
+ . instruction ( )
87
+ . then ( ( instruction ) => {
88
+ expect ( instruction . data ) . toStrictEqual ( Buffer . from ( [ 2 , 0 , 0 , 0 , 13 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 42 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 9 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
89
+ ) )
90
+ const decoded = ( pythOracle . coder as PythOracleCoder ) . instruction . decode ( instruction . data )
91
+ expect ( decoded ?. name ) . toBe ( 'updPriceNoFailOnError' )
92
+ expect ( decoded ?. data . status === 1 ) . toBeTruthy ( )
93
+ expect ( decoded ?. data . price . eq ( new BN ( 42 ) ) ) . toBeTruthy ( )
94
+ expect ( decoded ?. data . conf . eq ( new BN ( 9 ) ) ) . toBeTruthy ( )
95
+ expect ( decoded ?. data . pubSlot . eq ( new BN ( 1 ) ) ) . toBeTruthy ( )
96
+ } ) ;
97
+
98
+ pythOracle . methods
99
+ . aggPrice ( 1 , 0 , new BN ( 42 ) , new BN ( 9 ) , new BN ( 1 ) )
100
+ . accounts ( { fundingAccount : PublicKey . unique ( ) , priceAccount : PublicKey . unique ( ) } )
101
+ . instruction ( )
102
+ . then ( ( instruction ) => {
103
+ expect ( instruction . data ) . toStrictEqual ( Buffer . from ( [ 2 , 0 , 0 , 0 , 8 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 42 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 9 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
104
+ ) )
105
+ const decoded = ( pythOracle . coder as PythOracleCoder ) . instruction . decode ( instruction . data )
106
+ expect ( decoded ?. name ) . toBe ( 'aggPrice' )
107
+ expect ( decoded ?. data . status === 1 ) . toBeTruthy ( )
108
+ expect ( decoded ?. data . price . eq ( new BN ( 42 ) ) ) . toBeTruthy ( )
109
+ expect ( decoded ?. data . conf . eq ( new BN ( 9 ) ) ) . toBeTruthy ( )
110
+ expect ( decoded ?. data . pubSlot . eq ( new BN ( 1 ) ) ) . toBeTruthy ( )
111
+ } ) ;
112
+
113
+ pythOracle . methods
114
+ . setMinPub ( 5 , [ 0 , 0 , 0 ] )
115
+ . accounts ( { fundingAccount : PublicKey . unique ( ) , priceAccount : PublicKey . unique ( ) } )
116
+ . instruction ( )
117
+ . then ( ( instruction ) => {
118
+ expect ( instruction . data ) . toStrictEqual ( Buffer . from ( [ 2 , 0 , 0 , 0 , 12 , 0 , 0 , 0 , 5 , 0 , 0 , 0 ]
119
+ ) )
120
+ const decoded = ( pythOracle . coder as PythOracleCoder ) . instruction . decode ( instruction . data )
121
+ expect ( decoded ?. name ) . toBe ( 'setMinPub' )
122
+ expect ( decoded ?. data . minPub === 5 ) . toBeTruthy ( )
123
+ } ) ;
124
+
125
+ pythOracle . methods
126
+ . updPermissions ( new PublicKey ( 6 ) , new PublicKey ( 7 ) , new PublicKey ( 8 ) )
127
+ . accounts ( { upgradeAuthority : PublicKey . unique ( ) , programAccount : PublicKey . unique ( ) , programDataAccount : PublicKey . unique ( ) } )
128
+ . instruction ( )
129
+ . then ( ( instruction ) => {
130
+ const expectedPda = PublicKey . findProgramAddressSync ( [ Buffer . from ( "permissions" ) ] , pythOracle . programId ) [ 0 ] ;
131
+ expect ( expectedPda . equals ( instruction . keys [ 3 ] . pubkey ) ) ;
132
+ expect ( instruction . data ) . toStrictEqual ( Buffer . from ( [ 2 , 0 , 0 , 0 , 17 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 6 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 8 ] ) )
133
+ const decoded = ( pythOracle . coder as PythOracleCoder ) . instruction . decode ( instruction . data )
134
+ expect ( decoded ?. name ) . toBe ( 'updPermissions' )
135
+ expect ( decoded ?. data . masterAuthority . equals ( new PublicKey ( 6 ) ) ) . toBeTruthy ( )
136
+ expect ( decoded ?. data . dataCurationAuthority . equals ( new PublicKey ( 7 ) ) ) . toBeTruthy ( )
137
+ expect ( decoded ?. data . securityAuthority . equals ( new PublicKey ( 8 ) ) ) . toBeTruthy ( )
138
+ } ) ;
139
+
140
+
141
+
142
+
143
+
43
144
done ( )
44
145
} )
0 commit comments