Boilertalk / Web3.swift

A pure swift Ethereum Web3 library
MIT License
642 stars 189 forks source link

Invoking contract methods using a [ABIEncodable] alongside ABIEncodable... parameters #52

Open Nixsm opened 6 years ago

Nixsm commented 6 years ago

Problem

Currently we have this method to invoke methods of a contract:

public subscript(_ name: String) -> ((ABIEncodable...) -> SolidityInvocation)? {
    return methods[name]?.invoke
}

However, variadic functions a bit of pain to work with, in a sense that we can't send an array of parameters, we have to type each parameter separated by commas.

Possible Solution

I suggest we drop the variadic parameter and create a subscript method that receives an array of ABIEncodable and then, create a normal method to be used with variadic parameters, something like this:

// New subscript method
public subscript(_ name: String) -> (([ABIEncodable]) -> SolidityInvocation)? {
    return methods[name]?.invoke
}

public func invokeMethod(with name: String) -> ((ABIEncodable...) -> SolidityInvocation)? {
    return methods[name]?.invoke
}

Of course we'd need to refactor the SolidityInvocation implementations.

Disclaimer

koraykoska commented 6 years ago

I think this should be fine. Is there a reason why we currently use a variadic parameter instead of an array? Just convenience? @pixelmatrix

pixelmatrix commented 6 years ago

I was just trying to emulate a natural way to call these so it actually looks like a function. I totally agree that variadics are a pain to work with if you're not calling them directly though. I think it just depends what we want the default usage to be.

myDynamicContract['getBalance']?(myAddress)

or…

myDynamicContract['getBalance']?([myAddress])
// alternatively…
myDynamicContract.invokeMethod(with: 'getBalance')?(myAddress)

What I was really aiming for though was to support dynamic member subscripts once Swift 4.2 is released so we could do:

myDynamicContract.getBalance?(myAccount)
Nixsm commented 6 years ago

I'll implement this asap and send a PR, thanks for the response guys!

Nixsm commented 6 years ago

I'll leave the default subscript method and add a new invokeMethod to the contract, so we keep the variadic interface working