-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtransactionUtil.ts
More file actions
70 lines (64 loc) · 2.01 KB
/
transactionUtil.ts
File metadata and controls
70 lines (64 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type {
FullTransaction,
TransactionVersion,
TransactionMessageWithFeePayer,
TransactionMessageWithBlockhashLifetime,
} from 'gill';
import { getBase58Decoder, compileTransaction, getBase64Decoder } from 'gill';
/**
* Converts a compiled Solana transaction to a base58-encoded string.
*
* Note: Squads still requires base58 encoded transactions.
*
* @param transaction - The full transaction object to encode.
* @returns The base58-encoded transaction as a string.
*/
export const transactionToB58 = (
transaction: FullTransaction<
TransactionVersion,
TransactionMessageWithFeePayer,
TransactionMessageWithBlockhashLifetime
>
): string => {
const compiledTransaction = compileTransaction(transaction);
return getBase58Decoder().decode(compiledTransaction.messageBytes);
};
/**
* Converts a compiled Solana transaction to a base64-encoded string.
*
* Base64 encoded transactions are recommended for most use cases.
*
* @param transaction - The full transaction object to encode.
* @returns The base64-encoded transaction as a string.
*/
export const transactionToB64 = (
transaction: FullTransaction<
TransactionVersion,
TransactionMessageWithFeePayer,
TransactionMessageWithBlockhashLifetime
>
): string => {
const compiledTransaction = compileTransaction(transaction);
return getBase64Decoder().decode(compiledTransaction.messageBytes);
};
/**
* Converts a decimal amount to raw token amount based on mint decimals
*
* @param decimalAmount - The decimal amount (e.g., 1.5)
* @param decimals - The number of decimals the token has
* @returns The raw token amount as bigint
*/
export function decimalAmountToRaw(
decimalAmount: number,
decimals: number
): bigint {
if (decimals < 0 || decimals > 9) {
throw new Error('Decimals must be between 0 and 9');
}
const multiplier = Math.pow(10, decimals);
const rawAmount = Math.floor(decimalAmount * multiplier);
if (rawAmount < 0) {
throw new Error('Amount must be positive');
}
return BigInt(rawAmount);
}