I am using the web3swift library and I managed to do some transactions, mostly gets (balanceOf, owner etc). I read the whole readme(documentation), but I am not quite sure, can we use this library to call functions from our custom smart contracts? For example I have store smart contract and I want to call the buy function from it? I saw that we can transfer eth and ERC20 tokens but that is not enough for me. Any help on this?
Asked
Active
Viewed 1,004 times
3
-
1I have the same question and don't see this answered anywhere in the project except briefly discussed here: https://github.com/BANKEX/web3swift/issues/46 I opened an issue here https://github.com/BANKEX/web3swift/issues/188 – mattgabor Jul 15 '18 at 22:57
-
1fyi: @mattgabor the official repo supporters moved it to https://github.com/matterinc/web3swift – skywinder Sep 12 '18 at 03:08
1 Answers
3
Yes, you can call any function on your custom smart contract. Here is an example.
let infura = Web3.InfuraMainnetWeb3()
// 1
let contract = infura.contract(someABI, at: ethContractAddress, abiVersion: 2)
// 2
var options = Web3Options.defaultOptions()
options.from = address
// 3
let transactionIntermediate = contract?.method("accountExists", parameters:[address] as [AnyObject], options: options)
// 4
let result = transactionIntermediate!.call(options: options)
switch result {
// 5
case .success(let res):
let ans = res["0"] as! Bool
DispatchQueue.main.async {
completion(Result.Success(ans))
}
case .failure(let error):
DispatchQueue.main.async {
completion(Result.Error(error))
}
}
}
- Setting up the contract and ABI. You need contract address for this in Data or String format.
let ethContractAddress = EthereumAddress("0xfa28eC7198028438514b49a3CF353BcA5541ce1d")!
You can get the ABI of your contract directly from Remix IDE. - Set up all the options you want.
- Probably one of the main parts of the answer - here you create the transaction with contract method name and put into it all the parameters this method needs.
4.Here you can either call or send the transaction.
call
method is for methods withview
identifier in solidity, so you won't pay for it and method send() is for the methods of smart contract that should be paid with gas for execution. - Here you just parse the result, returned by the method. You should know data types of the variables you want to obtain from the concrete method in order to parse them correctly.
I hope my answer will help you! If something is still unclear - feel free to ask! :)

Georgy Fesenko
- 76
- 4
-
1Thanks for the answer. This works, I managed to do it myself by very similar code to this. – maptuhec Aug 29 '18 at 07:54
-
@maptuhec glad to hear that! could you please accept the answer in this case? :) – skywinder Oct 02 '18 at 01:36
-
Thanks for the answer. How can you know the paramaters types? I'm trying to use a solitidy functions typed like this: function get_data_string_by_id(uint256 tokenId){..} – Jørgen Svennevik Notland Apr 24 '19 at 21:53
-
+ Seems like Web3Options.defaultOptions() is deprecated? https://github.com/matter-labs/web3swift/pull/120 Maybe use: let infura = Web3.InfuraMainnetWeb3() + infura.transactionOptions.from = address – Jørgen Svennevik Notland Apr 24 '19 at 21:56
-
Did you find a solution for uint256, I have the same problem? – Julien Levallois Nov 30 '21 at 00:45
-