code.vegaprotocol.io/vega@v0.79.0/core/events/asset_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package events_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/core/events"
    23  	"code.vegaprotocol.io/vega/core/types"
    24  	"code.vegaprotocol.io/vega/libs/num"
    25  	proto "code.vegaprotocol.io/vega/protos/vega"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestAssetBuiltInAssetDeepClone(t *testing.T) {
    31  	ctx := context.Background()
    32  
    33  	a := types.Asset{
    34  		ID: "Id",
    35  		Details: &types.AssetDetails{
    36  			Name:     "Name",
    37  			Symbol:   "Symbol",
    38  			Decimals: 5,
    39  			Source: &types.AssetDetailsBuiltinAsset{
    40  				BuiltinAsset: &types.BuiltinAsset{
    41  					MaxFaucetAmountMint: num.NewUint(100000000),
    42  				},
    43  			},
    44  		},
    45  	}
    46  
    47  	assetEvent := events.NewAssetEvent(ctx, a)
    48  	a2 := assetEvent.Asset()
    49  
    50  	// Change the original and check we are not updating the wrapped event
    51  	a.ID = "Changed"
    52  	a.Details.Name = "Changed"
    53  	a.Details.Symbol = "Changed"
    54  	a.Details.Decimals = 999
    55  
    56  	as := a.Details.Source.(*types.AssetDetailsBuiltinAsset)
    57  	bia := as.BuiltinAsset
    58  	bia.MaxFaucetAmountMint = num.NewUint(999)
    59  
    60  	as2 := a2.Details.Source.(*proto.AssetDetails_BuiltinAsset)
    61  	bia2 := as2.BuiltinAsset
    62  
    63  	assert.NotEqual(t, a.ID, a2.Id)
    64  	assert.NotEqual(t, a.Details.Name, a2.Details.Name)
    65  	assert.NotEqual(t, a.Details.Symbol, a2.Details.Symbol)
    66  	assert.NotEqual(t, a.Details.Decimals, a2.Details.Decimals)
    67  
    68  	assert.NotEqual(t, bia.MaxFaucetAmountMint, bia2.MaxFaucetAmountMint)
    69  }
    70  
    71  func TestAssetERCDeepClone(t *testing.T) {
    72  	ctx := context.Background()
    73  
    74  	a := types.Asset{
    75  		ID: "Id",
    76  		Details: &types.AssetDetails{
    77  			Name:     "Name",
    78  			Symbol:   "Symbol",
    79  			Decimals: 5,
    80  			Source: &types.AssetDetailsErc20{
    81  				ERC20: &types.ERC20{
    82  					ChainID:         "1",
    83  					ContractAddress: "Contact Address",
    84  				},
    85  			},
    86  		},
    87  	}
    88  
    89  	assetEvent := events.NewAssetEvent(ctx, a)
    90  	a2 := assetEvent.Asset()
    91  
    92  	// Change the original and check we are not updating the wrapped event
    93  	a.ID = "Changed"
    94  	a.Details.Name = "Changed"
    95  	a.Details.Symbol = "Changed"
    96  	a.Details.Decimals = 999
    97  
    98  	as := a.Details.Source.(*types.AssetDetailsErc20)
    99  	erc := as.ERC20
   100  	erc.ContractAddress = "Changed"
   101  
   102  	as2 := a2.Details.Source.(*proto.AssetDetails_Erc20)
   103  	erc2 := as2.Erc20
   104  
   105  	assert.NotEqual(t, a.ID, a2.Id)
   106  	assert.NotEqual(t, a.Details.Name, a2.Details.Name)
   107  	assert.NotEqual(t, a.Details.Symbol, a2.Details.Symbol)
   108  	assert.NotEqual(t, a.Details.Decimals, a2.Details.Decimals)
   109  
   110  	assert.NotEqual(t, erc.ContractAddress, erc2.ContractAddress)
   111  }