@solana/spl-token#getMint TypeScript Examples
The following examples show how to use
@solana/spl-token#getMint.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: providers.ts From amman with Apache License 2.0 | 6 votes |
private async _tryResolveAccountFromBuiltins(address: PublicKey) {
for (const provider of [getMint, getAccount]) {
try {
const account = await provider(this.connection, address, 'singleGossip')
if (account != null) {
const ammanAccount = await this._toAmmanAccount(account)
return {
account: ammanAccount,
rendered: undefined,
}
}
} catch (err) {
logTrace(err)
}
}
}
Example #2
Source File: providers.ts From amman with Apache License 2.0 | 6 votes |
private async _getMintDecimals(
publicKey: PublicKey
): Promise<{ decimals: number; divisor: number }> {
let decimals: number
try {
const mint = await getMint(this.connection, publicKey, 'singleGossip')
decimals = mint.decimals
} catch (err) {
decimals = DEFAULT_MINT_DECIMALS
}
const divisor = Math.pow(10, decimals)
return { decimals, divisor }
}
Example #3
Source File: createTransfer.ts From solana-pay with Apache License 2.0 | 6 votes |
async function createSPLTokenInstruction(
recipient: PublicKey,
amount: BigNumber,
splToken: PublicKey,
sender: PublicKey,
connection: Connection
): Promise<TransactionInstruction> {
// Check that the token provided is an initialized mint
const mint = await getMint(connection, splToken);
if (!mint.isInitialized) throw new CreateTransferError('mint not initialized');
// Check that the amount provided doesn't have greater precision than the mint
if (amount.decimalPlaces() > mint.decimals) throw new CreateTransferError('amount decimals invalid');
// Convert input decimal amount to integer tokens according to the mint decimals
amount = amount.times(TEN.pow(mint.decimals)).integerValue(BigNumber.ROUND_FLOOR);
// Get the sender's ATA and check that the account exists and can send tokens
const senderATA = await getAssociatedTokenAddress(splToken, sender);
const senderAccount = await getAccount(connection, senderATA);
if (!senderAccount.isInitialized) throw new CreateTransferError('sender not initialized');
if (senderAccount.isFrozen) throw new CreateTransferError('sender frozen');
// Get the recipient's ATA and check that the account exists and can receive tokens
const recipientATA = await getAssociatedTokenAddress(splToken, recipient);
const recipientAccount = await getAccount(connection, recipientATA);
if (!recipientAccount.isInitialized) throw new CreateTransferError('recipient not initialized');
if (recipientAccount.isFrozen) throw new CreateTransferError('recipient frozen');
// Check that the sender has enough tokens
const tokens = BigInt(String(amount));
if (tokens > senderAccount.amount) throw new CreateTransferError('insufficient funds');
// Create an instruction to transfer SPL tokens, asserting the mint and decimals match
return createTransferCheckedInstruction(senderATA, splToken, recipientATA, sender, tokens, mint.decimals);
}