github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/blockchain/contract_test.go (about)

     1  package blockchain
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/ethereum/go-ethereum/common"
     7  	ethtypes "github.com/ethereum/go-ethereum/core/types"
     8  	. "github.com/onsi/gomega"
     9  
    10  	"github.com/machinefi/w3bstream/pkg/models"
    11  )
    12  
    13  func TestContractParseTopic(t *testing.T) {
    14  	c := contract{}
    15  
    16  	t.Run("Empty String", func(t *testing.T) {
    17  		r := c.parseTopic("")
    18  		NewWithT(t).Expect(r).To(BeNil())
    19  	})
    20  
    21  	t.Run("Success", func(t *testing.T) {
    22  		str := "0xb9fb64ccf647f3e7ba45742b97b6b8e464a822c67817276accb7b1f905d292a2"
    23  		r := c.parseTopic(str)
    24  		NewWithT(t).Expect(*r).To(Equal(common.HexToHash(str)))
    25  	})
    26  }
    27  
    28  func TestContractGetTopic(t *testing.T) {
    29  	c := contract{}
    30  	tStr := "0xb9fb64ccf647f3e7ba45742b97b6b8e464a822c67817276accb7b1f905d292a2"
    31  	tHash := *c.parseTopic(tStr)
    32  
    33  	t.Run("Empty Topic", func(t *testing.T) {
    34  		rs, mrs := c.getTopic(nil)
    35  		NewWithT(t).Expect(len(rs)).To(Equal(0))
    36  		NewWithT(t).Expect(len(mrs)).To(Equal(0))
    37  	})
    38  
    39  	t.Run("Have Topic 0", func(t *testing.T) {
    40  		ms := []*models.ContractLog{{
    41  			ContractLogData: models.ContractLogData{
    42  				ContractLogInfo: models.ContractLogInfo{
    43  					Topic0: tStr,
    44  				},
    45  			},
    46  		}}
    47  		rs, mrs := c.getTopic(ms)
    48  		NewWithT(t).Expect(len(rs)).To(Equal(1))
    49  		NewWithT(t).Expect(len(mrs)).To(Equal(1))
    50  		NewWithT(t).Expect(rs[0][0]).To(Equal(tHash))
    51  	})
    52  
    53  	t.Run("Have Topic 1", func(t *testing.T) {
    54  		ms := []*models.ContractLog{{
    55  			ContractLogData: models.ContractLogData{
    56  				ContractLogInfo: models.ContractLogInfo{
    57  					Topic1: tStr,
    58  				},
    59  			},
    60  		}}
    61  		rs, mrs := c.getTopic(ms)
    62  		NewWithT(t).Expect(len(rs)).To(Equal(2))
    63  		NewWithT(t).Expect(len(mrs)).To(Equal(1))
    64  		NewWithT(t).Expect(rs[1][0]).To(Equal(tHash))
    65  	})
    66  
    67  	t.Run("Have Topic 2", func(t *testing.T) {
    68  		ms := []*models.ContractLog{{
    69  			ContractLogData: models.ContractLogData{
    70  				ContractLogInfo: models.ContractLogInfo{
    71  					Topic2: tStr,
    72  				},
    73  			},
    74  		}}
    75  		rs, mrs := c.getTopic(ms)
    76  		NewWithT(t).Expect(len(rs)).To(Equal(3))
    77  		NewWithT(t).Expect(len(mrs)).To(Equal(1))
    78  		NewWithT(t).Expect(rs[2][0]).To(Equal(tHash))
    79  	})
    80  
    81  	t.Run("Have Topic 3", func(t *testing.T) {
    82  		ms := []*models.ContractLog{{
    83  			ContractLogData: models.ContractLogData{
    84  				ContractLogInfo: models.ContractLogInfo{
    85  					Topic3: tStr,
    86  				},
    87  			},
    88  		}}
    89  		rs, mrs := c.getTopic(ms)
    90  		NewWithT(t).Expect(len(rs)).To(Equal(4))
    91  		NewWithT(t).Expect(len(mrs)).To(Equal(1))
    92  		NewWithT(t).Expect(rs[3][0]).To(Equal(tHash))
    93  	})
    94  
    95  	t.Run("Have all Topics", func(t *testing.T) {
    96  		ms := []*models.ContractLog{{
    97  			ContractLogData: models.ContractLogData{
    98  				ContractLogInfo: models.ContractLogInfo{
    99  					Topic0: tStr,
   100  					Topic1: tStr,
   101  					Topic2: tStr,
   102  					Topic3: tStr,
   103  				},
   104  			},
   105  		}}
   106  		rs, mrs := c.getTopic(ms)
   107  		NewWithT(t).Expect(len(rs)).To(Equal(4))
   108  		NewWithT(t).Expect(len(mrs)).To(Equal(1))
   109  		NewWithT(t).Expect(rs[0][0]).To(Equal(tHash))
   110  		NewWithT(t).Expect(rs[1][0]).To(Equal(tHash))
   111  		NewWithT(t).Expect(rs[2][0]).To(Equal(tHash))
   112  		NewWithT(t).Expect(rs[3][0]).To(Equal(tHash))
   113  	})
   114  }
   115  
   116  func TestContractGetAddresses(t *testing.T) {
   117  	c := contract{}
   118  	aStr := "0x1AA325E5144f763a520867c56FC77cC1411430d0"
   119  	aHash := common.HexToAddress(aStr)
   120  
   121  	t.Run("Empty Address", func(t *testing.T) {
   122  		rs, mrs := c.getAddresses(nil)
   123  		NewWithT(t).Expect(len(rs)).To(Equal(0))
   124  		NewWithT(t).Expect(len(mrs)).To(Equal(0))
   125  	})
   126  
   127  	t.Run("Have Address", func(t *testing.T) {
   128  		ms := []*models.ContractLog{{
   129  			ContractLogData: models.ContractLogData{
   130  				ContractLogInfo: models.ContractLogInfo{
   131  					ContractAddress: aStr,
   132  				},
   133  			},
   134  		}}
   135  		rs, mrs := c.getAddresses(ms)
   136  		NewWithT(t).Expect(len(rs)).To(Equal(1))
   137  		NewWithT(t).Expect(len(mrs)).To(Equal(1))
   138  		NewWithT(t).Expect(rs[0]).To(Equal(aHash))
   139  	})
   140  }
   141  
   142  func TestContractGetExpectedContractLogs(t *testing.T) {
   143  	c := contract{}
   144  	tStr := "0xb9fb64ccf647f3e7ba45742b97b6b8e464a822c67817276accb7b1f905d292a2"
   145  	aStr := "0x1AA325E5144f763a520867c56FC77cC1411430d0"
   146  	tHash := *c.parseTopic(tStr)
   147  	aHash := common.HexToAddress(aStr)
   148  
   149  	m := &models.ContractLog{
   150  		ContractLogData: models.ContractLogData{
   151  			ContractLogInfo: models.ContractLogInfo{
   152  				Topic0:          tStr,
   153  				ContractAddress: aStr,
   154  			},
   155  		},
   156  	}
   157  	ms := []*models.ContractLog{m}
   158  
   159  	_, mts := c.getTopic(ms)
   160  	_, mas := c.getAddresses(ms)
   161  
   162  	t.Run("Match Failed", func(t *testing.T) {
   163  		res := c.getExpectedContractLogs(&ethtypes.Log{}, mas, mts)
   164  		NewWithT(t).Expect(len(res)).To(Equal(0))
   165  	})
   166  
   167  	t.Run("Success", func(t *testing.T) {
   168  		log := &ethtypes.Log{
   169  			Address: aHash,
   170  			Topics:  []common.Hash{tHash},
   171  		}
   172  
   173  		res := c.getExpectedContractLogs(log, mas, mts)
   174  		NewWithT(t).Expect(len(res)).To(Equal(1))
   175  		NewWithT(t).Expect(res[0]).To(Equal(m))
   176  	})
   177  }
   178  
   179  func TestContractPruneListChainGroups(t *testing.T) {
   180  	c := contract{}
   181  	ms := []*models.ContractLog{{
   182  		ContractLogData: models.ContractLogData{
   183  			ContractLogInfo: models.ContractLogInfo{
   184  				BlockCurrent: 100,
   185  			},
   186  		},
   187  	},
   188  		{
   189  			ContractLogData: models.ContractLogData{
   190  				ContractLogInfo: models.ContractLogInfo{
   191  					BlockCurrent: 100,
   192  				},
   193  			},
   194  		},
   195  	}
   196  	gs := []*listChainGroup{{cs: ms}}
   197  
   198  	t.Run("Have the same block current", func(t *testing.T) {
   199  		c.pruneListChainGroups(gs)
   200  		NewWithT(t).Expect(gs).To(Equal(gs))
   201  	})
   202  
   203  	t.Run("Don't have the same block current", func(t *testing.T) {
   204  		ngs := gs
   205  		ngs[0].cs = append(ngs[0].cs, &models.ContractLog{
   206  			ContractLogData: models.ContractLogData{
   207  				ContractLogInfo: models.ContractLogInfo{
   208  					BlockCurrent: 200,
   209  				},
   210  			},
   211  		},
   212  		)
   213  		c.pruneListChainGroups(gs)
   214  		NewWithT(t).Expect(ngs[0].cs).To(Equal(gs[0].cs))
   215  		NewWithT(t).Expect(ngs[0].toBlock).To(Equal(uint64(199)))
   216  	})
   217  }
   218  
   219  func TestContractGroupContractLog(t *testing.T) {
   220  	c := contract{}
   221  	ms := []models.ContractLog{
   222  		{
   223  			ContractLogData: models.ContractLogData{
   224  				ProjectName: "test1",
   225  				ContractLogInfo: models.ContractLogInfo{
   226  					ChainID:      1,
   227  					BlockCurrent: 100,
   228  				},
   229  			},
   230  		},
   231  		{
   232  			ContractLogData: models.ContractLogData{
   233  				ProjectName: "test1",
   234  				ContractLogInfo: models.ContractLogInfo{
   235  					ChainID:      2,
   236  					BlockCurrent: 100,
   237  				},
   238  			},
   239  		},
   240  	}
   241  
   242  	gs := c.groupContractLog(ms)
   243  	NewWithT(t).Expect(len(gs)).To(Equal(int(2)))
   244  	// NewWithT(t).Expect(gs[0].cs[0].ChainID).To(Equal(uint64(1)))
   245  }