update gas fee

This commit is contained in:
Daniel Liu 2023-07-18 19:43:36 +08:00
parent 148e2f1699
commit 7adb98fbe8
6 changed files with 27 additions and 52 deletions

View file

@ -4,15 +4,16 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"math/big"
"time"
"github.com/XinFinOrg/XDPoSChain/accounts/abi/bind"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
"github.com/XinFinOrg/XDPoSChain/contracts/trc21issuer"
"github.com/XinFinOrg/XDPoSChain/contracts/trc21issuer/simulation"
"github.com/XinFinOrg/XDPoSChain/ethclient"
"log"
"math/big"
"time"
)
var (
@ -48,11 +49,10 @@ func airDropTokenToAccountNoXDC() {
if err != nil {
log.Fatal("can't transaction's receipt ", err, "hash", tx.Hash().Hex())
}
fee := big.NewInt(0).SetUint64(hexutil.MustDecodeUint64(receipt["gasUsed"].(string)))
if hexutil.MustDecodeUint64(receipt["blockNumber"].(string)) > common.TIPTRC21Fee.Uint64() {
fee = fee.Mul(fee, common.TRC21GasPrice)
}
fmt.Println("fee", fee.Uint64(), "number", hexutil.MustDecodeUint64(receipt["blockNumber"].(string)))
gasUsed := hexutil.MustDecodeUint64(receipt["gasUsed"].(string))
blockNumber := hexutil.MustDecodeUint64(receipt["blockNumber"].(string))
fee := common.GetGasFee(blockNumber, gasUsed)
fmt.Println("fee", fee.Uint64(), "number", blockNumber)
remainFee = big.NewInt(0).Sub(remainFee, fee)
//check balance fee
balanceIssuerFee, err := trc21IssuerInstance.GetTokenCapacity(trc21TokenAddr)
@ -111,11 +111,10 @@ func testTransferTRC21TokenWithAccountNoXDC() {
if err != nil {
log.Fatal("can't transaction's receipt ", err, "hash", tx.Hash().Hex())
}
fee := big.NewInt(0).SetUint64(hexutil.MustDecodeUint64(receipt["gasUsed"].(string)))
if hexutil.MustDecodeUint64(receipt["blockNumber"].(string)) > common.TIPTRC21Fee.Uint64() {
fee = fee.Mul(fee, common.TRC21GasPrice)
}
fmt.Println("fee", fee.Uint64(), "number", hexutil.MustDecodeUint64(receipt["blockNumber"].(string)))
gasUsed := hexutil.MustDecodeUint64(receipt["gasUsed"].(string))
blockNumber := hexutil.MustDecodeUint64(receipt["blockNumber"].(string))
fee := common.GetGasFee(blockNumber, gasUsed)
fmt.Println("fee", fee.Uint64(), "number", blockNumber)
remainFee = big.NewInt(0).Sub(remainFee, fee)
//check balance fee
balanceIssuerFee, err := trc21IssuerInstance.GetTokenCapacity(trc21TokenAddr)
@ -178,11 +177,10 @@ func testTransferTrc21Fail() {
if err != nil {
log.Fatal("can't transaction's receipt ", err, "hash", tx.Hash().Hex())
}
fee := big.NewInt(0).SetUint64(hexutil.MustDecodeUint64(receipt["gasUsed"].(string)))
if hexutil.MustDecodeUint64(receipt["blockNumber"].(string)) > common.TIPTRC21Fee.Uint64() {
fee = fee.Mul(fee, common.TRC21GasPrice)
}
fmt.Println("fee", fee.Uint64(), "number", hexutil.MustDecodeUint64(receipt["blockNumber"].(string)))
gasUsed := hexutil.MustDecodeUint64(receipt["gasUsed"].(string))
blockNumber := hexutil.MustDecodeUint64(receipt["blockNumber"].(string))
fee := common.GetGasFee(blockNumber, gasUsed)
fmt.Println("fee", fee.Uint64(), "number", blockNumber)
remainFee = big.NewInt(0).Sub(remainFee, fee)
//check balance fee
balanceIssuerFee, err = trc21IssuerInstance.GetTokenCapacity(trc21TokenAddr)

View file

@ -1,13 +1,14 @@
package trc21issuer
import (
"math/big"
"testing"
"github.com/XinFinOrg/XDPoSChain/accounts/abi/bind"
"github.com/XinFinOrg/XDPoSChain/accounts/abi/bind/backends"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core"
"github.com/XinFinOrg/XDPoSChain/crypto"
"math/big"
"testing"
)
var (
@ -81,10 +82,7 @@ func TestFeeTxWithTRC21Token(t *testing.T) {
if err != nil {
t.Fatal("can't transaction's receipt ", err, "hash", tx.Hash())
}
fee := big.NewInt(0).SetUint64(receipt.GasUsed)
if receipt.Logs[0].BlockNumber > common.TIPTRC21Fee.Uint64() {
fee = fee.Mul(fee, common.TRC21GasPrice)
}
fee := common.GetGasFee(receipt.Logs[0].BlockNumber, receipt.GasUsed)
remainFee := big.NewInt(0).Sub(minApply, fee)
// check balance trc21 again
@ -133,10 +131,7 @@ func TestFeeTxWithTRC21Token(t *testing.T) {
if err != nil {
t.Fatal("can't transaction's receipt ", err, "hash", tx.Hash())
}
fee = big.NewInt(0).SetUint64(receipt.GasUsed)
if receipt.Logs[0].BlockNumber > common.TIPTRC21Fee.Uint64() {
fee = fee.Mul(fee, common.TRC21GasPrice)
}
fee = common.GetGasFee(receipt.Logs[0].BlockNumber, receipt.GasUsed)
remainFee = big.NewInt(0).Sub(remainFee, fee)
//check balance fee
balanceIssuerFee, err = trc21Issuer.GetTokenCapacity(trc21TokenAddr)

View file

@ -108,10 +108,7 @@ func (b *BlockGen) AddTxWithChain(bc *BlockChain, tx *types.Transaction) {
b.txs = append(b.txs, tx)
b.receipts = append(b.receipts, receipt)
if tokenFeeUsed {
fee := new(big.Int).SetUint64(gas)
if b.header.Number.Cmp(common.TIPTRC21Fee) > 0 {
fee = fee.Mul(fee, common.TRC21GasPrice)
}
fee := common.GetGasFee(b.header.Number.Uint64(), gas)
state.UpdateTRC21Fee(b.statedb, map[common.Address]*big.Int{*tx.To(): new(big.Int).Sub(feeCapacity[*tx.To()], new(big.Int).SetUint64(gas))}, fee)
}
}

View file

@ -120,10 +120,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, tra
receipts = append(receipts, receipt)
allLogs = append(allLogs, receipt.Logs...)
if tokenFeeUsed {
fee := new(big.Int).SetUint64(gas)
if block.Header().Number.Cmp(common.TIPTRC21Fee) > 0 {
fee = fee.Mul(fee, common.TRC21GasPrice)
}
fee := common.GetGasFee(block.Header().Number.Uint64(), gas)
balanceFee[*tx.To()] = new(big.Int).Sub(balanceFee[*tx.To()], fee)
balanceUpdated[*tx.To()] = balanceFee[*tx.To()]
totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee)
@ -201,10 +198,7 @@ func (p *StateProcessor) ProcessBlockNoValidator(cBlock *CalculatedBlock, stated
receipts[i] = receipt
allLogs = append(allLogs, receipt.Logs...)
if tokenFeeUsed {
fee := new(big.Int).SetUint64(gas)
if block.Header().Number.Cmp(common.TIPTRC21Fee) > 0 {
fee = fee.Mul(fee, common.TRC21GasPrice)
}
fee := common.GetGasFee(block.Header().Number.Uint64(), gas)
balanceFee[*tx.To()] = new(big.Int).Sub(balanceFee[*tx.To()], fee)
balanceUpdated[*tx.To()] = balanceFee[*tx.To()]
totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee)

View file

@ -706,10 +706,7 @@ func (api *PrivateDebugAPI) computeTxEnv(blockHash common.Hash, txIndex int, ree
}
if tokenFeeUsed {
fee := new(big.Int).SetUint64(gas)
if block.Header().Number.Cmp(common.TIPTRC21Fee) > 0 {
fee = fee.Mul(fee, common.TRC21GasPrice)
}
fee := common.GetGasFee(block.Header().Number.Uint64(), gas)
feeCapacity[*tx.To()] = new(big.Int).Sub(feeCapacity[*tx.To()], fee)
balanceUpdated[*tx.To()] = feeCapacity[*tx.To()]
totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee)

View file

@ -931,10 +931,7 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
log.Debug("Add Special Transaction failed, account skipped", "hash", tx.Hash(), "sender", from, "nonce", tx.Nonce(), "to", tx.To(), "err", err)
}
if tokenFeeUsed {
fee := new(big.Int).SetUint64(gas)
if env.header.Number.Cmp(common.TIPTRC21Fee) > 0 {
fee = fee.Mul(fee, common.TRC21GasPrice)
}
fee := common.GetGasFee(env.header.Number.Uint64(), gas)
balanceFee[*tx.To()] = new(big.Int).Sub(balanceFee[*tx.To()], fee)
balanceUpdated[*tx.To()] = balanceFee[*tx.To()]
totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee)
@ -1049,10 +1046,7 @@ func (env *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Ad
txs.Shift()
}
if tokenFeeUsed {
fee := new(big.Int).SetUint64(gas)
if env.header.Number.Cmp(common.TIPTRC21Fee) > 0 {
fee = fee.Mul(fee, common.TRC21GasPrice)
}
fee := common.GetGasFee(env.header.Number.Uint64(), gas)
balanceFee[*tx.To()] = new(big.Int).Sub(balanceFee[*tx.To()], fee)
balanceUpdated[*tx.To()] = balanceFee[*tx.To()]
totalFeeUsed = totalFeeUsed.Add(totalFeeUsed, fee)