github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/intabi/abi/abi.go (about)

     1  package abi
     2  
     3  import (
     4  	"github.com/intfoundation/intchain/accounts/abi"
     5  	"github.com/intfoundation/intchain/common"
     6  	"math/big"
     7  	"strings"
     8  )
     9  
    10  type FunctionType struct {
    11  	id    int
    12  	cross bool // Tx type, cross chain / non cross chain
    13  	main  bool // allow to be execute on main chain or not
    14  	child bool // allow to be execute on child chain or not
    15  }
    16  
    17  var (
    18  	// Cross Chain Function
    19  	CreateChildChain       = FunctionType{0, true, true, false}
    20  	JoinChildChain         = FunctionType{1, true, true, false}
    21  	DepositInMainChain     = FunctionType{2, true, true, false}
    22  	DepositInChildChain    = FunctionType{3, true, false, true}
    23  	WithdrawFromChildChain = FunctionType{4, true, false, true}
    24  	WithdrawFromMainChain  = FunctionType{5, true, true, false}
    25  	SaveDataToMainChain    = FunctionType{6, true, true, false}
    26  	SetBlockReward         = FunctionType{7, true, false, true}
    27  	// Non-Cross Chain Function
    28  	VoteNextEpoch  = FunctionType{10, false, true, true}
    29  	RevealVote     = FunctionType{11, false, true, true}
    30  	Delegate       = FunctionType{12, false, true, true}
    31  	UnDelegate     = FunctionType{13, false, true, true}
    32  	Register       = FunctionType{14, false, true, true}
    33  	UnRegister     = FunctionType{15, false, true, true}
    34  	EditValidator  = FunctionType{16, false, true, true}
    35  	WithdrawReward = FunctionType{17, false, true, true}
    36  	UnForbidden    = FunctionType{18, false, true, true}
    37  	SetCommission  = FunctionType{19, false, true, true}
    38  	SetAddress     = FunctionType{20, false, true, true}
    39  	// Unknown
    40  	Unknown = FunctionType{-1, false, false, false}
    41  )
    42  
    43  func (t FunctionType) IsCrossChainType() bool {
    44  	return t.cross
    45  }
    46  
    47  func (t FunctionType) AllowInMainChain() bool {
    48  	return t.main
    49  }
    50  
    51  func (t FunctionType) AllowInChildChain() bool {
    52  	return t.child
    53  }
    54  
    55  func (t FunctionType) RequiredGas() uint64 {
    56  	switch t {
    57  	case CreateChildChain:
    58  		return 42000
    59  	case JoinChildChain:
    60  		return 21000
    61  	case DepositInMainChain:
    62  		return 42000
    63  	case DepositInChildChain:
    64  		return 0
    65  	case WithdrawFromChildChain:
    66  		return 42000
    67  	case WithdrawFromMainChain:
    68  		return 0
    69  	case SaveDataToMainChain:
    70  		return 0
    71  	case VoteNextEpoch:
    72  		return 21000
    73  	case RevealVote:
    74  		return 21000
    75  	case Delegate, UnDelegate, Register, UnRegister:
    76  		return 21000
    77  	case SetBlockReward:
    78  		return 21000
    79  	case EditValidator:
    80  		return 21000
    81  	case WithdrawReward:
    82  		return 21000
    83  	case UnForbidden:
    84  		return 21000
    85  	case SetCommission:
    86  		return 21000
    87  	case SetAddress:
    88  		return 21000
    89  	default:
    90  		return 0
    91  	}
    92  }
    93  
    94  func (t FunctionType) String() string {
    95  	switch t {
    96  	case CreateChildChain:
    97  		return "CreateChildChain"
    98  	case JoinChildChain:
    99  		return "JoinChildChain"
   100  	case DepositInMainChain:
   101  		return "DepositInMainChain"
   102  	case DepositInChildChain:
   103  		return "DepositInChildChain"
   104  	case WithdrawFromChildChain:
   105  		return "WithdrawFromChildChain"
   106  	case WithdrawFromMainChain:
   107  		return "WithdrawFromMainChain"
   108  	case SaveDataToMainChain:
   109  		return "SaveDataToMainChain"
   110  	case VoteNextEpoch:
   111  		return "VoteNextEpoch"
   112  	case RevealVote:
   113  		return "RevealVote"
   114  	case Delegate:
   115  		return "Delegate"
   116  	case UnDelegate:
   117  		return "UnDelegate"
   118  	case Register:
   119  		return "Register"
   120  	case UnRegister:
   121  		return "UnRegister"
   122  	case SetBlockReward:
   123  		return "SetBlockReward"
   124  	case EditValidator:
   125  		return "EditValidator"
   126  	case WithdrawReward:
   127  		return "WithdrawReward"
   128  	case UnForbidden:
   129  		return "UnForbidden"
   130  	case SetCommission:
   131  		return "SetCommission"
   132  	case SetAddress:
   133  		return "SetAddress"
   134  	default:
   135  		return "UnKnown"
   136  	}
   137  }
   138  
   139  func StringToFunctionType(s string) FunctionType {
   140  	switch s {
   141  	case "CreateChildChain":
   142  		return CreateChildChain
   143  	case "JoinChildChain":
   144  		return JoinChildChain
   145  	case "DepositInMainChain":
   146  		return DepositInMainChain
   147  	case "DepositInChildChain":
   148  		return DepositInChildChain
   149  	case "WithdrawFromChildChain":
   150  		return WithdrawFromChildChain
   151  	case "WithdrawFromMainChain":
   152  		return WithdrawFromMainChain
   153  	case "SaveDataToMainChain":
   154  		return SaveDataToMainChain
   155  	case "VoteNextEpoch":
   156  		return VoteNextEpoch
   157  	case "RevealVote":
   158  		return RevealVote
   159  	case "Delegate":
   160  		return Delegate
   161  	case "UnDelegate":
   162  		return UnDelegate
   163  	case "Register":
   164  		return Register
   165  	case "UnRegister":
   166  		return UnRegister
   167  	case "SetBlockReward":
   168  		return SetBlockReward
   169  	case "EditValidator":
   170  		return EditValidator
   171  	case "WithdrawReward":
   172  		return WithdrawReward
   173  	case "UnForbidden":
   174  		return UnForbidden
   175  	case "SetCommission":
   176  		return SetCommission
   177  	case "SetAddress":
   178  		return SetAddress
   179  	default:
   180  		return Unknown
   181  	}
   182  }
   183  
   184  type CreateChildChainArgs struct {
   185  	ChainId          string
   186  	MinValidators    uint16
   187  	MinDepositAmount *big.Int
   188  	StartBlock       *big.Int
   189  	EndBlock         *big.Int
   190  }
   191  
   192  type JoinChildChainArgs struct {
   193  	PubKey    []byte
   194  	ChainId   string
   195  	Signature []byte
   196  }
   197  
   198  type DepositInMainChainArgs struct {
   199  	ChainId string
   200  }
   201  
   202  type DepositInChildChainArgs struct {
   203  	ChainId string
   204  	TxHash  common.Hash
   205  }
   206  
   207  type WithdrawFromChildChainArgs struct {
   208  	ChainId string
   209  }
   210  
   211  type WithdrawFromMainChainArgs struct {
   212  	ChainId string
   213  	Amount  *big.Int
   214  	TxHash  common.Hash
   215  }
   216  
   217  type VoteNextEpochArgs struct {
   218  	VoteHash common.Hash
   219  }
   220  
   221  type RevealVoteArgs struct {
   222  	PubKey    []byte
   223  	Amount    *big.Int
   224  	Salt      string
   225  	Signature []byte
   226  }
   227  
   228  type DelegateArgs struct {
   229  	Candidate common.Address
   230  }
   231  
   232  type UnDelegateArgs struct {
   233  	Candidate common.Address
   234  	Amount    *big.Int
   235  }
   236  
   237  type RegisterArgs struct {
   238  	Pubkey     []byte
   239  	Signature  []byte
   240  	Commission uint8
   241  }
   242  
   243  type SetBlockRewardArgs struct {
   244  	ChainId string
   245  	Reward  *big.Int
   246  }
   247  
   248  type EditValidatorArgs struct {
   249  	Moniker  string
   250  	Website  string
   251  	Identity string
   252  	Details  string
   253  }
   254  
   255  type WithdrawRewardArgs struct {
   256  	DelegateAddress common.Address
   257  	Amount          *big.Int
   258  }
   259  
   260  type UnForbiddenArgs struct {
   261  }
   262  
   263  type SetCommissionArgs struct {
   264  	Commission uint8
   265  }
   266  
   267  type SetAddressArgs struct {
   268  	FAddress common.Address
   269  }
   270  
   271  const jsonChainABI = `
   272  [
   273  	{
   274  		"type": "function",
   275  		"name": "CreateChildChain",
   276  		"constant": false,
   277  		"inputs": [
   278  			{
   279  				"name": "chainId",
   280  				"type": "string"
   281  			},
   282  			{
   283  				"name": "minValidators",
   284  				"type": "uint16"
   285  			},
   286  			{
   287  				"name": "minDepositAmount",
   288  				"type": "uint256"
   289  			},
   290  			{
   291  				"name": "startBlock",
   292  				"type": "uint256"
   293  			},
   294  			{
   295  				"name": "endBlock",
   296  				"type": "uint256"
   297  			}
   298  		]
   299  	},
   300  	{
   301  		"type": "function",
   302  		"name": "JoinChildChain",
   303  		"constant": false,
   304  		"inputs": [
   305  			{
   306  				"name": "pubKey",
   307  				"type": "bytes"
   308  			},
   309  			{
   310  				"name": "chainId",
   311  				"type": "string"
   312  			},
   313  			{
   314  				"name": "signature",
   315  				"type": "bytes"
   316  			}
   317  		]
   318  	},
   319  	{
   320  		"type": "function",
   321  		"name": "DepositInMainChain",
   322  		"constant": false,
   323  		"inputs": [
   324  			{
   325  				"name": "chainId",
   326  				"type": "string"
   327  			}
   328  		]
   329  	},
   330  	{
   331  		"type": "function",
   332  		"name": "DepositInChildChain",
   333  		"constant": false,
   334  		"inputs": [
   335  			{
   336  				"name": "chainId",
   337  				"type": "string"
   338  			},
   339  			{
   340  				"name": "txHash",
   341  				"type": "bytes32"
   342  			}
   343  		]
   344  	},
   345  	{
   346  		"type": "function",
   347  		"name": "WithdrawFromChildChain",
   348  		"constant": false,
   349  		"inputs": [
   350  			{
   351  				"name": "chainId",
   352  				"type": "string"
   353  			}
   354  		]
   355  	},
   356  	{
   357  		"type": "function",
   358  		"name": "WithdrawFromMainChain",
   359  		"constant": false,
   360  		"inputs": [
   361  			{
   362  				"name": "chainId",
   363  				"type": "string"
   364  			},
   365  			{
   366  				"name": "amount",
   367  				"type": "uint256"
   368  			},
   369  			{
   370  				"name": "txHash",
   371  				"type": "bytes32"
   372  			}
   373  		]
   374  	},
   375  	{
   376  		"type": "function",
   377  		"name": "SaveDataToMainChain",
   378  		"constant": false,
   379  		"inputs": [
   380  			{
   381  				"name": "data",
   382  				"type": "bytes"
   383  			}
   384  		]
   385  	},
   386  	{
   387  		"type": "function",
   388  		"name": "VoteNextEpoch",
   389  		"constant": false,
   390  		"inputs": [
   391  			{
   392  				"name": "voteHash",
   393  				"type": "bytes32"
   394  			}
   395  		]
   396  	},
   397  	{
   398  		"type": "function",
   399  		"name": "RevealVote",
   400  		"constant": false,
   401  		"inputs": [
   402  			{
   403  				"name": "pubKey",
   404  				"type": "bytes"
   405  			},
   406  			{
   407  				"name": "amount",
   408  				"type": "uint256"
   409  			},
   410  			{
   411  				"name": "salt",
   412  				"type": "string"
   413  			},
   414  			{
   415  				"name": "signature",
   416  				"type": "bytes"
   417  			}
   418  		]
   419  	},
   420  	{
   421  		"type": "function",
   422  		"name": "Delegate",
   423  		"constant": false,
   424  		"inputs": [
   425  			{
   426  				"name": "candidate",
   427  				"type": "address"
   428  			}
   429  		]
   430  	},
   431  	{
   432  		"type": "function",
   433  		"name": "UnDelegate",
   434  		"constant": false,
   435  		"inputs": [
   436  			{
   437  				"name": "candidate",
   438  				"type": "address"
   439  			},
   440  			{
   441  				"name": "amount",
   442  				"type": "uint256"
   443  			}
   444  		]
   445  	},
   446  	{
   447  		"type": "function",
   448  		"name": "Register",
   449  		"constant": false,
   450  		"inputs": [
   451  			{
   452  				"name": "pubkey",
   453  				"type": "bytes"
   454  			},
   455              {
   456  				"name": "signature",
   457  				"type": "bytes"
   458  			},
   459  			{
   460  				"name": "commission",
   461  				"type": "uint8"
   462  			}
   463  		]
   464  	},
   465  	{
   466  		"type": "function",
   467  		"name": "UnRegister",
   468  		"constant": false,
   469  		"inputs": []
   470  	},
   471  	{
   472  		"type": "function",
   473  		"name": "SetBlockReward",
   474  		"constant": false,
   475  		"inputs": [
   476  			{
   477  				"name": "chainId",
   478  				"type": "string"
   479  			},
   480  			{
   481  				"name": "reward",
   482  				"type": "uint256"
   483  			}
   484  		]
   485  	},
   486  	{
   487  		"type": "function",
   488  		"name": "EditValidator",
   489  		"constant": false,
   490  		"inputs": [
   491  			{
   492  				"name": "moniker",
   493  				"type": "string"
   494  			},
   495  			{
   496  				"name": "website",
   497  				"type": "string"
   498  			},
   499  			{
   500  				"name": "identity",
   501  				"type": "string"
   502  			},
   503  			{
   504  				"name": "details",
   505  				"type": "string"
   506  			}
   507  		]
   508  	},
   509  	{
   510  		"type": "function",
   511  		"name": "WithdrawReward",
   512  		"constant": false,
   513  		"inputs": [
   514  			{
   515  				"name": "delegateAddress",
   516  				"type": "address"
   517  			},
   518  			{
   519  				"name": "amount",
   520  				"type": "uint256"
   521  			}
   522  		]
   523  	},
   524  	{
   525  		"type": "function",
   526  		"name": "UnForbidden",
   527  		"constant": false,
   528  		"inputs": []
   529  	},
   530  	{
   531  		"type": "function",
   532  		"name": "SetCommission",
   533  		"constant": false,
   534  		"inputs": [
   535  			{
   536  				"name": "commission",
   537  				"type": "uint8"
   538  			}
   539  		]
   540  	},
   541  	{
   542  		"type": "function",
   543  		"name": "SetAddress",
   544  		"constant": false,
   545  		"inputs": [
   546  			{
   547  				"name": "fAddress",
   548  				"type": "address"
   549  			}
   550  		]
   551  	}
   552  ]`
   553  
   554  // INT Chain Child Chain Token Incentive Address
   555  var ChildChainTokenIncentiveAddr = common.HexToAddress("0x0000000000000000000000000000000000001002")
   556  
   557  // INT Chain Internal Contract Address
   558  var ChainContractMagicAddr = common.HexToAddress("0x0000000000000000000000000000000000001001") // don't conflict with intchain/core/vm/contracts.go
   559  
   560  var ChainABI abi.ABI
   561  
   562  func init() {
   563  	var err error
   564  	ChainABI, err = abi.JSON(strings.NewReader(jsonChainABI))
   565  	if err != nil {
   566  		panic("fail to create the chain ABI: " + err.Error())
   567  	}
   568  }
   569  
   570  func IsIntChainContractAddr(addr *common.Address) bool {
   571  	return addr != nil && *addr == ChainContractMagicAddr
   572  }
   573  
   574  func FunctionTypeFromId(sigdata []byte) (FunctionType, error) {
   575  	m, err := ChainABI.MethodById(sigdata)
   576  	if err != nil {
   577  		return Unknown, err
   578  	}
   579  
   580  	return StringToFunctionType(m.Name), nil
   581  }