safe-core-sdk icon indicating copy to clipboard operation
safe-core-sdk copied to clipboard

Decode MultiSend transactions into MetaTransactionData[]

Open germartinez opened this issue 1 year ago • 0 comments

Currently MultiSend transactions can only be encoded in the safe-core-sdk. It would be a nice to have to allow decoding transactions in the safe-core-sdk as well.

Currently only this option to decode is available in the safe-service-client, but it does not return a MetaTransactionData[]:

const decodedData = await safeService.decodeData(data)

Use this code from Manu as a reference:

export const decodeMultiSendTxs = (data: string): MetaTransactionData[] => {
  const multiSendInterface = new ethers.utils.Interface([
    'function multiSend(bytes memory transactions) public payable ',
  ])
  const abiCoder = new ethers.utils.AbiCoder()
  // decode multiSend and remove '0x'
  let remainingData = multiSendInterface.decodeFunctionData('multiSend', data)[0].slice(2)

  const txs: MetaTransactionData[] = []
  while (remainingData.length > 0) {
    const txDataEncoded = ethers.utils.hexZeroPad(`0x${remainingData.slice(2, 170)}`, 32 * 3)
    const [txTo, txValue, txDataByteLength] = abiCoder.decode(['address', 'uint256', 'uint256'], txDataEncoded)
    remainingData = remainingData.slice(170)

    const dataLength = (txDataByteLength as BigNumber).toNumber() * 2
    let txData = `0x${remainingData.slice(0, dataLength)}`
    remainingData = remainingData.slice(dataLength)
    txs.push({
      to: txTo.toString(),
      value: txValue.toString(),
      data: txData,
      operation: 0,
    })
  }

  return txs
}

germartinez avatar Nov 18 '22 14:11 germartinez