Skip to content

Commit 29a8f10

Browse files
Update README.md
Updated detailed documentation for Contract transaction with ABI string
1 parent 5484e81 commit 29a8f10

File tree

1 file changed

+106
-14
lines changed

1 file changed

+106
-14
lines changed

README.md

Lines changed: 106 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ options.from = walletAddress
141141
options.gasPrice = .automatic
142142
options.gasLimit = .automatic
143143
let tx = contract.write(
144-
"fallback",
145-
parameters: [AnyObject](),
146-
extraData: Data(),
147-
transactionOptions: options)!
144+
"fallback",
145+
parameters: [AnyObject](),
146+
extraData: Data(),
147+
transactionOptions: options)!
148148
```
149149

150150
##### Send ERC-20 Token
@@ -164,10 +164,10 @@ options.gasPrice = .automatic
164164
options.gasLimit = .automatic
165165
let method = "transfer"
166166
let tx = contract.write(
167-
method,
168-
parameters: [toAddress, amount] as [AnyObject],
169-
extraData: Data(),
170-
transactionOptions: options)!
167+
method,
168+
parameters: [toAddress, amount] as [AnyObject],
169+
extraData: Data(),
170+
transactionOptions: options)!
171171
```
172172

173173

@@ -199,12 +199,103 @@ options.from = walletAddress
199199
options.gasPrice = .automatic
200200
options.gasLimit = .automatic
201201
let tx = contract.write(
202-
contractMethod,
203-
parameters: parameters,
204-
extraData: extraData,
205-
transactionOptions: options)!
202+
contractMethod,
203+
parameters: parameters,
204+
extraData: extraData,
205+
transactionOptions: options)!
206206
```
207207

208+
209+
##### Write Transaction with your custom contract ABI
210+
### Requirement : Your custom contract ABI string
211+
```Code
212+
213+
let yourCoin = self.yourbalance.text ?? "0.0" //Get token for sending
214+
let userDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] //get user directory for keystore
215+
if (FileManager.default.fileExists(atPath: userDir + "/keystore/key.json")) {
216+
//Create Keystore
217+
guard let manager = FilestoreWrapper.getKeystoreManager() else {
218+
print("Manager not found ")
219+
return
220+
}
221+
wethioKeystoreManager = manager
222+
guard let urlStr = URL(string: "Your rpc url here") else { return }
223+
guard let kManager = yourKeystoreManager else { return }
224+
/*
225+
Create Web3Provider Instance with key manager
226+
*/
227+
web3ProvideInstance = Web3HttpProvider(urlStr, keystoreManager: kManager)
228+
guard let wProvier = self.web3ProvideInstance else {return}
229+
self.web3Instance = Web3(provider: wProvier) //Set provide instance with web3
230+
guard let wInstance = self.web3Instance else {return}
231+
self.receiverAddressString = self.walletAddressTF.text //get receiver address string
232+
print("Receiver address is : ", self.receiverAddressString ?? " ")
233+
self.etheriumAccountAddress = self.wethioKeystoreManager?.addresses.first?.address //get sender address in string
234+
/*
235+
convert address string into etherium addresss
236+
*/
237+
let senderEthAddress = EthereumAddress(self.etheriumAccountAddress ?? "")
238+
DispatchQueue.global(qos: .background).async {
239+
do {
240+
/*
241+
Convert receiver address in to etherium address
242+
*/
243+
let toaddress = EthereumAddress(self.receiverAddressString ?? "")
244+
/*
245+
Create web3 options
246+
*/
247+
var options = Web3Options.defaultOptions()
248+
/*
249+
Convert amount into BIGINT
250+
*/
251+
let amountDouble = BigInt((Double(yourCoin) ?? 0.1)*pow(10, 18))
252+
/*
253+
Here i am using 0.1 as an default value please use validation for amount (coin or token)
254+
*/
255+
print("Total amount in double value : ", amountDouble)
256+
/*
257+
Convert amount in BIG UI iNt
258+
*/
259+
var amount = BigUInt.init(amountDouble)
260+
/*
261+
get gas price
262+
*/
263+
let estimateGasPrice = try wInstance.eth.getGasPrice()
264+
guard let eGasReult = self.estimatedGasResult else {
265+
print("Unable to find gas price")
266+
return
267+
}
268+
/*
269+
Get nonce
270+
*/
271+
let nonce = try wInstance.eth.getTransactionCount(address: senderEthAddress)
272+
print("Is the Transaction count", nonce)
273+
let fee = estimateGasPrice * eGasReult
274+
/*
275+
adding
276+
- sender address
277+
- Gas Result
278+
- Gas price
279+
- amount
280+
*/
281+
var sendTransactionIntermediateOptions = Web3Options.defaultOptions()
282+
sendTransactionIntermediateOptions.from = senderEthAddress
283+
sendTransactionIntermediateOptions.gasLimit = eGasReult
284+
sendTransactionIntermediateOptions.gasPrice = estimateGasPrice
285+
286+
var tokenTransactionIntermediate: TransactionIntermediate!
287+
tokenTransactionIntermediate = try wInstance.contract("Your custom contract ABI string", at: contractAddress).method("transfer", args: toaddress, amount, options: sendTransactionIntermediateOptions)
288+
289+
let mainTransaction = try tokenTransactionIntermediate.send(options: sendTransactionIntermediateOptions, onBlock: "latest")
290+
291+
print(mainTransaction.hash, "is the hash")
292+
}
293+
```
294+
295+
296+
297+
298+
208299
### Web3View example
209300

210301
You can see how to our demo project: **WKWebView with injected "web3" provider**:
@@ -237,7 +328,7 @@ For full documentation details and FAQ, please look at [Documentation](https://g
237328

238329
*If you need to find or understand an API, check [Usage.md](https://github.com/matter-labs/web3swift/blob/master/Documentation/Usage.md).*
239330

240-
**FAQ moved [Documentation Page](https://github.com/matter-labs/web3swift/blob/master/Documentation/)**
331+
**FAQ moved [Documentation Page](https://github.com/matter-labs/web3swift/blob/master/Documentation/)**
241332

242333
Here are quick references for essential features:
243334

@@ -311,7 +402,7 @@ You are more than welcome to participate! **Your contribution will be paid via
311402

312403
- [ ] Convenient methods for namespaces
313404

314-
405+
315406

316407
## Credits
317408

@@ -327,3 +418,4 @@ If you believe you have identified a security vulnerability with web3swift, you
327418
## License
328419

329420
web3swift is available under the Apache License 2.0 license. See the [LICENSE](https://github.com/matter-labs/web3swift/blob/master/LICENSE) for details.
421+

0 commit comments

Comments
 (0)