Advertisement
viettiennguyen029

Check contract 01

Jan 24th, 2022
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.52 KB | None | 0 0
  1. func (repo *ContractRepo) Restore(
  2.     addr string,
  3.     source string,
  4.     startBlock uint64,
  5. ) error {
  6.     return repo.db.Unscoped().
  7.         Model(&Contract{}).
  8.         Where("contract_addr = ?", addr).
  9.         Updates(map[string]interface{}{
  10.             "deleted_at":              0,
  11.             "source":                  source,
  12.             "created_at_block_height": startBlock}).
  13.         Error
  14. }
  15.  
  16.  
  17. func (repo *ContractRepo) GetContractByAddr(
  18.     contractAddr string,
  19.     unScoped bool,
  20. ) (Contract, error) {
  21.     var contract Contract
  22.  
  23.     if unScoped {
  24.         if res := repo.db.Unscoped().Where("contract_addr = ?", contractAddr).First(&contract); res.Error != nil {
  25.             return Contract{}, res.Error
  26.         }
  27.     } else {
  28.         if res := repo.db.Where("contract_addr = ?", contractAddr).First(&contract); res.Error != nil {
  29.             return Contract{}, res.Error
  30.         }
  31.     }
  32.  
  33.     return contract, nil
  34. }
  35.  
  36.  
  37.  
  38. // Check if contract address is existed
  39.     existedContract, err := s.repos.GetContractRepo().GetContractByAddr(contract.ContractAddr, true)
  40.     if err != nil {
  41.         if err == consts.ErrRecordNotFound {
  42.             // Add new imported contract to db
  43.             err = s.repos.GetContractRepo().Create(contract)
  44.             if err != nil {
  45.                 return err
  46.             }
  47.         } else {
  48.             return err
  49.         }
  50.     } else {
  51.         fmt.Println(existedContract.DeletedAt)
  52.         if existedContract.DeletedAt != 0 {
  53.  
  54.             // Restore contract address
  55.             err = s.repos.GetContractRepo().Restore(
  56.                 existedContract.ContractAddr,
  57.                 contract.Source,
  58.                 startBlock)
  59.             if err != nil {
  60.                 return err
  61.             }
  62.         } else {
  63.  
  64.             return consts.ErrDuplicatedContractAddr
  65.         }
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement