Skip to content

Commit

Permalink
add example of ERC20 transfer in Example
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Vlasov committed Apr 24, 2018
1 parent abbf3a2 commit 089a77d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
24 changes: 22 additions & 2 deletions Example/web3swiftExample/web3swiftExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ViewController: UIViewController {
let constractAddress = EthereumAddress("0x45245bc59219eeaaf6cd3f382e078a461ff9de7b")
let gasPriceResult = web3Main.eth.getGasPrice()
guard case .success(let gasPrice) = gasPriceResult else {return}
var options = Web3Options()
var options = Web3Options.defaultOptions()
options.gasPrice = gasPrice
options.from = EthereumAddress("0xE6877A4d8806e9A9F12eB2e8561EA6c1db19978d")
let parameters = [] as [AnyObject]
Expand All @@ -69,6 +69,9 @@ class ViewController: UIViewController {
guard case .success(let bkxBalance) = bkxBalanceResult, let bal = bkxBalance["0"] as? BigUInt else {return}
print("BKX token balance = " + String(bal))

// Test token transfer on Rinkeby


var eip67Data = Web3.EIP67Code.init(address: EthereumAddress("0x6394b37Cf80A7358b38068f0CA4760ad49983a1B"))
eip67Data.gasLimit = BigUInt(21000)
eip67Data.amount = BigUInt("1000000000000000000")
Expand Down Expand Up @@ -99,7 +102,7 @@ class ViewController: UIViewController {
print("Address mismatch")
}
guard case .success(let sendingResult) = sendResult else {return}
let txid = sendingResult["txhash"] as? String
let txid = sendingResult["txhash"]
print("On Rinkeby TXid = " + txid!)

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


guard case .success(let gasPriceRinkeby) = web3Rinkeby.eth.getGasPrice() else {return}
web3Rinkeby.addKeystoreManager(keystoreManager)
var tokenTransferOptions = Web3Options.defaultOptions()
tokenTransferOptions.gasPrice = gasPriceRinkeby
tokenTransferOptions.from = ks?.addresses?.first!
let testToken = web3Rinkeby.contract(Web3.Utils.erc20ABI, at: EthereumAddress("0xa407dd0cbc9f9d20cdbd557686625e586c85b20a"), abiVersion: 2)!
let intermediateForTokenTransfer = testToken.method("transfer", parameters: [EthereumAddress("0x6394b37Cf80A7358b38068f0CA4760ad49983a1B"), BigUInt(1)] as [AnyObject], options: tokenTransferOptions)!
let tokenTransferResult = intermediateForTokenTransfer.send(password: "BANKEXFOUNDATION")
switch tokenTransferResult {
case .success(let res):
print("Token transfer successful")
print(res)
case .failure(let error):
print(error)
}

//Balance on Rinkeby
let balanceResult = web3Rinkeby.eth.getBalance(address: coldWalletAddress)
guard case .success(let balance) = balanceResult else {return}
Expand Down
2 changes: 1 addition & 1 deletion web3swift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ s.source = { :git => 'https://github.com/bankex/web3swift.git', :tag =
s.social_media_url = 'https://twitter.com/shamatar'

s.pod_target_xcconfig = {
'SWIFT_VERSION' => '4.0'
'SWIFT_VERSION' => '4.1'
}

s.module_name = 'web3swift'
Expand Down
4 changes: 2 additions & 2 deletions web3swift/Web3/Classes/Web3+Eth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension web3.Eth {
return Result.failure(Web3Error.dataError)
}
let hash = resultString.addHexPrefix().lowercased()
return Result(["txhash": hash, "txhashCalculated" : transaction.hash!.toHexString()] as [String: String])
return Result(["txhash": hash, "txhashCalculated" : transaction.hash!.toHexString().addHexPrefix()] as [String: String])
}
}

Expand Down Expand Up @@ -103,7 +103,7 @@ extension web3.Eth {
return Result.failure(Web3Error.dataError)
}
let hash = resultString.addHexPrefix().lowercased()
return Result(["txhash": hash, "txhashCalculated" : transaction.hash!.toHexString()] as [String: String])
return Result(["txhash": hash, "txhashCalculated" : transaction.hash!.toHexString().addHexPrefix()] as [String: String])
}
}

Expand Down

0 comments on commit 089a77d

Please sign in to comment.