Skip to content

Commit

Permalink
accounts/abi/bind/backends: replace NewSimulatedBackend with NewXDCSi…
Browse files Browse the repository at this point in the history
…mulatedBackend
  • Loading branch information
gzliudan committed Jan 24, 2025
1 parent b5f6104 commit c1913bb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 28 deletions.
2 changes: 1 addition & 1 deletion accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func NewXDCSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64, chainConfi
return backend
}

// NewSimulatedBackend creates a new binding backend based on the given database
// SimulOldNewSimulatedBackendatedBackend creates a new binding backend based on the given database
// and uses a simulated blockchain for testing purposes.
// A simulated backend always uses chainID 1337.
func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend {
Expand Down
33 changes: 24 additions & 9 deletions accounts/abi/bind/backends/simulated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ func TestNewSimulatedBackend(t *testing.T) {

func TestAdjustTime(t *testing.T) {
t.Parallel()
sim := NewSimulatedBackend(
core.GenesisAlloc{}, 10000000,
sim := NewXDCSimulatedBackend(
core.GenesisAlloc{},
10000000,
params.TestXDPoSMockChainConfig,
)
defer sim.Close()

Expand Down Expand Up @@ -223,8 +225,10 @@ func TestBalanceAt(t *testing.T) {

func TestBlockByHash(t *testing.T) {
t.Parallel()
sim := NewSimulatedBackend(
core.GenesisAlloc{}, 10000000,
sim := NewXDCSimulatedBackend(
core.GenesisAlloc{},
10000000,
params.TestXDPoSMockChainConfig,
)
defer sim.Close()
bgCtx := context.Background()
Expand All @@ -245,8 +249,10 @@ func TestBlockByHash(t *testing.T) {

func TestBlockByNumber(t *testing.T) {
t.Parallel()
sim := NewSimulatedBackend(
core.GenesisAlloc{}, 10000000,
sim := NewXDCSimulatedBackend(
core.GenesisAlloc{},
10000000,
params.TestXDPoSMockChainConfig,
)
defer sim.Close()
bgCtx := context.Background()
Expand Down Expand Up @@ -439,7 +445,11 @@ func TestEstimateGas(t *testing.T) {
// opts := bind.NewKeyedTransactor(key)
opts, _ := bind.NewKeyedTransactorWithChainID(key, big.NewInt(1337))

sim := NewXDCSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(params.Ether)}}, 10000000, params.TestXDPoSMockChainConfig)
sim := NewXDCSimulatedBackend(
core.GenesisAlloc{addr: {Balance: big.NewInt(params.Ether)}},
10000000,
params.TestXDPoSMockChainConfig,
)
defer sim.Close()

parsed, _ := abi.JSON(strings.NewReader(contractAbi))
Expand Down Expand Up @@ -546,7 +556,11 @@ func TestEstimateGasWithPrice(t *testing.T) {
key, _ := crypto.GenerateKey()
addr := crypto.PubkeyToAddress(key.PublicKey)

sim := NewSimulatedBackend(core.GenesisAlloc{addr: {Balance: big.NewInt(params.Ether*2 + 2e17)}}, 10000000)
sim := NewXDCSimulatedBackend(
core.GenesisAlloc{addr: {Balance: big.NewInt(params.Ether*2 + 2e17)}},
10000000,
params.TestXDPoSMockChainConfig,
)
defer sim.Close()

recipient := common.HexToAddress("deadbeef")
Expand Down Expand Up @@ -893,9 +907,10 @@ func TestTransactionReceipt(t *testing.T) {

func TestSuggestGasPrice(t *testing.T) {
t.Parallel()
sim := NewSimulatedBackend(
sim := NewXDCSimulatedBackend(
core.GenesisAlloc{},
10000000,
params.TestXDPoSMockChainConfig,
)
defer sim.Close()
bgCtx := context.Background()
Expand Down
1 change: 1 addition & 0 deletions contracts/randomize/randomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestRandomize(t *testing.T) {

func TestSendTxRandomizeSecretAndOpening(t *testing.T) {
genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(1000000000000)}}
// TODO(daniel): replace NewSimulatedBackend with NewXDCSimulatedBackend
backend := backends.NewSimulatedBackend(genesis, 42000000)
backend.Commit()
signer := types.HomesteadSigner{}
Expand Down
11 changes: 8 additions & 3 deletions contracts/tests/Inherited_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/core"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/params"
)

var (
Expand All @@ -26,9 +27,13 @@ func TestPriceFeed(t *testing.T) {

common.TIPXDCXCancellationFee = big.NewInt(0)
// init genesis
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{
mainAddr: {Balance: big.NewInt(0).Mul(big.NewInt(10000000000000), big.NewInt(10000000000000))},
}, 42000000)
contractBackend := backends.NewXDCSimulatedBackend(
core.GenesisAlloc{
mainAddr: {Balance: big.NewInt(0).Mul(big.NewInt(10000000000000), big.NewInt(10000000000000))},
},
42000000,
params.TestXDPoSMockChainConfig,
)
transactOpts := bind.NewKeyedTransactor(mainKey)
// deploy payer swap SMC
addr, contract, err := DeployMyInherited(transactOpts, contractBackend)
Expand Down
38 changes: 23 additions & 15 deletions contracts/trc21issuer/trc21issuer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/core"
"github.com/XinFinOrg/XDPoSChain/crypto"
"github.com/XinFinOrg/XDPoSChain/params"
)

var (
Expand All @@ -29,29 +30,35 @@ var (
)

func TestFeeTxWithTRC21Token(t *testing.T) {

// init genesis
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{
mainAddr: {Balance: big.NewInt(0).Mul(big.NewInt(10000000000000), big.NewInt(10000000000000))},
}, 42000000)
contractBackend := backends.NewXDCSimulatedBackend(
core.GenesisAlloc{
mainAddr: {Balance: big.NewInt(0).Mul(big.NewInt(10000000000000), big.NewInt(10000000000000))},
},
42000000,
params.TestXDPoSMockChainConfig,
)
transactOpts := bind.NewKeyedTransactor(mainKey)

// deploy payer swap SMC
trc21IssuerAddr, trc21Issuer, err := DeployTRC21Issuer(transactOpts, contractBackend, minApply)

//set contract address to config
common.TRC21IssuerSMC = trc21IssuerAddr
if err != nil {
t.Fatal("can't deploy smart contract: ", err)
t.Fatal("can't deploy TRC21Issuer contract, err:", err)
}
contractBackend.Commit()

// set contract address to config
common.TRC21IssuerSMC = trc21IssuerAddr
cap := big.NewInt(0).Mul(big.NewInt(10000000), big.NewInt(10000000000000))
TRC21fee := big.NewInt(100)
// deploy a TRC21 SMC

// deploy a TRC21 SMC
trc21TokenAddr, trc21, err := DeployTRC21(transactOpts, contractBackend, "TEST", "XDC", 18, cap, TRC21fee)
if err != nil {
t.Fatal("can't deploy smart contract: ", err)
t.Fatal("can't deploy TRC21 contract, err:", err)
}
contractBackend.Commit()

// add trc21 address to list token trc21Issuer
trc21Issuer.TransactOpts.Value = minApply
_, err = trc21Issuer.Apply(trc21TokenAddr)
Expand All @@ -60,19 +67,20 @@ func TestFeeTxWithTRC21Token(t *testing.T) {
}
contractBackend.Commit()

//check trc21 SMC balance
// check trc21 SMC balance
balance, err := contractBackend.BalanceAt(context.TODO(), trc21IssuerAddr, nil)
if err != nil || balance.Cmp(minApply) != 0 {
t.Fatal("can't get balance in trc21Issuer SMC: ", err, "got", balance, "wanted", minApply)
}

//check balance fee
// check balance fee
balanceIssuerFee, err := trc21Issuer.GetTokenCapacity(trc21TokenAddr)
if err != nil || balanceIssuerFee.Cmp(minApply) != 0 {
t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", minApply)
}
trc21Issuer.TransactOpts.Value = big.NewInt(0)
airDropAmount := big.NewInt(1000000000)

// airdrop token trc21 to a address no XDC
tx, err := trc21.Transfer(airdropAddr, airDropAmount)
if err != nil {
Expand Down Expand Up @@ -100,7 +108,7 @@ func TestFeeTxWithTRC21Token(t *testing.T) {
if balanceIssuerFee.Cmp(remainFee) != 0 {
t.Fatal("check balance token fee in smart contract: got", balanceIssuerFee, "wanted", remainFee)
}
//check trc21 SMC balance
// check trc21 SMC balance
balance, err = contractBackend.BalanceAt(context.TODO(), trc21IssuerAddr, nil)
if err != nil || balance.Cmp(remainFee) != 0 {
t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", remainFee)
Expand Down Expand Up @@ -137,12 +145,12 @@ func TestFeeTxWithTRC21Token(t *testing.T) {
}
fee = common.GetGasFee(receipt.Logs[0].BlockNumber, receipt.GasUsed)
remainFee = big.NewInt(0).Sub(remainFee, fee)
//check balance fee
// check balance fee
balanceIssuerFee, err = trc21Issuer.GetTokenCapacity(trc21TokenAddr)
if err != nil || balanceIssuerFee.Cmp(remainFee) != 0 {
t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", remainFee)
}
//check trc21 SMC balance
// check trc21 SMC balance
balance, err = contractBackend.BalanceAt(context.TODO(), trc21IssuerAddr, nil)
if err != nil || balance.Cmp(remainFee) != 0 {
t.Fatal("can't get balance token fee in smart contract: ", err, "got", balanceIssuerFee, "wanted", remainFee)
Expand Down

0 comments on commit c1913bb

Please sign in to comment.