Skip to content

Commit 70202c1

Browse files
Merge pull request #765 from yaroslavyaroslav/feat/pod-fix
2 parents 57463b8 + ae022a7 commit 70202c1

File tree

1 file changed

+52
-50
lines changed

1 file changed

+52
-50
lines changed

Sources/Web3Core/EthereumABI/ABIElements.swift

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ extension ABI.Element {
262262

263263
extension ABI.Element.Function {
264264
public func decodeInputData(_ rawData: Data) -> [String: Any]? {
265-
return Web3Core.decodeInputData(rawData, methodEncoding: methodEncoding, inputs: inputs)
265+
return ABIDecoder.decodeInputData(rawData, methodEncoding: methodEncoding, inputs: inputs)
266266
}
267267

268268
/// Decodes data returned by a function call. Able to decode `revert(string)`, `revert CustomError(...)` and `require(expression, string)` calls.
@@ -432,61 +432,63 @@ extension ABI.Element.Function {
432432

433433
extension ABI.Element.Constructor {
434434
public func decodeInputData(_ rawData: Data) -> [String: Any]? {
435-
return Web3Core.decodeInputData(rawData, inputs: inputs)
435+
return ABIDecoder.decodeInputData(rawData, inputs: inputs)
436436
}
437437
}
438438

439-
/// Generic input decoding function.
440-
/// - Parameters:
441-
/// - rawData: data to decode. Must match the following criteria: `data.count == 0 || data.count % 32 == 4`.
442-
/// - methodEncoding: 4 bytes representing method signature like `0xFFffFFff`. Can be omitted to avoid checking method encoding.
443-
/// - inputs: expected input types. Order must be the same as in function declaration.
444-
/// - Returns: decoded dictionary of input arguments mapped to their indices and arguments' names if these are not empty.
445-
/// If decoding of at least one argument fails, `rawData` size is invalid or `methodEncoding` doesn't match - `nil` is returned.
446-
private func decodeInputData(_ rawData: Data,
447-
methodEncoding: Data? = nil,
448-
inputs: [ABI.Element.InOut]) -> [String: Any]? {
449-
let data: Data
450-
let sig: Data?
451-
452-
switch rawData.count % 32 {
453-
case 0:
454-
sig = nil
455-
data = Data()
456-
break
457-
case 4:
458-
sig = rawData[0 ..< 4]
459-
data = Data(rawData[4 ..< rawData.count])
460-
default:
461-
return nil
462-
}
439+
extension ABIDecoder {
440+
/// Generic input decoding function.
441+
/// - Parameters:
442+
/// - rawData: data to decode. Must match the following criteria: `data.count == 0 || data.count % 32 == 4`.
443+
/// - methodEncoding: 4 bytes representing method signature like `0xFFffFFff`. Can be omitted to avoid checking method encoding.
444+
/// - inputs: expected input types. Order must be the same as in function declaration.
445+
/// - Returns: decoded dictionary of input arguments mapped to their indices and arguments' names if these are not empty.
446+
/// If decoding of at least one argument fails, `rawData` size is invalid or `methodEncoding` doesn't match - `nil` is returned.
447+
static func decodeInputData(_ rawData: Data,
448+
methodEncoding: Data? = nil,
449+
inputs: [ABI.Element.InOut]) -> [String: Any]? {
450+
let data: Data
451+
let sig: Data?
452+
453+
switch rawData.count % 32 {
454+
case 0:
455+
sig = nil
456+
data = Data()
457+
break
458+
case 4:
459+
sig = rawData[0 ..< 4]
460+
data = Data(rawData[4 ..< rawData.count])
461+
default:
462+
return nil
463+
}
463464

464-
if methodEncoding != nil && sig != nil && sig != methodEncoding {
465-
return nil
466-
}
465+
if methodEncoding != nil && sig != nil && sig != methodEncoding {
466+
return nil
467+
}
467468

468-
var returnArray = [String: Any]()
469+
var returnArray = [String: Any]()
469470

470-
if data.count == 0 && inputs.count == 1 {
471-
let name = "0"
472-
let value = inputs[0].type.emptyValue
473-
returnArray[name] = value
474-
if inputs[0].name != "" {
475-
returnArray[inputs[0].name] = value
476-
}
477-
} else {
478-
guard inputs.count * 32 <= data.count else { return nil }
479-
480-
var i = 0
481-
guard let values = ABIDecoder.decode(types: inputs, data: data) else {return nil}
482-
for input in inputs {
483-
let name = "\(i)"
484-
returnArray[name] = values[i]
485-
if input.name != "" {
486-
returnArray[input.name] = values[i]
471+
if data.count == 0 && inputs.count == 1 {
472+
let name = "0"
473+
let value = inputs[0].type.emptyValue
474+
returnArray[name] = value
475+
if inputs[0].name != "" {
476+
returnArray[inputs[0].name] = value
477+
}
478+
} else {
479+
guard inputs.count * 32 <= data.count else { return nil }
480+
481+
var i = 0
482+
guard let values = ABIDecoder.decode(types: inputs, data: data) else {return nil}
483+
for input in inputs {
484+
let name = "\(i)"
485+
returnArray[name] = values[i]
486+
if input.name != "" {
487+
returnArray[input.name] = values[i]
488+
}
489+
i = i + 1
487490
}
488-
i = i + 1
489491
}
492+
return returnArray
490493
}
491-
return returnArray
492-
}
494+
}

0 commit comments

Comments
 (0)