Skip to content

Commit 1606def

Browse files
Update Usage.md
1 parent 78fbd28 commit 1606def

File tree

1 file changed

+67
-32
lines changed

1 file changed

+67
-32
lines changed

Documentation/Usage.md

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,10 @@ print("w3s token balance = " + String(bal))
297297

298298
## Transactions Operations
299299

300-
#### Web3Options
300+
301+
### Prepare Transaction
302+
303+
#### The class created to bound provider
301304

302305
```swift
303306
/// A web3 instance bound to provider. All further functionality is provided under web.*. namespaces.
@@ -447,7 +450,16 @@ public class web3: Web3OptionsInheritable {
447450
}
448451
```
449452

450-
### Prepare Transaction
453+
#### Getting Contract By Address
454+
455+
```swift
456+
func contract(for address: String, web3: web3) -> web3.web3contract? {
457+
guard let ethAddress = EthereumAddress(address) else {
458+
return nil
459+
}
460+
return web3.contract(Web3.Utils.erc20ABI, at: ethAddress)
461+
}
462+
```
451463

452464
#### Setting Transaction Options
453465

@@ -466,7 +478,7 @@ options.from = EthereumAddress("0xE6877A4d8806e9A9F12eB2e8561EA6c1db19978d")
466478
#### Preparing Transaction For Sending Ether
467479

468480
```swift
469-
public func prepareTransactionForSendingEther(destinationAddressString: String,
481+
func prepareTransactionForSendingEther(destinationAddressString: String,
470482
amountString: String,
471483
gasLimit: BigUInt,
472484
completion: @escaping (Result<TransactionIntermediate>) -> Void) {
@@ -537,10 +549,10 @@ public func prepareTransactionForSendingEther(destinationAddressString: String,
537549
}
538550
```
539551

540-
#### Preparing Transaction For Sending ERC-20 tokens
552+
#### Preparing Transaction For Sending ERC-20 Tokens
541553

542554
```swift
543-
public func prepareTransactionForSendingERC(destinationAddressString: String,
555+
func prepareTransactionForSendingERC(destinationAddressString: String,
544556
amountString: String,
545557
gasLimit: BigUInt,
546558
tokenAddress token: String,
@@ -623,7 +635,7 @@ public func prepareTransactionForSendingERC(destinationAddressString: String,
623635
#### Preparing Transaction For Sending To Some Contract
624636

625637
```swift
626-
public func prepareTransactionToContract(data: [AnyObject],
638+
func prepareTransactionToContract(data: [AnyObject],
627639
contractAbi: String,
628640
contractAddress: String,
629641
method: String,
@@ -668,31 +680,60 @@ public func prepareTransactionToContract(data: [AnyObject],
668680

669681
### Send Transaction
670682

671-
#### Sending ETH
683+
#### Sending Tokens
672684

673685
```swift
674-
let web3Rinkeby = Web3.InfuraRinkebyWeb3()
675-
web3Rinkeby.addKeystoreManager(bip32keystoreManager) // attach a keystore if you want to sign locally. Otherwise unsigned request will be sent to remote node
676-
options.from = bip32ks?.addresses?.first! // specify from what address you want to send it
677-
intermediateSend = web3Rinkeby.contract(Web3.Utils.coldWalletABI, at: coldWalletAddress, abiVersion: 2)!.method(options: options)! // an address with a private key attached in not different from any other address, just has very simple ABI
678-
let sendResultBip32 = intermediateSend.send(password: "changeme")
686+
func sendToken(transaction: TransactionIntermediate,
687+
with password: String? = "YOURPASSWORD",
688+
options: Web3Options? = nil,
689+
completion: @escaping (Result<TransactionSendingResult>) -> Void) {
690+
DispatchQueue.global(qos: .userInitiated).async {
691+
let result = transaction.send(password: password ?? "YOURPASSWORD",
692+
options: options)
693+
if let error = result.error {
694+
DispatchQueue.main.async {
695+
completion(Result.Error(error))
696+
}
697+
return
698+
}
699+
guard let value = result.value else {
700+
DispatchQueue.main.async {
701+
completion(Result.Error(SendErrors.emptyResult))
702+
}
703+
return
704+
}
705+
DispatchQueue.main.async {
706+
completion(Result.Success(value))
707+
}
708+
}
709+
}
679710
```
680711

681-
#### Sending ERC20
712+
#### Sending To Contract
682713
```swift
683-
var convenienceTransferOptions = Web3Options.defaultOptions()
684-
convenienceTransferOptions.gasPrice = gasPriceRinkeby
685-
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
686-
let gasEstimateResult = convenienceTokenTransfer!.estimateGas(options: nil)
687-
guard case .success(let gasEstimate) = gasEstimateResult else {return}
688-
convenienceTransferOptions.gasLimit = gasEstimate
689-
let convenienceTransferResult = convenienceTokenTransfer!.send(password: "changeme", options: convenienceTransferOptions)
690-
switch convenienceTransferResult {
691-
case .success(let res):
692-
print("Token transfer successful")
693-
print(res)
694-
case .failure(let error):
695-
print(error)
714+
public func sendToContract(transaction: TransactionIntermediate,
715+
with password: String? = "YOURPASSWORD",
716+
options: Web3Options? = nil,
717+
completion: @escaping (Result<TransactionSendingResult>) -> Void) {
718+
DispatchQueue.global(qos: .userInitiated).async {
719+
let result = transaction.send(password: password ?? "YOURPASSWORD",
720+
options: transaction.options)
721+
if let error = result.error {
722+
DispatchQueue.main.async {
723+
completion(Result.Error(error))
724+
}
725+
return
726+
}
727+
guard let value = result.value else {
728+
DispatchQueue.main.async {
729+
completion(Result.Error(SendErrors.emptyResult))
730+
}
731+
return
732+
}
733+
DispatchQueue.main.async {
734+
completion(Result.Success(value))
735+
}
736+
}
696737
}
697738
```
698739

@@ -704,12 +745,6 @@ let gasPriceResult = web3Main.eth.getGasPrice()
704745
guard case .success(let gasPrice) = gasPriceResult else {return}
705746
```
706747

707-
### Serialize And Deserialize transactions
708-
709-
710-
### Get Result
711-
712-
713748
## Chain state
714749

715750
### Get Block number

0 commit comments

Comments
 (0)