Skip to content

Commit 7d78b72

Browse files
author
Alex Vlasov
committed
improve the readme
1 parent 5b7f258 commit 7d78b72

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ fastlane/Preview.html
6666
fastlane/screenshots
6767
fastlane/test_output
6868
API_keys.plist
69+
web3swiftTests/key.json

README.md

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -129,55 +129,63 @@ $ pod install
129129
Here you can see a few examples of use of our library
130130
### Initializing Ethereum address
131131
```bash
132-
let coldWalletAddress = EthereumAddress("0x6394b37Cf80A7358b38068f0CA4760ad49983a1B")!
133-
let constractAddress = EthereumAddress("0x45245bc59219eeaaf6cd3f382e078a461ff9de7b")!
132+
let coldWalletAddress = EthereumAddress("0x6394b37Cf80A7358b38068f0CA4760ad49983a1B")
133+
let constractAddress = EthereumAddress("0x45245bc59219eeaaf6cd3f382e078a461ff9de7b")
134134
```
135+
Ethereum addresses are checksum checked if they are not lowercased and always length checked
135136

137+
138+
### Setting options
139+
```bash
140+
var options = Web3Options.defaultOptions()
141+
// public var to: EthereumAddress? = nil - to what address transaction is aimed
142+
// public var from: EthereumAddress? = nil - form what address it should be sent (either signed locally or on the node)
143+
// public var gasLimit: BigUInt? = BigUInt(90000) - default gas limit
144+
// public var gasPrice: BigUInt? = BigUInt(5000000000) - default gas price, quite small
145+
// public var value: BigUInt? = BigUInt(0) - amount of WEI sent along the transaction
146+
options.gasPrice = gasPrice
147+
options.gasLimit = gasLimit
148+
options.from = EthereumAddress("0xE6877A4d8806e9A9F12eB2e8561EA6c1db19978d")
149+
```
150+
### Getting ETH balance
151+
```bash
152+
let address = EthereumAddress("0xE6877A4d8806e9A9F12eB2e8561EA6c1db19978d")!
153+
let web3Main = Web3.InfuraMainnetWeb3()
154+
let balanceResult = web3Main.eth.getBalance(address)
155+
guard case .success(let balance) = balanceResult else {return}
156+
```
136157
### Getting gas price
137158
```bash
138159
let web3Main = Web3.InfuraMainnetWeb3()
139160
let gasPriceResult = web3Main.eth.getGasPrice()
140161
guard case .success(let gasPrice) = gasPriceResult else {return}
141162
```
142-
### Setting options
143-
```bash
144-
var options = Web3Options.defaultOptions()
145-
options.gasPrice = gasPrice
146-
options.from = EthereumAddress("0xE6877A4d8806e9A9F12eB2e8561EA6c1db19978d")!
147-
let parameters = [] as [AnyObject]
148-
```
149-
150-
### Getting balance
163+
### Getting ERC20 token balance
151164
```bash
152-
guard let bkxBalanceResult = contract.method("balanceOf", parameters: [coldWalletAddress] as [AnyObject], options: options)?.call(options: nil) else {return}
153-
guard case .success(let bkxBalance) = bkxBalanceResult, let bal = bkxBalance["0"] as? BigUInt else {return}
165+
let contractAddress = EthereumAddress("0x45245bc59219eeaaf6cd3f382e078a461ff9de7b")! // BKX token on Ethereum mainnet
166+
let contract = web3.contract(Web3.Utils.erc20ABI, at: contractAddress, abiVersion: 2)! // utilize precompiled ERC20 ABI for your concenience
167+
guard let bkxBalanceResult = contract.method("balanceOf", parameters: [coldWalletAddress] as [AnyObject], options: options)?.call(options: nil) else {return} // encode parameters for transaction
168+
guard case .success(let bkxBalance) = bkxBalanceResult, let bal = bkxBalance["0"] as? BigUInt else {return} // bkxBalance is [String: Any], and parameters are enumerated as "0", "1", etc in order of being returned. If returned parameter has a name in ABI, it is also duplicated
154169
print("BKX token balance = " + String(bal))
155170
```
156171
157172
### Sending ETH
158173
```bash
159174
let web3Rinkeby = Web3.InfuraRinkebyWeb3()
160-
161-
web3Rinkeby.addKeystoreManager(bip32keystoreManager)
162-
options.from = bip32ks?.addresses?.first!
175+
web3Rinkeby.addKeystoreManager(bip32keystoreManager) // attach a keystore if you want to sign locally. Otherwise unsigned request will be sent to remote node
176+
options.from = bip32ks?.addresses?.first! // specify from what address you want to send it
163177
intermediateSend = web3Rinkeby.contract(coldWalletABI, at: coldWalletAddress, abiVersion: 2)!.method(options: options)!
164178
let sendResultBip32 = intermediateSend.send(password: "BANKEXFOUNDATION")
165-
switch sendResultBip32 {
166-
case .success(let r):
167-
print(r)
168-
case .failure(let err):
169-
print(err)
170-
}
171179
```
172180
173181
### Sending ERC20
174182
```bash
175183
var convenienceTransferOptions = Web3Options.defaultOptions()
176184
convenienceTransferOptions.gasPrice = gasPriceRinkeby
177-
let convenienceTokenTransfer = web3Rinkeby.eth.sendERC20tokensWithNaturalUnits(tokenAddress: EthereumAddress("0xa407dd0cbc9f9d20cdbd557686625e586c85b20a")!, from: (ks?.addresses?.first!)!, to: EthereumAddress("0x6394b37Cf80A7358b38068f0CA4760ad49983a1B")!, amount: "0.0001", options: convenienceTransferOptions)
178-
let gasEstimateResult2 = convenienceTokenTransfer!.estimateGas(options: nil)
179-
guard case .success(let gasEstimate2) = gasEstimateResult2 else {return}
180-
convenienceTransferOptions.gasLimit = gasEstimate2
185+
let convenienceTokenTransfer = web3Rinkeby.eth.sendERC20tokensWithNaturalUnits(tokenAddress: EthereumAddress("0xa407dd0cbc9f9d20cdbd557686625e586c85b20a")!, from: (ks?.addresses?.first!)!, to: EthereumAddress("0x6394b37Cf80A7358b38068f0CA4760ad49983a1B")!, amount: "0.0001", options: convenienceTransferOptions) // there are also convenience functions to send ETH and ERC20 under the .eth structure
186+
let gasEstimateResult = convenienceTokenTransfer!.estimateGas(options: nil)
187+
guard case .success(let gasEstimate) = gasEstimateResult else {return}
188+
convenienceTransferOptions.gasLimit = gasEstimate
181189
let convenienceTransferResult = convenienceTokenTransfer!.send(password: "BANKEXFOUNDATION", options: convenienceTransferOptions)
182190
switch convenienceTransferResult {
183191
case .success(let res):

0 commit comments

Comments
 (0)