From f48d8bfadc9fc8c87d1a79b94eaf1563d52bc3f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=94=E8=B7=91=E7=9A=84=E4=BA=8C=E5=8F=89=E6=A0=91?= Date: Fri, 5 Jan 2024 15:08:03 +0800 Subject: [PATCH 1/3] Update ABIType.swift --- Sources/Solidity/ABIType.swift | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Sources/Solidity/ABIType.swift b/Sources/Solidity/ABIType.swift index 3427a75..7543f89 100644 --- a/Sources/Solidity/ABIType.swift +++ b/Sources/Solidity/ABIType.swift @@ -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 + ")" } } } From 9954ce706da39d52619fc896ecec2534d03f6257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=94=E8=B7=91=E7=9A=84=E4=BA=8C=E5=8F=89=E6=A0=91?= Date: Fri, 5 Jan 2024 15:09:50 +0800 Subject: [PATCH 2/3] Update ABIValue.swift --- Sources/Solidity/ABIValue.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sources/Solidity/ABIValue.swift b/Sources/Solidity/ABIValue.swift index 6a1d281..863432c 100644 --- a/Sources/Solidity/ABIValue.swift +++ b/Sources/Solidity/ABIValue.swift @@ -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 }) } From c22c1be6717b8001d73d51ff67e7b5e1d12468b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=94=E8=B7=91=E7=9A=84=E4=BA=8C=E5=8F=89=E6=A0=91?= Date: Fri, 5 Jan 2024 15:11:32 +0800 Subject: [PATCH 3/3] Update ERC20Encoder.swift --- Sources/Solidity/ERC20Encoder.swift | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Sources/Solidity/ERC20Encoder.swift b/Sources/Solidity/ERC20Encoder.swift index b0de8ae..6d999e8 100644 --- a/Sources/Solidity/ERC20Encoder.swift +++ b/Sources/Solidity/ERC20Encoder.swift @@ -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 + } }