-
Notifications
You must be signed in to change notification settings - Fork 4
feature: Added batch instruction #7
base: main
Are you sure you want to change the base?
Conversation
For reference, I have also added an alternative version here called This version uses about ~300 CUs less on our test vectors (from 1594 in @febo's current refactor of this implementation down to 1297) because it dedups the accounts and relies upon local indexes. It also disables the use of multisig signers and only allows the fast subset of instructions to be batched. One downside is that it currently uses a Vec for deduplication. It would perhaps be better to use a VecArray or something. We also have not yet figured out how we would handle CPI, but it would seem for the most part to more or less do what our tests do. |
Added a few improvements and the CU is down to Additionally, completed a CPI test with 3 instructions:
Standard approach (3 CPIs)Executing instruction as a sequence of CPIs: InitializeMint {
mint: &mint,
rent_sysvar: &rent_sysvar,
mint_authority: owner.key(),
freeze_authority: None,
decimals: 2,
}
.invoke()?;
InitializeAccount {
account: &account,
mint: &mint,
owner: &owner,
rent_sysvar: &rent_sysvar,
}
.invoke()?;
MintTo {
mint: &mint,
account: &account,
mint_authority: &owner,
amount: 10,
}
.invoke()?; Total CU Batch approach (1 CPI)Executing 3 instructions in a single CPI: let initialize_mint = InitializeMint {
mint: &mint,
rent_sysvar: &rent_sysvar,
mint_authority: owner.key(),
freeze_authority: None,
decimals: 2,
};
let initialize_account = InitializeAccount {
account: &account,
mint: &mint,
owner: &owner,
rent_sysvar: &rent_sysvar,
};
let mint_to = MintTo {
mint: &mint,
account: &account,
mint_authority: &owner,
amount: 10,
};
unsafe {
Batch {
instructions: &[&initialize_mint, &initialize_account, &mint_to],
}
.invoke_signed_unchecked(&[]);
} Total CU |
The vast majority of CUs being burnt on the Token Program now that it has been cleaned up are on CPI. In the case of an AMM, one of the most common smart contracts on Solana, at a bare minimum, we typically need to perform:
These CPIs each incur a 1000 CU fee. By batching, we could reduce the bare minimum cost by at least 1000 CUs.
How does it work?
We introduce a new instruction with discriminator
255
called "Batch".Batch takes in:
u8
- Counter of how many IXs to batchFor each IX in the counter:
u8
- Discriminator of IXu8 * N
- indices in the &[AccountInfo] of the Batch instruction where each of the accounts used can be mapped, similar to how instructions in transaction messages are encoded on Solana.&[u8]
- Instruction dataThis function will execute the processors of each instruction recursively until either an error occurs, or it reaches the end of the batch counter and returns successfully. By using a single CPI entry to perform multiple commonly executed instructions, we can dramatically optimize the average CU usage of the Token Program even further.