2021-04-19 08:32:36 +00:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-04-21 13:05:24 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2021-04-19 08:32:36 +00:00
|
|
|
"net/http"
|
2022-01-28 13:51:35 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
sdtypes "github.com/ethereum/go-ethereum/statediff/types"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
2021-04-19 08:32:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ContractDeployed struct {
|
2021-04-19 20:07:38 +00:00
|
|
|
Address string `json:"address"`
|
|
|
|
TransactionHash string `json:"txHash"`
|
|
|
|
BlockNumber int `json:"blockNumber"`
|
|
|
|
BlockHash string `json:"blockHash"`
|
2021-04-19 08:32:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 11:00:10 +00:00
|
|
|
type ContractDestroyed struct {
|
2021-09-23 10:36:09 +00:00
|
|
|
BlockNumber int64 `json:"blockNumber"`
|
2021-09-06 14:44:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 13:05:24 +00:00
|
|
|
type Tx struct {
|
|
|
|
From string `json:"from"`
|
|
|
|
To string `json:"to"`
|
|
|
|
Value *big.Int `json:"value"`
|
|
|
|
TransactionHash string `json:"txHash"`
|
|
|
|
BlockNumber int `json:"blockNumber"`
|
|
|
|
BlockHash string `json:"blockHash"`
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:18:16 +00:00
|
|
|
type StorageKey struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
}
|
|
|
|
|
2022-02-03 09:58:26 +00:00
|
|
|
type CountIncremented struct {
|
|
|
|
BlockNumber int64 `json:"blockNumber"`
|
|
|
|
}
|
|
|
|
|
2021-04-21 13:05:24 +00:00
|
|
|
const srvUrl = "http://localhost:3000"
|
|
|
|
|
2021-04-19 08:32:36 +00:00
|
|
|
func DeployContract() (*ContractDeployed, error) {
|
2021-04-21 13:05:24 +00:00
|
|
|
res, err := http.Get(fmt.Sprintf("%s/v1/deployContract", srvUrl))
|
2021-04-19 08:32:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
var contract ContractDeployed
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(res.Body)
|
|
|
|
err = decoder.Decode(&contract)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &contract, nil
|
|
|
|
}
|
2021-04-21 13:05:24 +00:00
|
|
|
|
2021-09-22 11:00:10 +00:00
|
|
|
func DestroyContract(addr string) (*ContractDestroyed, error) {
|
|
|
|
res, err := http.Get(fmt.Sprintf("%s/v1/destroyContract?addr=%s", srvUrl, addr))
|
2021-09-06 14:44:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2021-09-22 11:00:10 +00:00
|
|
|
var data ContractDestroyed
|
2021-09-06 14:44:06 +00:00
|
|
|
decoder := json.NewDecoder(res.Body)
|
|
|
|
|
|
|
|
return &data, decoder.Decode(&data)
|
|
|
|
}
|
|
|
|
|
2021-04-21 13:05:24 +00:00
|
|
|
func SendEth(to string, value string) (*Tx, error) {
|
|
|
|
res, err := http.Get(fmt.Sprintf("%s/v1/sendEth?to=%s&value=%s", srvUrl, to, value))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
var tx Tx
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(res.Body)
|
|
|
|
err = decoder.Decode(&tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tx, nil
|
|
|
|
}
|
2022-01-27 14:18:16 +00:00
|
|
|
|
|
|
|
func DeploySLVContract() (*ContractDeployed, error) {
|
|
|
|
res, err := http.Get(fmt.Sprintf("%s/v1/deploySLVContract", srvUrl))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
var contract ContractDeployed
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(res.Body)
|
|
|
|
err = decoder.Decode(&contract)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &contract, nil
|
|
|
|
}
|
|
|
|
|
2022-02-03 09:58:26 +00:00
|
|
|
func IncrementCount(addr string, count string) (*CountIncremented, error) {
|
|
|
|
res, err := http.Get(fmt.Sprintf("%s/v1/incrementCount%s?addr=%s", srvUrl, count, addr))
|
2022-01-27 14:18:16 +00:00
|
|
|
if err != nil {
|
2022-02-03 09:58:26 +00:00
|
|
|
return nil, err
|
2022-01-27 14:18:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 09:58:26 +00:00
|
|
|
var blockNumber CountIncremented
|
2022-01-27 14:18:16 +00:00
|
|
|
|
2022-02-03 09:58:26 +00:00
|
|
|
decoder := json.NewDecoder(res.Body)
|
|
|
|
err = decoder.Decode(&blockNumber)
|
2022-01-27 14:18:16 +00:00
|
|
|
if err != nil {
|
2022-02-03 09:58:26 +00:00
|
|
|
return nil, err
|
2022-01-27 14:18:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 09:58:26 +00:00
|
|
|
return &blockNumber, nil
|
2022-01-27 14:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetStorageSlotKey(contract string, label string) (*StorageKey, error) {
|
|
|
|
res, err := http.Get(fmt.Sprintf("%s/v1/getStorageKey?contract=%s&label=%s", srvUrl, contract, label))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
var key StorageKey
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(res.Body)
|
|
|
|
err = decoder.Decode(&key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &key, nil
|
|
|
|
}
|
2022-01-28 13:51:35 +00:00
|
|
|
|
|
|
|
func ClearWatchedAddresses(gethRPCClient *rpc.Client) error {
|
|
|
|
gethMethod := "statediff_watchAddress"
|
|
|
|
args := []sdtypes.WatchAddressArg{}
|
|
|
|
|
|
|
|
// Clear watched addresses
|
2022-02-03 09:58:26 +00:00
|
|
|
gethErr := gethRPCClient.Call(nil, gethMethod, statediff.Clear, args)
|
2022-01-28 13:51:35 +00:00
|
|
|
if gethErr != nil {
|
|
|
|
return gethErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|