github.com/ChainSafe/chainbridge-core@v1.4.2/chains/evm/cli/erc20/get-allowance.go (about) 1 package erc20 2 3 import ( 4 "fmt" 5 6 "github.com/ChainSafe/chainbridge-core/chains/evm/calls/contracts/erc20" 7 "github.com/ChainSafe/chainbridge-core/chains/evm/calls/evmtransaction" 8 "github.com/ChainSafe/chainbridge-core/chains/evm/cli/flags" 9 "github.com/ChainSafe/chainbridge-core/chains/evm/cli/initialize" 10 "github.com/ChainSafe/chainbridge-core/chains/evm/cli/logger" 11 "github.com/ChainSafe/chainbridge-core/util" 12 "github.com/ethereum/go-ethereum/common" 13 "github.com/rs/zerolog/log" 14 "github.com/spf13/cobra" 15 ) 16 17 var getAllowanceCmd = &cobra.Command{ 18 Use: "get-allowance", 19 Short: "Get the allowance of a spender for an address", 20 Long: "The get-allowance subcommand returns the allowance of a spender for an address", 21 PreRun: func(cmd *cobra.Command, args []string) { 22 logger.LoggerMetadata(cmd.Name(), cmd.Flags()) 23 }, 24 PersistentPreRunE: func(cmd *cobra.Command, args []string) error { 25 return util.CallPersistentPreRun(cmd, args) 26 }, 27 RunE: func(cmd *cobra.Command, args []string) error { 28 c, err := initialize.InitializeClient(url, senderKeyPair) 29 if err != nil { 30 return err 31 } 32 t, err := initialize.InitializeTransactor(gasPrice, evmtransaction.NewTransaction, c, prepare) 33 if err != nil { 34 return err 35 } 36 return GetAllowanceCmd(cmd, args, erc20.NewERC20Contract(c, Erc20Addr, t)) 37 }, 38 Args: func(cmd *cobra.Command, args []string) error { 39 err := ValidateDepositFlags(cmd, args) 40 if err != nil { 41 return err 42 } 43 return nil 44 }, 45 } 46 47 func BindGetAllowanceFlags(cmd *cobra.Command) { 48 cmd.Flags().StringVar(&Erc20Address, "contract", "", "ERC20 contract address") 49 cmd.Flags().StringVar(&OwnerAddress, "owner", "", "Address of token owner") 50 cmd.Flags().StringVar(&SpenderAddress, "spender", "", "Address of spender") 51 flags.MarkFlagsAsRequired(cmd, "contract", "owner", "spender") 52 } 53 54 func init() { 55 BindGetAllowanceFlags(getAllowanceCmd) 56 } 57 func ValidateGetAllowanceFlags(cmd *cobra.Command, args []string) error { 58 if !common.IsHexAddress(Erc20Address) { 59 return fmt.Errorf("invalid contract address %s", Erc20Address) 60 } 61 if !common.IsHexAddress(OwnerAddress) { 62 return fmt.Errorf("invalid owner address %s", OwnerAddress) 63 } 64 if !common.IsHexAddress(SpenderAddress) { 65 return fmt.Errorf("invalid spender address %s", SpenderAddress) 66 } 67 return nil 68 } 69 70 func GetAllowanceCmd(cmd *cobra.Command, args []string, contract *erc20.ERC20Contract) error { 71 log.Debug().Msgf(` 72 Determing allowance 73 ERC20 address: %s 74 Owner address: %s 75 Spender address: %s`, 76 Erc20Address, OwnerAddress, SpenderAddress) 77 return nil 78 79 /* 80 url := cctx.String("url") 81 gasLimit := cctx.Uint64("gasLimit") 82 gasPrice := cctx.Uint64("gasPrice") 83 sender, err := cliutils.DefineSender(cctx) 84 if err != nil { 85 return err 86 } 87 erc20 := cctx.String("erc20Address") 88 if !common.IsHexAddress(erc20) { 89 return errors.New("invalid erc20Address address") 90 } 91 erc20Address := common.HexToAddress(erc20) 92 93 spender := cctx.String("spender") 94 if !common.IsHexAddress(spender) { 95 return errors.New("invalid spender address") 96 } 97 spenderAddress := common.HexToAddress(spender) 98 99 owner := cctx.String("owner") 100 if !common.IsHexAddress(owner) { 101 return errors.New("invalid owner address") 102 } 103 ownerAddress := common.HexToAddress(owner) 104 105 ethClient, err := client.NewClient(url, false, sender, big.NewInt(0).SetUint64(gasLimit), big.NewInt(0).SetUint64(gasPrice), big.NewFloat(1)) 106 if err != nil { 107 log.Error().Err(fmt.Errorf("eth client intialization error: %v", err)) 108 return err 109 } 110 balance, err := utils.ERC20Allowance(ethClient, erc20Address, spenderAddress, ownerAddress) 111 if err != nil { 112 return err 113 } 114 log.Info().Msgf("allowance of %s to spend from address %s is %s", spenderAddress.String(), ownerAddress.String(), balance.String()) 115 return nil 116 */ 117 }