Skip to content

Commit f35c5de

Browse files
committed
Tests complete
1 parent 2cace22 commit f35c5de

File tree

3 files changed

+549
-780
lines changed

3 files changed

+549
-780
lines changed

src/__tests__/Anchor.test.ts

Lines changed: 109 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AnchorProvider, Wallet } from '@coral-xyz/anchor'
22
import { Connection, Keypair, PublicKey } from '@solana/web3.js'
3+
import { BN } from 'bn.js'
34
import { getPythProgramKeyForCluster, pythOracleProgram, PythOracleCoder } from '../index'
45

56
test('Anchor', (done) => {
@@ -12,7 +13,7 @@ test('Anchor', (done) => {
1213
const pythOracle = pythOracleProgram(getPythProgramKeyForCluster('mainnet-beta'), provider)
1314
pythOracle.methods
1415
.initMapping()
15-
.accounts({ fundingAccount: new PublicKey(0), freshMappingAccount: new PublicKey(1) })
16+
.accounts({ fundingAccount: PublicKey.unique(), freshMappingAccount: PublicKey.unique() })
1617
.instruction()
1718
.then((instruction) => {
1819
expect(instruction.data).toStrictEqual(Buffer.from([2, 0, 0, 0, 0, 0, 0, 0]))
@@ -22,7 +23,7 @@ test('Anchor', (done) => {
2223
})
2324
pythOracle.methods
2425
.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() })
2627
.instruction()
2728
.then((instruction) => {
2829
expect(instruction.data).toStrictEqual(Buffer.from([2, 0, 0, 0, 1, 0, 0, 0]))
@@ -31,14 +32,114 @@ test('Anchor', (done) => {
3132
expect(decoded?.data).toStrictEqual({})
3233
})
3334
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() })
3648
.instruction()
3749
.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]))
3951
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+
43144
done()
44145
})

0 commit comments

Comments
 (0)