From 3efe31a920487399b91e9f53d212b6579566eead Mon Sep 17 00:00:00 2001 From: HaoyangLiu Date: Sun, 6 Sep 2020 18:56:15 +0800 Subject: [PATCH] update readme --- README.md | 16 ++++++---------- command/command.go | 42 +++++++++++++++++++++++++----------------- config/config.go | 10 ---------- const/const.go | 1 + script/bind.sh | 35 +++++++++++++++++++---------------- utils/utils.go | 8 ++++++++ 6 files changed, 59 insertions(+), 53 deletions(-) mode change 100644 => 100755 script/bind.sh diff --git a/README.md b/README.md index db359b2..6846b40 100644 --- a/README.md +++ b/README.md @@ -53,26 +53,22 @@ make build You can refer to [BEP20 Template](https://github.com/binance-chain/bsc-genesis-contract/blob/master/contracts/bep20_template/BEP20Token.template) and modify it according to your own requirements. Compile your contract with [Remix](https://remix.ethereum.org) and get contract byte code: ![img](pictures/compile.png) -7. Edit `script/config.json` +7. Edit `script/contract.json` ```json { - "contract_data": "", - "bep20_symbol": "", - "bep2_symbol": "", - "final_bep20_owner": "" + "contract_data": "" } ``` - Fill contract byte code to `contract_data`, fill BEP20 contract symbol to `bep20_symbol`, fill BEP2 token symbol to `bep2_symbol` and fill your owner account to `final_bep20_owner`. Once the bind is success, the BEP20 token ownership will be transfer to your owner account. - -## Bind BEP2 token with BEP20 token + Fill contract byte code to `contract_data` +## Bind BEP2 token with BEP20 token ```shell script - ./bind {network type} {bep2TokenOwnerKeyName} {peggyAmount} {bep2 token symbol} {final token owner} {path to bnbcli or tbnbcli} + ./script/bind.sh {network type} {bep2TokenOwnerKeyName} {password} {peggyAmount} {bep2 token symbol} {token owner} {path to bnbcli or tbnbcli} ``` Example command: ```shell script - ./bind.sh mainnet bep2TokenOwner 0 ETH-2C2 0xaa25Aa7a19f9c426E07dee59b12f944f4d9f1DD3 $HOME/go/bin/bnbcli + ./script/bind.sh testnet bep2TokenOwner "12345678" 0 ABC-83D 0xaa25Aa7a19f9c426E07dee59b12f944f4d9f1DD3 $HOME/go/bin/tbnbcli ``` \ No newline at end of file diff --git a/command/command.go b/command/command.go index 5e80dd2..b02d9c8 100644 --- a/command/command.go +++ b/command/command.go @@ -195,15 +195,18 @@ func ApproveBindAndTransferOwnershipCmd() *cobra.Command { if err != nil { return err } - configPath := viper.GetString(constValue.ConfigPath) - configData, err := config.ReadConfigData(configPath) - if err != nil { - return err - } bep20ContractAddr := viper.GetString(constValue.BEP20ContractAddr) if !strings.HasPrefix(bep20ContractAddr, "0x") || len(bep20ContractAddr) != constValue.BSCAddrLength { return fmt.Errorf("invalid bep20 contract address") } + bep20Owner := viper.GetString(constValue.BEP20Owner) + if utils.ValidatorBSCAddr(bep20Owner) !=nil { + return err + } + bep2Symbol := viper.GetString(constValue.BEP2Symbol) + if len(bep2Symbol) == 0 { + return fmt.Errorf("missing bep2 symbol") + } keystorePath := viper.GetString(constValue.KeystorePath) keyStore, tempAccount, err := generateOrGetTempAccount(keystorePath, chainId) @@ -215,12 +218,13 @@ func ApproveBindAndTransferOwnershipCmd() *cobra.Command { peggyAmount = big.NewInt(0) peggyAmount.SetString(viper.GetString(constValue.PeggyAmount), 10) } - return ApproveBindAndTransferOwnershipAndRestBalanceBackToLedgerAccount(ethClient, keyStore, tempAccount, configData, common.HexToAddress(bep20ContractAddr), peggyAmount, chainId) + return ApproveBindAndTransferOwnershipAndRestBalanceBackToLedgerAccount(ethClient, keyStore, tempAccount, common.HexToAddress(bep20ContractAddr), peggyAmount, bep2Symbol, common.HexToAddress(bep20Owner), chainId) }, } cmd.Flags().String(constValue.KeystorePath, constValue.BindKeystore, "keystore path") - cmd.Flags().String(constValue.ConfigPath, "", "config file path") cmd.Flags().String(constValue.BEP20ContractAddr, "", "bep20 contract address") + cmd.Flags().String(constValue.BEP20Owner, "", "bep20 token owner") + cmd.Flags().String(constValue.BEP2Symbol, "", "bep2 token symbol") cmd.Flags().String(constValue.PeggyAmount, "", "peggy amount, derived from peggy amount in bind transaction on Binance Chain. If bep20 decimals is 18(bep2 token decimals is always 8), peggy amount in bind transfer is 100, then here the peggy amount should be 100*10^10") return cmd } @@ -244,16 +248,20 @@ func DeployBEP20ContractTransferTotalSupplyAndOwnershipCmd() *cobra.Command { if err != nil { return err } + bep20Owner := viper.GetString(constValue.BEP20Owner) + if utils.ValidatorBSCAddr(bep20Owner) !=nil { + return err + } contractAddr, err := DeployContractFromTempAccount(ethClient, keyStore, tempAccount, config.ContractData, chainId) if err != nil { return err } - return TransferTokenAndOwnership(ethClient, keyStore, tempAccount, common.HexToAddress(config.FinalBEP20Owner), contractAddr, chainId) + return TransferTokenAndOwnership(ethClient, keyStore, tempAccount, common.HexToAddress(bep20Owner), contractAddr, chainId) }, } cmd.Flags().String(constValue.KeystorePath, constValue.BindKeystore, "keystore path") cmd.Flags().String(constValue.ConfigPath, "", "config file path") - cmd.Flags().String(constValue.BEP20ContractAddr, "", "bep20 contract address") + cmd.Flags().String(constValue.BEP20Owner, "", "bep20 contract address") return cmd } @@ -349,7 +357,7 @@ func DeployContractFromTempAccount(ethClient *ethclient.Client, keyStore *keysto return contractAddr, nil } -func ApproveBindAndTransferOwnershipAndRestBalanceBackToLedgerAccount(ethClient *ethclient.Client, keyStore *keystore.KeyStore, tempAccount accounts.Account, configData config.Config, bep20ContractAddr common.Address, peggyAmount *big.Int, chainId *big.Int) error { +func ApproveBindAndTransferOwnershipAndRestBalanceBackToLedgerAccount(ethClient *ethclient.Client, keyStore *keystore.KeyStore, tempAccount accounts.Account, bep20ContractAddr common.Address, peggyAmount *big.Int, bep2Symbol string, bep20Owner common.Address, chainId *big.Int) error { bep20Instance, err := bep20.NewBep20(bep20ContractAddr, ethClient) if err != nil { return err @@ -360,7 +368,7 @@ func ApproveBindAndTransferOwnershipAndRestBalanceBackToLedgerAccount(ethClient } var lockAmount *big.Int if peggyAmount == nil { - lockAmount, err = tokenManagerInstance.QueryRequiredLockAmountForBind(utils.GetCallOpts(), configData.BEP2Symbol) + lockAmount, err = tokenManagerInstance.QueryRequiredLockAmountForBind(utils.GetCallOpts(), bep2Symbol) if err != nil { return err } @@ -375,7 +383,7 @@ func ApproveBindAndTransferOwnershipAndRestBalanceBackToLedgerAccount(ethClient } } - fmt.Println(fmt.Sprintf("Approve %s:%s to TokenManager from %s", lockAmount.String(), configData.BEP20Symbol, tempAccount.Address.String())) + fmt.Println(fmt.Sprintf("Approve %s:%s to TokenManager from %s", lockAmount.String(), bep2Symbol, tempAccount.Address.String())) approveTxHash, err := bep20Instance.Approve(utils.GetTransactor(ethClient, keyStore, tempAccount, big.NewInt(0)), constValue.TokenManagerContractAddr, lockAmount) if err != nil { return err @@ -393,7 +401,7 @@ func ApproveBindAndTransferOwnershipAndRestBalanceBackToLedgerAccount(ethClient return err } - approveBindTx, err := tokenManagerInstance.ApproveBind(utils.GetTransactor(ethClient, keyStore, tempAccount, miniRelayerFee), bep20ContractAddr, configData.BEP2Symbol) + approveBindTx, err := tokenManagerInstance.ApproveBind(utils.GetTransactor(ethClient, keyStore, tempAccount, miniRelayerFee), bep20ContractAddr, bep2Symbol) if err != nil { return err } @@ -408,7 +416,7 @@ func ApproveBindAndTransferOwnershipAndRestBalanceBackToLedgerAccount(ethClient fmt.Println("Track approveBind Tx status") if approveBindTxRecipient.Status != 1 { fmt.Println("Approve Bind Failed") - rejectBindTx, err := tokenManagerInstance.RejectBind(utils.GetTransactor(ethClient, keyStore, tempAccount, miniRelayerFee), bep20ContractAddr, configData.BEP2Symbol) + rejectBindTx, err := tokenManagerInstance.RejectBind(utils.GetTransactor(ethClient, keyStore, tempAccount, miniRelayerFee), bep20ContractAddr, bep2Symbol) if err != nil { return err } @@ -430,8 +438,8 @@ func ApproveBindAndTransferOwnershipAndRestBalanceBackToLedgerAccount(ethClient return err } if restBEP20Balance.Cmp(big.NewInt(0)) > 0 { - fmt.Println(fmt.Sprintf("Refund rest BEP20 balance %s to %s", restBEP20Balance.String(), configData.FinalBEP20Owner)) - refundRestBEP20BalanceTxHash, err := bep20Instance.Transfer(utils.GetTransactor(ethClient, keyStore, tempAccount, big.NewInt(0)), common.HexToAddress(configData.FinalBEP20Owner), restBEP20Balance) + fmt.Println(fmt.Sprintf("Refund rest BEP20 balance %s to %s", restBEP20Balance.String(), bep20Owner)) + refundRestBEP20BalanceTxHash, err := bep20Instance.Transfer(utils.GetTransactor(ethClient, keyStore, tempAccount, big.NewInt(0)), bep20Owner, restBEP20Balance) if err != nil { return err } @@ -444,7 +452,7 @@ func ApproveBindAndTransferOwnershipAndRestBalanceBackToLedgerAccount(ethClient return err } fmt.Println(fmt.Sprintf("Transfer ownership to account %s", tempAccount.Address.String())) - transferOwnerShipTxHash, err := ownershipInstance.TransferOwnership(utils.GetTransactor(ethClient, keyStore, tempAccount, big.NewInt(0)), common.HexToAddress(configData.FinalBEP20Owner)) + transferOwnerShipTxHash, err := ownershipInstance.TransferOwnership(utils.GetTransactor(ethClient, keyStore, tempAccount, big.NewInt(0)), bep20Owner) if err != nil { return err } diff --git a/config/config.go b/config/config.go index 4680289..a3a601f 100644 --- a/config/config.go +++ b/config/config.go @@ -6,14 +6,10 @@ import ( "fmt" "io/ioutil" "os" - "strings" ) type Config struct { ContractData string `json:"contract_data"` - BEP20Symbol string `json:"bep20_symbol"` - BEP2Symbol string `json:"bep2_symbol"` - FinalBEP20Owner string `json:"final_bep20_owner"` } func (bindConfig *Config) validate() error { @@ -21,12 +17,6 @@ func (bindConfig *Config) validate() error { if err != nil { return fmt.Errorf("invalid contract byte code: %s", err.Error()) } - if len(bindConfig.BEP2Symbol) == 0 { - return fmt.Errorf("missing bep2 token symbol") - } - if !strings.HasPrefix(bindConfig.FinalBEP20Owner, "0x") || len(bindConfig.FinalBEP20Owner) != 42 { - return fmt.Errorf("invalid BEP20 owner account, expect bsc address, like 0x4E656459ed25bF986Eea1196Bc1B00665401645d") - } return nil } diff --git a/const/const.go b/const/const.go index d02aceb..a8d5caa 100644 --- a/const/const.go +++ b/const/const.go @@ -12,6 +12,7 @@ const ( ConfigPath = "config-path" Operation = "operation" BEP20ContractAddr = "bep20-contract-addr" + BEP20Owner = "bep20-owner" BEP2Symbol = "bep2-symbol" Recipient = "recipient" PeggyAmount = "peggy-amount" diff --git a/script/bind.sh b/script/bind.sh old mode 100644 new mode 100755 index 7f99016..918e63b --- a/script/bind.sh +++ b/script/bind.sh @@ -2,19 +2,21 @@ networkType=$1 bep2TokenOwnerKeyName=$2 -peggyAmount=$3 -bep2TokenSymbol=$4 -tokenOwner=$5 -binaryPath=$6 - -echo "network type:" $networkType -echo "bep2TokenOwnerKeyName:" $bep2TokenOwnerKeyName -echo "token name:" $tokenName -echo "peggy amount:" $peggyAmount -echo "bep2 symbol:" $bep2TokenSymbol -echo "token owner" $tokenOwner - -chmod +x ./binary/* +passwd=$3 +peggyAmount=$4 +bep2TokenSymbol=$5 +tokenOwner=$6 +binaryPath=$7 + +echo "network type: " $networkType +echo "bep2TokenOwnerKeyName: " $bep2TokenOwnerKeyName +echo "passwd: " $passwd +echo "peggy amount: " $peggyAmount +echo "bep2 symbol: " $bep2TokenSymbol +echo "token owner: " $tokenOwner +echo "bnbcli or tbnbcli path: " $binaryPath + +echo "start to bind" chainId="Binance-Chain-Tigris" if [ $networkType == "testnet" ] @@ -27,16 +29,17 @@ then nodeUrl="http://data-seed-pre-0-s3.binance.org:80" fi -./build/token-bind-tool deployContract --config-path script/config.json --network-type $networkType +./build/token-bind-tool deployContract --config-path script/contract.json --network-type $networkType echo "Please input the new deploy bep20 contract address " read bep20ContractAddr -$binaryPath bridge bind --symbol $bep2TokenSymbol --amount $peggyAmount --expire-time `expr $(date +%s) + 3600` \ +echo $passwd | $binaryPath bridge bind --symbol $bep2TokenSymbol --amount $peggyAmount --expire-time `expr $(date +%s) + 3600` \ --contract-decimals 18 --from $bep2TokenOwnerKeyName --chain-id $chainId --contract-address $bep20ContractAddr \ --node $nodeUrl echo "Sleep 10 second" sleep 10 -./build/token-bind-tool approveBindAndTransferOwnership --config-path tokens/$tokenName/$tokenName.json --bep20-contract-addr $bep20ContractAddr --network-type $networkType --peggy-amount $peggyAmount +./build/token-bind-tool approveBindAndTransferOwnership --bep20-contract-addr $bep20ContractAddr \ +--network-type $networkType --peggy-amount $peggyAmount --bep2-symbol $bep2TokenSymbol --bep20-owner $tokenOwner diff --git a/utils/utils.go b/utils/utils.go index e1f6a11..8b81b37 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "math/big" + "strings" "time" bindconst "github.com/binance-chain/token-bind-tool/const" @@ -179,3 +180,10 @@ func SendTransactionFromLedger(rpcClient *ethclient.Client, wallet accounts.Wall } return signTx, rpcClient.SendTransaction(context.Background(), signTx) } + +func ValidatorBSCAddr(addr string) error { + if !strings.HasPrefix(addr, "0x") || len(addr) != 42 { + return fmt.Errorf("invalid BEP20 owner account, expect bsc address, like 0x4E656459ed25bF986Eea1196Bc1B00665401645d") + } + return nil +} \ No newline at end of file