3.0.0-RC2
Pre-releaseWhat's Changed
- Generic network layer by @yaroslavyaroslav in #577
How to
Using new API is as easy as write three line of a code:
func feeHistory(blockCount: UInt, block: BlockNumber, percentiles:[Double]) async throws -> Web3.Oracle.FeeHistory {
let requestCall: APIRequest = .feeHistory(blockCount, block, percentiles)
let response: APIResponse<Web3.Oracle.FeeHistory> = try await APIRequest.sendRequest(with: web3.provider, for: requestCall) /// explicitly declaring `Result` type is **required**.
return response.result
}
- On the first line you’re creating a request where passing all required and strictly typed parameters.
- On a second you’re both declaring expected
Result
type and making a network request. - On the third one you’re reaching result itself.
And that’s it, you’re done here.
Types overview
There’s follow types have been implemented
Main types
public enum APIRequest
This is the main type of whole network layer API. It responsible to both compose API request to a node and to send it to a node with a given provider (which stores Node URL and session), as cases it have all available JSON RPC requests and most of them have associated values to pass request parameters there.
Additionally it have follow computed properties:
public responseType: APIResultType.Type
- this variable returns appropriateResult
generic parameter type for each API call. Which can be split generally in two parts:- Literals (e.g.
Int
,BigInt
) which could not be extended on client side. - Decodable structures (e.g.
Block
) which could be extended on client side. That said that user able to add additionalResult
type on their side if it’s not literal (e.g. if it’s astruct
orclass
).
- Literals (e.g.
method: REST
- this internal variable returns REST method for each API call. Currently its returning onlyPOST
one.parameters: [RequestParameter]
- this internal variable is purposed to return parameters of request as an heterogeneous Array which is Node expected in most cases.encodedBody: Data
- this internal variable returns encoded data ofRequestBody
type, which is required to compose correct request to a Node.call: String
- this internal variable returns method call string, which is one of property ofRequestBody
type.
There’s two methods are provided for API calls.
public static func sendRequest<Result>(with provider: Web3Provider, for call: APIRequest) async throws -> APIResponse<Result>
- this method is the main one. It composes and sends request to a Node. This method could be called only with explicitly return type declaration likelet response: APIResponse<BigUInt> = try await APIRequest.sendRequest(with: self.provider, for: .gasPrice)
, whereresponse
is the whole APIResponse struct with a service properties and theresponse.result
is a point of interests in example above — gas price.
static func setupRequest(for call: APIRequest, with provider: Web3Provider) -> URLRequest
- this internal method is just composing network request from all properties of relatedAPIRequest
case.
public struct APIResponse<Result>: Decodable where Result: APIResultType
This generic struct is any Ethereum node response container, where all stored properties are utility fields and one generic result: Result
is the property that stores strictly typed result of any given request.
Protocols
To make things work there’s are some protocols be presented which both adds restriction to types that could be passed within new API and add some common methods to Literal types to be able initialize it from a hex string.
APIResultType
This protocol responsible for any nonliteral type that could be stored within APIResponse<Result>
generic struct. This protocol have no requirements except it conforms Decodable
protocol. So any decodable type could be extended to conforms it.
LiteralInitiableFromString
This protocol responsible for any literal types that could be stored within APIResponse<Result>
. This protocol conforms APIResultType
and it adds some requirements to it, like initializer from hex string. Despite that a given type could be extended to implement such initializer ==this should be done on a user side== because to make it work it requires some work within sendRequest
method to be done.
IntegerInitableWithRadix
This protocol is just utility one, which declares some convenient initializer which have both Int
and BigInt
types, but don’t have any common protocol which declares such requirement.
Utility types
struct RequestBody: Encodable
— just a request body that passes into request body.public enum REST: String
— enum of REST methods. OnlyPOST
andGET
presented yet.enum RequestParameter
— this enum is a request composing helper. It make happened to encode request attribute as heterogeneous array.protocol APIRequestParameterType: Encodable
— this type is part of theRequestParameter
enum mechanism which purpose is to restrict types that can be passed as associated types withinRequestParameter
cases.protocol APIRequestParameterElementType: Encodable
— this type purpose is the same asAPIRequestParameterType
one except this one is made forElement
s of anArray
s when the latter is an associated type of a givenRequestParameter
case.
Full Changelog: 3.0.0-RC1...3.0.0-RC2