github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/pkg/stafidecoder/customType.go (about)

     1  package stafi_decoder
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  
     7  	"github.com/itering/scale.go/utiles"
     8  )
     9  
    10  func newStruct(names, typeString []string) *TypeMapping {
    11  	if len(names) != len(typeString) {
    12  		panic("init newStruct names and typeString length not equal")
    13  	}
    14  	if len(names) == 0 {
    15  		return nil
    16  	}
    17  	return &TypeMapping{Names: names, Types: typeString}
    18  }
    19  
    20  func RegCustomTypes(registry map[string]TypeStruct) {
    21  	for key, typeStruct := range registry {
    22  		key = strings.ToLower(key)
    23  		switch typeStruct.Type {
    24  		case "string":
    25  			typeString := typeStruct.TypeString
    26  			instant := TypeRegistry[strings.ToLower(typeString)]
    27  			if instant != nil {
    28  				regCustomKey(key, instant)
    29  				continue
    30  			}
    31  
    32  			// Explained
    33  			if explainedType, ok := registry[typeString]; ok {
    34  				if explainedType.Type == "string" {
    35  					instant := TypeRegistry[strings.ToLower(explainedType.TypeString)]
    36  					if instant != nil {
    37  						regCustomKey(key, instant)
    38  						continue
    39  					}
    40  				} else {
    41  					RegCustomTypes(map[string]TypeStruct{key: registry[typeString]})
    42  				}
    43  			}
    44  
    45  			// sub type vec|option
    46  			if typeString[len(typeString)-1:] == ">" {
    47  				reg := regexp.MustCompile("^([^<]*)<(.+)>$")
    48  				typeParts := reg.FindStringSubmatch(typeString)
    49  				if len(typeParts) > 2 {
    50  					if strings.ToLower(typeParts[1]) == "vec" {
    51  						v := Vec{}
    52  						v.SubType = typeParts[2]
    53  						regCustomKey(key, &v)
    54  						continue
    55  					} else if strings.ToLower(typeParts[1]) == "option" {
    56  						v := Option{}
    57  						v.SubType = typeParts[2]
    58  						regCustomKey(key, &v)
    59  						continue
    60  					} else if strings.ToLower(typeParts[1]) == "compact" {
    61  						v := Compact{}
    62  						v.SubType = typeParts[2]
    63  						regCustomKey(key, &v)
    64  						continue
    65  					}
    66  				}
    67  
    68  			}
    69  
    70  			// Tuple
    71  			if typeString != "()" && string(typeString[0]) == "(" && typeString[len(typeString)-1:] == ")" {
    72  				s := Struct{}
    73  				s.TypeString = typeString
    74  				s.buildStruct()
    75  				regCustomKey(key, &s)
    76  				continue
    77  			}
    78  
    79  			// Array
    80  			if typeString != "[]" && string(typeString[0]) == "[" && string(typeString[len(typeString)-1:]) == "]" {
    81  				if typePart := strings.Split(string(typeString[1:len(typeString)-1]), ";"); len(typePart) == 2 {
    82  					fixed := FixedLengthArray{
    83  						FixedLength: utiles.StringToInt(strings.TrimSpace(typePart[1])),
    84  						SubType:     strings.TrimSpace(typePart[0]),
    85  					}
    86  					regCustomKey(key, &fixed)
    87  					continue
    88  				}
    89  			}
    90  		case "struct":
    91  			var names, typeStrings []string
    92  			for _, v := range typeStruct.TypeMapping {
    93  				names = append(names, v[0])
    94  				typeStrings = append(typeStrings, v[1])
    95  			}
    96  			s := Struct{}
    97  			s.TypeMapping = newStruct(names, typeStrings)
    98  
    99  			regCustomKey(key, &s)
   100  		case "enum":
   101  			var names, typeStrings []string
   102  			for _, v := range typeStruct.TypeMapping {
   103  				names = append(names, v[0])
   104  				typeStrings = append(typeStrings, v[1])
   105  			}
   106  			e := Enum{ValueList: typeStruct.ValueList}
   107  			e.TypeMapping = newStruct(names, typeStrings)
   108  			regCustomKey(key, &e)
   109  		case "set":
   110  			regCustomKey(key, &Set{ValueList: typeStruct.ValueList, BitLength: typeStruct.BitLength})
   111  		}
   112  	}
   113  }
   114  
   115  func regCustomKey(key string, rt interface{}) {
   116  	slice := strings.Split(key, "#")
   117  	if len(slice) == 2 { // for Special
   118  		special := Special{Registry: rt, Version: []int{0, 99999999}}
   119  		if version := strings.Split(slice[1], "-"); len(version) == 2 {
   120  			special.Version[0] = utiles.StringToInt(version[0])
   121  			if version[1] != "?" {
   122  				special.Version[1] = utiles.StringToInt(version[1])
   123  			}
   124  		}
   125  		if specialRegistry == nil {
   126  			specialRegistry = make(map[string][]Special)
   127  		}
   128  		if instant, ok := specialRegistry[slice[0]]; ok {
   129  			instant = append(instant, special)
   130  			specialRegistry[slice[0]] = instant
   131  		} else {
   132  			specialRegistry[slice[0]] = []Special{special}
   133  		}
   134  
   135  	} else {
   136  		TypeRegistry[key] = rt
   137  	}
   138  
   139  }
   140  
   141  var DefaultStafiCustumTypes = `{
   142  	"Weight": "u64",
   143  	"DispatchResult": {
   144  	  "type": "enum",
   145  	  "type_mapping": [
   146  		[
   147  		  "Ok",
   148  		  "Null"
   149  		],
   150  		[
   151  		  "Error",
   152  		  "DispatchError"
   153  		]
   154  	  ]
   155  	},
   156  	"Address": "GenericAddress",
   157  	"LookupSource": "GenericAddress",
   158  	"Keys": {
   159  	  "type": "struct",
   160  	  "type_mapping": [
   161  		[
   162  		  "grandpa",
   163  		  "AccountId"
   164  		],
   165  		[
   166  		  "babe",
   167  		  "AccountId"
   168  		],
   169  		[
   170  		  "im_online",
   171  		  "AccountId"
   172  		],
   173  		[
   174  		  "authority_discovery",
   175  		  "AccountId"
   176  		]
   177  	  ]
   178  	},
   179  	"ChainId": "u8",
   180  	"RateType": "u64",
   181  	"ResourceId": "[u8; 32]",
   182  	"DepositNonce": "u64",
   183  	"U256": "H256",
   184  	"XSymbol": {
   185  	  "type": "enum",
   186  	  "value_list": [
   187  		"WRA"
   188  	  ]
   189  	},
   190  	"AccountXData": {
   191  	  "type": "struct",
   192  	  "type_mapping": [
   193  		[
   194  		  "free",
   195  		  "u128"
   196  		]
   197  	  ]
   198  	},
   199  	"RSymbol": {
   200  	  "type": "enum",
   201  	  "value_list": [
   202  		"RFIS",
   203  		"RDOT",
   204  		"RKSM",
   205  		"RATOM",
   206  		"RSOL",
   207  		"RMATIC",
   208  		"RBNB",
   209  		"RETH"
   210  	  ]
   211  	},
   212  	"AccountRData": {
   213  	  "type": "struct",
   214  	  "type_mapping": [
   215  		[
   216  		  "free",
   217  		  "u128"
   218  		]
   219  	  ]
   220  	},
   221  	"ProposalStatus": {
   222  	  "type": "enum",
   223  	  "value_list": [
   224  		"Active",
   225  		"Passed",
   226  		"Expired",
   227  		"Executed"
   228  	  ]
   229  	},
   230  	"ProposalVotes": {
   231  	  "type": "struct",
   232  	  "type_mapping": [
   233  		[
   234  		  "voted",
   235  		  "Vec<AccountId>"
   236  		],
   237  		[
   238  		  "status",
   239  		  "ProposalStatus"
   240  		],
   241  		[
   242  		  "expiry",
   243  		  "BlockNumber"
   244  		]
   245  	  ]
   246  	},
   247  	"BondRecord": {
   248  	  "type": "struct",
   249  	  "type_mapping": [
   250  		[
   251  		  "bonder",
   252  		  "AccountId"
   253  		],
   254  		[
   255  		  "symbol",
   256  		  "RSymbol"
   257  		],
   258  		[
   259  		  "pubkey",
   260  		  "Vec<u8>"
   261  		],
   262  		[
   263  		  "pool",
   264  		  "Vec<u8>"
   265  		],
   266  		[
   267  		  "blockhash",
   268  		  "Vec<u8>"
   269  		],
   270  		[
   271  		  "txhash",
   272  		  "Vec<u8>"
   273  		],
   274  		[
   275  		  "amount",
   276  		  "u128"
   277  		]
   278  	  ]
   279  	},
   280  	"BondReason": {
   281  	  "type": "enum",
   282  	  "value_list": [
   283  		"Pass",
   284  		"BlockhashUnmatch",
   285  		"TxhashUnmatch",
   286  		"PubkeyUnmatch",
   287  		"PoolUnmatch",
   288  		"AmountUnmatch"
   289  	  ]
   290  	},
   291  	"BondState": {
   292  	  "type": "enum",
   293  	  "value_list": [
   294  		"Dealing",
   295  		"Fail",
   296  		"Success"
   297  	  ]
   298  	},
   299  	"RproposalStatus": {
   300  	  "type": "enum",
   301  	  "value_list": [
   302  		"Initiated",
   303  		"Approved",
   304  		"Rejected",
   305  		"Expired"
   306  	  ]
   307  	},
   308  	"RproposalVotes": {
   309  	  "type": "struct",
   310  	  "type_mapping": [
   311  		[
   312  		  "votes_for",
   313  		  "Vec<AccountId>"
   314  		],
   315  		[
   316  		  "votes_against",
   317  		  "Vec<AccountId>"
   318  		],
   319  		[
   320  		  "status",
   321  		  "RproposalStatus"
   322  		],
   323  		[
   324  		  "expiry",
   325  		  "BlockNumber"
   326  		]
   327  	  ]
   328  	},
   329  	"LinkChunk": {
   330  	  "type": "struct",
   331  	  "type_mapping": [
   332  		[
   333  		  "bond",
   334  		  "u128"
   335  		],
   336  		[
   337  		  "unbond",
   338  		  "u128"
   339  		],
   340  		[
   341  		  "active",
   342  		  "u128"
   343  		]
   344  	  ]
   345  	},
   346  	"Unbonding": {
   347  	  "type": "struct",
   348  	  "type_mapping": [
   349  		[
   350  		  "who",
   351  		  "AccountId"
   352  		],
   353  		[
   354  		  "value",
   355  		  "u128"
   356  		],
   357  		[
   358  		  "recipient",
   359  		  "Vec<u8>"
   360  		]
   361  	  ]
   362  	},
   363  	"OriginalTxType": {
   364  	  "type": "enum",
   365  	  "value_list": [
   366  		"Transfer",
   367  		"Bond",
   368  		"Unbond",
   369  		"WithdrawUnbond",
   370  		"ClaimRewards"
   371  	  ]
   372  	},
   373  	"PoolBondState": {
   374  	  "type": "enum",
   375  	  "value_list": [
   376  		"EraUpdated",
   377  		"BondReported",
   378  		"ActiveReported",
   379  		"WithdrawSkipped",
   380  		"WithdrawReported",
   381  		"TransferReported"
   382  	  ]
   383  	},
   384  	"BondSnapshot": {
   385  	  "type": "struct",
   386  	  "type_mapping": [
   387  		[
   388  		  "symbol",
   389  		  "RSymbol"
   390  		],
   391  		[
   392  		  "era",
   393  		  "u32"
   394  		],
   395  		[
   396  		  "pool",
   397  		  "Vec<u8>"
   398  		],
   399  		[
   400  		  "bond",
   401  		  "u128"
   402  		],
   403  		[
   404  		  "unbond",
   405  		  "u128"
   406  		],
   407  		[
   408  		  "active",
   409  		  "u128"
   410  		],
   411  		[
   412  		  "last_voter",
   413  		  "AccountId"
   414  		],
   415  		[
   416  		  "bond_state",
   417  		  "PoolBondState"
   418  		]
   419  	  ]
   420  	},
   421  	"UserUnlockChunk": {
   422  	  "type": "struct",
   423  	  "type_mapping": [
   424  		[
   425  		  "pool",
   426  		  "Vec<u8>"
   427  		],
   428  		[
   429  		  "unlock_era",
   430  		  "u32"
   431  		],
   432  		[
   433  		  "value",
   434  		  "u128"
   435  		],
   436  		[
   437  		  "recipient",
   438  		  "Vec<u8>"
   439  		]
   440  	  ]
   441  	},
   442  	"SigVerifyResult": {
   443  	  "type": "enum",
   444  	  "value_list": [
   445  		"InvalidPubkey",
   446  		"Fail",
   447  		"Pass"
   448  	  ]
   449  	},
   450  	"BondAction": {
   451  	  "type": "enum",
   452  	  "value_list": [
   453  		"BondOnly",
   454  		"UnbondOnly",
   455  		"BothBondUnbond",
   456  		"EitherBondUnbond",
   457  		"InterDeduct"
   458  	  ]
   459  	},
   460  	"SwapTransactionInfo": {
   461  	  "type": "struct",
   462  	  "type_mapping": [
   463  		[
   464  		  "account",
   465  		  "AccountId"
   466  		],
   467  		[
   468  		  "receiver",
   469  		  "Vec<u8>"
   470  		],
   471  		[
   472  		  "value",
   473  		  "u128"
   474  		],
   475  		[
   476  		  "is_deal",
   477  		  "bool"
   478  		]
   479  	  ]
   480  	},
   481  	"SwapRate": {
   482  	  "type": "struct",
   483  	  "type_mapping": [
   484  		[
   485  		  "lock_number",
   486  		  "u64"
   487  		],
   488  		[
   489  		  "rate",
   490  		  "u128"
   491  		]
   492  	  ]
   493  	},
   494  	"ClaimInfo": {
   495  	  "type": "struct",
   496  	  "type_mapping": [
   497  		[
   498  		  "mint_amount",
   499  		  "u128"
   500  		],
   501  		[
   502  		  "native_token_amount",
   503  		  "u128"
   504  		],
   505  		[
   506  		  "total_reward",
   507  		  "Balance"
   508  		],
   509  		[
   510  		  "total_claimed",
   511  		  "Balance"
   512  		],
   513  		[
   514  		  "latest_claimed_block",
   515  		  "BlockNumber"
   516  		],
   517  		[
   518  		  "mint_block",
   519  		  "BlockNumber"
   520  		]
   521  	  ]
   522  	},
   523  	"MintRewardAct": {
   524  	  "type": "struct",
   525  	  "type_mapping": [
   526  		[
   527  		  "begin",
   528  		  "BlockNumber"
   529  		],
   530  		[
   531  		  "end",
   532  		  "BlockNumber"
   533  		],
   534  		[
   535  		  "cycle",
   536  		  "u32"
   537  		],
   538  		[
   539  		  "reward_rate",
   540  		  "u128"
   541  		],
   542  		[
   543  		  "total_reward",
   544  		  "Balance"
   545  		],
   546  		[
   547  		  "left_amount",
   548  		  "Balance"
   549  		],
   550  		[
   551  		  "user_limit",
   552  		  "Balance"
   553  		],
   554  		[
   555  		  "locked_blocks",
   556  		  "u32"
   557  		],
   558  		[
   559  		  "total_rtoken_amount",
   560  		  "u128"
   561  		],
   562  		[
   563  		  "total_native_token_amount",
   564  		  "u128"
   565  		]
   566  	  ]
   567  	},
   568  	"BondSwap": {
   569  	  "type": "struct",
   570  	  "type_mapping": [
   571  		[
   572  		  "bonder",
   573  		  "AccountId"
   574  		],
   575  		[
   576  		  "swap_fee",
   577  		  "Balance"
   578  		],
   579  		[
   580  		  "swap_receiver",
   581  		  "AccountId"
   582  		],
   583  		[
   584  		  "bridger",
   585  		  "AccountId"
   586  		],
   587  		[
   588  		  "recipient",
   589  		  "Vec<u8>"
   590  		],
   591  		[
   592  		  "dest_id",
   593  		  "ChainId"
   594  		],
   595  		[
   596  		  "expire",
   597  		  "BlockNumber"
   598  		],
   599  		[
   600  		  "bond_state",
   601  		  "BondState"
   602  		],
   603  		[
   604  		  "refunded",
   605  		  "bool"
   606  		]
   607  	  ]
   608  	},
   609  	"SwapPool": {
   610  	  "type": "struct",
   611  	  "type_mapping": [
   612  		[
   613  		  "symbol",
   614  		  "RSymbol"
   615  		],
   616  		[
   617  		  "fis_balance",
   618  		  "u128"
   619  		],
   620  		[
   621  		  "rtoken_balance",
   622  		  "u128"
   623  		],
   624  		[
   625  		  "total_unit",
   626  		  "u128"
   627  		]
   628  	  ]
   629  	},
   630  	"StakePool": {
   631  	  "type": "struct",
   632  	  "type_mapping": [
   633  		[
   634  		  "symbol",
   635  		  "RSymbol"
   636  		],
   637  		[
   638  		  "emergency_switch",
   639  		  "bool"
   640  		],
   641  		[
   642  		  "total_stake_lp",
   643  		  "u128"
   644  		],
   645  		[
   646  		  "start_block",
   647  		  "u32"
   648  		],
   649  		[
   650  		  "reward_per_block",
   651  		  "u128"
   652  		],
   653  		[
   654  		  "total_reward",
   655  		  "u128"
   656  		],
   657  		[
   658  		  "left_reward",
   659  		  "u128"
   660  		],
   661  		[
   662  		  "lp_locked_blocks",
   663  		  "u32"
   664  		],
   665  		[
   666  		  "last_reward_block",
   667  		  "u32"
   668  		],
   669  		[
   670  		  "reward_per_share",
   671  		  "u128"
   672  		],
   673  		[
   674  		  "guard_impermanent_loss",
   675  		  "bool"
   676  		]
   677  	  ]
   678  	},
   679  	"StakeUser": {
   680  	  "type": "struct",
   681  	  "type_mapping": [
   682  		[
   683  		  "account",
   684  		  "AccountId"
   685  		],
   686  		[
   687  		  "lp_amount",
   688  		  "u128"
   689  		],
   690  		[
   691  		  "reward_debt",
   692  		  "u128"
   693  		],
   694  		[
   695  		  "reserved_lp_reward",
   696  		  "u128"
   697  		],
   698  		[
   699  		  "total_fis_value",
   700  		  "u128"
   701  		],
   702  		[
   703  		  "total_rtoken_value",
   704  		  "u128"
   705  		],
   706  		[
   707  		  "deposit_height",
   708  		  "u32"
   709  		],
   710  		[
   711  		  "grade_index",
   712  		  "u32"
   713  		],
   714  		[
   715  		  "claimed_reward",
   716  		  "u128"
   717  		]
   718  	  ]
   719  	},
   720  	"AccountLpData": {
   721  	  "type": "struct",
   722  	  "type_mapping": [
   723  		[
   724  		  "free",
   725  		  "u128"
   726  		]
   727  	  ]
   728  	}
   729    }`