primitivefinance / portfolio

Portfolio is an automated market making protocol for implementing custom strategies at the lowest cost possible.

Home Page:https://www.primitive.xyz/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Explore: Remove Alternative Codecs in favor of Public "Jump Instruction" function

Alexangelj opened this issue · comments

Description

  • Alternative codecs have marginal gas improvements, but they have not been fully optimized, making them marginally beneficial.
  • Gut feeling is that the design to get the alternative codecs working adds more gas cost than it saves in the high probability scenarios (one-off actions, or two bundled instructions).
  • Jump instruction is really powerful, but its gated by a complex mechanism to use it
  • This potentially reduces a lot of the contract's size described in here: #209

Tasks

  • Measure gas of each internal function, the gas of the public function wrappers, and the gas of the processing logic.
  • Explore the benefits with respect to swap the most. Swaps are very important, so they should get the optimization.
  • Explore a public function that will process encoded calldata. Only difference is that the function signature is 4 bytes instead of the jump instruction code.

We can also add better error catching. In between processing instructions we can increment an internal nonce for which instruction we are on and throw an error that will bubble up specifically which instruction reverted, and maybe include the data for the reason why.

Spec

Proposed signature for optimized (packed) multi-instruction processing:

function multijump(bytes calldata data) external returns(bytes[] memory results);

An un-optimized version that pads every instruction to 32 bytes:

function multijump(bytes[] calldata data) external returns(bytes[] memory results);

Compare this with regular multicall:

Standard:
function multicall(address[] calldata targets, bytes[] calldata data) external returns(bytes[] memory);

UniswapV3Periphery:
function multicall(bytes[] calldata data) external returns(bytes[] memory results);

This will enable developers to build multiple orders much easier, but there is still difficulty in building the data. The unoptimized version would be the easiest version to use. We can potentially have both public functions if the contract size limit allows it.

The optimized verison would require packed encoding with instructions, while the unoptimized verison can just check the regular function signatures.