micro-sol-signer
Version:
Create, sign & decode Solana transactions with minimum deps
38 lines • 2.27 kB
JavaScript
import * as sol from "./index.js";
const tokenName = (address, tl) => tl[address]?.symbol || address;
const hints = {
[sol.SYS_PROGRAM]: {
createAccount: (o) => `Create new account=${o.newAccount} with balance of ${sol.Decimal.encode(o.lamports)} and owner program ${o.programAddress}, using funding account ${o.payer}`,
assign: (o) => `Assign account=${o.account} to owner program=${o.programAddress}`,
transferSol: (o) => `Transfer ${sol.Decimal.encode(o.amount)} SOL from ${o.source} to ${o.destination}`,
advanceNonceAccount: (o) => `Consume nonce in nonce account=${o.nonceAccount} (owner: ${o.nonceAuthority})`,
withdrawNonceAccount: (o) => `Withdraw ${sol.Decimal.encode(o.withdrawAmount)} SOL from nonce account=${o.nonceAccount} (owner: ${o.nonceAuthority}) to ${o.recipientAccount}`,
authorizeNonceAccount: (o) => `Change owner of nonce account=${o.nonceAccount} from ${o.nonceAuthority} to ${o.newNonceAuthority}`,
},
[sol.ASSOCIATED_TOKEN_PROGRAM]: {
createAssociatedToken: (o, tl) => `Initialize associated token account=${o.ata} with owner=${o.owner} for token=${tokenName(o.mint, tl)}, payed by ${o.payer}`,
},
};
export function hintInstruction(instruction, tl = {}) {
const raw = sol.parseInstruction(instruction);
const hint = hints[instruction.program] &&
hints[instruction.program][raw.TAG] &&
hints[instruction.program][raw.TAG](raw.data, tl);
if (hint)
return hint;
return undefined;
}
// https://raw.githubusercontent.com/solana-labs/token-list/main/src/tokens/solana.tokenlist.json
export const COMMON_TOKENS = {
So11111111111111111111111111111111111111112: { decimals: 9, symbol: 'SOL' }, // Wrapped SOL
Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB: { decimals: 6, symbol: 'USDT', price: 1 },
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v: { decimals: 6, symbol: 'USDC', price: 1 },
'2b1kV6DkPAnxd5ixfnxCpjxmKwqjjaYmCZfHsFu24GXo': { decimals: 6, symbol: 'PYUSD', price: 1 }, // PayPal USD
};
export function tokenFromSymbol(symbol, tokens = COMMON_TOKENS) {
for (let c in tokens)
if (tokens[c].symbol === symbol)
return { ...tokens[c], contract: c };
return;
}
//# sourceMappingURL=hint.js.map