Skip to content

Anatomy of a Crescendo compatible Approval transaction

KillerByte edited this page Nov 15, 2020 · 1 revision

There are 256 bits able to be set in the allowance field set by an ERC20 approval transaction, with the only restriction being that the value of allowance must be larger than the actual withdrawn amount. Each implementation of a crescendo batched transaction must decide their own allocation of the data based on storage requirements and efficiency. Below, an example of how the CrecUniswapAir class stores this data

CrecUniswapAir

Bits Field Description
0-128 amt Amount of tokens to trade
128-159 deadline Trade should be completed before the given block; otherwise, the transaction is invalid
160-175 id Uniswap Pair ID to trade on
176-256 minTradeAmount Minimum amount expected out of the trade (effectively a "limit" order). Otherwise do not execute

0-127: transaction amount 128-160

As an added bonus, since the bottom 128 bits define the transaction amount, the decrement done by transferFrom from allowance value naturally and completely eliminates any concern for a replay.

Example JS code to set crecscendo approval:

function createCrecendoApproval(amt: string, id: number, deadline: number, minTradeAmount = '0') {
  return ethers.utils.parseEther(amt)
    .add(ethers.BigNumber.from(deadline).shl(128))
    .add(ethers.BigNumber.from(id).shl(160))
    .add(ethers.utils.parseEther(minTradeAmount).shl(176));
}
Clone this wiki locally