MCPcopy Create free account
hub / github.com/wevm/viem / encodeBytes

Function encodeBytes

src/utils/abi/encodeAbiParameters.ts:294–323  ·  view source on GitHub ↗
(
  value: Hex,
  { param }: { param: param },
)

Source from the content-addressed store, hash-verified

292 | ErrorType
293
294function encodeBytes<const param extends AbiParameter>(
295 value: Hex,
296 { param }: { param: param },
297): PreparedParam {
298 const [, paramSize] = param.type.split('bytes')
299 const bytesSize = size(value)
300 if (!paramSize) {
301 let value_ = value
302 // If the size is not divisible by 32 bytes, pad the end
303 // with empty bytes to the ceiling 32 bytes.
304 if (bytesSize % 32 !== 0)
305 value_ = padHex(value_, {
306 dir: 'right',
307 size: Math.ceil((value.length - 2) / 2 / 32) * 32,
308 })
309 return {
310 dynamic: true,
311 encoded: concatHex([
312 padHex(numberToHex(bytesSize, { size: 32 })),
313 value_,
314 ]),
315 }
316 }
317 if (bytesSize !== Number.parseInt(paramSize, 10))
318 throw new AbiEncodingBytesSizeMismatchError({
319 expectedSize: Number.parseInt(paramSize, 10),
320 value,
321 })
322 return { dynamic: false, encoded: padHex(value, { dir: 'right' }) }
323}
324
325type EncodeBoolErrorType = PadHexErrorType | BoolToHexErrorType | ErrorType
326

Callers 1

prepareParamFunction · 0.85

Calls 4

sizeFunction · 0.85
padHexFunction · 0.85
concatHexFunction · 0.85
numberToHexFunction · 0.85

Tested by

no test coverage detected