-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adds initial raydium support * fix lint
- Loading branch information
Showing
27 changed files
with
4,682 additions
and
408 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,20 @@ | ||
## AMM v4 (Legacy) | ||
|
||
### 1 Pool | ||
|
||
```javascript | ||
let route = await Web3Exchanges.raydium.route({ | ||
blockchain: 'solana', | ||
tokenIn: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', | ||
tokenOut: 'HhJpBhRRn4g56VsyLuT8DL5Bv31HkXqsrahTTUCZeZg4', | ||
amountOutMin: 0.01 | ||
}) | ||
|
||
let wallets = await Web3Wallets.getWallets() | ||
let wallet = wallets[1] | ||
let account = await wallet.account() | ||
let transaction = await route.getTransaction({ account }) | ||
|
||
wallet.sendTransaction(transaction) | ||
``` | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Exchange from '../classes/Exchange' | ||
import Raydium from '../platforms/solana/raydium' | ||
|
||
const exchange = { | ||
|
||
name: 'raydium', | ||
label: 'Raydium', | ||
logo: 'data:image/svg+xml;base64,PHN2ZyBmaWxsPSJub25lIiBoZWlnaHQ9IjMzIiB2aWV3Qm94PSIwIDAgMjkgMzMiIHdpZHRoPSIyOSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+PGxpbmVhckdyYWRpZW50IGlkPSJhIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjI4LjMxNjgiIHgyPSItMS43MzMzNiIgeTE9IjguMTkxNjIiIHkyPSIyMC4yMDg2Ij48c3RvcCBvZmZzZXQ9IjAiIHN0b3AtY29sb3I9IiNjMjAwZmIiLz48c3RvcCBvZmZzZXQ9Ii40ODk2NTgiIHN0b3AtY29sb3I9IiMzNzcyZmYiLz48c3RvcCBvZmZzZXQ9Ii40ODk3NTgiIHN0b3AtY29sb3I9IiMzNzczZmUiLz48c3RvcCBvZmZzZXQ9IjEiIHN0b3AtY29sb3I9IiM1YWM0YmUiLz48L2xpbmVhckdyYWRpZW50PjxnIGZpbGw9InVybCgjYSkiPjxwYXRoIGQ9Im0yNi44NjI1IDEyLjI4MXYxMS40MTA0bC0xMi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44MTQwNXYtMS43MjAwNWwtMS40NjUzLS44NDk0MS0xLjQ2NTIuODQ5NDF6Ii8+PC9nPjwvc3ZnPg==', | ||
protocol: 'raydium', | ||
|
||
slippage: true, | ||
|
||
blockchains: ['solana'], | ||
|
||
solana: { | ||
|
||
router_amm: { | ||
address: '675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8', | ||
api: Raydium.AMM_LAYOUT, | ||
}, | ||
|
||
router_cpamm: { | ||
address: 'CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C', | ||
api: Raydium.CPAMM_LAYOUT | ||
}, | ||
|
||
router_clmm: { | ||
address: 'CAMMCzo5YL8w4VFF8KVHrK22GGUsp5VTaW7grrKgrWqK', | ||
api: Raydium.CLMM_LAYOUT | ||
}, | ||
} | ||
} | ||
|
||
export default (scope)=>{ | ||
|
||
return new Exchange( | ||
|
||
Object.assign(exchange, { | ||
scope, | ||
|
||
findPath: (args)=>Raydium.findPath({ ...args, exchange }), | ||
pathExists: (args)=>Raydium.pathExists({ ...args, exchange }), | ||
getAmounts: (args)=>Raydium.getAmounts({ ...args, exchange }), | ||
getPrep: (args)=>{}, | ||
getTransaction: (args)=>Raydium.getTransaction({ ...args, exchange }), | ||
}) | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { findPath, pathExists } from './path' | ||
import { getAmounts } from './amounts' | ||
import { getTransaction } from './transaction' | ||
import { WHIRLPOOL_LAYOUT } from './layouts' | ||
|
||
export default { | ||
findPath, | ||
pathExists, | ||
getAmounts, | ||
getTransaction, | ||
WHIRLPOOL_LAYOUT, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/*#if _EVM | ||
/*#elif _SOLANA | ||
import { request } from '@depay/web3-client-solana' | ||
import { BN } from '@depay/solana-web3.js' | ||
//#else */ | ||
|
||
import { request } from '@depay/web3-client' | ||
import { BN } from '@depay/solana-web3.js' | ||
|
||
//#endif | ||
|
||
import Blockchains from '@depay/web3-blockchains' | ||
import { AMM_LAYOUT } from '../layouts' | ||
|
||
export const LIQUIDITY_FEES_NUMERATOR = new BN(25) | ||
export const LIQUIDITY_FEES_DENOMINATOR = new BN(10000) | ||
|
||
const BNDivCeil = (bn1, bn2)=> { | ||
const { div, mod } = bn1.divmod(bn2) | ||
|
||
if (mod.gt(new BN(0))) { | ||
return div.add(new BN(1)) | ||
} else { | ||
return div | ||
} | ||
} | ||
|
||
const getPairs = (base, quote)=>{ | ||
return request(`solana://675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8/getProgramAccounts`, { | ||
params: { filters: [ | ||
{ dataSize: AMM_LAYOUT.span }, | ||
{ memcmp: { offset: 0, bytes: [6,0,0] }}, // filters for status 6 (Swap) | ||
{ memcmp: { offset: 400, bytes: base }}, | ||
{ memcmp: { offset: 432, bytes: quote }}, | ||
]}, | ||
api: AMM_LAYOUT, | ||
cache: 86400, // 24h, | ||
cacheKey: ['raydium', base.toString(), quote.toString()].join('-') | ||
}) | ||
} | ||
|
||
const getPairsWithPrice = async({ tokenIn, tokenOut, amountIn, amountInMax, amountOut, amountOutMin })=>{ | ||
|
||
let accounts = await getPairs(tokenIn, tokenOut) | ||
|
||
if(accounts.length == 0) { | ||
accounts = await getPairs(tokenOut, tokenIn) | ||
} | ||
|
||
const pairs = await Promise.all(accounts.map(async(account)=>{ | ||
|
||
const baseMint = account.data.baseMint.toString() | ||
const quoteMint = account.data.quoteMint.toString() | ||
|
||
const balances = await Promise.all([ | ||
request(`solana://${account.data.baseVault.toString()}/getTokenAccountBalance`, { cache: 5 }), | ||
request(`solana://${account.data.quoteVault.toString()}/getTokenAccountBalance`, { cache: 5 }) | ||
]) | ||
const baseReserve = (balances[0]?.value?.amount ? new BN(balances[0]?.value?.amount) : new BN(0)).sub(account.data.baseNeedTakePnl) | ||
const quoteReserve = (balances[1]?.value?.amount ? new BN(balances[1]?.value?.amount) : new BN(0)).sub(account.data.quoteNeedTakePnl) | ||
|
||
if(baseMint === Blockchains.solana.wrapped.address) { | ||
if(baseReserve.lt(new BN(50000000))) { | ||
return // to little liquidity | ||
} | ||
} else if (quoteMint === Blockchains.solana.wrapped.address) { | ||
if(quoteReserve.lt(new BN(50000000))) { | ||
return // to little liquidity | ||
} | ||
} else if (Blockchains.solana.stables.usd.includes(baseMint)) { | ||
if((parseFloat(baseReserve.toString()) / 10 ** account.data.baseDecimal.toNumber()) < 10000) { | ||
return // to little liquidity | ||
} | ||
} else if (Blockchains.solana.stables.usd.includes(quoteMint)) { | ||
if((parseFloat(quoteReserve.toString()) / 10 ** account.data.quoteDecimal.toNumber()) < 10000) { | ||
return // to little liquidity | ||
} | ||
} | ||
|
||
const reserves = [baseReserve, quoteReserve] | ||
|
||
if(tokenOut === baseMint) { | ||
reserves.reverse() | ||
} | ||
|
||
const [reserveIn, reserveOut] = reserves | ||
|
||
let price | ||
if(amountOut || amountOutMin) { // compute amountIn | ||
|
||
const amountInRaw = new BN(0) | ||
let amountOutRaw = new BN((amountOut || amountOutMin).toString()) | ||
|
||
if (amountOutRaw.gt(reserveOut)) { | ||
amountOutRaw = reserveOut.sub(new BN(1)) | ||
} | ||
|
||
const denominator = reserveOut.sub(amountOutRaw) | ||
const amountInWithoutFee = reserveIn.mul(amountOutRaw).div(denominator) | ||
|
||
price = amountInWithoutFee | ||
.mul(LIQUIDITY_FEES_DENOMINATOR) | ||
.div(LIQUIDITY_FEES_DENOMINATOR.sub(LIQUIDITY_FEES_NUMERATOR)) | ||
.toString() | ||
|
||
} else { // compute amountOut | ||
|
||
const amountOutRaw = new BN(0) | ||
const amountInRaw = new BN((amountIn || amountInMin).toString()) | ||
const feeRaw = BNDivCeil(amountInRaw.mul(LIQUIDITY_FEES_NUMERATOR), LIQUIDITY_FEES_DENOMINATOR) | ||
|
||
const amountInWithFee = amountInRaw.sub(feeRaw) | ||
const denominator = reserveIn.add(amountInWithFee) | ||
|
||
price = reserveOut.mul(amountInWithFee).div(denominator).toString() | ||
} | ||
|
||
return { | ||
publicKey: account.pubkey.toString(), | ||
baseMint, | ||
quoteMint, | ||
price, | ||
data: account.data | ||
} | ||
|
||
})) | ||
|
||
return pairs.filter(Boolean) | ||
} | ||
|
||
export { | ||
getPairsWithPrice | ||
} |
Oops, something went wrong.