Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I fetch information such as balance, decimal,symbol and name of ERC20token ? #199

Closed
DiwakarThapa opened this issue May 21, 2019 · 4 comments

Comments

@DiwakarThapa
Copy link

By changing the method in following code we can fetch the balance , decimal , symbol and name of ERC20Token. Is there any way to fetch all at once instead of changing method all the time?

   if let address = tokenAddress,
            let walletAddress = self.publicAddress {
            let exploredAddress = EthereumAddress(walletAddress)
            let erc20TokenAddress = EthereumAddress(address)
            guard let tokenContract = web3?.contract(Web3.Utils.erc20ABI, at: erc20TokenAddress, abiVersion: 2) else {
                print("error no created token address")
                return
            }
            var options = Web3Options.defaultOptions()
            options.from = EthereumAddress(walletAddress)
            let method = "balanceOf"
            let transactionContract = tokenContract.method(method, parameters: [exploredAddress] as [AnyObject], options: options)
            DispatchQueue.main.async {
                let results =  transactionContract?.call(options: options)
                switch results! {
                    
                case .success(let response):
                    #warning("decimals fetch from contract!")
                    
                                        let tokenBalance = "\(response.map({$0}).first?.value)"
                                        let actualTknBalance = CGFloat(BigUInt(tokenBalance)! / BigUInt(10000000))
                    completion("\(actualTknBalance)")
                    
                    
                case .failure(let failure):
                    print(failure.localizedDescription)
                }
                
            }
            
            
        }
@BaldyAsh
Copy link
Collaborator

Hi, @DiwakarThapa . Sorry, I was on my vacation.
Yes, you can, except balance:

let web3 = Web3.InfuraMainnetWeb3()
let token = ERC20(web3: web3, provider: Web3.InfuraMainnetWeb3().provider, address: EthereumAddress("0x8932404A197D84Ec3Ea55971AADE11cdA1dddff1")!)
token.readProperties()
print(token.decimals)
print(token.symbol)
print(token.name)

For balance use this:

let balance = token.getBalance(account: <EthereumAddress>)

@liuwei-1035
Copy link

HI @BaldyAsh
I user this to get ERC20 balance
let balance = token.getBalance(account: )
in bsc chain It worked
but in gate.io RPC: https://evm.gatenode.cc node: 86, it throw in web3.Eth class callPromise method.
web3swift.Web3Error.nodeError(desc: "{"codespace":"framework","code":1,"message":"gas price cannot be 0"}")
I user method 'Get ERC20 token balance' example it error too
Can you help me how to solve the problem?

@liuwei-1035
Copy link

ContractAddress: 0x669eb09082798178d006ea5526ce0fc12ff42871

@yaroslavyaroslav
Copy link
Collaborator

@PigWithCabbage please open a new one issue about your situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants