github.com/annchain/OG@v0.0.9/vm/vm_test/multi_contract_test.go (about)

     1  package vm_test
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"github.com/annchain/OG/common"
     7  	"github.com/annchain/OG/common/math"
     8  	"github.com/annchain/OG/vm/eth/core/vm"
     9  	"github.com/annchain/OG/vm/ovm"
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"strconv"
    13  	"testing"
    14  )
    15  
    16  func TestMultiContract(t *testing.T) {
    17  	from := common.HexToAddress("0xABCDEF88")
    18  	coinBase := common.HexToAddress("0x1234567812345678AABBCCDDEEFF998877665544")
    19  
    20  	tracer := vm.NewStructLogger(&vm.LogConfig{
    21  		Debug: true,
    22  	})
    23  	ldb := DefaultLDB(from, coinBase)
    24  
    25  	contracts := make(map[string]common.Address)
    26  
    27  	//for _, file := range ([]string{"ABBToken", "owned", "SafeMath", "TokenCreator", "TokenERC20"}) {
    28  	for _, file := range []string{"ABBToken"} {
    29  		var params []byte
    30  
    31  		switch file {
    32  		case "ABBToken":
    33  			params = EncodeParams([]interface{}{1, "AAAAAA", "ZZZZZZ"})
    34  			fmt.Println(hex.Dump(params))
    35  		}
    36  
    37  		rt := &Runtime{
    38  			Tracer:    tracer,
    39  			VmContext: ovm.NewOVMContext(&ovm.DefaultChainContext{}, &coinBase, ldb),
    40  			TxContext: &ovm.TxContext{
    41  				From: common.HexToAddress("0xABCDEF88"),
    42  				//To:       common.HexToAddress("0x02"),
    43  				Value:      math.NewBigInt(0),
    44  				Data:       readFile(file + ".bin"),
    45  				GasPrice:   math.NewBigInt(1),
    46  				GasLimit:   DefaultGasLimit,
    47  				Coinbase:   coinBase,
    48  				SequenceID: 0,
    49  			},
    50  		}
    51  		_, contractAddr, _, err := DeployContract(file+".bin", from, coinBase, rt, params)
    52  		assert.NoError(t, err)
    53  		contracts[file] = contractAddr
    54  
    55  		switch file {
    56  		case "ABBToken":
    57  			fmt.Println(ldb.String())
    58  			//vm.WriteTrace(os.Stdout, tracer.Logs)
    59  		}
    60  
    61  	}
    62  	rt := &Runtime{
    63  		Tracer:    tracer,
    64  		VmContext: ovm.NewOVMContext(&ovm.DefaultChainContext{}, &coinBase, ldb),
    65  		TxContext: &ovm.TxContext{
    66  			From:       common.HexToAddress("0xABCDEF88"),
    67  			To:         contracts["TokenERC20"],
    68  			Value:      math.NewBigInt(0),
    69  			GasPrice:   math.NewBigInt(1),
    70  			GasLimit:   DefaultGasLimit,
    71  			Coinbase:   coinBase,
    72  			SequenceID: 0,
    73  		},
    74  	}
    75  
    76  	// query symbol name
    77  	//ret, _, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "95d89b41", nil)
    78  	//ret, _, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "18160ddd", nil)
    79  	//ret, _, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "8da5cb5b", nil)
    80  	// get balance
    81  	//params := EncodeParams([]interface{}{common.HexToAddress("0xABCDEF88")})
    82  	//ret, _, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "70a08231", params)
    83  
    84  	//symbol
    85  	{
    86  		ret, leftGas, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "95d89b41", nil)
    87  		dump(t, ldb, ret, leftGas, err)
    88  	}
    89  	//totalsupply
    90  	{
    91  		ret, leftGas, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "18160ddd", nil)
    92  		dump(t, ldb, ret, leftGas, err)
    93  	}
    94  	//owner
    95  	{
    96  		ret, leftGas, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "8da5cb5b", nil)
    97  		dump(t, ldb, ret, leftGas, err)
    98  	}
    99  	//frozenAccount
   100  	//{
   101  	//	params := EncodeParams([]interface{}{common.HexToAddress("0xABCDEF88")})
   102  	//	ret, _, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "b414d4b6", params)
   103  	//	dump(t, ldb, ret, err)
   104  	//}
   105  	//freezeAccount
   106  	//{
   107  	//	params := EncodeParams([]interface{}{common.HexToAddress("0xABCDEF88"), true})
   108  	//	ret, _, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "e724529c", params)
   109  	//	dump(t, ldb, ret, err)
   110  	//}
   111  	////frozenAccount
   112  	//{
   113  	//	params := EncodeParams([]interface{}{common.HexToAddress("0xABCDEF88")})
   114  	//	ret, _, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "b414d4b6", params)
   115  	//	dump(t, ldb, ret, err)
   116  	//}
   117  	//burn
   118  	//{
   119  	//	params := EncodeParams([]interface{}{100000000})
   120  	//	ret, _, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "42966c68", params)
   121  	//	dump(t, ldb, ret, err)
   122  	//}
   123  	//burn2
   124  	//{
   125  	//	params := EncodeParams([]interface{}{100000000})
   126  	//	ret, _, err := CallContract(contracts["ABBToken"], common.HexToAddress("0xDEADBEEF"), coinBase, rt, math.NewBigInt(0), "42966c68", params)
   127  	//	dump(t, ldb, ret, err)
   128  	//}
   129  	// burn
   130  	//{
   131  	//	params := EncodeParams([]interface{}{100000000})
   132  	//	ret, _, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "42966c68", params)
   133  	//	dump(t, ldb, ret, err)
   134  	//}
   135  	// query balance
   136  	{
   137  		params := EncodeParams([]interface{}{common.HexToAddress("0xABCDEF88")})
   138  		ret, leftGas, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "70a08231", params)
   139  		dump(t, ldb, ret, leftGas, err)
   140  	}
   141  
   142  	// transfer
   143  	{
   144  		u64, err := strconv.ParseUint("0101", 16, 64)
   145  		assert.NoError(t, err)
   146  		params := EncodeParams([]interface{}{common.HexToAddress("0xABCDEF87"), u64})
   147  		ret, leftGas, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "a9059cbb", params)
   148  		dump(t, ldb, ret, leftGas, err)
   149  	}
   150  	// query again
   151  	{
   152  		params := EncodeParams([]interface{}{common.HexToAddress("0xABCDEF88")})
   153  		ret, leftGas, err := CallContract(contracts["ABBToken"], from, coinBase, rt, math.NewBigInt(0), "70a08231", params)
   154  		dump(t, ldb, ret, leftGas, err)
   155  	}
   156  }
   157  
   158  func TestInterCall(t *testing.T) {
   159  	from := common.HexToAddress("0xABCDEF88")
   160  	coinBase := common.HexToAddress("0x1234567812345678AABBCCDDEEFF998877665544")
   161  
   162  	tracer := vm.NewStructLogger(&vm.LogConfig{
   163  		Debug: true,
   164  	})
   165  	ldb := DefaultLDB(from, coinBase)
   166  
   167  	contracts := make(map[string]common.Address)
   168  
   169  	for _, file := range []string{"C1", "C2"} {
   170  
   171  		var params []byte
   172  
   173  		rt := &Runtime{
   174  			Tracer:    tracer,
   175  			VmContext: ovm.NewOVMContext(&ovm.DefaultChainContext{}, &coinBase, ldb),
   176  			TxContext: &ovm.TxContext{
   177  				From:       from,
   178  				Value:      math.NewBigInt(0),
   179  				Data:       readFile(file + ".bin"),
   180  				GasPrice:   math.NewBigInt(1),
   181  				GasLimit:   DefaultGasLimit,
   182  				Coinbase:   coinBase,
   183  				SequenceID: 0,
   184  			},
   185  		}
   186  		_, contractAddr, _, err := DeployContract(file+".bin", from, coinBase, rt, params)
   187  		assert.NoError(t, err)
   188  		contracts[file] = contractAddr
   189  	}
   190  
   191  	rt := &Runtime{
   192  		Tracer:    tracer,
   193  		VmContext: ovm.NewOVMContext(&ovm.DefaultChainContext{}, &coinBase, ldb),
   194  		TxContext: &ovm.TxContext{
   195  			From:       common.HexToAddress("0xABCDEF88"),
   196  			To:         contracts["TokenERC20"],
   197  			Value:      math.NewBigInt(0),
   198  			GasPrice:   math.NewBigInt(1),
   199  			GasLimit:   DefaultGasLimit,
   200  			Coinbase:   coinBase,
   201  			SequenceID: 0,
   202  		},
   203  	}
   204  
   205  	{
   206  		ret, leftGas, err := CallContract(contracts["C1"], from, coinBase, rt, math.NewBigInt(0), "c27fc305", nil)
   207  		dump(t, ldb, ret, leftGas, err)
   208  	}
   209  	{
   210  		params := EncodeParams([]interface{}{contracts["C1"]})
   211  		ret, leftGas, err := CallContract(contracts["C2"], from, coinBase, rt, math.NewBigInt(0), "c3642756", params)
   212  		dump(t, ldb, ret, leftGas, err)
   213  	}
   214  
   215  }