Skip to content

Commit 089a77d

Browse files
author
Alex Vlasov
committed
add example of ERC20 transfer in Example
1 parent abbf3a2 commit 089a77d

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

Example/web3swiftExample/web3swiftExample/ViewController.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ViewController: UIViewController {
5353
let constractAddress = EthereumAddress("0x45245bc59219eeaaf6cd3f382e078a461ff9de7b")
5454
let gasPriceResult = web3Main.eth.getGasPrice()
5555
guard case .success(let gasPrice) = gasPriceResult else {return}
56-
var options = Web3Options()
56+
var options = Web3Options.defaultOptions()
5757
options.gasPrice = gasPrice
5858
options.from = EthereumAddress("0xE6877A4d8806e9A9F12eB2e8561EA6c1db19978d")
5959
let parameters = [] as [AnyObject]
@@ -69,6 +69,9 @@ class ViewController: UIViewController {
6969
guard case .success(let bkxBalance) = bkxBalanceResult, let bal = bkxBalance["0"] as? BigUInt else {return}
7070
print("BKX token balance = " + String(bal))
7171

72+
// Test token transfer on Rinkeby
73+
74+
7275
var eip67Data = Web3.EIP67Code.init(address: EthereumAddress("0x6394b37Cf80A7358b38068f0CA4760ad49983a1B"))
7376
eip67Data.gasLimit = BigUInt(21000)
7477
eip67Data.amount = BigUInt("1000000000000000000")
@@ -99,7 +102,7 @@ class ViewController: UIViewController {
99102
print("Address mismatch")
100103
}
101104
guard case .success(let sendingResult) = sendResult else {return}
102-
let txid = sendingResult["txhash"] as? String
105+
let txid = sendingResult["txhash"]
103106
print("On Rinkeby TXid = " + txid!)
104107

105108
//Send ETH on Rinkeby using BIP32 keystore. Should fail due to insufficient balance
@@ -114,6 +117,23 @@ class ViewController: UIViewController {
114117
print(err)
115118
}
116119

120+
121+
guard case .success(let gasPriceRinkeby) = web3Rinkeby.eth.getGasPrice() else {return}
122+
web3Rinkeby.addKeystoreManager(keystoreManager)
123+
var tokenTransferOptions = Web3Options.defaultOptions()
124+
tokenTransferOptions.gasPrice = gasPriceRinkeby
125+
tokenTransferOptions.from = ks?.addresses?.first!
126+
let testToken = web3Rinkeby.contract(Web3.Utils.erc20ABI, at: EthereumAddress("0xa407dd0cbc9f9d20cdbd557686625e586c85b20a"), abiVersion: 2)!
127+
let intermediateForTokenTransfer = testToken.method("transfer", parameters: [EthereumAddress("0x6394b37Cf80A7358b38068f0CA4760ad49983a1B"), BigUInt(1)] as [AnyObject], options: tokenTransferOptions)!
128+
let tokenTransferResult = intermediateForTokenTransfer.send(password: "BANKEXFOUNDATION")
129+
switch tokenTransferResult {
130+
case .success(let res):
131+
print("Token transfer successful")
132+
print(res)
133+
case .failure(let error):
134+
print(error)
135+
}
136+
117137
//Balance on Rinkeby
118138
let balanceResult = web3Rinkeby.eth.getBalance(address: coldWalletAddress)
119139
guard case .success(let balance) = balanceResult else {return}

web3swift.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ s.source = { :git => 'https://github.com/bankex/web3swift.git', :tag =
1414
s.social_media_url = 'https://twitter.com/shamatar'
1515

1616
s.pod_target_xcconfig = {
17-
'SWIFT_VERSION' => '4.0'
17+
'SWIFT_VERSION' => '4.1'
1818
}
1919

2020
s.module_name = 'web3swift'

web3swift/Web3/Classes/Web3+Eth.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extension web3.Eth {
5454
return Result.failure(Web3Error.dataError)
5555
}
5656
let hash = resultString.addHexPrefix().lowercased()
57-
return Result(["txhash": hash, "txhashCalculated" : transaction.hash!.toHexString()] as [String: String])
57+
return Result(["txhash": hash, "txhashCalculated" : transaction.hash!.toHexString().addHexPrefix()] as [String: String])
5858
}
5959
}
6060

@@ -103,7 +103,7 @@ extension web3.Eth {
103103
return Result.failure(Web3Error.dataError)
104104
}
105105
let hash = resultString.addHexPrefix().lowercased()
106-
return Result(["txhash": hash, "txhashCalculated" : transaction.hash!.toHexString()] as [String: String])
106+
return Result(["txhash": hash, "txhashCalculated" : transaction.hash!.toHexString().addHexPrefix()] as [String: String])
107107
}
108108
}
109109

0 commit comments

Comments
 (0)