github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/erc20/types/contract.go (about)

     1  package types
     2  
     3  import (
     4  	_ "embed"
     5  	"encoding/hex"
     6  	"encoding/json"
     7  	"errors"
     8  
     9  	"github.com/ethereum/go-ethereum/accounts/abi"
    10  	"github.com/ethereum/go-ethereum/common"
    11  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    12  	authtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    13  )
    14  
    15  // CompiledContract contains compiled bytecode and abi
    16  type CompiledContract struct {
    17  	ABI abi.ABI
    18  	Bin string
    19  }
    20  
    21  var (
    22  	IbcEvmModuleETHAddr  common.Address
    23  	IbcEvmModuleBechAddr sdk.AccAddress
    24  
    25  	// ModuleERC20Contract is the compiled okc erc20 contract
    26  	ModuleERC20Contract CompiledContract
    27  
    28  	//go:embed contracts/implement.json
    29  	implementationERC20ContractJson []byte
    30  	//go:embed contracts/proxy.json
    31  	proxyERC20ContractJson []byte
    32  )
    33  
    34  const (
    35  	IbcEvmModuleName = "ibc-evm"
    36  
    37  	ContractMintMethod = "mint_by_okc_module"
    38  
    39  	ProxyContractUpgradeTo   = "upgradeTo"
    40  	ProxyContractChangeAdmin = "changeAdmin"
    41  )
    42  
    43  func init() {
    44  	IbcEvmModuleBechAddr = authtypes.NewModuleAddress(IbcEvmModuleName)
    45  	IbcEvmModuleETHAddr = common.BytesToAddress(IbcEvmModuleBechAddr.Bytes())
    46  	MustUnmarshalCompileContract(implementationERC20ContractJson)
    47  	MustUnmarshalCompileContract(proxyERC20ContractJson)
    48  }
    49  
    50  func (c CompiledContract) ValidBasic() error {
    51  	if len(c.Bin) == 0 {
    52  		return errors.New("empty bin data")
    53  	}
    54  	_, err := hex.DecodeString(c.Bin)
    55  	return err
    56  }
    57  
    58  func MustMarshalCompileContract(data CompiledContract) []byte {
    59  	ret, err := MarshalCompileContract(data)
    60  	if nil != err {
    61  		panic(err)
    62  	}
    63  	return ret
    64  }
    65  
    66  func MarshalCompileContract(data CompiledContract) ([]byte, error) {
    67  	return json.Marshal(data)
    68  }
    69  
    70  func MustUnmarshalCompileContract(data []byte) CompiledContract {
    71  	ret, err := UnmarshalCompileContract(data)
    72  	if nil != err {
    73  		panic(err)
    74  	}
    75  	return ret
    76  }
    77  
    78  func UnmarshalCompileContract(data []byte) (CompiledContract, error) {
    79  	var ret CompiledContract
    80  	err := json.Unmarshal(data, &ret)
    81  	if nil != err {
    82  		return CompiledContract{}, err
    83  	}
    84  	return ret, nil
    85  }
    86  
    87  func GetInternalImplementationBytes() []byte {
    88  	return implementationERC20ContractJson
    89  }
    90  
    91  func GetInternalProxyBytes() []byte {
    92  	return proxyERC20ContractJson
    93  }