@@ -35,7 +35,7 @@ extension Web3 {
3535 public var gasLimit : BigUInt ?
3636 public var gasPrice : BigUInt ?
3737 public var amount : BigUInt ?
38- public var function : Function ?
38+ public var function : ABIv2 . Element . Function ?
3939
4040 public enum TargetAddress {
4141 case ethereumAddress( EthereumAddress )
@@ -54,36 +54,26 @@ extension Web3 {
5454 self . targetAddress = targetAddress
5555 }
5656
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)]
8160//
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+ // }
8576// }
86-
8777 }
8878
8979 public struct EIP681CodeParser {
@@ -100,7 +90,7 @@ extension Web3 {
10090 let striped = string. components ( separatedBy: " ethereum: " )
10191 guard striped. count == 2 else { return nil }
10292 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}
10494 let matcher = try ! NSRegularExpression ( pattern: addressRegex, options: NSRegularExpression . Options. dotMatchesLineSeparators)
10595 let match = matcher. matches ( in: encoding, options: NSRegularExpression . MatchingOptions. anchored, range: encoding. fullNSRange)
10696 guard match. count == 1 else { return nil }
@@ -138,22 +128,30 @@ extension Web3 {
138128 code. functionName = components. path
139129 }
140130 guard let queryItems = components. queryItems else { return code}
131+ var inputNumber : Int = 0
132+ var inputs = [ ABIv2 . Element. InOut] ( )
141133 for comp in queryItems {
142134 if let inputType = try ? ABIv2TypeParser . parseTypeString ( comp. name) {
143135 guard let value = comp. value else { continue }
136+ var nativeValue : AnyObject ? = nil
144137 switch inputType {
145138 case . address:
146139 let val = EIP681Code . TargetAddress ( value)
147- code . parameters . append ( EIP681Code . EIP681Parameter ( type : inputType , value : val as AnyObject ) )
140+ nativeValue = val as AnyObject
148141 case . uint( bits: _) :
149142 if let val = BigUInt ( value, radix: 10 ) {
150- code . parameters . append ( EIP681Code . EIP681Parameter ( type : inputType , value : val as AnyObject ) )
143+ nativeValue = val as AnyObject
151144 } 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
153146 }
154147 default :
155148 continue
156149 }
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+ }
157155 } else {
158156 switch comp. name {
159157 case " value " :
@@ -177,6 +175,12 @@ extension Web3 {
177175 }
178176 }
179177 }
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+
180184 print ( code)
181185 return code
182186 }
0 commit comments