333 lines
12 KiB
JavaScript
333 lines
12 KiB
JavaScript
import { pipe } from '@solana/functional';
|
|
import { getSolanaErrorFromJsonRpcError } from '@solana/errors';
|
|
|
|
// src/request-transformer.ts
|
|
|
|
// src/tree-traversal.ts
|
|
var KEYPATH_WILDCARD = {};
|
|
function getTreeWalker(visitors) {
|
|
return function traverse(node, state) {
|
|
if (Array.isArray(node)) {
|
|
return node.map((element, ii) => {
|
|
const nextState = {
|
|
...state,
|
|
keyPath: [...state.keyPath, ii]
|
|
};
|
|
return traverse(element, nextState);
|
|
});
|
|
} else if (typeof node === "object" && node !== null) {
|
|
const out = {};
|
|
for (const propName in node) {
|
|
if (!Object.prototype.hasOwnProperty.call(node, propName)) {
|
|
continue;
|
|
}
|
|
const nextState = {
|
|
...state,
|
|
keyPath: [...state.keyPath, propName]
|
|
};
|
|
out[propName] = traverse(node[propName], nextState);
|
|
}
|
|
return out;
|
|
} else {
|
|
return visitors.reduce((acc, visitNode) => visitNode(acc, state), node);
|
|
}
|
|
};
|
|
}
|
|
function getTreeWalkerRequestTransformer(visitors, initialState) {
|
|
return (request) => {
|
|
const traverse = getTreeWalker(visitors);
|
|
return Object.freeze({
|
|
...request,
|
|
params: traverse(request.params, initialState)
|
|
});
|
|
};
|
|
}
|
|
function getTreeWalkerResponseTransformer(visitors, initialState) {
|
|
return (json) => getTreeWalker(visitors)(json, initialState);
|
|
}
|
|
|
|
// src/request-transformer-bigint-downcast.ts
|
|
function getBigIntDowncastRequestTransformer() {
|
|
return getTreeWalkerRequestTransformer([downcastNodeToNumberIfBigint], { keyPath: [] });
|
|
}
|
|
function downcastNodeToNumberIfBigint(value) {
|
|
return typeof value === "bigint" ? (
|
|
// FIXME(solana-labs/solana/issues/30341) Create a data type to represent u64 in the Solana
|
|
// JSON RPC implementation so that we can throw away this entire patcher instead of unsafely
|
|
// downcasting `bigints` to `numbers`.
|
|
Number(value)
|
|
) : value;
|
|
}
|
|
|
|
// src/request-transformer-default-commitment.ts
|
|
function getDefaultCommitmentRequestTransformer({
|
|
defaultCommitment,
|
|
optionsObjectPositionByMethod
|
|
}) {
|
|
return (request) => {
|
|
const { params, methodName } = request;
|
|
if (!Array.isArray(params)) {
|
|
return request;
|
|
}
|
|
const optionsObjectPositionInParams = optionsObjectPositionByMethod[methodName];
|
|
if (optionsObjectPositionInParams == null) {
|
|
return request;
|
|
}
|
|
return Object.freeze({
|
|
methodName,
|
|
params: applyDefaultCommitment({
|
|
commitmentPropertyName: methodName === "sendTransaction" ? "preflightCommitment" : "commitment",
|
|
optionsObjectPositionInParams,
|
|
overrideCommitment: defaultCommitment,
|
|
params
|
|
})
|
|
});
|
|
};
|
|
}
|
|
function applyDefaultCommitment({
|
|
commitmentPropertyName,
|
|
params,
|
|
optionsObjectPositionInParams,
|
|
overrideCommitment
|
|
}) {
|
|
const paramInTargetPosition = params[optionsObjectPositionInParams];
|
|
if (
|
|
// There's no config.
|
|
paramInTargetPosition === void 0 || // There is a config object.
|
|
paramInTargetPosition && typeof paramInTargetPosition === "object" && !Array.isArray(paramInTargetPosition)
|
|
) {
|
|
if (
|
|
// The config object already has a commitment set.
|
|
paramInTargetPosition && commitmentPropertyName in paramInTargetPosition
|
|
) {
|
|
if (!paramInTargetPosition[commitmentPropertyName] || paramInTargetPosition[commitmentPropertyName] === "finalized") {
|
|
const nextParams = [...params];
|
|
const {
|
|
[commitmentPropertyName]: _,
|
|
// eslint-disable-line @typescript-eslint/no-unused-vars
|
|
...rest
|
|
} = paramInTargetPosition;
|
|
if (Object.keys(rest).length > 0) {
|
|
nextParams[optionsObjectPositionInParams] = rest;
|
|
} else {
|
|
if (optionsObjectPositionInParams === nextParams.length - 1) {
|
|
nextParams.length--;
|
|
} else {
|
|
nextParams[optionsObjectPositionInParams] = void 0;
|
|
}
|
|
}
|
|
return nextParams;
|
|
}
|
|
} else if (overrideCommitment !== "finalized") {
|
|
const nextParams = [...params];
|
|
nextParams[optionsObjectPositionInParams] = {
|
|
...paramInTargetPosition,
|
|
[commitmentPropertyName]: overrideCommitment
|
|
};
|
|
return nextParams;
|
|
}
|
|
}
|
|
return params;
|
|
}
|
|
|
|
// src/request-transformer-integer-overflow.ts
|
|
function getIntegerOverflowRequestTransformer(onIntegerOverflow) {
|
|
return (request) => {
|
|
const transformer = getTreeWalkerRequestTransformer(
|
|
[getIntegerOverflowNodeVisitor((...args) => onIntegerOverflow(request, ...args))],
|
|
{ keyPath: [] }
|
|
);
|
|
return transformer(request);
|
|
};
|
|
}
|
|
function getIntegerOverflowNodeVisitor(onIntegerOverflow) {
|
|
return (value, { keyPath }) => {
|
|
if (typeof value === "bigint") {
|
|
if (onIntegerOverflow && (value > Number.MAX_SAFE_INTEGER || value < -Number.MAX_SAFE_INTEGER)) {
|
|
onIntegerOverflow(keyPath, value);
|
|
}
|
|
}
|
|
return value;
|
|
};
|
|
}
|
|
|
|
// src/request-transformer-options-object-position-config.ts
|
|
var OPTIONS_OBJECT_POSITION_BY_METHOD = {
|
|
accountNotifications: 1,
|
|
blockNotifications: 1,
|
|
getAccountInfo: 1,
|
|
getBalance: 1,
|
|
getBlock: 1,
|
|
getBlockHeight: 0,
|
|
getBlockProduction: 0,
|
|
getBlocks: 2,
|
|
getBlocksWithLimit: 2,
|
|
getEpochInfo: 0,
|
|
getFeeForMessage: 1,
|
|
getInflationGovernor: 0,
|
|
getInflationReward: 1,
|
|
getLargestAccounts: 0,
|
|
getLatestBlockhash: 0,
|
|
getLeaderSchedule: 1,
|
|
getMinimumBalanceForRentExemption: 1,
|
|
getMultipleAccounts: 1,
|
|
getProgramAccounts: 1,
|
|
getSignaturesForAddress: 1,
|
|
getSlot: 0,
|
|
getSlotLeader: 0,
|
|
getStakeMinimumDelegation: 0,
|
|
getSupply: 0,
|
|
getTokenAccountBalance: 1,
|
|
getTokenAccountsByDelegate: 2,
|
|
getTokenAccountsByOwner: 2,
|
|
getTokenLargestAccounts: 1,
|
|
getTokenSupply: 1,
|
|
getTransaction: 1,
|
|
getTransactionCount: 0,
|
|
getVoteAccounts: 0,
|
|
isBlockhashValid: 1,
|
|
logsNotifications: 1,
|
|
programNotifications: 1,
|
|
requestAirdrop: 2,
|
|
sendTransaction: 1,
|
|
signatureNotifications: 1,
|
|
simulateTransaction: 1
|
|
};
|
|
|
|
// src/request-transformer.ts
|
|
function getDefaultRequestTransformerForSolanaRpc(config) {
|
|
const handleIntegerOverflow = config?.onIntegerOverflow;
|
|
return (request) => {
|
|
return pipe(
|
|
request,
|
|
handleIntegerOverflow ? getIntegerOverflowRequestTransformer(handleIntegerOverflow) : (r) => r,
|
|
getBigIntDowncastRequestTransformer(),
|
|
getDefaultCommitmentRequestTransformer({
|
|
defaultCommitment: config?.defaultCommitment,
|
|
optionsObjectPositionByMethod: OPTIONS_OBJECT_POSITION_BY_METHOD
|
|
})
|
|
);
|
|
};
|
|
}
|
|
|
|
// src/response-transformer-bigint-upcast.ts
|
|
function getBigIntUpcastResponseTransformer(allowedNumericKeyPaths) {
|
|
return getTreeWalkerResponseTransformer([getBigIntUpcastVisitor(allowedNumericKeyPaths)], { keyPath: [] });
|
|
}
|
|
function getBigIntUpcastVisitor(allowedNumericKeyPaths) {
|
|
return function upcastNodeToBigIntIfNumber(value, { keyPath }) {
|
|
const isInteger = typeof value === "number" && Number.isInteger(value) || typeof value === "bigint";
|
|
if (!isInteger) return value;
|
|
if (keyPathIsAllowedToBeNumeric(keyPath, allowedNumericKeyPaths)) {
|
|
return Number(value);
|
|
} else {
|
|
return BigInt(value);
|
|
}
|
|
};
|
|
}
|
|
function keyPathIsAllowedToBeNumeric(keyPath, allowedNumericKeyPaths) {
|
|
return allowedNumericKeyPaths.some((prohibitedKeyPath) => {
|
|
if (prohibitedKeyPath.length !== keyPath.length) {
|
|
return false;
|
|
}
|
|
for (let ii = keyPath.length - 1; ii >= 0; ii--) {
|
|
const keyPathPart = keyPath[ii];
|
|
const prohibitedKeyPathPart = prohibitedKeyPath[ii];
|
|
if (prohibitedKeyPathPart !== keyPathPart && (prohibitedKeyPathPart !== KEYPATH_WILDCARD || typeof keyPathPart !== "number")) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
|
|
// src/response-transformer-result.ts
|
|
function getResultResponseTransformer() {
|
|
return (json) => json.result;
|
|
}
|
|
function getThrowSolanaErrorResponseTransformer() {
|
|
return (json) => {
|
|
const jsonRpcResponse = json;
|
|
if ("error" in jsonRpcResponse) {
|
|
throw getSolanaErrorFromJsonRpcError(jsonRpcResponse.error);
|
|
}
|
|
return jsonRpcResponse;
|
|
};
|
|
}
|
|
|
|
// src/response-transformer.ts
|
|
function getDefaultResponseTransformerForSolanaRpc(config) {
|
|
return (response, request) => {
|
|
const methodName = request.methodName;
|
|
const keyPaths = config?.allowedNumericKeyPaths && methodName ? config.allowedNumericKeyPaths[methodName] : void 0;
|
|
return pipe(
|
|
response,
|
|
(r) => getThrowSolanaErrorResponseTransformer()(r, request),
|
|
(r) => getResultResponseTransformer()(r, request),
|
|
(r) => getBigIntUpcastResponseTransformer(keyPaths ?? [])(r, request)
|
|
);
|
|
};
|
|
}
|
|
function getDefaultResponseTransformerForSolanaRpcSubscriptions(config) {
|
|
return (response, request) => {
|
|
const methodName = request.methodName;
|
|
const keyPaths = config?.allowedNumericKeyPaths && methodName ? config.allowedNumericKeyPaths[methodName] : void 0;
|
|
return pipe(response, (r) => getBigIntUpcastResponseTransformer(keyPaths ?? [])(r, request));
|
|
};
|
|
}
|
|
|
|
// src/response-transformer-allowed-numeric-values.ts
|
|
var jsonParsedTokenAccountsConfigs = [
|
|
// parsed Token/Token22 token account
|
|
["data", "parsed", "info", "tokenAmount", "decimals"],
|
|
["data", "parsed", "info", "tokenAmount", "uiAmount"],
|
|
["data", "parsed", "info", "rentExemptReserve", "decimals"],
|
|
["data", "parsed", "info", "rentExemptReserve", "uiAmount"],
|
|
["data", "parsed", "info", "delegatedAmount", "decimals"],
|
|
["data", "parsed", "info", "delegatedAmount", "uiAmount"],
|
|
["data", "parsed", "info", "extensions", KEYPATH_WILDCARD, "state", "olderTransferFee", "transferFeeBasisPoints"],
|
|
["data", "parsed", "info", "extensions", KEYPATH_WILDCARD, "state", "newerTransferFee", "transferFeeBasisPoints"],
|
|
["data", "parsed", "info", "extensions", KEYPATH_WILDCARD, "state", "preUpdateAverageRate"],
|
|
["data", "parsed", "info", "extensions", KEYPATH_WILDCARD, "state", "currentRate"]
|
|
];
|
|
var jsonParsedAccountsConfigs = [
|
|
...jsonParsedTokenAccountsConfigs,
|
|
// parsed AddressTableLookup account
|
|
["data", "parsed", "info", "lastExtendedSlotStartIndex"],
|
|
// parsed Config account
|
|
["data", "parsed", "info", "slashPenalty"],
|
|
["data", "parsed", "info", "warmupCooldownRate"],
|
|
// parsed Token/Token22 mint account
|
|
["data", "parsed", "info", "decimals"],
|
|
// parsed Token/Token22 multisig account
|
|
["data", "parsed", "info", "numRequiredSigners"],
|
|
["data", "parsed", "info", "numValidSigners"],
|
|
// parsed Stake account
|
|
["data", "parsed", "info", "stake", "delegation", "warmupCooldownRate"],
|
|
// parsed Sysvar rent account
|
|
["data", "parsed", "info", "exemptionThreshold"],
|
|
["data", "parsed", "info", "burnPercent"],
|
|
// parsed Vote account
|
|
["data", "parsed", "info", "commission"],
|
|
["data", "parsed", "info", "votes", KEYPATH_WILDCARD, "confirmationCount"]
|
|
];
|
|
var innerInstructionsConfigs = [
|
|
["index"],
|
|
["instructions", KEYPATH_WILDCARD, "accounts", KEYPATH_WILDCARD],
|
|
["instructions", KEYPATH_WILDCARD, "programIdIndex"],
|
|
["instructions", KEYPATH_WILDCARD, "stackHeight"]
|
|
];
|
|
var messageConfig = [
|
|
["addressTableLookups", KEYPATH_WILDCARD, "writableIndexes", KEYPATH_WILDCARD],
|
|
["addressTableLookups", KEYPATH_WILDCARD, "readonlyIndexes", KEYPATH_WILDCARD],
|
|
["header", "numReadonlySignedAccounts"],
|
|
["header", "numReadonlyUnsignedAccounts"],
|
|
["header", "numRequiredSignatures"],
|
|
["instructions", KEYPATH_WILDCARD, "accounts", KEYPATH_WILDCARD],
|
|
["instructions", KEYPATH_WILDCARD, "programIdIndex"],
|
|
["instructions", KEYPATH_WILDCARD, "stackHeight"]
|
|
];
|
|
|
|
export { KEYPATH_WILDCARD, OPTIONS_OBJECT_POSITION_BY_METHOD, applyDefaultCommitment, downcastNodeToNumberIfBigint, getBigIntDowncastRequestTransformer, getBigIntUpcastResponseTransformer, getBigIntUpcastVisitor, getDefaultCommitmentRequestTransformer, getDefaultRequestTransformerForSolanaRpc, getDefaultResponseTransformerForSolanaRpc, getDefaultResponseTransformerForSolanaRpcSubscriptions, getIntegerOverflowNodeVisitor, getIntegerOverflowRequestTransformer, getResultResponseTransformer, getThrowSolanaErrorResponseTransformer, getTreeWalker, getTreeWalkerRequestTransformer, getTreeWalkerResponseTransformer, innerInstructionsConfigs, jsonParsedAccountsConfigs, jsonParsedTokenAccountsConfigs, messageConfig };
|
|
//# sourceMappingURL=index.node.mjs.map
|
|
//# sourceMappingURL=index.node.mjs.map
|