Skip to content

3.0.0-RC2

Pre-release
Pre-release
Compare
Choose a tag to compare
@yaroslavyaroslav yaroslavyaroslav released this 10 Jun 15:57
· 910 commits to develop since this release
aa155f4

What's Changed

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
}
  1. On the first line you’re creating a request where passing all required and strictly typed parameters.
  2. On a second you’re both declaring expected Result type and making a network request.
  3. 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 appropriate Result 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 additional Result type on their side if it’s not literal (e.g. if it’s a struct or class).
  • method: REST - this internal variable returns REST method for each API call. Currently its returning only POST 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 of RequestBody 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 of RequestBody 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 like let response: APIResponse<BigUInt> = try await APIRequest.sendRequest(with: self.provider, for: .gasPrice), where response is the whole APIResponse struct with a service properties and the response.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 related APIRequest 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. Only POST and GET 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 the RequestParameter enum mechanism which purpose is to restrict types that can be passed as associated types within RequestParameter cases.
  • protocol APIRequestParameterElementType: Encodable — this type purpose is the same as APIRequestParameterType one except this one is made for Elements of an Array s when the latter is an associated type of a given RequestParameter case.

Full Changelog: 3.0.0-RC1...3.0.0-RC2