Skip to content

Commit a7e75d0

Browse files
authored
Merge pull request #80 from solana-developers/fixes
fix: nodewallet and types
2 parents 03102cd + 6b8bc16 commit a7e75d0

File tree

5 files changed

+33
-60
lines changed

5 files changed

+33
-60
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.8.1
2+
3+
- Fix issue with CJS import of Anchor's `NodeWallet` to use the correct `Wallet` class exported
4+
- Fixed type errors on `ArrayBuffer`s and `Uint8Array`s
5+
16
## 2.8
27

38
- Added `sendVersionedTransaction()` to send a versioned transaction with lookup tables. Also adds priority fee support.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solana-developers/helpers",
3-
"version": "2.8.0",
3+
"version": "2.8.1",
44
"description": "Solana helper functions",
55
"type": "module",
66
"main": "./dist/esm/index.js",

src/lib/idl.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import {
1313
EventParser,
1414
BorshAccountsCoder,
1515
BorshInstructionCoder,
16+
Wallet,
1617
} from "@coral-xyz/anchor";
1718
import BN from "bn.js";
18-
import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet";
1919
import { formatIdl } from "./convertLegacyIdl";
2020

2121
/**
@@ -60,8 +60,7 @@ export async function getIdlByProgramId<Idl>(
6060
provider: AnchorProvider,
6161
): Promise<Idl> {
6262
var idl = await Program.fetchIdl(programId, provider);
63-
if (!idl)
64-
throw new Error(`IDL not found for program ${programId.toString()}`);
63+
if (!idl) throw new Error(`IDL not found for program ${programId.toString()}`);
6564

6665
return idl as Idl;
6766
}
@@ -86,7 +85,7 @@ export function createProviderForConnection(
8685
commitment: "confirmed",
8786
},
8887
): AnchorProvider {
89-
return new AnchorProvider(connection, new NodeWallet(keypair), options);
88+
return new AnchorProvider(connection, new Wallet(keypair), options);
9089
}
9190

9291
/**
@@ -258,17 +257,15 @@ export async function decodeAnchorTransaction(
258257
const decoded = instructionCoder.decode(Buffer.from(ix.data));
259258
if (decoded) {
260259
const ixType = idl.instructions.find((i) => i.name === decoded.name);
261-
const accountIndices =
262-
"accounts" in ix ? ix.accounts : ix.accountKeyIndexes;
260+
const accountIndices = "accounts" in ix ? ix.accounts : ix.accountKeyIndexes;
263261

264262
// Get all accounts involved in this instruction
265263
const accounts: InvolvedAccount[] = await Promise.all(
266264
accountIndices.map(async (index, i) => {
267265
const pubkey = accountKeys.get(index);
268266
if (!pubkey) return null;
269267
const accountMeta = ixType?.accounts[i];
270-
const accountInfo =
271-
await provider.connection.getAccountInfo(pubkey);
268+
const accountInfo = await provider.connection.getAccountInfo(pubkey);
272269

273270
let accountData;
274271
if (accountInfo?.owner.equals(program.programId)) {
@@ -277,9 +274,7 @@ export async function decodeAnchorTransaction(
277274
accountInfo.data
278275
.slice(0, 8)
279276
.equals(
280-
accountsCoder.accountDiscriminator(
281-
acc.name.toLowerCase(),
282-
),
277+
accountsCoder.accountDiscriminator(acc.name.toLowerCase()) as Uint8Array,
283278
),
284279
);
285280
if (accountType) {
@@ -312,19 +307,15 @@ export async function decodeAnchorTransaction(
312307
accounts,
313308
toString: function () {
314309
let output = `\nInstruction: ${this.name}\n`;
315-
output += `├─ Arguments: ${JSON.stringify(
316-
formatData(this.data),
317-
)}\n`;
310+
output += `├─ Arguments: ${JSON.stringify(formatData(this.data))}\n`;
318311
output += `└─ Accounts:\n`;
319312
this.accounts.forEach((acc) => {
320313
output += ` ├─ ${acc.name}:\n`;
321314
output += ` │ ├─ Address: ${acc.pubkey}\n`;
322315
output += ` │ ├─ Signer: ${acc.isSigner}\n`;
323316
output += ` │ ├─ Writable: ${acc.isWritable}\n`;
324317
if (acc.data) {
325-
output += ` │ └─ Data: ${JSON.stringify(
326-
formatData(acc.data),
327-
)}\n`;
318+
output += ` │ └─ Data: ${JSON.stringify(formatData(acc.data))}\n`;
328319
}
329320
});
330321
return output;
@@ -358,9 +349,7 @@ function formatData(data: any): any {
358349
return data.map(formatData);
359350
}
360351
if (typeof data === "object" && data !== null) {
361-
return Object.fromEntries(
362-
Object.entries(data).map(([k, v]) => [k, formatData(v)]),
363-
);
352+
return Object.fromEntries(Object.entries(data).map(([k, v]) => [k, formatData(v)]));
364353
}
365354
return data;
366355
}

src/lib/transaction.ts

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ export const getSimulationComputeUnits = async (
8484
const simulationInstructions = [...instructions];
8585

8686
// Replace or add compute limit instruction
87-
const computeLimitIndex = simulationInstructions.findIndex(
88-
isSetComputeLimitInstruction,
89-
);
87+
const computeLimitIndex = simulationInstructions.findIndex(isSetComputeLimitInstruction);
9088
const simulationLimitIx = ComputeBudgetProgram.setComputeUnitLimit({
9189
units: 1_400_000,
9290
});
@@ -116,8 +114,7 @@ export const getSimulationComputeUnits = async (
116114
if (rpcResponse?.value?.err) {
117115
const logs = rpcResponse.value.logs?.join("\n • ") || "No logs available";
118116
throw new Error(
119-
`Transaction simulation failed:\n •${logs}` +
120-
JSON.stringify(rpcResponse?.value?.err),
117+
`Transaction simulation failed:\n •${logs}` + JSON.stringify(rpcResponse?.value?.err),
121118
);
122119
}
123120

@@ -173,9 +170,7 @@ export type ComputeUnitBuffer = {
173170
/**
174171
* Default configuration values for transaction sending
175172
*/
176-
export const DEFAULT_SEND_OPTIONS: Required<
177-
Omit<SendTransactionOptions, "onStatusUpdate">
178-
> = {
173+
export const DEFAULT_SEND_OPTIONS: Required<Omit<SendTransactionOptions, "onStatusUpdate">> = {
179174
maxRetries: MAX_RETRIES,
180175
initialDelayMs: RETRY_INTERVAL_MS,
181176
commitment: "confirmed",
@@ -239,7 +234,7 @@ export async function sendTransaction(
239234
// Skip compute preparation if transaction is already signed or has compute instructions
240235
if (transaction.signatures.length > 0) {
241236
console.log("Transaction already signed, skipping compute preparation");
242-
return sendRawTransactionWithRetry(connection, transaction.serialize(), {
237+
return sendRawTransactionWithRetry(connection, transaction.serialize() as Uint8Array, {
243238
commitment,
244239
...sendOptions,
245240
});
@@ -256,7 +251,7 @@ export async function sendTransaction(
256251
);
257252

258253
transaction.sign(...signers);
259-
return sendRawTransactionWithRetry(connection, transaction.serialize(), {
254+
return sendRawTransactionWithRetry(connection, transaction.serialize() as Uint8Array, {
260255
commitment,
261256
...sendOptions,
262257
});
@@ -318,8 +313,7 @@ export async function sendVersionedTransaction(
318313
...sendOptions
319314
} = options;
320315

321-
const hasComputeLimitInstructions =
322-
hasSetComputeLimitInstruction(instructions);
316+
const hasComputeLimitInstructions = hasSetComputeLimitInstruction(instructions);
323317

324318
if (!hasComputeLimitInstructions) {
325319
const computeUnitBuffer = userComputeBuffer ?? { multiplier: 1.1 };
@@ -336,20 +330,15 @@ export async function sendVersionedTransaction(
336330

337331
const messageV0 = new TransactionMessage({
338332
payerKey: signers[0].publicKey,
339-
recentBlockhash: (await connection.getLatestBlockhash(commitment))
340-
.blockhash,
333+
recentBlockhash: (await connection.getLatestBlockhash(commitment)).blockhash,
341334
instructions,
342335
}).compileToV0Message(lookupTables);
343336

344337
const transaction = new VersionedTransaction(messageV0);
345338

346339
transaction.sign(signers);
347340

348-
return await sendRawTransactionWithRetry(
349-
connection,
350-
transaction.serialize(),
351-
sendOptions,
352-
);
341+
return await sendRawTransactionWithRetry(connection, transaction.serialize(), sendOptions);
353342
}
354343

355344
/**
@@ -399,9 +388,7 @@ export async function addComputeInstructions(
399388
// Apply buffer to compute units
400389
let finalComputeUnits = simulatedCompute;
401390
if (computeUnitBuffer.multiplier) {
402-
finalComputeUnits = Math.floor(
403-
finalComputeUnits * computeUnitBuffer.multiplier,
404-
);
391+
finalComputeUnits = Math.floor(finalComputeUnits * computeUnitBuffer.multiplier);
405392
}
406393
if (computeUnitBuffer.fixed) {
407394
finalComputeUnits += computeUnitBuffer.fixed;
@@ -541,12 +528,11 @@ export async function createLookupTable(
541528
): Promise<[PublicKey, AddressLookupTableAccount]> {
542529
const slot = await connection.getSlot();
543530

544-
const [lookupTableInst, lookupTableAddress] =
545-
AddressLookupTableProgram.createLookupTable({
546-
authority: sender.publicKey,
547-
payer: sender.publicKey,
548-
recentSlot: slot,
549-
});
531+
const [lookupTableInst, lookupTableAddress] = AddressLookupTableProgram.createLookupTable({
532+
authority: sender.publicKey,
533+
payer: sender.publicKey,
534+
recentSlot: slot,
535+
});
550536

551537
const extendInstruction = AddressLookupTableProgram.extendLookupTable({
552538
payer: sender.publicKey,
@@ -565,16 +551,9 @@ export async function createLookupTable(
565551
);
566552

567553
// Need to wait until the lookup table is active
568-
await confirmTransaction(
569-
connection,
570-
lookupTableInstructionsSignature,
571-
"finalized",
572-
);
554+
await confirmTransaction(connection, lookupTableInstructionsSignature, "finalized");
573555

574-
console.log(
575-
"Lookup table instructions signature",
576-
lookupTableInstructionsSignature,
577-
);
556+
console.log("Lookup table instructions signature", lookupTableInstructionsSignature);
578557

579558
const lookupTableAccount = (
580559
await connection.getAddressLookupTable(lookupTableAddress, {

0 commit comments

Comments
 (0)