@marinade.finance/anchor-common
Advanced tools
Comparing version 2.1.4 to 2.2.0
{ | ||
"name": "@marinade.finance/anchor-common", | ||
"version": "2.1.4", | ||
"version": "2.2.0", | ||
"description": "Solana Anchor JS utilities", | ||
@@ -29,3 +29,3 @@ "repository": { | ||
"@coral-xyz/anchor": "^0.29.0", | ||
"@marinade.finance/ts-common": "^2.1.4" | ||
"@marinade.finance/ts-common": "^2.2.0" | ||
}, | ||
@@ -36,3 +36,3 @@ "peerDependencies": { | ||
"@coral-xyz/anchor": "^0.29.0", | ||
"@marinade.finance/ts-common": "^2.1.4" | ||
"@marinade.finance/ts-common": "^2.2.0" | ||
}, | ||
@@ -39,0 +39,0 @@ "scripts": { |
import * as anchor from '@coral-xyz/anchor'; | ||
import { SuccessfulTxSimulationResponse } from '@coral-xyz/anchor/dist/cjs/utils/rpc'; | ||
import { Provider } from '@coral-xyz/anchor'; | ||
import { Commitment, ConfirmOptions, Connection, PublicKey, SendOptions, Signer, Transaction, VersionedTransaction } from '@solana/web3.js'; | ||
import { LoggerPlaceholder } from '@marinade.finance/ts-common'; | ||
export declare function verifyError(e: any, idl: anchor.Idl, errCode: number, logger?: LoggerPlaceholder | undefined): void; | ||
export declare function transaction(provider: Provider): Promise<Transaction>; | ||
export declare class NullAnchorProvider implements Provider { | ||
readonly connection: Connection; | ||
publicKey?: PublicKey | undefined; | ||
constructor(connection: Connection); | ||
send?(tx: Transaction | VersionedTransaction, signers?: Signer[] | undefined, opts?: SendOptions | undefined): Promise<string>; | ||
sendAndConfirm?(tx: Transaction | VersionedTransaction, signers?: Signer[] | undefined, opts?: ConfirmOptions | undefined): Promise<string>; | ||
sendAll?<T extends Transaction | VersionedTransaction>(txWithSigners: { | ||
tx: T; | ||
signers?: Signer[] | undefined; | ||
}[], opts?: ConfirmOptions | undefined): Promise<string[]>; | ||
simulate?(tx: Transaction | VersionedTransaction, signers?: Signer[] | undefined, commitment?: Commitment | undefined, includeAccounts?: boolean | PublicKey[] | undefined): Promise<SuccessfulTxSimulationResponse>; | ||
} | ||
export declare function verifyError(e: any, idl: anchor.Idl | Map<number, string>, errCode: number, errMessage?: string, logger?: LoggerPlaceholder | undefined): void; | ||
type ToString = { | ||
toString(): string; | ||
}; | ||
export declare function checkErrorMessage(e: unknown, message: ToString): boolean; | ||
export {}; |
@@ -26,3 +26,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.NullAnchorProvider = exports.transaction = exports.verifyError = void 0; | ||
exports.checkErrorMessage = exports.verifyError = void 0; | ||
const anchor = __importStar(require("@coral-xyz/anchor")); | ||
@@ -33,14 +33,33 @@ const web3_js_1 = require("@solana/web3.js"); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
e, idl, errCode, logger = undefined) { | ||
const errorsMap = anchor.parseIdlErrors(idl); | ||
const errMsg = errorsMap.get(errCode); | ||
if (errMsg === undefined) { | ||
e, idl, errCode, errMessage, logger = undefined) { | ||
let anchorErrorMap; | ||
if (idl instanceof Map) { | ||
anchorErrorMap = idl; | ||
} | ||
else { | ||
anchorErrorMap = anchor.parseIdlErrors(idl); | ||
} | ||
const anchorErrorMsg = anchorErrorMap.get(errCode); | ||
if (anchorErrorMsg === undefined) { | ||
throw new Error(`Error ${errCode} not found in IDL`); | ||
} | ||
if (errMessage !== undefined && !anchorErrorMsg.includes(errMessage)) { | ||
throw new Error(`Error ${errCode} with message ${anchorErrorMsg} ` + | ||
`does not match expected errMessage ${errMessage}`); | ||
} | ||
let decNum; | ||
if (errCode.toString().startsWith('0x')) { | ||
decNum = parseInt(errCode.toString(), 16); | ||
} | ||
else { | ||
decNum = parseInt(errCode.toString()); | ||
} | ||
const hexNumber = '0x' + decNum.toString(16); | ||
const decimalNumber = decNum.toString(); | ||
if (e instanceof anchor.ProgramError) { | ||
expect(e.msg).toStrictEqual(errMsg); | ||
expect(e.msg).toStrictEqual(anchorErrorMsg); | ||
expect(e.code).toStrictEqual(errCode); | ||
} | ||
else if (e instanceof anchor.AnchorError) { | ||
expect(e.error.errorMessage).toStrictEqual(errMsg); | ||
expect(e.error.errorMessage).toStrictEqual(anchorErrorMsg); | ||
expect(e.error.errorCode.number).toStrictEqual(errCode); | ||
@@ -50,9 +69,12 @@ } | ||
expect(e.logs).toBeDefined(); | ||
expect(e.logs.find(l => l.indexOf(errMsg) > -1)).toBeDefined(); | ||
expect(e.logs.find(l => l.indexOf(anchorErrorMsg) > -1)).toBeDefined(); | ||
} | ||
else if (e instanceof Error) { | ||
expect(e.message).toContain(errCode.toString()); | ||
else if (e && 'cause' in e && e.cause) { | ||
if (!checkErrorMessage(e, errCode)) { | ||
verifyError(e.cause, idl, errCode, errMessage, logger); | ||
} | ||
} | ||
else { | ||
(0, ts_common_1.logError)(logger, e); | ||
else if (!checkErrorMessage(e, decimalNumber) && | ||
!checkErrorMessage(e, hexNumber)) { | ||
(0, ts_common_1.logError)(logger, `Error does not include error number '${errCode}: [${e}]`); | ||
throw e; | ||
@@ -62,30 +84,10 @@ } | ||
exports.verifyError = verifyError; | ||
async function transaction(provider) { | ||
const bh = await provider.connection.getLatestBlockhash(); | ||
return new web3_js_1.Transaction({ | ||
feePayer: provider.publicKey, | ||
blockhash: bh.blockhash, | ||
lastValidBlockHeight: bh.lastValidBlockHeight, | ||
}); | ||
function checkErrorMessage(e, message) { | ||
return (typeof e === 'object' && | ||
e !== null && | ||
'message' in e && | ||
typeof e.message === 'string' && | ||
e.message.includes(message.toString())); | ||
} | ||
exports.transaction = transaction; | ||
class NullAnchorProvider { | ||
constructor(connection) { | ||
this.connection = connection; | ||
this.connection = connection; | ||
} | ||
send(tx, signers, opts) { | ||
throw new Error('Method not implemented.'); | ||
} | ||
sendAndConfirm(tx, signers, opts) { | ||
throw new Error('Method not implemented.'); | ||
} | ||
sendAll(txWithSigners, opts) { | ||
throw new Error('Method not implemented.'); | ||
} | ||
simulate(tx, signers, commitment, includeAccounts) { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} | ||
exports.NullAnchorProvider = NullAnchorProvider; | ||
exports.checkErrorMessage = checkErrorMessage; | ||
//# sourceMappingURL=anchor.js.map |
Sorry, the diff of this file is not supported yet
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
8
1
6
8443
114