github.com/InjectiveLabs/sdk-go@v1.53.0/chain/peggy/types/batch.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"strings"
     7  
     8  	accountsabi "github.com/ethereum/go-ethereum/accounts/abi"
     9  	"github.com/ethereum/go-ethereum/common"
    10  	"github.com/ethereum/go-ethereum/crypto"
    11  )
    12  
    13  // GetCheckpoint gets the checkpoint signature from the given outgoing tx batch
    14  func (b OutgoingTxBatch) GetCheckpoint(peggyIDstring string) common.Hash {
    15  
    16  	abi, err := accountsabi.JSON(strings.NewReader(OutgoingBatchTxCheckpointABIJSON))
    17  	if err != nil {
    18  		panic("Bad ABI constant!")
    19  	}
    20  
    21  	// the contract argument is not a arbitrary length array but a fixed length 32 byte
    22  	// array, therefore we have to utf8 encode the string (the default in this case) and
    23  	// then copy the variable length encoded data into a fixed length array. This function
    24  	// will panic if peggyId is too long to fit in 32 bytes
    25  	peggyID, err := strToFixByteArray(peggyIDstring)
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  
    30  	// Create the methodName argument which salts the signature
    31  	methodNameBytes := []uint8("transactionBatch")
    32  	var batchMethodName [32]uint8
    33  	copy(batchMethodName[:], methodNameBytes)
    34  
    35  	// Run through the elements of the batch and serialize them
    36  	txAmounts := make([]*big.Int, len(b.Transactions))
    37  	txDestinations := make([]common.Address, len(b.Transactions))
    38  	txFees := make([]*big.Int, len(b.Transactions))
    39  	for i, tx := range b.Transactions {
    40  		txAmounts[i] = tx.Erc20Token.Amount.BigInt()
    41  		txDestinations[i] = common.HexToAddress(tx.DestAddress)
    42  		txFees[i] = tx.Erc20Fee.Amount.BigInt()
    43  	}
    44  
    45  	// the methodName needs to be the same as the 'name' above in the checkpointAbiJson
    46  	// but other than that it's a constant that has no impact on the output. This is because
    47  	// it gets encoded as a function name which we must then discard.
    48  	abiEncodedBatch, err := abi.Pack("submitBatch",
    49  		peggyID,
    50  		batchMethodName,
    51  		txAmounts,
    52  		txDestinations,
    53  		txFees,
    54  		big.NewInt(int64(b.BatchNonce)),
    55  		common.HexToAddress(b.TokenContract),
    56  		big.NewInt(int64(b.BatchTimeout)),
    57  	)
    58  
    59  	// this should never happen outside of test since any case that could crash on encoding
    60  	// should be filtered above.
    61  	if err != nil {
    62  		panic(fmt.Sprintf("Error packing checkpoint! %s/n", err))
    63  	}
    64  
    65  	hash := crypto.Keccak256Hash(abiEncodedBatch[4:])
    66  	return hash
    67  }