marginTrade3 with passed in jup instructions
This commit is contained in:
parent
ab3abc69e7
commit
ab0b77f5f6
|
@ -21,6 +21,7 @@ import {
|
||||||
PublicKey,
|
PublicKey,
|
||||||
Signer,
|
Signer,
|
||||||
SystemProgram,
|
SystemProgram,
|
||||||
|
SYSVAR_INSTRUCTIONS_PUBKEY,
|
||||||
SYSVAR_RENT_PUBKEY,
|
SYSVAR_RENT_PUBKEY,
|
||||||
Transaction,
|
Transaction,
|
||||||
TransactionInstruction,
|
TransactionInstruction,
|
||||||
|
@ -1421,14 +1422,14 @@ export class MangoClient {
|
||||||
inputToken,
|
inputToken,
|
||||||
amountIn,
|
amountIn,
|
||||||
outputToken,
|
outputToken,
|
||||||
slippage = 0.5,
|
userDefinedInstructions,
|
||||||
}: {
|
}: {
|
||||||
group: Group;
|
group: Group;
|
||||||
mangoAccount: MangoAccount;
|
mangoAccount: MangoAccount;
|
||||||
inputToken: string;
|
inputToken: string;
|
||||||
amountIn: number;
|
amountIn: number;
|
||||||
outputToken: string;
|
outputToken: string;
|
||||||
slippage: number;
|
userDefinedInstructions: TransactionInstruction[];
|
||||||
}): Promise<TransactionSignature> {
|
}): Promise<TransactionSignature> {
|
||||||
const inputBank = group.banksMap.get(inputToken);
|
const inputBank = group.banksMap.get(inputToken);
|
||||||
const outputBank = group.banksMap.get(outputToken);
|
const outputBank = group.banksMap.get(outputToken);
|
||||||
|
@ -1444,13 +1445,11 @@ export class MangoClient {
|
||||||
(pk) =>
|
(pk) =>
|
||||||
({
|
({
|
||||||
pubkey: pk,
|
pubkey: pk,
|
||||||
isWritable:
|
isWritable: false,
|
||||||
pk.equals(inputBank.publicKey) || pk.equals(outputBank.publicKey)
|
|
||||||
? true
|
|
||||||
: false,
|
|
||||||
isSigner: false,
|
isSigner: false,
|
||||||
} as AccountMeta),
|
} as AccountMeta),
|
||||||
);
|
);
|
||||||
|
console.log('1');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Find or create associated token accounts
|
* Find or create associated token accounts
|
||||||
|
@ -1497,62 +1496,9 @@ export class MangoClient {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
console.log('2');
|
||||||
|
|
||||||
const nativeInputAmount = toU64(
|
if (preInstructions.length) {
|
||||||
amountIn,
|
|
||||||
inputBank.mintDecimals,
|
|
||||||
).toNumber();
|
|
||||||
const instructions: TransactionInstruction[] = [];
|
|
||||||
|
|
||||||
// TODO: move out of client and into ui
|
|
||||||
// Start Jupiter
|
|
||||||
|
|
||||||
const jupiter = await Jupiter.load({
|
|
||||||
connection: this.program.provider.connection,
|
|
||||||
cluster: 'mainnet-beta',
|
|
||||||
user: mangoAccount.owner, // or public key
|
|
||||||
// platformFeeAndAccounts: NO_PLATFORM_FEE,
|
|
||||||
routeCacheDuration: 10_000, // Will not refetch data on computeRoutes for up to 10 seconds
|
|
||||||
});
|
|
||||||
|
|
||||||
const routes = await jupiter.computeRoutes({
|
|
||||||
inputMint: inputBank.mint, // Mint address of the input token
|
|
||||||
outputMint: outputBank.mint, // Mint address of the output token
|
|
||||||
inputAmount: nativeInputAmount, // raw input amount of tokens
|
|
||||||
slippage, // The slippage in % terms
|
|
||||||
forceFetch: false, // false is the default value => will use cache if not older than routeCacheDuration
|
|
||||||
});
|
|
||||||
|
|
||||||
const routesInfosWithoutRaydium = routes.routesInfos.filter((r) => {
|
|
||||||
if (r.marketInfos.length > 1) {
|
|
||||||
for (const mkt of r.marketInfos) {
|
|
||||||
if (mkt.amm.label === 'Raydium' || mkt.amm.label === 'Serum')
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
const selectedRoute = routesInfosWithoutRaydium[0];
|
|
||||||
|
|
||||||
const { transactions } = await jupiter.exchange({
|
|
||||||
routeInfo: selectedRoute,
|
|
||||||
});
|
|
||||||
const { setupTransaction, swapTransaction } = transactions;
|
|
||||||
|
|
||||||
for (const ix of swapTransaction.instructions) {
|
|
||||||
if (
|
|
||||||
ix.programId.toBase58() ===
|
|
||||||
'JUP2jxvXaqu7NQY1GmNF4m1vodw12LVXYxbFL2uJvfo'
|
|
||||||
) {
|
|
||||||
instructions.push(ix);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// End Jupiter
|
|
||||||
|
|
||||||
if (setupTransaction) {
|
|
||||||
await this.program.provider.sendAndConfirm(setupTransaction);
|
|
||||||
} else if (preInstructions.length) {
|
|
||||||
const tx = new Transaction();
|
const tx = new Transaction();
|
||||||
for (const ix of preInstructions) {
|
for (const ix of preInstructions) {
|
||||||
tx.add(ix);
|
tx.add(ix);
|
||||||
|
@ -1561,21 +1507,37 @@ export class MangoClient {
|
||||||
|
|
||||||
await this.program.provider.sendAndConfirm(tx);
|
await this.program.provider.sendAndConfirm(tx);
|
||||||
}
|
}
|
||||||
|
console.log('3');
|
||||||
|
|
||||||
const bankAccounts = {
|
const sourceBankAccounts = {
|
||||||
isWritable: true,
|
|
||||||
pubkey: inputBank.publicKey,
|
pubkey: inputBank.publicKey,
|
||||||
|
isWritable: true,
|
||||||
isSigner: false,
|
isSigner: false,
|
||||||
};
|
};
|
||||||
const bankVaults = {
|
const targetBankAccount = {
|
||||||
|
pubkey: outputBank.publicKey,
|
||||||
isWritable: true,
|
isWritable: true,
|
||||||
|
isSigner: false,
|
||||||
|
};
|
||||||
|
const sourceBankVault = {
|
||||||
pubkey: inputBank.vault,
|
pubkey: inputBank.vault,
|
||||||
|
isWritable: true,
|
||||||
isSigner: false,
|
isSigner: false,
|
||||||
};
|
};
|
||||||
const aTokenAccounts = {
|
const targetBankVault = {
|
||||||
|
pubkey: outputBank.vault,
|
||||||
isWritable: true,
|
isWritable: true,
|
||||||
|
isSigner: false,
|
||||||
|
};
|
||||||
|
const sourceATA = {
|
||||||
pubkey: inputTokenAccountPk,
|
pubkey: inputTokenAccountPk,
|
||||||
isSigner: true,
|
isWritable: true,
|
||||||
|
isSigner: false,
|
||||||
|
};
|
||||||
|
const targetATA = {
|
||||||
|
pubkey: outputTokenAccountPk,
|
||||||
|
isWritable: false,
|
||||||
|
isSigner: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const flashLoanEndIx = await this.program.methods
|
const flashLoanEndIx = await this.program.methods
|
||||||
|
@ -1584,19 +1546,54 @@ export class MangoClient {
|
||||||
account: mangoAccount.publicKey,
|
account: mangoAccount.publicKey,
|
||||||
owner: (this.program.provider as AnchorProvider).wallet.publicKey,
|
owner: (this.program.provider as AnchorProvider).wallet.publicKey,
|
||||||
})
|
})
|
||||||
.remainingAccounts([...parsedHealthAccounts, bankVaults, aTokenAccounts])
|
.remainingAccounts([
|
||||||
|
...parsedHealthAccounts,
|
||||||
|
sourceBankVault,
|
||||||
|
targetBankVault,
|
||||||
|
sourceATA,
|
||||||
|
{
|
||||||
|
isWritable: true,
|
||||||
|
pubkey: outputTokenAccountPk,
|
||||||
|
isSigner: false,
|
||||||
|
},
|
||||||
|
])
|
||||||
.instruction();
|
.instruction();
|
||||||
|
console.log('4');
|
||||||
|
|
||||||
return await this.program.methods
|
// userDefinedInstructions.push(flashLoanEndIx);
|
||||||
.flashLoan3Begin([toU64(amountIn, inputBank.mintDecimals)])
|
|
||||||
|
const x = await this.program.methods
|
||||||
|
.flashLoan3Begin([
|
||||||
|
toNativeDecimals(amountIn, inputBank.mintDecimals),
|
||||||
|
new BN(
|
||||||
|
0,
|
||||||
|
) /* we don't care about borrowing the target amount, this is just a dummy */,
|
||||||
|
])
|
||||||
.accounts({
|
.accounts({
|
||||||
group: group.publicKey,
|
group: group.publicKey,
|
||||||
|
instructions: SYSVAR_INSTRUCTIONS_PUBKEY,
|
||||||
})
|
})
|
||||||
.remainingAccounts([bankAccounts, bankVaults, aTokenAccounts])
|
.remainingAccounts([
|
||||||
.postInstructions([...instructions, flashLoanEndIx])
|
sourceBankAccounts,
|
||||||
.rpc({ skipPreflight: true });
|
targetBankAccount,
|
||||||
}
|
sourceBankVault,
|
||||||
|
targetBankVault,
|
||||||
|
sourceATA,
|
||||||
|
targetATA,
|
||||||
|
])
|
||||||
|
.instruction();
|
||||||
|
|
||||||
|
console.log('begin', x);
|
||||||
|
console.log('end', flashLoanEndIx);
|
||||||
|
|
||||||
|
const tx = new Transaction();
|
||||||
|
tx.add(x);
|
||||||
|
for (const i of userDefinedInstructions) {
|
||||||
|
tx.add(i);
|
||||||
|
}
|
||||||
|
tx.add(flashLoanEndIx);
|
||||||
|
return this.program.provider.sendAndConfirm(tx);
|
||||||
|
}
|
||||||
/// liquidations
|
/// liquidations
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
|
|
|
@ -16,7 +16,8 @@ export {
|
||||||
Serum3Side,
|
Serum3Side,
|
||||||
} from './accounts/serum3';
|
} from './accounts/serum3';
|
||||||
export * from './constants';
|
export * from './constants';
|
||||||
// export * from './integrations/orca/index';
|
export * from './utils';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
Group,
|
Group,
|
||||||
StubOracle,
|
StubOracle,
|
||||||
|
|
|
@ -13,17 +13,22 @@ import { QUOTE_DECIMALS } from '../accounts/bank';
|
||||||
import { MangoClient } from '../client';
|
import { MangoClient } from '../client';
|
||||||
import { getAssociatedTokenAddress } from '../utils';
|
import { getAssociatedTokenAddress } from '../utils';
|
||||||
|
|
||||||
|
const CLUSTER_URL =
|
||||||
|
process.env.CLUSTER_URL ||
|
||||||
|
'https://mango.rpcpool.com/946ef7337da3f5b8d3e4a34e7f88';
|
||||||
|
const MANGO_MAINNET_PAYER_KEYPAIR =
|
||||||
|
process.env.MANGO_MAINNET_PAYER_KEYPAIR ||
|
||||||
|
'/Users/tylershipe/.config/solana/deploy.json';
|
||||||
|
|
||||||
// NOTE: we assume that ATA for source and target already exist for wallet
|
// NOTE: we assume that ATA for source and target already exist for wallet
|
||||||
async function main() {
|
async function main() {
|
||||||
const options = AnchorProvider.defaultOptions();
|
const options = AnchorProvider.defaultOptions();
|
||||||
const connection = new Connection(process.env.CLUSTER_URL, options);
|
const connection = new Connection(CLUSTER_URL, options);
|
||||||
|
|
||||||
// load user key
|
// load user key
|
||||||
const user = Keypair.fromSecretKey(
|
const user = Keypair.fromSecretKey(
|
||||||
Buffer.from(
|
Buffer.from(
|
||||||
JSON.parse(
|
JSON.parse(fs.readFileSync(MANGO_MAINNET_PAYER_KEYPAIR!, 'utf-8')),
|
||||||
fs.readFileSync(process.env.MANGO_MAINNET_PAYER_KEYPAIR!, 'utf-8'),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
const userWallet = new Wallet(user);
|
const userWallet = new Wallet(user);
|
||||||
|
@ -37,9 +42,7 @@ async function main() {
|
||||||
// load admin key
|
// load admin key
|
||||||
const admin = Keypair.fromSecretKey(
|
const admin = Keypair.fromSecretKey(
|
||||||
Buffer.from(
|
Buffer.from(
|
||||||
JSON.parse(
|
JSON.parse(fs.readFileSync(MANGO_MAINNET_PAYER_KEYPAIR!, 'utf-8')),
|
||||||
fs.readFileSync(process.env.MANGO_MAINNET_PAYER_KEYPAIR!, 'utf-8'),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
console.log(`Admin ${admin.publicKey.toBase58()}`);
|
console.log(`Admin ${admin.publicKey.toBase58()}`);
|
||||||
|
|
Loading…
Reference in New Issue