@@ -35,7 +35,7 @@ extension Web3 {
35
35
public var gasLimit : BigUInt ?
36
36
public var gasPrice : BigUInt ?
37
37
public var amount : BigUInt ?
38
- public var function : Function ?
38
+ public var function : ABIv2 . Element . Function ?
39
39
40
40
public enum TargetAddress {
41
41
case ethereumAddress( EthereumAddress )
@@ -54,36 +54,26 @@ extension Web3 {
54
54
self . targetAddress = targetAddress
55
55
}
56
56
57
- public struct Function {
58
- public var method : String
59
- public var parameters : [ ( ABIv2 . Element . ParameterType , AnyObject ) ]
60
-
61
- public func toString( ) -> String ? {
62
- let encoding = method + " ( " + parameters. map ( { ( el) -> String in
63
- if let string = el. 1 as? String {
64
- return el. 0 . abiRepresentation + " " + string
65
- } else if let number = el. 1 as? BigUInt {
66
- return el. 0 . abiRepresentation + " " + String( number, radix: 10 )
67
- } else if let number = el. 1 as? BigInt {
68
- return el. 0 . abiRepresentation + " " + String( number, radix: 10 )
69
- } else if let data = el. 1 as? Data {
70
- return el. 0 . abiRepresentation + " " + data. toHexString ( ) . addHexPrefix ( )
71
- }
72
- return " "
73
- } ) . joined ( separator: " , " ) + " ) "
74
- return encoding
75
- }
76
- }
77
-
78
- // public init (address : EthereumAddress) {
79
- // self.address = address
80
- // }
57
+ // public struct Function {
58
+ // public var method: String
59
+ // public var parameters: [(ABIv2.Element.ParameterType, AnyObject)]
81
60
//
82
- // public init? (address : String) {
83
- // guard let addr = EthereumAddress(address) else {return nil}
84
- // self.address = addr
61
+ // public func toString() -> String? {
62
+ // let encoding = method + "(" + parameters.map({ (el) -> String in
63
+ // if let string = el.1 as? String {
64
+ // return el.0.abiRepresentation + " " + string
65
+ // } else if let number = el.1 as? BigUInt {
66
+ // return el.0.abiRepresentation + " " + String(number, radix: 10)
67
+ // } else if let number = el.1 as? BigInt {
68
+ // return el.0.abiRepresentation + " " + String(number, radix: 10)
69
+ // } else if let data = el.1 as? Data {
70
+ // return el.0.abiRepresentation + " " + data.toHexString().addHexPrefix()
71
+ // }
72
+ // return ""
73
+ // }).joined(separator: ", ") + ")"
74
+ // return encoding
75
+ // }
85
76
// }
86
-
87
77
}
88
78
89
79
public struct EIP681CodeParser {
@@ -100,7 +90,7 @@ extension Web3 {
100
90
let striped = string. components ( separatedBy: " ethereum: " )
101
91
guard striped. count == 2 else { return nil }
102
92
guard let encoding = striped [ 1 ] . removingPercentEncoding else { return nil }
103
- guard let url = URL . init ( string: encoding) else { return nil }
93
+ // guard let url = URL.init(string: encoding) else {return nil}
104
94
let matcher = try ! NSRegularExpression ( pattern: addressRegex, options: NSRegularExpression . Options. dotMatchesLineSeparators)
105
95
let match = matcher. matches ( in: encoding, options: NSRegularExpression . MatchingOptions. anchored, range: encoding. fullNSRange)
106
96
guard match. count == 1 else { return nil }
@@ -138,22 +128,30 @@ extension Web3 {
138
128
code. functionName = components. path
139
129
}
140
130
guard let queryItems = components. queryItems else { return code}
131
+ var inputNumber : Int = 0
132
+ var inputs = [ ABIv2 . Element. InOut] ( )
141
133
for comp in queryItems {
142
134
if let inputType = try ? ABIv2TypeParser . parseTypeString ( comp. name) {
143
135
guard let value = comp. value else { continue }
136
+ var nativeValue : AnyObject ? = nil
144
137
switch inputType {
145
138
case . address:
146
139
let val = EIP681Code . TargetAddress ( value)
147
- code . parameters . append ( EIP681Code . EIP681Parameter ( type : inputType , value : val as AnyObject ) )
140
+ nativeValue = val as AnyObject
148
141
case . uint( bits: _) :
149
142
if let val = BigUInt ( value, radix: 10 ) {
150
- code . parameters . append ( EIP681Code . EIP681Parameter ( type : inputType , value : val as AnyObject ) )
143
+ nativeValue = val as AnyObject
151
144
} else if let val = BigUInt ( value. stripHexPrefix ( ) , radix: 16 ) {
152
- code . parameters . append ( EIP681Code . EIP681Parameter ( type : inputType , value : val as AnyObject ) )
145
+ nativeValue = val as AnyObject
153
146
}
154
147
default :
155
148
continue
156
149
}
150
+ if nativeValue != nil {
151
+ inputs. append ( ABIv2 . Element. InOut ( name: String ( inputNumber) , type: inputType) )
152
+ code. parameters. append ( EIP681Code . EIP681Parameter ( type: inputType, value: nativeValue!) )
153
+ inputNumber = inputNumber + 1
154
+ }
157
155
} else {
158
156
switch comp. name {
159
157
case " value " :
@@ -177,6 +175,12 @@ extension Web3 {
177
175
}
178
176
}
179
177
}
178
+
179
+ if code. functionName != nil {
180
+ let functionEncoding = ABIv2 . Element. Function ( name: code. functionName!, inputs: inputs, outputs: [ ABIv2 . Element. InOut] ( ) , constant: false , payable: code. amount != nil )
181
+ code. function = functionEncoding
182
+ }
183
+
180
184
print ( code)
181
185
return code
182
186
}
0 commit comments