Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
b26f3a6
moved keystore manager to web3 main object
odanylovych Dec 29, 2021
d3b410c
renamed attachedKeystoreManager
odanylovych Dec 29, 2021
791a863
added Web3SubscriptionProvider protocol
odanylovych Dec 31, 2021
9368301
some fixes for subscription provider
odanylovych Dec 31, 2021
5659133
Merge branch 'skywinder:develop' into develop
odanylovych Dec 31, 2021
98b0796
optional websocket delegator
odanylovych Jan 4, 2022
21779a2
moved common methods to default websocket provider
odanylovych Jan 4, 2022
d65eae1
merged jsonrpc methods
odanylovych Jan 4, 2022
c716c26
added verification for supported request methods
odanylovych Jan 4, 2022
e0bff8d
implemented sendAsync in WebsocketProvider
odanylovych Jan 5, 2022
a8a2120
implemented sendAsync for batch requests
odanylovych Jan 5, 2022
0ed27cf
added filter requests
odanylovych Jan 6, 2022
400913e
added subscribe methods to Eth
odanylovych Jan 7, 2022
ec49477
set queue when calling subscribe
odanylovych Jan 10, 2022
a3f7b5c
added internalQueue to WebsocketProvider
odanylovych Jan 10, 2022
46abd66
removed unneeded IWebsocketProvider
odanylovych Jan 10, 2022
4e6a096
added queue as param in subscribe
odanylovych Jan 10, 2022
90329c3
added method to convert SubscribeEventFilter to params
odanylovych Jan 10, 2022
9044573
reworked writeTimer to pendingRequests
odanylovych Jan 10, 2022
1ba9885
moved infura specific constructor
odanylovych Jan 11, 2022
4db90c1
removed url session from websocket
odanylovych Jan 11, 2022
bf9997c
fixes for subscribe methods
odanylovych Jan 12, 2022
86088cf
tests for subscribe methods
odanylovych Jan 12, 2022
c338181
added SignatureProvider protocol
odanylovych Jan 12, 2022
9af2fae
added KeystoreSignatureProvider, made SignatureProvider async
odanylovych Jan 12, 2022
1278028
used signature provider in web3
odanylovych Jan 13, 2022
72b5ac1
added methods to Web3Signer
odanylovych Jan 13, 2022
c5f6973
added signature provider to web3.init
odanylovych Jan 13, 2022
e2c5dee
used Promise.wait in sync methods
odanylovych Jan 13, 2022
f6661d2
made browserFunctions async
odanylovych Jan 13, 2022
644552e
added promise wallet methods
odanylovych Jan 13, 2022
ae9458c
SignatureProvider extension with promises
odanylovych Jan 14, 2022
b570545
added init to ErrorMessage, made batch requests public
odanylovych Jan 19, 2022
b19a470
Merge branch 'develop' into signature-provider
odanylovych Jan 19, 2022
69b0a3f
created JSONRPCresponse.Result struct
odanylovych Jan 20, 2022
54a8f71
Merge branch 'develop' into signature-provider
odanylovych Jan 20, 2022
428251d
minor improvement: removed guard statement
odanylovych Feb 23, 2022
2d90894
removed LogItem and used EventLog
odanylovych Feb 24, 2022
54cf7e9
Merge remote-tracking branch 'upstream/develop' into develop
odanylovych May 3, 2022
4067b48
Merge branch 'develop' into signature-provider
odanylovych May 6, 2022
d1add3b
added hash field in BlockHeader struct
odanylovych May 6, 2022
a5e6224
fixed build errors
odanylovych May 6, 2022
2c3cf29
Merge remote-tracking branch 'upstream/develop' into develop
odanylovych May 6, 2022
e94385a
Merge branch 'develop' into signature-provider
odanylovych May 6, 2022
907d3da
included new files in project
odanylovych May 7, 2022
0554482
Merge branch 'develop' into signature-provider
odanylovych May 7, 2022
41cb5e0
included new files in project
odanylovych May 7, 2022
7c90489
updated websockets documentation
odanylovych May 13, 2022
fbb2d1d
Merge branch 'develop' into signature-provider
odanylovych May 13, 2022
4cfa03b
made EthereumTransaction.encode public
odanylovych May 17, 2022
a1fee08
Merge branch 'develop' into signature-provider
odanylovych May 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 110 additions & 45 deletions Documentation/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,32 @@
- [Send Ether](#send-ether)
- [Send ERC-20 Token](#send-erc-20-token)
- [Write Transaction and call smart contract method](#write-transaction-and-call-smart-contract-method)
- [Read Transaction to call smart contract method](#read-transaction-to-call-smart-contract-method)
- [Other Transaction Types (EIP-1559)](#other-transaction-types)
- [Read Transaction from call smart contract method](#read-transaction-from-call-smart-contract-method)
- [Send Transaction](#send-transaction)
- [Write](#write)
- [Read](#read)
- [Other Transaction Types](#other-transaction-types)
- [Chain state](#chain-state)
- [Get Block number](#get-block-number)
- [Websockets](#websockets)
- [Web3socketDelegate](#web3socketdelegate)
- [Custom Websocket Provider](#custom-websocket-provider)
- [Connect to custom endpoint](#connect-to-custom-endpoint)
- [Send message](#send-message)
- [Send request](#send-request)
- [Infura Websocket interactions](#infura-websocket-interactions)
- [Connect to Infura endpoint](#connect-to-infura-endpoint)
- [Connect to custom Infura-like endpoint](#connect-to-custom-infura-like-endpoint)
- [Set a filter in the node to notify when something happened](#set-a-filter-in-the-node-to-notify-when-something-happened)
- [Get new pending transactions](#get-new-pending-transactions)
- [New filter](#new-filter)
- [Get filter changes](#get-filter-changes)
- [Get filter logs](#get-filter-logs)
- [Uninstall filter](#uninstall-filter)
- [Create a new subscription over particular events](#create-a-new-subscription-over-particular-events)
- [Subscribe on new pending transactions](#subscribe-on-new-pending-transactions)
- [Subscribe on logs](https://github.com/matter-labs/web3swift/blob/develop/Documentation/Usage.md#subscribe-on-logs)
- [Subscribe on new heads](https://github.com/matter-labs/web3swift/blob/develop/Documentation/Usage.md#subscribe-on-new-heads)
- [Subscribe on new pending transactions](#subscribe-on-new-pending-transactions)
- [Subscribe on logs](#subscribe-on-logs)
- [Subscribe on new heads](#subscribe-on-new-heads)
- [Subscribe on syncing](#subscribe-on-syncing)
- [Unsubscribe](#unsubscribe)
- [ENS](#ens)
- [Registry](#registry)
- [Resolver](#resolver)
Expand Down Expand Up @@ -402,17 +407,17 @@ let blockNumber = try! web3.eth.getBlockNumber()

### Web3socketDelegate

To receive messages from endpoint you need to create a class that adopts to Web3SocketDelegate protocol.
You can create a class that adopts to Web3SocketDelegate protocol in order to receive notifications about connection state or errors.
Later, to open a connection to WebSocket server, you will use socket provider (`WebsocketProvider` or `InfuraWebsocketProvider`). And we recommend you to make it a property, so it doesn't get deallocated right after being setup.
```swift
class DelegateClass: Web3SocketDelegate {
var socketProvider: WebsocketProvider? = nil // WebSocket Provider
var socketProvider: InfuraWebsocketProvider? = nil // Infura WebSocket Provider

// Protocol method, here will be messages, received from WebSocket server
func received(message: Any) {
// Make something with message
}
public class DelegateClass: Web3SocketDelegate {
public func socketConnected(_ headers: [String : String]) {
// Do something when socket connected
}

public func gotError(error: Error) {
// Do something when got an error from socket connection
}
}
```

Expand All @@ -422,7 +427,7 @@ class DelegateClass: Web3SocketDelegate {

You can create WebsocketProvider and connect/disconnect it.
```swift
socketProvider = WebsocketProvider("ws://your.endpoint", delegate: delegate)
let socketProvider = WebsocketProvider("ws://your.endpoint", delegate: delegate, network: .Mainnet)
socketProvider.connectSocket()
/// Some code
/// ...
Expand All @@ -431,80 +436,140 @@ socketProvider.disconnectSocket()

Or you can create already connected WebsocketProvider
```swift
socketProvider = WebsocketProvider.connectToSocket("ws://your.endpoint", delegate: delegate)
let socketProvider = WebsocketProvider.connectToSocket("ws://your.endpoint", delegate: delegate, network: .Mainnet)
```

#### Send message
#### Send request

```swift
// String message
socketProvider.writeMessage(String())
// Data message
socketProvider.writeMessage(Data())
// One request
socketProvider.sendAsync(<JSONRPCrequest>, queue: <DispatchQueue>).map { response in
// Use response
}

// A batch of requests
socketProvider.sendAsync(<JSONRPCrequestBatch>, queue: <DispatchQueue>).map { responses in
// Use responses
}
```

### Infura Websocket interactions

#### Connect to Infura endpoint

```swift
socketProvider = InfuraWebsocketProvider.connectToInfuraSocket(.Mainnet, delegate: delegate)
let socketProvider = InfuraWebsocketProvider.connectToInfuraSocket(.Mainnet, delegate: delegate)
```

#### Connect to custom Infura-like endpoint

```swift
socketProvider = InfuraWebsocketProvider.connectToSocket("ws://your.endpoint", delegate: delegate)
let socketProvider = InfuraWebsocketProvider.connectToSocket("ws://your.endpoint", delegate: delegate, network: .Mainnet)
```

#### Set a filter in the node to notify when something happened

To study possible filters read [Infura WSS filters documentation](https://infura.io/docs/ethereum/wss/introduction)
To study possible filters read [Infura WSS filters documentation](https://docs.infura.io/infura/networks/ethereum/json-rpc-methods/filter-methods)

```swift
// Use websocket provider
let eth = web3(provider: socketProvider).eth
```

##### New filter

```swift
// Getting logs
try! socketProvider.setFilterAndGetLogs(method: <InfuraWebsocketMethod>, params: <[Encodable]?>)
// Getting changes
try! socketProvider.setFilterAndGetChanges(method: <InfuraWebsocketMethod>, params: <[Encodable]?>)
// New filter
let filterPromise = eth.newFilterPromise(addresses: <[EthereumAddress]>, topics: <[String]>)
// or
let filterPromise = eth.newFilterPromise(addresses: <[EthereumAddress]>, fromBlock: <BlockNumber?>, toBlock: <BlockNumber?>, topics: <[String]>)

// New block filter
let filterPromise = eth.newBlockFilterPromise()

// New pending transactions filter
let filterPromise = eth.newPendingTransactionFilterPromise()
```
Or you can provide parameters in more convenient way:

##### Get filter changes

```swift
// Getting logs
try! socketProvider.setFilterAndGetLogs(method: <InfuraWebsocketMethod>, address: <EthereumAddress?>, fromBlock: <BlockNumber?>, toBlock: <BlockNumber?>, topics: <[String]?>)
// Getting changes
try! socketProvider.setFilterAndGetChanges(method: <InfuraWebsocketMethod>, address: <EthereumAddress?>, fromBlock: <BlockNumber?>, toBlock: <BlockNumber?>, topics: <[String]?>)
filterPromise.map { filterID in
eth.getFilterChangesPromise(filterID: filterID).map { filterChanges in
// Use filterChanges
}
}
```

#### Get new pending transactions
##### Get filter logs

```swift
try! socketProvider.setFilterAndGetLogs(method: .newPendingTransactionFilter)
filterPromise.map { filterID in
eth.getFilterLogsPromise(filterID: filterID).map { filterLogs in
// Use filterLogs
}
}
```

##### Uninstall filter

```swift
filterPromise.map { filterID in
eth.uninstallFilterPromise(filterID: filterID).map { isUninstalled in
// Use isUninstalled
}
}
```

#### Create a new subscription over particular events

To study possible subscriptions read [Infura WSS subscriptions documentation](https://infura.io/docs/ethereum/wss/eth_subscribe)
To study possible subscriptions read [Infura WSS subscriptions documentation](https://docs.infura.io/infura/networks/ethereum/json-rpc-methods/subscription-methods)

```swift
try! socketProvider.subscribe(params: <[Encodable]>)
// Use websocket provider
let eth = web3(provider: socketProvider).eth
```

#### Subscribe on new pending transactions
##### Subscribe on new pending transactions

```swift
try! socketProvider.subscribeOnNewPendingTransactions()
let subscription = try eth.subscribeOnNewPendingTransactions { result in
let transactionHash = try! result.get()
// Use transactionHash
}
```

#### Subscribe on logs
##### Subscribe on logs

```swift
try! socketProvider.subscribeOnLogs(addresses: <[EthereumAddress]?>, topics: <[String]?>)
let subscription = try eth.subscribeOnLogs { result in
let logItem = try! result.get()
// Use logItem
}
```

##### Subscribe on new heads

```swift
let subscription = try eth.subscribeOnNewHeads { result in
let blockHeader = try! result.get()
// Use blockHeader
}
```

##### Subscribe on syncing

```swift
let subscription = try eth.subscribeOnSyncing { result in
let syncingInfo = try! result.get()
// Use syncingInfo
}
```

#### Subscribe on new heads
##### Unsubscribe

```swift
try! socketProvider.subscribeOnNewHeads()
subscription.unsubscribe()
```

## ENS
Expand Down
32 changes: 18 additions & 14 deletions Sources/web3swift/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ open class BrowserViewController: UIViewController {
}, for: "getRPCurl")

self.webView.bridge.register({ (parameters, completion) in
let allAccounts = web3.browserFunctions.getAccounts()
completion(.success(["accounts": allAccounts as Any]))
web3.browserFunctions.getAccounts().done(on: DispatchQueue.main) { allAccounts in
completion(.success(["accounts": allAccounts as Any]))
}
}, for: "eth_getAccounts")

self.webView.bridge.register({ (parameters, completion) in
let coinbase = web3.browserFunctions.getCoinbase()
completion(.success(["coinbase": coinbase as Any]))
web3.browserFunctions.getCoinbase().done(on: DispatchQueue.main) { coinbase in
completion(.success(["coinbase": coinbase as Any]))
}
}, for: "eth_coinbase")

self.webView.bridge.register({ (parameters, completion) in
Expand All @@ -88,12 +90,13 @@ open class BrowserViewController: UIViewController {
completion(.failure(Bridge.JSError(code: 0, description: "Not enough parameters provided")))
return
}
let result = web3.browserFunctions.personalSign(personalMessage!, account: account!)
if result == nil {
completion(.failure(Bridge.JSError(code: 0, description: "Account or data is invalid")))
return
web3.browserFunctions.personalSign(personalMessage!, account: account!).done(on: DispatchQueue.main) { result in
if result == nil {
completion(.failure(Bridge.JSError(code: 0, description: "Account or data is invalid")))
return
}
completion(.success(["signedMessage": result as Any]))
}
completion(.success(["signedMessage": result as Any]))
}, for: "eth_sign")

self.webView.bridge.register({ (parameters, completion) in
Expand All @@ -106,12 +109,13 @@ open class BrowserViewController: UIViewController {
completion(.failure(Bridge.JSError(code: 0, description: "Not enough parameters provided")))
return
}
let result = web3.browserFunctions.signTransaction(transaction!)
if result == nil {
completion(.failure(Bridge.JSError(code: 0, description: "Data is invalid")))
return
web3.browserFunctions.signTransaction(transaction!).done(on: DispatchQueue.main) { result in
if result == nil {
completion(.failure(Bridge.JSError(code: 0, description: "Data is invalid")))
return
}
completion(.success(["signedTransaction": result as Any]))
}
completion(.success(["signedTransaction": result as Any]))
}, for: "eth_signTransaction")
}
}
Expand Down
Loading