github.com/trezor/blockbook@v0.4.1-0.20240328132726-e9a08582ee2c/tests/dbtestdata/fakechain_ethereumtype.go (about) 1 package dbtestdata 2 3 import ( 4 "encoding/json" 5 "math/big" 6 "strconv" 7 8 "github.com/trezor/blockbook/bchain" 9 ) 10 11 type fakeBlockChainEthereumType struct { 12 *fakeBlockChain 13 } 14 15 // NewFakeBlockChainEthereumType returns mocked blockchain RPC interface used for tests 16 func NewFakeBlockChainEthereumType(parser bchain.BlockChainParser) (bchain.BlockChain, error) { 17 return &fakeBlockChainEthereumType{&fakeBlockChain{&bchain.BaseChain{Parser: parser}}}, nil 18 } 19 20 func (c *fakeBlockChainEthereumType) CreateMempool(chain bchain.BlockChain) (bchain.Mempool, error) { 21 return bchain.NewMempoolEthereumType(chain, 1, false), nil 22 } 23 24 func (c *fakeBlockChainEthereumType) GetChainInfo() (v *bchain.ChainInfo, err error) { 25 return &bchain.ChainInfo{ 26 Chain: c.GetNetworkName(), 27 Blocks: 2, 28 Headers: 2, 29 Bestblockhash: GetTestEthereumTypeBlock2(c.Parser).BlockHeader.Hash, 30 Version: "001001", 31 Subversion: c.GetSubversion(), 32 }, nil 33 } 34 35 func (c *fakeBlockChainEthereumType) GetBestBlockHash() (v string, err error) { 36 return GetTestEthereumTypeBlock2(c.Parser).BlockHeader.Hash, nil 37 } 38 39 func (c *fakeBlockChainEthereumType) GetBestBlockHeight() (v uint32, err error) { 40 return GetTestEthereumTypeBlock2(c.Parser).BlockHeader.Height, nil 41 } 42 43 func (c *fakeBlockChainEthereumType) GetBlockHash(height uint32) (v string, err error) { 44 b1 := GetTestEthereumTypeBlock1(c.Parser) 45 if height == b1.BlockHeader.Height { 46 return b1.BlockHeader.Hash, nil 47 } 48 b2 := GetTestEthereumTypeBlock2(c.Parser) 49 if height == b2.BlockHeader.Height { 50 return b2.BlockHeader.Hash, nil 51 } 52 return "", bchain.ErrBlockNotFound 53 } 54 55 func (c *fakeBlockChainEthereumType) GetBlockHeader(hash string) (v *bchain.BlockHeader, err error) { 56 b1 := GetTestEthereumTypeBlock1(c.Parser) 57 if hash == b1.BlockHeader.Hash { 58 return &b1.BlockHeader, nil 59 } 60 b2 := GetTestEthereumTypeBlock2(c.Parser) 61 if hash == b2.BlockHeader.Hash { 62 return &b2.BlockHeader, nil 63 } 64 return nil, bchain.ErrBlockNotFound 65 } 66 67 func (c *fakeBlockChainEthereumType) GetBlock(hash string, height uint32) (v *bchain.Block, err error) { 68 b1 := GetTestEthereumTypeBlock1(c.Parser) 69 if hash == b1.BlockHeader.Hash || height == b1.BlockHeader.Height { 70 return b1, nil 71 } 72 b2 := GetTestEthereumTypeBlock2(c.Parser) 73 if hash == b2.BlockHeader.Hash || height == b2.BlockHeader.Height { 74 return b2, nil 75 } 76 return nil, bchain.ErrBlockNotFound 77 } 78 79 func (c *fakeBlockChainEthereumType) GetBlockInfo(hash string) (v *bchain.BlockInfo, err error) { 80 b1 := GetTestEthereumTypeBlock1(c.Parser) 81 if hash == b1.BlockHeader.Hash { 82 return getBlockInfo(b1), nil 83 } 84 b2 := GetTestEthereumTypeBlock2(c.Parser) 85 if hash == b2.BlockHeader.Hash { 86 return getBlockInfo(b2), nil 87 } 88 return nil, bchain.ErrBlockNotFound 89 } 90 91 func (c *fakeBlockChainEthereumType) GetTransaction(txid string) (v *bchain.Tx, err error) { 92 v = getTxInBlock(GetTestEthereumTypeBlock1(c.Parser), txid) 93 if v == nil { 94 v = getTxInBlock(GetTestEthereumTypeBlock2(c.Parser), txid) 95 } 96 if v != nil { 97 return v, nil 98 } 99 return nil, bchain.ErrTxNotFound 100 } 101 102 func (c *fakeBlockChainEthereumType) GetTransactionSpecific(tx *bchain.Tx) (v json.RawMessage, err error) { 103 txS, _ := tx.CoinSpecificData.(bchain.EthereumSpecificData) 104 105 rm, err := json.Marshal(txS) 106 if err != nil { 107 return nil, err 108 } 109 return json.RawMessage(rm), nil 110 } 111 112 func (c *fakeBlockChainEthereumType) EthereumTypeGetBalance(addrDesc bchain.AddressDescriptor) (*big.Int, error) { 113 return big.NewInt(123450000 + int64(addrDesc[0])), nil 114 } 115 116 func (c *fakeBlockChainEthereumType) EthereumTypeGetNonce(addrDesc bchain.AddressDescriptor) (uint64, error) { 117 return uint64(addrDesc[0]), nil 118 } 119 120 func (c *fakeBlockChainEthereumType) GetContractInfo(contractDesc bchain.AddressDescriptor) (*bchain.ContractInfo, error) { 121 addresses, _, _ := c.Parser.GetAddressesFromAddrDesc(contractDesc) 122 return &bchain.ContractInfo{ 123 Type: bchain.ERC20TokenType, 124 Contract: addresses[0], 125 Name: "Contract " + strconv.Itoa(int(contractDesc[0])), 126 Symbol: "S" + strconv.Itoa(int(contractDesc[0])), 127 Decimals: 18, 128 CreatedInBlock: 12345, 129 }, nil 130 } 131 132 // EthereumTypeGetErc20ContractBalance returns simulated balance 133 func (c *fakeBlockChainEthereumType) EthereumTypeGetErc20ContractBalance(addrDesc, contractDesc bchain.AddressDescriptor) (*big.Int, error) { 134 return big.NewInt(1000000000 + int64(addrDesc[0])*1000 + int64(contractDesc[0])), nil 135 } 136 137 // GetTokenURI returns URI derived from the input contractDesc 138 func (c *fakeBlockChainEthereumType) GetTokenURI(contractDesc bchain.AddressDescriptor, tokenID *big.Int) (string, error) { 139 return "https://ipfs.io/ipfs/" + contractDesc.String()[3:] + ".json", nil 140 }