Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Sources/Solidity/ABIType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,16 @@ public indirect enum ABIType: Equatable, CustomStringConvertible {
case .dynamicBytes:
return "bytes"
case .string:
return "bytes"
return "string"
case .dynamicArray(let type):
return "\(type)[]"
case .tuple(let types):
return types.reduce("", { $0 + $1.description })
var typeString = "("
for type in types {
typeString.append(type.description + ",")
}
typeString.removeLast()
return typeString + ")"
}
}
}
12 changes: 11 additions & 1 deletion Sources/Solidity/ABIValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,17 @@ public indirect enum ABIValue {
let dataLength = string.data(using: .utf8)?.count ?? 0
return 32 + ((dataLength + 31) / 32) * 32
case .dynamicArray(_, let array):
return 32 + array.reduce(0, { $0 + $1.length })
var isDynamic = true
for item in array {
if !item.isDynamic {
isDynamic = false
}
}
if isDynamic {
return 32 + (array.count * 32) + array.reduce(0, { $0 + $1.length })
} else {
return 32 + array.reduce(0, { $0 + $1.length })
}
case .tuple(let array):
return array.reduce(0, { $0 + $1.length })
}
Expand Down
26 changes: 26 additions & 0 deletions Sources/Solidity/ERC20Encoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,30 @@ public final class ERC20Encoder {
try! encoder.encode(function: function, arguments: [tokensBought, maxTokensSold, maxTrxSold, deadline, tokenAddr])
return encoder.data
}

/// Encodes a function call to `queryDIDAddress`
///
/// Solidity function: `function queryDIDAddress(String did) public returns (address);`
public static func encodeQueryDIDAddress(did: String) -> Data {
let function = Function(name: "queryAddress", parameters: [.string])
let encoder = ABIEncoder()
try! encoder.encode(function: function, arguments: [did])
return encoder.data
}

/// Swap V3
public static func encodeSwapExactInput(path: [Address],
poolVersion: [String],
versionLen: [BigUInt],
fees: [BigUInt],
tuple: [Any]) -> Data {
let function = Function(name: "swapExactInput", parameters: [.dynamicArray(.address),
.dynamicArray(.string),
.dynamicArray(.uint(bits: 256)),
.dynamicArray(.uint(bits: 24)),
.tuple([.uint(bits: 256), .uint(bits: 256), .address, .uint(bits: 256)])])
let encoder = ABIEncoder()
try! encoder.encode(function: function, arguments: [path, poolVersion, versionLen, fees, tuple])
return encoder.data
}
}