@okxweb3/coin-solana
Solana SDK is used to interact with the Solana Chain, it contains the main functions you need when interact with Solana Ecosystem.
Getting Started
Installing Solana SDK
npm install @okxweb3/coin-solana
What Can Solana SDK Do
- getRandomPrivateKey
- getNewAddress
- validAddress
- caclTxHash
- signTransaction
- signMessage
- validSignedTransaction
Using Solana SDK
Get Private Key
random private key
import {SolWallet} from "@okxweb3/coin-solana";
let wallet = new SolWallet()
let privateKey = await wallet.getRandomPrivateKey();
derived private key
import {SolWallet} from "@okxweb3/coin-solana";
let wallet = new SolWallet()
let param = {
mnemonic: 'stool trumpet fame umbrella bench provide battle toward story fruit lock view',
hdPath: "m/44'/501'/0'/0'"
};
let privateKey = await wallet.getDerivedPrivateKey(param);
Get Address / Validate Address
import {SolWallet} from "@okxweb3/coin-solana";
let wallet = new SolWallet()
let params = {
privateKey: "548yT115QRHH7Mpchg9JJ8YPX9RTKuan7oeB9ruMULDGhdqBmG18RBSv54Fpv2BvrC1yVpGdjzAPKHNYUwPBePKc"
};
let addr = await wallet.getNewAddress(params);
let addrParam{
address: addr
};
let valid = await wallet.validAddress(addrParam);
Transaction
signTransaction parameter definitions are as follows
type transaction type, support "transfer" | "tokenTransfer" | "mplTransfer"
transfer represents SOL token transfer
tokenTransfer represents SPL token transfer, similar to ERC20 protocol tokens
mplTransfer represents NFT token transfer
payer address, represents the address that pays transaction fees
blockHash, 32-byte array representing the recent block hash
from address, represents the SOL token sender address
to address, represents the SOL token recipient address
amount number, represents the amount of SOL tokens to transfer, SOL token precision is 8
mint address, represents the token address
createAssociatedAddress boolean, indicates whether to create ATA address
version number, transaction version
tokenStandard TokenStandard, represents the transaction standard, with the following five values
- NonFungible,
- FungibleAsset,
- Fungible,
- NonFungibleEdition,
- ProgrammableNonFungible,
token2022 boolean, true represents token2022 standard SPL tokens, false represents SPL token standard tokens
decimal number, represents the precision of token2022 standard SPL tokens, this field is required when transferring tokens of this standard.
computeUnitLimit represents the number of compute units each instruction can consume in the transaction
computeUnitPrice represents the price per compute unit
needPriorityFee boolean, used together with computeUnitLimit and computeUnitPrice to manually set transaction fee parameters.
transfer
import {SolWallet} from "@okxweb3/coin-solana";
let wallet = new SolWallet()
let params = {
privateKey: '548yT115QRHH7Mpchg9JJ8YPX9RTKuan7oeB9ruMULDGhdqBmG18RBSv54Fpv2BvrC1yVpGdjzAPKHNYUwPBePKc',
data: {
type: "transfer",
payer: "FZNZLT5diWHooSBjcng9qitykwcL9v3RiNrpC3fp9PU1",
blockHash: "6t1qEvLH5uC9NMmMTPhaE9tAaWdk1qvBjqsKsKNPB7sX",
from: "FZNZLT5diWHooSBjcng9qitykwcL9v3RiNrpC3fp9PU1",
to: "7NRmECq1R4tCtXNvmvDAuXmii3vN1J9DRZWhMCuuUnkM",
amount: 100000000,
computeUnitLimit: 140000,
computeUnitPrice: 10
}
}
let tx = await wallet.signTransaction(params);
const checkParam = {
tx: tx,
}
const ok = await wallet.validSignedTransaction(checkParam);
token transfer
import {SolWallet} from "@okxweb3/coin-solana";
let wallet = new SolWallet()
let param = {
privateKey: '548yT115QRHH7Mpchg9JJ8YPX9RTKuan7oeB9ruMULDGhdqBmG18RBSv54Fpv2BvrC1yVpGdjzAPKHNYUwPBePKc',
data: {
type: "tokenTransfer",
payer: "FZNZLT5diWHooSBjcng9qitykwcL9v3RiNrpC3fp9PU1",
blockHash: "HwN3QorABLpYftu9FeE1FGrwrBK1aAhhz3cirEVrN3Fn",
from: "FZNZLT5diWHooSBjcng9qitykwcL9v3RiNrpC3fp9PU1",
to: "7NRmECq1R4tCtXNvmvDAuXmii3vN1J9DRZWhMCuuUnkM",
amount: 100000,
mint: "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU",
createAssociatedAddress: false,
token2022: false,
decimal: 9,
computeUnitLimit: 140000,
computeUnitPrice: 10
}
}
let tx = await wallet.signTransaction(param);
const checkParam = {
tx: tx,
}
const v = await wallet.validSignedTransaction(checkParam);
pNft transfer
import {SolWallet} from "@okxweb3/coin-solana";
let wallet = new SolWallet()
let param = {
privateKey: '548yT115QRHH7Mpchg9JJ8YPX9RTKuan7oeB9ruMULDGhdqBmG18RBSv54Fpv2BvrC1yVpGdjzAPKHNYUwPBePKc',
data: {
type: "mplTransfer",
payer: "CS8ifB68oddKXdW87RAyrxFSoz1DMMcX9WsWeAgbYDCC",
blockHash: "8MnQifmv14ELwdkK5NJso9cofN8iNpHy6n6Nnxy7pn8v",
from: "CS8ifB68oddKXdW87RAyrxFSoz1DMMcX9WsWeAgbYDCC",
to: "9qinWp4oc3TvBocbwAvYZAZWfSswub2qM49Pn6rkCQ9q",
mint: "DberpiNB1sttkWdd66amQV5hrnMGacBeDeMbcEFMVBiR",
computeUnitLimit: 140000,
computeUnitPrice: 10
}
}
let tx = await wallet.signTransaction(param);
sign message
import {SolWallet} from "@okxweb3/coin-solana";
let wallet = new SolWallet()
let param = {
privateKey: "548yT115QRHH7Mpchg9JJ8YPX9RTKuan7oeB9ruMULDGhdqBmG18RBSv54Fpv2BvrC1yVpGdjzAPKHNYUwPBePKc",
data: 'your message to sign'
}
let tx = await wallet.signMessage(param);
deserialize message
import {SolWallet} from "@okxweb3/coin-solana";
let wallet = new SolWallet()
let param = {
data: ['your message to deserialize']
}
let res = await wallet.deserializeMessages(param);
calculate tx hash
import {SolWallet} from "@okxweb3/coin-solana";
let wallet = new SolWallet()
let param = {
data: "your data to sign",
};
const tx = await wallet.calcTxHash(param);
License: MIT