Skip to content

Commit 494570b

Browse files
SwenSchaeferjohannSwenananas-block
authored
chore: RPC patch (#1072)
* add preTokenBalances, postTokenBalances to txinfo * upd package json resolve node import err in ctoken * lint * version bump 0.7.0 and 0.8.0 * add cursor to remaining rpc methods * bump v to latest, fix cli lint * Update js/stateless.js/package.json * Update js/compressed-token/package.json --------- Co-authored-by: Swen <swen.schaeferjohann@code.berlin> Co-authored-by: ananas-block <58553958+ananas-block@users.noreply.github.com>
1 parent d7d5507 commit 494570b

23 files changed

+302
-188
lines changed

cli/src/commands/balance/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ class BalanceCommand extends Command {
3737

3838
loader.stop(false);
3939

40-
if (tokenAccounts.length === 0) {
40+
if (tokenAccounts.items.length === 0) {
4141
console.log("No token accounts found");
4242
return;
4343
}
4444

45-
const compressedTokenAccount = tokenAccounts.find((acc) =>
45+
const compressedTokenAccount = tokenAccounts.items.find((acc) =>
4646
acc.parsed.mint.equals(refMint),
4747
);
4848
if (compressedTokenAccount === undefined) {

js/compressed-token/package.json

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
{
22
"name": "@lightprotocol/compressed-token",
3-
"version": "0.4.1",
3+
"version": "0.10.1",
44
"description": "JS client to interact with the compressed-token program",
55
"sideEffects": false,
6-
"type": "module",
76
"main": "dist/cjs/node/index.cjs",
8-
"module": "dist/es/node/index.js",
9-
"browser": {
10-
"./dist/cjs/node/index.cjs": "./dist/cjs/browser/index.cjs",
11-
"./dist/es/node/index.js": "./dist/es/browser/index.js"
12-
},
13-
"types": "dist/types/index.d.ts",
7+
"type": "module",
148
"exports": {
159
".": {
16-
"import": "./dist/es/node/index.js",
1710
"require": "./dist/cjs/node/index.cjs",
18-
"browser": {
19-
"import": "./dist/es/browser/index.js",
20-
"require": "./dist/cjs/browser/index.cjs"
21-
},
2211
"types": "./dist/types/index.d.ts",
2312
"default": "./dist/cjs/node/index.cjs"
13+
},
14+
"./browser": {
15+
"import": "./dist/es/browser/index.js",
16+
"require": "./dist/cjs/browser/index.cjs",
17+
"types": "./dist/types/index.d.ts"
2418
}
2519
},
20+
"types": "./dist/types/index.d.ts",
2621
"files": [
2722
"dist"
2823
],

js/compressed-token/src/actions/decompress.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export async function decompress(
5858

5959
/// TODO: consider using a different selection algorithm
6060
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
61-
compressedTokenAccounts,
61+
compressedTokenAccounts.items,
6262
amount,
6363
);
6464

js/compressed-token/src/actions/transfer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export async function transfer(
5555
);
5656

5757
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
58-
compressedTokenAccounts,
58+
compressedTokenAccounts.items,
5959
amount,
6060
);
6161

js/compressed-token/tests/e2e/approve-and-mint-to.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ async function assertApproveAndMintTo(
110110
},
111111
);
112112

113-
const compressedTokenAccount = compressedTokenAccounts[0];
113+
const compressedTokenAccount = compressedTokenAccounts.items[0];
114114
expect(compressedTokenAccount.parsed.mint.toBase58()).toBe(
115115
refMint.toBase58(),
116116
);

js/compressed-token/tests/e2e/compress.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async function assertCompress(
3434
mint: refMint,
3535
});
3636

37-
const recipientSumPost = recipientCompressedTokenBalanceAfter.reduce(
37+
const recipientSumPost = recipientCompressedTokenBalanceAfter.items.reduce(
3838
(acc, curr) => bn(acc).add(curr.parsed.amount),
3939
bn(0),
4040
);
@@ -121,7 +121,7 @@ describe('compress', () => {
121121
mint,
122122
bn(700),
123123
charlie.publicKey,
124-
recipientCompressedTokenBalanceBefore,
124+
recipientCompressedTokenBalanceBefore.items,
125125
);
126126
});
127127
});

js/compressed-token/tests/e2e/decompress.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ async function assertDecompress(
2929
const refRecipientAtaBalanceAfter =
3030
await rpc.getTokenAccountBalance(refRecipientAta);
3131

32-
const senderCompressedTokenBalanceAfter =
32+
const senderCompressedTokenBalanceAfter = (
3333
await rpc.getCompressedTokenAccountsByOwner(refSender, {
3434
mint: refMint,
35-
});
35+
})
36+
).items;
3637

3738
const senderSumPost = senderCompressedTokenBalanceAfter.reduce(
3839
(acc, curr) => bn(acc).add(curr.parsed.amount),
@@ -126,7 +127,7 @@ describe('decompress', () => {
126127
mint,
127128
bn(5),
128129
bob.publicKey,
129-
senderCompressedTokenBalanceBefore,
130+
senderCompressedTokenBalanceBefore.items,
130131
);
131132
}
132133
});

js/compressed-token/tests/e2e/mint-to.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async function assertMintTo(
2929
},
3030
);
3131

32-
const compressedTokenAccount = compressedTokenAccounts[0];
32+
const compressedTokenAccount = compressedTokenAccounts.items[0];
3333
expect(compressedTokenAccount.parsed.mint.toBase58()).toBe(
3434
refMint.toBase58(),
3535
);

js/compressed-token/tests/e2e/rpc-token-interop.test.ts

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ import {
44
Rpc,
55
newAccountWithLamports,
66
bn,
7-
defaultTestStateTreeAccounts,
87
createRpc,
98
getTestRpc,
109
TestRpc,
1110
} from '@lightprotocol/stateless.js';
1211
import { WasmFactory } from '@lightprotocol/hasher.rs';
1312
import { createMint, mintTo, transfer } from '../../src/actions';
14-
import {
15-
PublicTransactionEvent,
16-
getParsedEvents,
17-
} from '../../../stateless.js/src';
1813

1914
const TEST_TOKEN_DECIMALS = 2;
2015

@@ -55,15 +50,15 @@ describe('rpc-interop token', () => {
5550
});
5651

5752
it('getCompressedTokenAccountsByOwner should match', async () => {
58-
const senderAccounts = await rpc.getCompressedTokenAccountsByOwner(
59-
bob.publicKey,
60-
{ mint },
61-
);
53+
const senderAccounts = (
54+
await rpc.getCompressedTokenAccountsByOwner(bob.publicKey, { mint })
55+
).items;
6256

63-
const senderAccountsTest =
57+
const senderAccountsTest = (
6458
await testRpc.getCompressedTokenAccountsByOwner(bob.publicKey, {
6559
mint,
66-
});
60+
})
61+
).items;
6762

6863
assert.equal(senderAccounts.length, senderAccountsTest.length);
6964

@@ -79,15 +74,17 @@ describe('rpc-interop token', () => {
7974
);
8075
});
8176

82-
const receiverAccounts = await rpc.getCompressedTokenAccountsByOwner(
83-
charlie.publicKey,
84-
{ mint },
85-
);
77+
const receiverAccounts = (
78+
await rpc.getCompressedTokenAccountsByOwner(charlie.publicKey, {
79+
mint,
80+
})
81+
).items;
8682

87-
const receiverAccountsTest =
83+
const receiverAccountsTest = (
8884
await testRpc.getCompressedTokenAccountsByOwner(charlie.publicKey, {
8985
mint,
90-
});
86+
})
87+
).items;
9188

9289
assert.equal(receiverAccounts.length, receiverAccountsTest.length);
9390
receiverAccounts.forEach((account, index) => {
@@ -110,40 +107,42 @@ describe('rpc-interop token', () => {
110107
);
111108

112109
const balance = await rpc.getCompressedTokenAccountBalance(
113-
bn(senderAccounts[0].compressedAccount.hash),
110+
bn(senderAccounts.items[0].compressedAccount.hash),
114111
);
115112
const balanceTest = await testRpc.getCompressedTokenAccountBalance(
116-
bn(senderAccounts[0].compressedAccount.hash),
113+
bn(senderAccounts.items[0].compressedAccount.hash),
117114
);
118115
assert.isTrue(balance.amount.eq(balanceTest.amount));
119116
assert.isNotNull(balance.amount);
120117
assert.isNotNull(balanceTest.amount);
121118
});
122119

123120
it('getCompressedTokenBalancesByOwner should match', async () => {
124-
const balances = await rpc.getCompressedTokenBalancesByOwner(
125-
bob.publicKey,
126-
{ mint },
127-
);
128-
const balancesTest = await testRpc.getCompressedTokenBalancesByOwner(
129-
bob.publicKey,
130-
{ mint },
131-
);
121+
const balances = (
122+
await rpc.getCompressedTokenBalancesByOwner(bob.publicKey, { mint })
123+
).items;
124+
const balancesTest = (
125+
await testRpc.getCompressedTokenBalancesByOwner(bob.publicKey, {
126+
mint,
127+
})
128+
).items;
132129

133130
assert.equal(balances.length, balancesTest.length);
134131

135132
balances.forEach((balance, index) => {
136133
assert.isTrue(balance.balance.eq(balancesTest[index].balance));
137134
});
138135

139-
const balancesReceiver = await rpc.getCompressedTokenBalancesByOwner(
140-
charlie.publicKey,
141-
{ mint },
142-
);
143-
const balancesReceiverTest =
136+
const balancesReceiver = (
137+
await rpc.getCompressedTokenBalancesByOwner(charlie.publicKey, {
138+
mint,
139+
})
140+
).items;
141+
const balancesReceiverTest = (
144142
await testRpc.getCompressedTokenBalancesByOwner(charlie.publicKey, {
145143
mint,
146-
});
144+
})
145+
).items;
147146

148147
assert.equal(balancesReceiver.length, balancesReceiverTest.length);
149148
balancesReceiver.forEach((balance, index) => {
@@ -154,25 +153,44 @@ describe('rpc-interop token', () => {
154153
});
155154

156155
it('[test-rpc missing] getSignaturesForTokenOwner should match', async () => {
157-
const signatures = await rpc.getCompressionSignaturesForTokenOwner(
158-
bob.publicKey,
159-
);
156+
const signatures = (
157+
await rpc.getCompressionSignaturesForTokenOwner(bob.publicKey)
158+
).items;
160159

161160
assert.equal(signatures.length, 2);
162161

163-
const signaturesReceiver =
164-
await rpc.getCompressionSignaturesForTokenOwner(charlie.publicKey);
165-
162+
const signaturesReceiver = (
163+
await rpc.getCompressionSignaturesForTokenOwner(charlie.publicKey)
164+
).items;
166165
assert.equal(signaturesReceiver.length, 1);
167166
});
168167

168+
it('[test-rpc missing] getTransactionWithCompressionInfo should return correct token pre and post balances', async () => {
169+
const signatures = (
170+
await rpc.getCompressionSignaturesForTokenOwner(bob.publicKey)
171+
).items;
172+
173+
const tx = await rpc.getTransactionWithCompressionInfo(
174+
// most recent
175+
signatures[0].signature,
176+
);
177+
assert.isTrue(
178+
tx!.compressionInfo.preTokenBalances![0].amount.eq(bn(1000)),
179+
);
180+
assert.isTrue(
181+
tx!.compressionInfo.postTokenBalances![0].amount.eq(bn(300)),
182+
);
183+
assert.isTrue(tx!.compressionInfo.postTokenBalances!.length === 2);
184+
assert.isTrue(tx!.compressionInfo.preTokenBalances!.length === 1);
185+
});
186+
169187
it('[delegate unused] getCompressedTokenAccountsByDelegate should match', async () => {
170188
const accs = await rpc.getCompressedTokenAccountsByDelegate(
171189
bob.publicKey,
172190
{ mint },
173191
);
174192

175-
assert.equal(accs.length, 0);
193+
assert.equal(accs.items.length, 0);
176194
});
177195

178196
it('[rpc] getCompressedTokenAccountsByOwner with 2 mints should return both mints', async () => {
@@ -194,12 +212,12 @@ describe('rpc-interop token', () => {
194212

195213
// check that mint and mint2 exist in list of senderaccounts at least once
196214
assert.isTrue(
197-
senderAccounts.some(
215+
senderAccounts.items.some(
198216
account => account.parsed.mint.toBase58() === mint.toBase58(),
199217
),
200218
);
201219
assert.isTrue(
202-
senderAccounts.some(
220+
senderAccounts.items.some(
203221
account => account.parsed.mint.toBase58() === mint2.toBase58(),
204222
),
205223
);

js/compressed-token/tests/e2e/transfer.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ async function assertTransfer(
2828
expectedAccountCountRecipientPost?: number,
2929
) {
3030
/// Transfer can merge input compressed accounts therefore we need to pass all as ref
31-
const senderPostCompressedTokenAccounts =
31+
const senderPostCompressedTokenAccounts = (
3232
await rpc.getCompressedTokenAccountsByOwner(refSender, {
3333
mint: refMint,
34-
});
34+
})
35+
).items;
3536
/// pre = post-amount
3637
const sumPre = senderPreCompressedTokenAccounts.reduce(
3738
(acc, curr) => bn(acc).add(curr.parsed.amount),
@@ -50,10 +51,11 @@ async function assertTransfer(
5051

5152
expect(sumPre.sub(refAmount).eq(sumPost)).toBe(true);
5253

53-
const recipientCompressedTokenAccounts =
54+
const recipientCompressedTokenAccounts = (
5455
await rpc.getCompressedTokenAccountsByOwner(refRecipient, {
5556
mint: refMint,
56-
});
57+
})
58+
).items;
5759

5860
if (expectedAccountCountRecipientPost) {
5961
expect(recipientCompressedTokenAccounts.length).toBe(
@@ -108,10 +110,11 @@ describe('transfer', () => {
108110
it('should transfer from bob -> charlie', async () => {
109111
/// send 700 from bob -> charlie
110112
/// bob: 300, charlie: 700
111-
const bobPreCompressedTokenAccounts =
113+
const bobPreCompressedTokenAccounts = (
112114
await rpc.getCompressedTokenAccountsByOwner(bob.publicKey, {
113115
mint,
114-
});
116+
})
117+
).items;
115118

116119
await transfer(
117120
rpc,
@@ -152,7 +155,7 @@ describe('transfer', () => {
152155

153156
await assertTransfer(
154157
rpc,
155-
bobPreCompressedTokenAccounts2,
158+
bobPreCompressedTokenAccounts2.items,
156159
mint,
157160
bn(200),
158161
bob.publicKey,
@@ -180,7 +183,7 @@ describe('transfer', () => {
180183

181184
await assertTransfer(
182185
rpc,
183-
charliePreCompressedTokenAccounts3,
186+
charliePreCompressedTokenAccounts3.items,
184187
mint,
185188
bn(5),
186189
charlie.publicKey,
@@ -199,7 +202,7 @@ describe('transfer', () => {
199202

200203
await assertTransfer(
201204
rpc,
202-
charliePreCompressedTokenAccounts4,
205+
charliePreCompressedTokenAccounts4.items,
203206
mint,
204207
bn(700),
205208
charlie.publicKey,

0 commit comments

Comments
 (0)