|
24 | 24 | + [CocoaPods](#cocoapods)
|
25 | 25 | + [Carthage](#carthage)
|
26 | 26 | * [Example Project](#example-project)
|
| 27 | + * [Popular questions](#popular-questions) |
27 | 28 | * [Credits](#credits)
|
28 | 29 | + [Security Disclosure](#security-disclosure)
|
29 | 30 | * [Donations](#donations)
|
@@ -149,6 +150,61 @@ You can try lib by running the example project:
|
149 | 150 | - Install Dependencies: `pod install`
|
150 | 151 | - Open: `open ./web3swiftExample.xcworkspace`
|
151 | 152 |
|
| 153 | +## Popular questions |
| 154 | + |
| 155 | +#### How to interact with custom smart-contract with web3swift? |
| 156 | + |
| 157 | +For example: you want to interact with smart-contract and all you know is - its address (address example: 0xfa28eC7198028438514b49a3CF353BcA5541ce1d). |
| 158 | + |
| 159 | +You can get the ABI of your contract directly from [Remix IDE](https://remix.ethereum.org/) ([Solution](https://ethereum.stackexchange.com/questions/27536/where-to-find-contract-abi-in-new-version-of-online-remix-solidity-compiler?rq=1)) |
| 160 | + |
| 161 | +Then you should use contract address and ABI in creating contract object: |
| 162 | +```swift |
| 163 | +let contract = Web3.InfuraMainnetWeb3().contract(<abiString: String>, at: <EthereumAddress?>, abiVersion: <Int>) |
| 164 | +``` |
| 165 | +To create transaction you should call some contract method: |
| 166 | +```swift |
| 167 | +let transaction = contract.method(<method: String>, parameters: <[AnyObject]>, extraData: <Data>, options: <Web3Options?>) |
| 168 | +``` |
| 169 | + |
| 170 | +Here is the function example that creates TransactionIntermediate object, that you can send to smart-contract: |
| 171 | +```swift |
| 172 | +let yourContractABI = """ |
| 173 | +<CONTRACT JSON ABI> |
| 174 | +""" |
| 175 | + |
| 176 | +func prepareTransaction(parameters: Data, gasLimit: BigUInt = 27500, completion: @escaping (Result<TransactionIntermediate>) -> Void) { |
| 177 | + DispatchQueue.global().async { |
| 178 | + guard let addressFrom: String = <YOURS WALLET ADDRESS> else {return} |
| 179 | + guard let ethAddressFrom = EthereumAddress(addressFrom) else {return} |
| 180 | + |
| 181 | + let yourContractAddress = "<CONTRACT ETH ADDRESS>" |
| 182 | + guard let ethContractAddress = EthereumAddress(contractAddress) else {return} |
| 183 | + |
| 184 | + let web3 = Web3.InfuraMainnetWeb3() //or any test network |
| 185 | + web3.addKeystoreManager(KeystoreManager.defaultManager) |
| 186 | + |
| 187 | + var options = Web3Options.defaultOptions() |
| 188 | + options.from = ethAddressFrom |
| 189 | + options.value = 0 // or any other value you want to send |
| 190 | + |
| 191 | + guard let contract = web3.contract(yourContractJsonABI, at: ethContractAddress, abiVersion: 2) else {return} |
| 192 | + |
| 193 | + guard let gasPrice = web3.eth.getGasPrice().value else {return} |
| 194 | + options.gasPrice = gasPrice |
| 195 | + options.gasLimit = gasLimit |
| 196 | + |
| 197 | + guard let transaction = contract.method("<METHOD OF CONTRACT YOU WANT TO CALL>", parameters: [parameters] as [AnyObject], options: options) else {return} |
| 198 | + guard case .success(let estimate) = transaction.estimateGas(options: options) else {return} //here is estimated gas - something like confirming that you made a transaction correctly |
| 199 | + print("estimated cost: \(estimate)") |
| 200 | + |
| 201 | + DispatchQueue.main.async { |
| 202 | + completion(Result.Success(transaction)) |
| 203 | + } |
| 204 | + } |
| 205 | +} |
| 206 | +``` |
| 207 | + |
152 | 208 | ## Credits
|
153 | 209 |
|
154 | 210 | Alex Vlasov, [@shamatar](https://github.com/shamatar), [email protected]
|
|
0 commit comments