Skip to content

Commit dbfecb1

Browse files
Merge pull request #2 from BaldyAsh/erc20fix
Fixed erc20 precompiled contract
2 parents 8c58d11 + 0d4ead1 commit dbfecb1

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

web3swift/PrecompiledContracts/ERC20/Web3+ERC20.swift

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@ import BigInt
99
import EthereumAddress
1010
import PromiseKit
1111

12+
protocol IERC20 {
13+
func getBalance(account: EthereumAddress) throws -> BigUInt
14+
func getAllowance(originalOwner: EthereumAddress, delegate: EthereumAddress) throws -> BigUInt
15+
func transfer(from: EthereumAddress, to: EthereumAddress, amount: String) throws -> WriteTransaction
16+
func transferFrom(from: EthereumAddress, to: EthereumAddress, originalOwner: EthereumAddress, amount: String) throws -> WriteTransaction
17+
func setAllowance(from: EthereumAddress, to: EthereumAddress, newAmount: String) throws -> WriteTransaction
18+
func approve(from: EthereumAddress, spender: EthereumAddress, amount: String) throws -> WriteTransaction
19+
}
20+
1221
// This namespace contains functions to work with ERC20 tokens.
1322
// variables are lazyly evaluated or global token information (name, ticker, total supply)
1423
// can be imperatively read and saved
15-
public class ERC20 {
24+
public class ERC20: IERC20 {
1625

1726
@available(*, deprecated, renamed: "transactionOptions")
1827
public var options: Web3Options = .init()
@@ -99,7 +108,7 @@ public class ERC20 {
99108
}.wait()
100109
}
101110

102-
func getBalance(account: EthereumAddress) throws -> BigUInt{
111+
public func getBalance(account: EthereumAddress) throws -> BigUInt {
103112
let contract = self.contract
104113
var transactionOptions = TransactionOptions()
105114
transactionOptions.callOnBlock = .latest
@@ -185,5 +194,26 @@ public class ERC20 {
185194
return tx
186195
}
187196

197+
public func approve(from: EthereumAddress, spender: EthereumAddress, amount: String) throws -> WriteTransaction {
198+
let contract = self.contract
199+
var basicOptions = TransactionOptions()
200+
basicOptions.from = from
201+
basicOptions.callOnBlock = .latest
202+
203+
// get the decimals manually
204+
let callResult = try contract.read("decimals", transactionOptions: basicOptions)!.call()
205+
var decimals = BigUInt(0)
206+
guard let dec = callResult["0"], let decTyped = dec as? BigUInt else {
207+
throw Web3Error.inputError(desc: "Contract may be not ERC20 compatible, can not get decimals")}
208+
decimals = decTyped
209+
210+
let intDecimals = Int(decimals)
211+
guard let value = Web3.Utils.parseToBigUInt(amount, decimals: intDecimals) else {
212+
throw Web3Error.inputError(desc: "Can not parse inputted amount")
213+
}
214+
215+
let tx = contract.write("approve", parameters: [spender, value] as [AnyObject], transactionOptions: basicOptions)!
216+
return tx
217+
}
188218

189219
}

0 commit comments

Comments
 (0)