Skip to content

Commit

Permalink
Merge pull request #25 from QOSGroup/develop
Browse files Browse the repository at this point in the history
bug fix #24: depending on the ERC20 token decimals
  • Loading branch information
ewagmig authored Jun 12, 2019
2 parents e381540 + 2b3a0d7 commit 8ee4425
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion litewallet/eth/getAccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func GetAccountERC20(node, addr, tokenAddr string) string {
}
digit := int(decimals)

//format the output in wei
//format the output in digit
fbalance := new(big.Float)
fbalance.SetString(balance.String())
ethValue := new(big.Float).Quo(fbalance, big.NewFloat(math.Pow10(digit)))
Expand Down
31 changes: 25 additions & 6 deletions litewallet/eth/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import (
"context"
"crypto/ecdsa"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"golang.org/x/crypto/sha3"
"log"
"math"
"math/big"
"strconv"
"github.com/QOSGroup/litewallet/litewallet/eth/contracts_erc20"

)

func TransferETH(rootDir, node, fromName, password, toAddr, gasPrice, amount string, GasLimit int64) string {
Expand Down Expand Up @@ -141,17 +145,32 @@ func TransferERC20(rootDir, node, fromName, password, toAddr, tokenAddr, tokenVa
methodID := hash.Sum(nil)[:4]
paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32)

//convert the tokenValue to wei in ERC20
vamount, err := strconv.ParseFloat(tokenValue,64)
//fetch the decimals from the tokenAddress in smart contract
instance, err := contracts_erc20.NewContractsErc20(tokenAddress, client)
if err != nil {
log.Fatal(err)
}
vwei := vamount*1000000000000000000
vstring := strconv.FormatFloat(vwei, 'f', -1, 32)
decimals, err := instance.Decimals(&bind.CallOpts{})
if err != nil {
log.Fatal(err)
}
digit := int(decimals)

//convert the tokenValue to decimals on corresponding ERC20
//vamount, err := strconv.ParseFloat(tokenValue,64)
//if err != nil {
// log.Fatal(err)
//}
//
//vwei := vamount*1000000000000000000
//vstring := strconv.FormatFloat(vdigit.String(), 'f', -1, 32)

vbalance := new(big.Float)
vbalance.SetString(tokenValue)

vdigit := new(big.Float).Mul(vbalance, big.NewFloat(math.Pow10(digit)))
Tamount := new(big.Int)
//1000 token to transfer
Tamount.SetString(vstring,10)
Tamount.SetString(vdigit.String(),10)

paddedAmount := common.LeftPadBytes(Tamount.Bytes(), 32)

Expand Down

0 comments on commit 8ee4425

Please sign in to comment.