github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/api/inspect.go (about)

     1  /*
     2   * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved.
     3   * This software is released under GPL3.
     4   * The full license information can be found under:
     5   * https://www.gnu.org/licenses/gpl-3.0.en.html
     6   *
     7   */
     8  
     9  package api
    10  
    11  import (
    12  	"math/big"
    13  	"time"
    14  
    15  	"github.com/ethereum/go-ethereum/common"
    16  	"github.com/ethereum/go-ethereum/ethclient"
    17  	"github.com/sirupsen/logrus"
    18  	"github.com/vchain-us/vcn/internal/blockchain"
    19  	"github.com/vchain-us/vcn/pkg/meta"
    20  )
    21  
    22  // BlockChainInspect returns an array of BlockchainVerification containing all verifications found for the given hash
    23  func BlockChainInspect(hash string) ([]BlockchainVerification, error) {
    24  	logger().WithFields(logrus.Fields{
    25  		"hash": hash,
    26  	}).Trace("BlockChainInspect")
    27  
    28  	// Connect and get verification count
    29  	client, err := ethclient.Dial(meta.MainNet())
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	contractAddress := common.HexToAddress(meta.AssetsRelayContractAddress())
    34  	instance, err := blockchain.NewAssetsRelay(contractAddress, client)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	count, err := instance.GetAssetCountForHash(nil, hash)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	l := count.Int64()
    43  
    44  	verifications := make([]BlockchainVerification, l)
    45  
    46  	// Iterate over verifications
    47  	for i := int64(0); i < l; i++ {
    48  		address, level, status, timestamp, err := instance.VerifyByIndex(nil, hash, big.NewInt(i))
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  		verifications[i] = BlockchainVerification{
    53  			Owner:     address,
    54  			Level:     meta.Level(level.Int64()),
    55  			Status:    meta.Status(status.Int64()),
    56  			Timestamp: time.Unix(timestamp.Int64(), 0),
    57  		}
    58  	}
    59  	return verifications, nil
    60  }