@orca-so/common-sdk
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -1,2 +0,4 @@ | ||
import { AccountInfo } from "@solana/spl-token"; | ||
import { AccountInfo, u64 } from "@solana/spl-token"; | ||
import { Connection, PublicKey } from "@solana/web3.js"; | ||
import { Instruction } from "../web3"; | ||
/** | ||
@@ -7,2 +9,18 @@ * @category Util | ||
static deserializeTokenAccount: (data: Buffer | undefined) => AccountInfo | null; | ||
/** | ||
* Create an ix to send a spl-token / native-mint to another wallet. | ||
* This function will handle the associated token accounts internally for spl-token. | ||
* SOL is sent directly to the user's wallet. | ||
* | ||
* @param connection - Connection object | ||
* @param sourceWallet - PublicKey for the sender's wallet | ||
* @param destinationWallet - PublicKey for the receiver's wallet | ||
* @param tokenMint - Mint for the token that is being sent. | ||
* @param tokenDecimals - Decimal for the token that is being sent. | ||
* @param amount - Amount of token to send | ||
* @param getAccountRentExempt - Fn to fetch the account rent exempt value | ||
* @param payer - PublicKey for the payer that would fund the possibly new token-accounts. (must sign the txn) | ||
* @returns | ||
*/ | ||
static createSendTokensToWalletInstruction(connection: Connection, sourceWallet: PublicKey, destinationWallet: PublicKey, tokenMint: PublicKey, tokenDecimals: number, amount: u64, getAccountRentExempt: () => Promise<number>, payer?: PublicKey): Promise<Instruction>; | ||
} |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -6,2 +29,5 @@ exports.TokenUtil = void 0; | ||
const web3_js_1 = require("@solana/web3.js"); | ||
const tiny_invariant_1 = __importDefault(require("tiny-invariant")); | ||
const math_1 = require("../math"); | ||
const web3_1 = require("../web3"); | ||
/** | ||
@@ -11,2 +37,43 @@ * @category Util | ||
class TokenUtil { | ||
/** | ||
* Create an ix to send a spl-token / native-mint to another wallet. | ||
* This function will handle the associated token accounts internally for spl-token. | ||
* SOL is sent directly to the user's wallet. | ||
* | ||
* @param connection - Connection object | ||
* @param sourceWallet - PublicKey for the sender's wallet | ||
* @param destinationWallet - PublicKey for the receiver's wallet | ||
* @param tokenMint - Mint for the token that is being sent. | ||
* @param tokenDecimals - Decimal for the token that is being sent. | ||
* @param amount - Amount of token to send | ||
* @param getAccountRentExempt - Fn to fetch the account rent exempt value | ||
* @param payer - PublicKey for the payer that would fund the possibly new token-accounts. (must sign the txn) | ||
* @returns | ||
*/ | ||
static createSendTokensToWalletInstruction(connection, sourceWallet, destinationWallet, tokenMint, tokenDecimals, amount, getAccountRentExempt, payer) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
(0, tiny_invariant_1.default)(!amount.eq(math_1.ZERO), "SendToken transaction must send more than 0 tokens."); | ||
// Specifically handle SOL, which is not a spl-token. | ||
if (tokenMint.equals(spl_token_1.NATIVE_MINT)) { | ||
const sendSolTxn = web3_js_1.SystemProgram.transfer({ | ||
fromPubkey: sourceWallet, | ||
toPubkey: destinationWallet, | ||
lamports: BigInt(amount.toString()) | ||
}); | ||
return { | ||
instructions: [sendSolTxn], | ||
cleanupInstructions: [], | ||
signers: [] | ||
}; | ||
} | ||
const sourceTokenAccount = yield (0, web3_1.deriveATA)(sourceWallet, tokenMint); | ||
const _a = yield (0, web3_1.resolveOrCreateATA)(connection, destinationWallet, tokenMint, getAccountRentExempt, amount, payer), { address: destinationTokenAccount } = _a, destinationAtaIx = __rest(_a, ["address"]); | ||
const transferIx = spl_token_1.Token.createTransferCheckedInstruction(spl_token_1.TOKEN_PROGRAM_ID, sourceTokenAccount, tokenMint, destinationTokenAccount, sourceWallet, [], new spl_token_1.u64(amount.toString()), tokenDecimals); | ||
return { | ||
instructions: destinationAtaIx.instructions.concat(transferIx), | ||
cleanupInstructions: destinationAtaIx.cleanupInstructions, | ||
signers: destinationAtaIx.signers, | ||
}; | ||
}); | ||
} | ||
} | ||
@@ -13,0 +80,0 @@ exports.TokenUtil = TokenUtil; |
{ | ||
"name": "@orca-so/common-sdk", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Common Typescript components across Orca", | ||
@@ -13,3 +13,4 @@ "repository": "https://github.com/orca-so/orca-sdks", | ||
"@solana/spl-token": "0.1.8", | ||
"decimal.js": "^10.3.1" | ||
"decimal.js": "^10.3.1", | ||
"tiny-invariant": "^1.2.0" | ||
}, | ||
@@ -20,3 +21,3 @@ "resolutions": { | ||
"devDependencies": { | ||
"@types/bn.js": "^4.11.6", | ||
"@types/bn.js": "~5.1.0", | ||
"@types/decimal.js": "^7.4.0", | ||
@@ -23,0 +24,0 @@ "@types/jest": "^26.0.24", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86537
1058
4
+ Addedtiny-invariant@^1.2.0
+ Addedtiny-invariant@1.3.3(transitive)