github.com/klaytn/klaytn@v1.10.2/node/sc/vt_contract_test.go (about)

     1  // Copyright 2019 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 sc
    18  
    19  import (
    20  	"io/ioutil"
    21  	"math/big"
    22  	"os"
    23  	"strconv"
    24  	"testing"
    25  
    26  	"github.com/klaytn/klaytn/accounts/abi/bind"
    27  	"github.com/klaytn/klaytn/common"
    28  	sctoken "github.com/klaytn/klaytn/contracts/sc_erc20"
    29  	scnft "github.com/klaytn/klaytn/contracts/sc_erc721"
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  // TestTokenPublicVariables checks the results of the public variables.
    34  func TestTokenPublicVariables(t *testing.T) {
    35  	tempDir, err := ioutil.TempDir(os.TempDir(), "sc")
    36  	assert.NoError(t, err)
    37  	defer func() {
    38  		if err := os.RemoveAll(tempDir); err != nil {
    39  			t.Fatalf("fail to delete file %v", err)
    40  		}
    41  	}()
    42  
    43  	info := prepare(t, func(info *testInfo) {
    44  		for i := 0; i < testTxCount; i++ {
    45  			ops[KLAY].request(info, info.localInfo)
    46  		}
    47  	})
    48  	defer info.sim.Close()
    49  
    50  	initSupply, err := info.tokenLocalBridge.INITIALSUPPLY(nil)
    51  	assert.NoError(t, err)
    52  	assert.Equal(t, "1000000000000000000000000000", initSupply.String())
    53  
    54  	allowance, err := info.tokenLocalBridge.Allowance(nil, info.chainAuth.From, info.chainAuth.From)
    55  	assert.NoError(t, err)
    56  	assert.Equal(t, "0", allowance.String())
    57  
    58  	balance, err := info.tokenLocalBridge.BalanceOf(nil, info.nodeAuth.From)
    59  	assert.NoError(t, err)
    60  	assert.Equal(t, "1000000000000000000000000000", balance.String())
    61  
    62  	decimal, err := info.tokenLocalBridge.DECIMALS(nil)
    63  	assert.NoError(t, err)
    64  	assert.Equal(t, uint8(0x12), decimal)
    65  
    66  	name, err := info.tokenLocalBridge.NAME(nil)
    67  	assert.NoError(t, err)
    68  	assert.Equal(t, "ServiceChainToken", name)
    69  
    70  	symbol, err := info.tokenLocalBridge.SYMBOL(nil)
    71  	assert.NoError(t, err)
    72  	assert.Equal(t, "SCT", symbol)
    73  
    74  	_, tx, _, err := sctoken.DeployServiceChainToken(info.nodeAuth, info.sim, common.Address{0})
    75  	assert.NoError(t, err)
    76  	info.sim.Commit()
    77  	assert.NotNil(t, bind.CheckWaitMined(info.sim, tx))
    78  }
    79  
    80  // TestTokenPublicVariables checks the results of the public variables.
    81  func TestNFTPublicVariables(t *testing.T) {
    82  	tempDir, err := ioutil.TempDir(os.TempDir(), "sc")
    83  	assert.NoError(t, err)
    84  	defer func() {
    85  		if err := os.RemoveAll(tempDir); err != nil {
    86  			t.Fatalf("fail to delete file %v", err)
    87  		}
    88  	}()
    89  
    90  	info := prepare(t, func(info *testInfo) {
    91  		for i := 0; i < testTxCount; i++ {
    92  			ops[KLAY].request(info, info.localInfo)
    93  		}
    94  	})
    95  	defer info.sim.Close()
    96  
    97  	_, tx, _, err := scnft.DeployServiceChainNFT(info.nodeAuth, info.sim, common.Address{0})
    98  	assert.NoError(t, err)
    99  	info.sim.Commit()
   100  	assert.NotNil(t, bind.CheckWaitMined(info.sim, tx))
   101  
   102  	balance, err := info.nftLocalBridge.BalanceOf(nil, info.nodeAuth.From)
   103  	assert.Equal(t, strconv.FormatInt(testTxCount, 10), balance.String())
   104  
   105  	bride, err := info.nftLocalBridge.Bridge(nil)
   106  	assert.Equal(t, info.localInfo.address, bride)
   107  
   108  	bof, err := info.nftLocalBridge.BalanceOf(nil, info.nodeAuth.From)
   109  	assert.Equal(t, strconv.FormatInt(testTxCount, 10), bof.String())
   110  
   111  	approved, err := info.nftLocalBridge.GetApproved(&bind.CallOpts{From: info.nodeAuth.From}, big.NewInt(int64(testNFT)))
   112  	assert.Equal(t, common.Address{0}, approved)
   113  
   114  	isApproved, err := info.nftLocalBridge.IsApprovedForAll(nil, info.nodeAuth.From, info.nodeAuth.From)
   115  	assert.Equal(t, false, isApproved)
   116  
   117  	isOwner, err := info.nftLocalBridge.IsOwner(&bind.CallOpts{From: info.nodeAuth.From})
   118  	assert.Equal(t, true, isOwner)
   119  
   120  	name, err := info.nftLocalBridge.Name(nil)
   121  	assert.Equal(t, "ServiceChainNFT", name)
   122  
   123  	owner, err := info.nftLocalBridge.Owner(nil)
   124  	assert.Equal(t, info.nodeAuth.From, owner)
   125  
   126  	ownerOf, err := info.nftLocalBridge.OwnerOf(nil, big.NewInt(int64(testNFT)))
   127  	assert.Equal(t, info.nodeAuth.From, ownerOf)
   128  
   129  	ifid := [4]byte{0}
   130  	sif, err := info.nftLocalBridge.SupportsInterface(nil, ifid)
   131  	assert.Equal(t, false, sif)
   132  
   133  	symbol, err := info.nftLocalBridge.Symbol(nil)
   134  	assert.Equal(t, "SCN", symbol)
   135  
   136  	tindex, err := info.nftLocalBridge.TokenByIndex(nil, big.NewInt(int64(0)))
   137  	assert.Equal(t, strconv.FormatInt(testNFT, 10), tindex.String())
   138  
   139  	ownerByIndex, err := info.nftLocalBridge.TokenOfOwnerByIndex(nil, info.nodeAuth.From, big.NewInt(int64(0)))
   140  	assert.Equal(t, strconv.FormatInt(testNFT, 10), ownerByIndex.String())
   141  
   142  	uri, err := info.nftLocalBridge.TokenURI(nil, big.NewInt(int64(0)))
   143  	assert.Equal(t, "", uri)
   144  
   145  	totalSupply, err := info.nftLocalBridge.TotalSupply(nil)
   146  	assert.Equal(t, strconv.FormatInt(testTxCount, 10), totalSupply.String())
   147  }