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

     1  package client
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/stafiprotocol/go-substrate-rpc-client/config"
     8  	"github.com/stafiprotocol/go-substrate-rpc-client/types"
     9  )
    10  
    11  func (c *GsrpcClient) CurrentChainEra(sym RSymbol) (uint32, error) {
    12  	symBz, err := types.EncodeToBytes(sym)
    13  	if err != nil {
    14  		return 0, err
    15  	}
    16  
    17  	var era uint32
    18  	exists, err := c.QueryStorage(config.RTokenLedgerModuleId, config.StorageChainEras, symBz, nil, &era)
    19  	if err != nil {
    20  		return 0, err
    21  	}
    22  
    23  	if !exists {
    24  		return 0, ErrorValueNotExist
    25  	}
    26  
    27  	return era, nil
    28  }
    29  
    30  func (c *GsrpcClient) ActiveChangeRateLimit(sym RSymbol) (uint32, error) {
    31  	symBz, err := types.EncodeToBytes(sym)
    32  	if err != nil {
    33  		return 0, err
    34  	}
    35  
    36  	var PerBill types.U32
    37  	exists, err := c.QueryStorage(config.RTokenLedgerModuleId, config.StorageActiveChangeRateLimit, symBz, nil, &PerBill)
    38  	if err != nil {
    39  		return 0, err
    40  	}
    41  
    42  	if !exists {
    43  		return 0, ErrorValueNotExist
    44  	}
    45  
    46  	return uint32(PerBill), nil
    47  }
    48  
    49  func (c *GsrpcClient) RTokenTotalIssuance(sym RSymbol) (types.U128, error) {
    50  	symBz, err := types.EncodeToBytes(sym)
    51  	if err != nil {
    52  		return types.U128{}, err
    53  	}
    54  
    55  	var issuance types.U128
    56  	exists, err := c.QueryStorage(config.RTokenBalanceModuleId, config.StorageTotalIssuance, symBz, nil, &issuance)
    57  	if err != nil {
    58  		return types.U128{}, err
    59  	}
    60  
    61  	if !exists {
    62  		return types.U128{}, ErrorValueNotExist
    63  	}
    64  
    65  	return issuance, nil
    66  }
    67  
    68  func (c *GsrpcClient) CurrentEraSnapshots(symbol RSymbol) ([]types.Hash, error) {
    69  	symBz, err := types.EncodeToBytes(symbol)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	ids := make([]types.Hash, 0)
    75  	exists, err := c.QueryStorage(config.RTokenLedgerModuleId, config.StorageCurrentEraSnapShots, symBz, nil, &ids)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	if !exists {
    80  		return nil, errors.New("storage not exit")
    81  	}
    82  	return ids, nil
    83  }
    84  
    85  func (c *GsrpcClient) ActLatestCycle(sym RSymbol) (uint32, error) {
    86  	symBz, err := types.EncodeToBytes(sym)
    87  	if err != nil {
    88  		return 0, err
    89  	}
    90  
    91  	var cycle uint32
    92  	exists, err := c.QueryStorage(config.RClaimModuleId, config.StorageActLatestCycle, symBz, nil, &cycle)
    93  	if err != nil {
    94  		return 0, err
    95  	}
    96  
    97  	if !exists {
    98  		return 0, ErrorValueNotExist
    99  	}
   100  
   101  	return cycle, nil
   102  }
   103  
   104  func (c *GsrpcClient) REthActLatestCycle() (uint32, error) {
   105  
   106  	var cycle uint32
   107  	exists, err := c.QueryStorage(config.RClaimModuleId, config.StorageREthActLatestCycle, nil, nil, &cycle)
   108  	if err != nil {
   109  		return 0, err
   110  	}
   111  
   112  	if !exists {
   113  		return 0, ErrorValueNotExist
   114  	}
   115  
   116  	return cycle, nil
   117  }
   118  
   119  func (c *GsrpcClient) Act(sym RSymbol, cycle uint32) (*MintRewardAct, error) {
   120  	key := struct {
   121  		Symbol RSymbol
   122  		Cycle  uint32
   123  	}{
   124  		sym,
   125  		cycle,
   126  	}
   127  	keyBz, err := types.EncodeToBytes(key)
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  
   132  	act := new(MintRewardAct)
   133  
   134  	exists, err := c.QueryStorage(config.RClaimModuleId, config.StorageActs, keyBz, nil, act)
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  
   139  	if !exists {
   140  		return nil, ErrorValueNotExist
   141  	}
   142  
   143  	return act, nil
   144  }
   145  func (c *GsrpcClient) RethAct(cycle uint32) (*MintRewardAct, error) {
   146  	cycleBz, err := types.EncodeToBytes(cycle)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	act := new(MintRewardAct)
   152  
   153  	exists, err := c.QueryStorage(config.RClaimModuleId, config.StorageREthActs, cycleBz, nil, act)
   154  	if err != nil {
   155  		return nil, err
   156  	}
   157  
   158  	if !exists {
   159  		return nil, ErrorValueNotExist
   160  	}
   161  
   162  	return act, nil
   163  }
   164  
   165  func (sc *GsrpcClient) GetEraRate(symbol RSymbol, era uint32) (rate uint64, err error) {
   166  	symBz, err := types.EncodeToBytes(symbol)
   167  	if err != nil {
   168  		return 0, err
   169  	}
   170  	eraIndex, err := types.EncodeToBytes(types.NewU32(era))
   171  	if err != nil {
   172  		return 0, err
   173  	}
   174  	exists, err := sc.QueryStorage(config.RTokenRateModuleId, config.StorageEraRate, symBz, eraIndex, &rate)
   175  	if err != nil {
   176  		return 0, err
   177  	}
   178  	if !exists {
   179  		return 0, ErrorValueNotExist
   180  	}
   181  	return rate, nil
   182  }
   183  
   184  func (sc *GsrpcClient) GetReceiver() (*types.AccountID, error) {
   185  	ac := new(types.AccountID)
   186  	exists, err := sc.QueryStorage(config.RTokenLedgerModuleId, config.StorageReceiver, nil, nil, ac)
   187  	if err != nil {
   188  		return nil, err
   189  	}
   190  	if !exists {
   191  		return nil, ErrorValueNotExist
   192  	}
   193  	return ac, nil
   194  }
   195  
   196  func (sc *GsrpcClient) GetRFisReceiver() (*types.AccountID, error) {
   197  	ac := new(types.AccountID)
   198  	exists, err := sc.QueryStorage(config.RFisModuleId, config.StorageReceiver, nil, nil, ac)
   199  	if err != nil {
   200  		return nil, err
   201  	}
   202  	if !exists {
   203  		return nil, ErrorValueNotExist
   204  	}
   205  	return ac, nil
   206  }
   207  
   208  func (gc *GsrpcClient) GetREthCurrentCycle() (uint32, error) {
   209  	var cycle uint32
   210  	exists, err := gc.QueryStorage(config.RClaimModuleId, config.StorageREthActCurrentCycle, nil, nil, &cycle)
   211  	if err != nil {
   212  		return 0, err
   213  	}
   214  	if !exists {
   215  		return 0, ErrorValueNotExist
   216  	}
   217  
   218  	return cycle, nil
   219  }
   220  
   221  func (gc *GsrpcClient) MintTxHashExist(txHash types.Bytes) (bool, error) {
   222  	txHashBytes, err := types.EncodeToBytes(txHash)
   223  	if err != nil {
   224  		return false, err
   225  	}
   226  	var txExists bool
   227  	exists, err := gc.QueryStorage(config.RClaimModuleId, config.StorageMintTxHashExist, txHashBytes, nil, &txExists)
   228  	if err != nil {
   229  		return false, err
   230  	}
   231  	if !exists {
   232  		return exists, nil
   233  	}
   234  
   235  	return txExists, nil
   236  }
   237  
   238  // when req reth info
   239  // update timestamp in database every time.
   240  // need send tx to stafi when:
   241  // 1 current cycle begin < now block < current cycle end
   242  // or
   243  // 2  current+x cycle begin < now < current+x cycle end
   244  func (gc *GsrpcClient) CurrentRethNeedSeed() (bool, error) {
   245  
   246  	currentCycleExist := false
   247  	currentCycle, err := gc.GetREthCurrentCycle()
   248  	if err != nil {
   249  		if err != ErrorValueNotExist {
   250  			return false, err
   251  		}
   252  	} else {
   253  		currentCycleExist = true
   254  	}
   255  
   256  	blockNumber, err := gc.GetLatestBlockNumber()
   257  	if err != nil {
   258  		return false, err
   259  	}
   260  	latestCycle, err := gc.REthActLatestCycle()
   261  	if err != nil {
   262  		if err == ErrorValueNotExist {
   263  			return false, nil
   264  		}
   265  		return false, err
   266  	}
   267  
   268  	if latestCycle == 0 {
   269  		return false, nil
   270  	}
   271  
   272  	if currentCycleExist && currentCycle > 0 {
   273  		currentAct, err := gc.RethAct(currentCycle)
   274  		if err != nil {
   275  			return false, err
   276  		}
   277  		//case 1
   278  		if uint64(currentAct.Begin) <= blockNumber && blockNumber <= uint64(currentAct.End) {
   279  			return true, nil
   280  		}
   281  	}
   282  
   283  	beginCycle := 1
   284  	if currentCycleExist {
   285  		beginCycle = int(currentCycle) + 1
   286  	}
   287  
   288  	for i := beginCycle; i <= int(latestCycle); i++ {
   289  		act, err := gc.RethAct(uint32(i))
   290  		if err != nil {
   291  			if err == ErrorValueNotExist {
   292  
   293  				return false, fmt.Errorf("cycle: %d err: %s", i, err)
   294  			}
   295  			return false, err
   296  		}
   297  
   298  		if act.Begin > types.U32(blockNumber) {
   299  			break
   300  		}
   301  		//case 2
   302  		if uint64(act.Begin) <= blockNumber && blockNumber <= uint64(act.End) {
   303  			return true, nil
   304  		}
   305  	}
   306  	return false, nil
   307  }