Function ethers_core::abi::encode_packed
source · pub fn encode_packed(tokens: &[Token]) -> Result<Vec<u8>, EncodePackedError>
Expand description
Encodes the given tokens into an ABI compliant vector of bytes.
This function uses non-standard packed mode, where:
- types shorter than 32 bytes are concatenated directly, without padding or sign extension;
- dynamic types are encoded in-place and without the length;
- array elements are padded, but still encoded in-place.
Since this encoding is ambiguous, there is no decoding function.
Note that this function has the same behaviour as its Solidity counterpart, and thus structs as well as nested arrays are not supported.
Uint
and Int
tokens will be encoded using the least number of bits, so no padding will be
added by default.
§Examples
Calculate the UniswapV2 pair address for two ERC20 tokens:
let factory: Address = "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f".parse()?;
let token_a: Address = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48".parse()?;
let token_b: Address = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2".parse()?;
let encoded = abi::encode_packed(&[Token::Address(token_a), Token::Address(token_b)])?;
let salt = utils::keccak256(encoded);
let init_code_hash: H256 = "0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f".parse()?;
let pair = utils::get_create2_address_from_hash(factory, salt, init_code_hash);
let weth_usdc = "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc".parse()?;
assert_eq!(pair, weth_usdc);