github.com/mavryk-network/mvgo@v1.19.9/rpc/origination.go (about)

     1  // Copyright (c) 2020-2021 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc
     3  
     4  package rpc
     5  
     6  import (
     7  	"github.com/mavryk-network/mvgo/mavryk"
     8  	"github.com/mavryk-network/mvgo/micheline"
     9  )
    10  
    11  // Ensure Origination implements the TypedOperation interface.
    12  var _ TypedOperation = (*Origination)(nil)
    13  
    14  // Origination represents a contract creation operation
    15  type Origination struct {
    16  	Manager
    17  	ManagerPubkey  mavryk.Address    `json:"manager_pubkey"` // proto v1 & >= v4
    18  	ManagerPubkey2 mavryk.Address    `json:"managerPubkey"`  // proto v2, v3
    19  	Balance        int64             `json:"balance,string"`
    20  	Spendable      *bool             `json:"spendable"`   // true when missing before v5 Babylon
    21  	Delegatable    *bool             `json:"delegatable"` // true when missing before v5 Babylon
    22  	Delegate       *mavryk.Address   `json:"delegate"`
    23  	Script         *micheline.Script `json:"script"`
    24  }
    25  
    26  func (o Origination) ManagerAddress() mavryk.Address {
    27  	if o.ManagerPubkey2.IsValid() {
    28  		return o.ManagerPubkey2
    29  	}
    30  	return o.ManagerPubkey
    31  }
    32  
    33  // Costs returns operation cost to implement TypedOperation interface.
    34  func (o Origination) Costs() mavryk.Costs {
    35  	res := o.Metadata.Result
    36  	cost := mavryk.Costs{
    37  		Fee:         o.Manager.Fee,
    38  		GasUsed:     res.Gas(),
    39  		StorageUsed: res.PaidStorageSizeDiff,
    40  	}
    41  	var i int
    42  	for _, v := range res.BalanceUpdates {
    43  		if v.Kind != CONTRACT {
    44  			continue
    45  		}
    46  		if res.PaidStorageSizeDiff > 0 && i == 0 {
    47  			burn := v.Amount()
    48  			cost.StorageBurn += -burn
    49  			cost.Burn += -burn
    50  			i++
    51  			continue
    52  		}
    53  		if len(res.OriginatedContracts) > 0 && i == 1 {
    54  			burn := v.Amount()
    55  			cost.AllocationBurn += -burn
    56  			cost.Burn += -burn
    57  			i++
    58  		}
    59  	}
    60  	return cost
    61  }