code.vegaprotocol.io/vega@v0.79.0/core/banking/builtin_assets.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 banking
    17  
    18  import (
    19  	"context"
    20  	"encoding/binary"
    21  	"encoding/hex"
    22  	"sync/atomic"
    23  
    24  	"code.vegaprotocol.io/vega/core/events"
    25  	"code.vegaprotocol.io/vega/core/types"
    26  	vgcrypto "code.vegaprotocol.io/vega/libs/crypto"
    27  	"code.vegaprotocol.io/vega/libs/num"
    28  	"code.vegaprotocol.io/vega/logging"
    29  )
    30  
    31  func (e *Engine) WithdrawBuiltinAsset(
    32  	ctx context.Context, id, party, assetID string, amount *num.Uint,
    33  ) error {
    34  	// build the withdrawal type
    35  	w, ref := e.newWithdrawal(id, party, assetID, amount, nil)
    36  	w.Status = types.WithdrawalStatusRejected // default
    37  	e.withdrawals[w.ID] = withdrawalRef{w, ref}
    38  
    39  	asset, err := e.assets.Get(assetID)
    40  	if err != nil {
    41  		e.broker.Send(events.NewWithdrawalEvent(ctx, *w))
    42  		e.log.Error("unable to get asset by id",
    43  			logging.AssetID(assetID),
    44  			logging.Error(err))
    45  		return err
    46  	}
    47  
    48  	if !asset.IsBuiltinAsset() {
    49  		e.broker.Send(events.NewWithdrawalEvent(ctx, *w))
    50  		return ErrWrongAssetTypeUsedInBuiltinAssetChainEvent
    51  	}
    52  
    53  	return e.finalizeWithdraw(ctx, w)
    54  }
    55  
    56  func (e *Engine) DepositBuiltinAsset(
    57  	ctx context.Context, d *types.BuiltinAssetDeposit, id string, nonce uint64,
    58  ) error {
    59  	dep := e.newDeposit(id, d.PartyID, d.VegaAssetID, d.Amount, "") // no hash
    60  	e.broker.Send(events.NewDepositEvent(ctx, *dep))
    61  	asset, err := e.assets.Get(d.VegaAssetID)
    62  	if err != nil {
    63  		dep.Status = types.DepositStatusCancelled
    64  		e.broker.Send(events.NewDepositEvent(ctx, *dep))
    65  		e.log.Error("unable to get asset by id",
    66  			logging.AssetID(d.VegaAssetID),
    67  			logging.Error(err))
    68  		return err
    69  	}
    70  	if !asset.IsBuiltinAsset() {
    71  		dep.Status = types.DepositStatusCancelled
    72  		e.broker.Send(events.NewDepositEvent(ctx, *dep))
    73  		return ErrWrongAssetTypeUsedInBuiltinAssetChainEvent
    74  	}
    75  
    76  	// create a pretend "hash" from the nonce (which is randomly generated by the faucet)
    77  	// ready for calls to getRef()
    78  	b := make([]byte, 8)
    79  	binary.BigEndian.PutUint64(b, nonce)
    80  
    81  	state := &atomic.Uint32{}
    82  	state.Store(pendingState)
    83  
    84  	aa := &assetAction{
    85  		id:       dep.ID,
    86  		state:    state,
    87  		builtinD: d,
    88  		asset:    asset,
    89  		txHash:   hex.EncodeToString(vgcrypto.Hash(b)),
    90  	}
    91  	e.assetActions[aa.id] = aa
    92  	e.deposits[dep.ID] = dep
    93  	return e.witness.StartCheck(aa, e.onCheckDone, e.timeService.GetTimeNow().Add(defaultValidationDuration))
    94  }
    95  
    96  func (e *Engine) EnableBuiltinAsset(ctx context.Context, assetID string) error {
    97  	return e.finalizeAssetList(ctx, assetID)
    98  }