isWritable: true,// increase in this address amount is transferred back to the sourceBank.vault above in this case whatever is residual of source bank loan
isSigner: false,
}asAccountMeta,
{
pubkey: awaitgetAssociatedTokenAddress(
targetBank.mint,
mangoAccount.owner,
),
isWritable: true,// increase in this address amount is transferred back to the targetBank.vault above in this case whatever is result of swap
isSigner: false,
}asAccountMeta,
])
.instruction();
instructions.push(flashLoadnEndIx);
// 2. build flash loan start ix, add end ix as a post ix
try{
res=awaitclient.program.methods
.flashLoan3Begin([
newBN(sourceAmount),
newBN(
0,
)/* we don't care about borrowing the target amount, this is just a dummy */,
])
.accounts({
group: group.publicKey,
// for observing ixs in the entire tx,
// e.g. apart from flash loan start and end no other ix should target mango v4 program
// e.g. forbid FlashLoan3Begin been called from CPI
instructions: SYSVAR_INSTRUCTIONS_PUBKEY,
})
.remainingAccounts([
{
pubkey: sourceBank.publicKey,
isWritable: true,// metadata for flash loan is updated
isSigner: false,
}asAccountMeta,
{
pubkey: targetBank.publicKey,
isWritable: true,// this is a dummy, its just done so that we match flash loan start and end ix
isSigner: false,
}asAccountMeta,
{
pubkey: sourceBank.vault,
isWritable: true,
isSigner: false,
}asAccountMeta,
{
pubkey: targetBank.vault,
isWritable: true,// this is a dummy, its just done so that we match flash loan start and end ix
isSigner: false,
}asAccountMeta,
{
pubkey: awaitgetAssociatedTokenAddress(
sourceBank.mint,
mangoAccount.owner,
),
isWritable: true,// token transfer i.e. loan to a desired token account e.g. user's ATA when using a route made for a specific user
isSigner: false,
}asAccountMeta,
{
pubkey: awaitgetAssociatedTokenAddress(
targetBank.mint,
mangoAccount.owner,
),
isWritable: false,// this is a dummy, its just done so that we match flash loan start and end ix
isSigner: false,
}asAccountMeta,
])
.postInstructions(instructions)
.rpc();
// break when success
break;
}catch(error){
console.log(error);
if(
(error.toString()asstring).includes('Transaction too large:')||
(error.toString()asstring).includes(
'encoding overruns Uint8Array',
)||
(error.toString()asstring).includes(
'The value of "offset" is out of range. It must be >= 0 and <= 1231. Received 1232',
)||
(error.toString()asstring).includes(
'The value of "value" is out of range. It must be >= 0 and <= 255. Received',
)||
i>10
){
console.log(`route ${i} was bad, trying next one...`);