2022-05-25 17:28:42 -07:00
|
|
|
import {
|
|
|
|
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
|
|
TOKEN_PROGRAM_ID,
|
|
|
|
} from '@solana/spl-token';
|
|
|
|
import { AccountMeta, PublicKey } from '@solana/web3.js';
|
2022-06-02 10:30:39 -07:00
|
|
|
import BN from 'bn.js';
|
2022-07-04 03:29:35 -07:00
|
|
|
import { QUOTE_DECIMALS } from './accounts/bank';
|
2022-06-21 03:38:01 -07:00
|
|
|
import { I80F48 } from './accounts/I80F48';
|
2022-06-02 10:30:39 -07:00
|
|
|
|
|
|
|
export const I64_MAX_BN = new BN('9223372036854775807').toTwos(64);
|
2022-04-02 02:52:28 -07:00
|
|
|
|
|
|
|
export function debugAccountMetas(ams: AccountMeta[]) {
|
|
|
|
for (const am of ams) {
|
|
|
|
console.log(
|
|
|
|
`${am.pubkey.toBase58()}, isSigner: ${am.isSigner
|
|
|
|
.toString()
|
|
|
|
.padStart(5, ' ')}, isWritable - ${am.isWritable
|
|
|
|
.toString()
|
|
|
|
.padStart(5, ' ')}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 11:15:56 -07:00
|
|
|
|
|
|
|
export async function findOrCreate<T>(
|
|
|
|
entityName: string,
|
|
|
|
findMethod: Function,
|
|
|
|
findArgs: any[],
|
|
|
|
createMethod: Function,
|
|
|
|
createArgs: any[],
|
|
|
|
): Promise<T> {
|
|
|
|
let many: T[] = await findMethod(...findArgs);
|
|
|
|
let one: T;
|
|
|
|
if (many.length > 0) {
|
|
|
|
one = many[0];
|
|
|
|
return one;
|
|
|
|
}
|
|
|
|
await createMethod(...createArgs);
|
|
|
|
many = await findMethod(...findArgs);
|
|
|
|
one = many[0];
|
|
|
|
return one;
|
|
|
|
}
|
2022-05-25 17:28:42 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the address of the associated token account for a given mint and owner
|
|
|
|
*
|
|
|
|
* @param mint Token mint account
|
|
|
|
* @param owner Owner of the new account
|
|
|
|
* @param allowOwnerOffCurve Allow the owner account to be a PDA (Program Derived Address)
|
|
|
|
* @param programId SPL Token program account
|
|
|
|
* @param associatedTokenProgramId SPL Associated Token program account
|
|
|
|
*
|
|
|
|
* @return Address of the associated token account
|
|
|
|
*/
|
|
|
|
export async function getAssociatedTokenAddress(
|
|
|
|
mint: PublicKey,
|
|
|
|
owner: PublicKey,
|
|
|
|
allowOwnerOffCurve = false,
|
|
|
|
programId = TOKEN_PROGRAM_ID,
|
|
|
|
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
|
|
): Promise<PublicKey> {
|
|
|
|
if (!allowOwnerOffCurve && !PublicKey.isOnCurve(owner.toBuffer()))
|
|
|
|
throw new Error('TokenOwnerOffCurve');
|
|
|
|
|
|
|
|
const [address] = await PublicKey.findProgramAddress(
|
|
|
|
[owner.toBuffer(), programId.toBuffer(), mint.toBuffer()],
|
|
|
|
associatedTokenProgramId,
|
|
|
|
);
|
|
|
|
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function toNativeDecimals(amount: number, decimals: number): BN {
|
2022-05-31 18:38:47 -07:00
|
|
|
return new BN(Math.trunc(amount * Math.pow(10, decimals)));
|
|
|
|
}
|
|
|
|
|
2022-07-04 03:29:35 -07:00
|
|
|
export function toUiDecimals(
|
|
|
|
amount: I80F48 | number,
|
|
|
|
decimals = QUOTE_DECIMALS,
|
|
|
|
): number {
|
|
|
|
amount = amount instanceof I80F48 ? amount.toNumber() : amount;
|
2022-06-10 06:02:03 -07:00
|
|
|
return amount / Math.pow(10, decimals);
|
|
|
|
}
|
|
|
|
|
2022-05-31 18:38:47 -07:00
|
|
|
export function toU64(amount: number, decimals): BN {
|
|
|
|
const bn = toNativeDecimals(amount, decimals).toString();
|
|
|
|
console.log('bn', bn);
|
|
|
|
|
2022-06-23 03:58:43 -07:00
|
|
|
return new BN(bn);
|
2022-05-25 17:28:42 -07:00
|
|
|
}
|
2022-06-21 03:38:01 -07:00
|
|
|
|
|
|
|
export function nativeI80F48ToUi(amount: I80F48, decimals: number): I80F48 {
|
|
|
|
return amount.div(I80F48.fromNumber(Math.pow(10, decimals)));
|
|
|
|
}
|