github.com/klaytn/klaytn@v1.12.1/contracts/reward/contract/KlaytnReward_test.go (about)

     1  // Copyright 2018 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package contract
    18  
    19  import (
    20  	"context"
    21  	"log"
    22  	"math/big"
    23  	"testing"
    24  
    25  	"github.com/klaytn/klaytn/accounts/abi/bind"
    26  	"github.com/klaytn/klaytn/accounts/abi/bind/backends"
    27  	"github.com/klaytn/klaytn/blockchain"
    28  	"github.com/klaytn/klaytn/crypto"
    29  	"github.com/klaytn/klaytn/params"
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestSmartContract(t *testing.T) {
    34  	// Generate a new random account and a funded simulator
    35  	key, _ := crypto.GenerateKey()
    36  	auth := bind.NewKeyedTransactor(key)
    37  
    38  	key2, _ := crypto.GenerateKey()
    39  	auth2 := bind.NewKeyedTransactor(key2)
    40  
    41  	initialValue := big.NewInt(params.KLAY)
    42  	withdrawValue := big.NewInt(500000000)
    43  
    44  	alloc := blockchain.GenesisAlloc{auth.From: {Balance: initialValue}, auth2.From: {Balance: initialValue}}
    45  	sim := backends.NewSimulatedBackend(alloc)
    46  	defer sim.Close()
    47  
    48  	// Deploy a token contract on the simulated blockchain
    49  	_, _, reward, err := DeployKlaytnReward(auth, sim)
    50  	if err != nil {
    51  		log.Fatalf("Failed to deploy new token contract: %v", err)
    52  	}
    53  	sim.Commit()
    54  
    55  	// Set reward
    56  	tx, err := reward.Reward(&bind.TransactOpts{
    57  		From: auth.From, Signer: auth.Signer,
    58  		Value: withdrawValue,
    59  	}, auth2.From)
    60  	if err != nil {
    61  		log.Fatalf("Failed to call reward : %v", err)
    62  	}
    63  	sim.Commit()
    64  	assert.Equal(t, uint(1), sim.BlockChain().GetReceiptByTxHash(tx.Hash()).Status)
    65  
    66  	// Check the balance before withdrawal
    67  	balance1, err := sim.BalanceAt(context.Background(), auth2.From, nil)
    68  	assert.Nil(t, err)
    69  	assert.Equal(t, initialValue, balance1)
    70  
    71  	// Withdraw reward
    72  	tx2, err := reward.SafeWithdrawal(&bind.TransactOpts{
    73  		From: auth2.From, Signer: auth2.Signer,
    74  		Value: big.NewInt(0),
    75  	})
    76  	if err != nil {
    77  		log.Fatalf("Failed to call reward : %v", err)
    78  	}
    79  	sim.Commit()
    80  	assert.Equal(t, uint(1), sim.BlockChain().GetReceiptByTxHash(tx2.Hash()).Status)
    81  
    82  	// Check the balance after withdrawal
    83  	balance2, err := sim.BalanceAt(context.Background(), auth2.From, nil)
    84  	assert.Nil(t, err)
    85  	assert.Equal(t, new(big.Int).Add(balance1, withdrawValue), balance2)
    86  }