// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.

package bor

import (
	"github.com/holiman/uint256"

	"github.com/erigontech/erigon/common"
	"github.com/erigontech/erigon/execution/types"
	"github.com/erigontech/erigon/execution/types/accounts"
	"github.com/erigontech/erigon/execution/vm/evmtypes"
)

var transferLogSig = common.HexToHash("0xe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c4")
var transferFeeLogSig = common.HexToHash("0x4dfe1bbbcf077ddc3e01291eea2d5c70c2b422b415d95645b9adcfd678cb1d63")
var feeAddress = common.HexToAddress("0x0000000000000000000000000000000000001010")

// addTransferLog adds transfer log into state
func addTransferLog(
	state evmtypes.IntraBlockState,
	eventSig common.Hash,

	sender,
	recipient accounts.Address,

	amount,
	input1,
	input2,
	output1,
	output2 uint256.Int,
) {
	// ignore if amount is 0
	if amount.IsZero() {
		return
	}

	data := make([]byte, 32*5)
	amount.WriteToSlice(data)
	input1.WriteToSlice(data[32:])
	input2.WriteToSlice(data[64:])
	output1.WriteToSlice(data[96:])
	output2.WriteToSlice(data[128:])

	// add transfer log
	state.AddLog(&types.Log{
		Address: feeAddress,
		Topics: []common.Hash{
			eventSig,
			feeAddress.Hash(),
			sender.Value().Hash(),
			recipient.Value().Hash(),
		},
		Data: data,
	})
}
