-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6463a9f
commit a4b274b
Showing
6 changed files
with
199 additions
and
8 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const swap_helper = require("./swap-helper"); | ||
const { wallet } = require("../../../../helpers/config"); | ||
const { getDecimals } = require("../../../../helpers/util"); | ||
const wsol = "So11111111111111111111111111111111111111112"; | ||
/** | ||
* Buys a token by performing a swap transaction. | ||
* | ||
* @param {string} tokenIn - The token to be swapped. | ||
* @param {number} amountTokenOut - The amount of token to be swapped. | ||
* @param {number} slippage - The slippage tolerance for the swap. | ||
* @returns {Promise<void>} - A promise that resolves when the swap transaction is completed. | ||
* @throws {Error} - If an error occurs during the swap transaction. | ||
*/ | ||
async function buy(tokenIn, amountTokenOut, slippage) { | ||
try { | ||
const decimals = await getDecimals(wsol); | ||
const convertedAmountOfTokenOut = swap_helper.convertToInteger( | ||
amountTokenOut, | ||
decimals | ||
); | ||
const quoteResponse = await swap_helper.getQuote( | ||
wsol, | ||
tokenIn, | ||
convertedAmountOfTokenOut, | ||
slippage | ||
); | ||
const wallet_PubKey = wallet.publicKey.toBase58(); | ||
const swapTransaction = await swap_helper.getSwapTransaction( | ||
quoteResponse, | ||
wallet_PubKey | ||
); | ||
const { confirmed, signature } = await swap_helper.finalizeTransaction( | ||
swapTransaction | ||
); | ||
if (confirmed) { | ||
console.log("http://solscan.io/tx/" + signature); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
module.exports = { buy }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const swap_helper = require("./swap-helper"); | ||
const { wallet } = require("../../../../helpers/config"); | ||
const { getDecimals } = require("../../../../helpers/util"); | ||
const wsol = "So11111111111111111111111111111111111111112"; | ||
|
||
async function sell(addressOfTokenOut, amountOfTokenToSell, slippage) { | ||
try { | ||
const decimals = await getDecimals(addressOfTokenOut); | ||
const convertedAmountOfTokenOut = swap_helper.convertToInteger( | ||
amountOfTokenToSell, | ||
decimals | ||
); | ||
const quoteResponse = await swap_helper.getQuote( | ||
addressOfTokenOut, | ||
wsol, | ||
convertedAmountOfTokenOut, | ||
slippage | ||
); | ||
const wallet_PubKey = wallet.publicKey.toBase58(); | ||
const swapTransaction = await swap_helper.getSwapTransaction( | ||
quoteResponse, | ||
wallet_PubKey | ||
); | ||
const { confirmed, signature } = await swap_helper.finalizeTransaction( | ||
swapTransaction | ||
); | ||
if (confirmed) { | ||
console.log("http://solscan.io/tx/" + signature); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
module.exports = { sell }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
const { VersionedTransaction } = require("@solana/web3.js"); | ||
const fetch = require("cross-fetch"); | ||
const { connection, wallet, jito_fee } = require("../../../../helpers/config"); | ||
const { | ||
jito_executeAndConfirm, | ||
} = require("../../../../Transactions/jito_tips_tx_executor"); | ||
/** | ||
* Get quote for the swap | ||
* @param {string} addressOfTokenOut The token that we are selling | ||
* @param {string} addressOfTokenIn The token that we are buying | ||
* @param {number} convertedAmountOfTokenOut The amount of tokens that we are selling | ||
* @param {number} slippage The slippage percentage | ||
* @returns Promise<QuoteResponse> | ||
*/ | ||
async function getQuote( | ||
tokenOut, | ||
tokenIn, | ||
convertedAmountOfTokenOut, | ||
slippage | ||
) { | ||
const url = `https://quote-api.jup.ag/v6/quote?inputMint=${tokenIn}\&outputMint=${tokenOut}\&amount=${convertedAmountOfTokenOut}\&slippageBps=${slippage}`; | ||
const response = await fetch(url); | ||
const quote = await response.json(); | ||
return quote; | ||
} | ||
|
||
/** | ||
* Get serialized transactions for the swap | ||
* @returns {Promise<string>} swapTransaction | ||
*/ | ||
async function getSwapTransaction(quoteResponse, wallet_pubKey) { | ||
try { | ||
let body = null; | ||
body = { | ||
quoteResponse, | ||
userPublicKey: wallet_pubKey, | ||
wrapAndUnwrapSol: true, | ||
restrictIntermediateTokens: false, | ||
prioritizationFeeLamports: "auto", | ||
autoMultiplier: 2, | ||
}; | ||
const resp = await fetch("https://quote-api.jup.ag/v6/swap", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(body), | ||
}); | ||
const swapResponse = await resp.json(); | ||
return swapResponse.swapTransaction; | ||
} catch (error) { | ||
throw new Error(error); | ||
} | ||
} | ||
async function convertToInteger(amount, decimals) { | ||
return Math.floor(amount * 10 ** decimals); | ||
} | ||
|
||
/** | ||
* @param {*} swapTransaction | ||
* @returns Promise<string> txid | ||
*/ | ||
async function finalizeTransaction(swapTransaction) { | ||
try { | ||
// deserialize the transaction | ||
const swapTransactionBuf = Buffer.from(swapTransaction, "base64"); | ||
let transaction = VersionedTransaction.deserialize(swapTransactionBuf); | ||
|
||
// sign the transaction | ||
transaction.sign([wallet]); | ||
|
||
const rawTransaction = transaction.serialize(); | ||
const latestBlockhash = await connection.getLatestBlockhash(); | ||
let { confirmed, signature } = await jito_executeAndConfirm( | ||
rawTransaction, | ||
wallet, | ||
latestBlockhash, | ||
jito_fee | ||
); | ||
while (!confirmed) { | ||
console.log("Transaction failed"); | ||
console.log("resubmitting transaction..."); | ||
confirmed, | ||
(signature = await jito_executeAndConfirm( | ||
rawTransaction, | ||
wallet, | ||
latestBlockhash, | ||
jito_fee | ||
)); | ||
} | ||
console.log(`Jito Transaction sent and confirmed with txid: ${signature}`); | ||
return { confirmed, signature }; | ||
} catch (error) { | ||
throw new Error(error); | ||
} | ||
return { confirmed: false, signature: null }; | ||
} | ||
|
||
module.exports = { | ||
getQuote, | ||
getSwapTransaction, | ||
finalizeTransaction, | ||
convertToInteger, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters