Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions relayer/gotron-sdk/pkg/http/fullnode/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fullnode

import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -33,7 +34,7 @@ type DeployContractResponse struct {
ContractAddress string `json:"contract_address"`
}

func (tc *Client) DeployContract(ownerAddress address.Address, contractName, abiJson, bytecode string, oeLimit, curPercent, feeLimit int, params []interface{}) (*DeployContractResponse, error) {
func (tc *Client) DeployContract(ctx context.Context, ownerAddress address.Address, contractName, abiJson, bytecode string, oeLimit, curPercent, feeLimit int, params []interface{}) (*DeployContractResponse, error) {
parsedABI, err := eABI.JSON(bytes.NewReader([]byte(abiJson)))
if err != nil {
return nil, fmt.Errorf("failed to parse ABI: %w", err)
Expand Down Expand Up @@ -61,7 +62,7 @@ func (tc *Client) DeployContract(ownerAddress address.Address, contractName, abi
}

response := DeployContractResponse{}
if err := tc.Post("/deploycontract", reqBody, &response); err != nil {
if err := tc.Post(ctx, "/deploycontract", reqBody, &response); err != nil {
return nil, err
}

Expand All @@ -85,9 +86,9 @@ type GetContractResponse struct {
CodeHash string `json:"code_hash,omitempty"` // code hash
}

func (tc *Client) GetContract(contractAddress address.Address) (*GetContractResponse, error) {
func (tc *Client) GetContract(ctx context.Context, contractAddress address.Address) (*GetContractResponse, error) {
contractInfo := GetContractResponse{}
err := tc.Post("/getcontract",
err := tc.Post(ctx, "/getcontract",
&GetContractRequest{
Value: contractAddress.String(),
Visible: true,
Expand Down Expand Up @@ -128,7 +129,7 @@ type TriggerSmartContractResponse struct {
Transaction *common.Transaction `json:"transaction"`
}

func (tc *Client) TriggerSmartContract(from, contractAddress address.Address, method string, params []any, feeLimit int32, tAmount int64) (*TriggerSmartContractResponse, error) {
func (tc *Client) TriggerSmartContract(ctx context.Context, from, contractAddress address.Address, method string, params []any, feeLimit int32, tAmount int64) (*TriggerSmartContractResponse, error) {
paramBytes, err := abi.GetPaddedParam(params)
if err != nil {
return nil, fmt.Errorf("failed to encode params: %w", err)
Expand All @@ -143,7 +144,7 @@ func (tc *Client) TriggerSmartContract(from, contractAddress address.Address, me
Visible: true,
}
contractResponse := TriggerSmartContractResponse{}
err = tc.Post("/triggersmartcontract", tcRequest, &contractResponse)
err = tc.Post(ctx, "/triggersmartcontract", tcRequest, &contractResponse)
if err != nil {
return nil, err
}
Expand All @@ -158,7 +159,7 @@ type BroadcastResponse struct {
Message string `json:"message"`
}

func (tc *Client) BroadcastTransaction(reqBody *common.Transaction) (*BroadcastResponse, error) {
func (tc *Client) BroadcastTransaction(ctx context.Context, reqBody *common.Transaction) (*BroadcastResponse, error) {
if reqBody == nil {
return nil, errors.New("empty body")
}
Expand All @@ -172,7 +173,7 @@ func (tc *Client) BroadcastTransaction(reqBody *common.Transaction) (*BroadcastR
}

response := BroadcastResponse{}
err := tc.Post("/broadcasttransaction", reqBody, &response)
err := tc.Post(ctx, "/broadcasttransaction", reqBody, &response)

if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion relayer/gotron-sdk/pkg/http/fullnode/contract_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fullnode

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -83,10 +84,11 @@ func TestDeployContract(t *testing.T) {
}))
defer testServer.Close()

ctx := context.Background()
fullnodeClient := NewClient(testServer.URL, httpClient)
owner, err := address.StringToAddress("TVSTZkvVosqh4YHLwHmmNuqeyn967aE2iv")
assert.NoError(t, err)
res, err := fullnodeClient.DeployContract(owner, "test", "[]", "0x1234", 0, 0, 0, nil)
res, err := fullnodeClient.DeployContract(ctx, owner, "test", "[]", "0x1234", 0, 0, 0, nil)
assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, "41306d7f39ffc367edb1dee2a9782847e1579795a0", res.ContractAddress)
Expand Down
6 changes: 4 additions & 2 deletions relayer/gotron-sdk/pkg/http/fullnode/energy.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package fullnode

import "context"

type EnergyPrices struct {
Prices string `json:"prices"` // All historical energy unit price information. Each unit price change is separated by a comma. Before the colon is the millisecond timestamp, and after the colon is the energy unit price in sun.
}

func (tc *Client) GetEnergyPrices() (*EnergyPrices, error) {
func (tc *Client) GetEnergyPrices(ctx context.Context) (*EnergyPrices, error) {
energyPrices := EnergyPrices{}
err := tc.Get("/getenergyprices", &energyPrices)
err := tc.Get(ctx, "/getenergyprices", &energyPrices)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion relayer/gotron-sdk/pkg/http/fullnode/energy_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fullnode

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand All @@ -20,8 +21,9 @@ func TestGetEnergyPrices(t *testing.T) {
}))
defer testServer.Close()

ctx := context.Background()
fullnodeClient := NewClient(testServer.URL, httpClient)
res, err := fullnodeClient.GetEnergyPrices()
res, err := fullnodeClient.GetEnergyPrices(ctx)
assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, "0:100,1575871200000:10,1606537680000:40,1614238080000:140,1635739080000:280,1681895880000:420", res.Prices)
Expand Down
5 changes: 3 additions & 2 deletions relayer/gotron-sdk/pkg/http/fullnode/transfer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fullnode

import (
"context"
"errors"

"github.com/fbsobreira/gotron-sdk/pkg/address"
Expand All @@ -14,9 +15,9 @@ type CreateTransactionRequest struct {
Visible bool `json:"visible"`
}

func (tc *Client) Transfer(fromAddress address.Address, toAddress address.Address, amount int64) (*common.Transaction, error) {
func (tc *Client) Transfer(ctx context.Context, fromAddress address.Address, toAddress address.Address, amount int64) (*common.Transaction, error) {
tx := common.Transaction{}
err := tc.Post("/createtransaction",
err := tc.Post(ctx, "/createtransaction",
&CreateTransactionRequest{
OwnerAddress: fromAddress.String(),
ToAddress: toAddress.String(),
Expand Down
4 changes: 3 additions & 1 deletion relayer/gotron-sdk/pkg/http/fullnode/transfer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fullnode

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -42,14 +43,15 @@ func TestTransfer(t *testing.T) {
}))
defer testServer.Close()

ctx := context.Background()
fullnodeClient := NewClient(testServer.URL, httpClient)
from, err := address.StringToAddress("TZ4UXDV5ZhNW7fb2AMSbgfAEZ7hWsnYS2g")
assert.NoError(t, err)
to, err := address.StringToAddress("TPswDDCAWhJAZGdHPidFg5nEf8TkNToDX1")
assert.NoError(t, err)
amount := int64(1000)

res, err := fullnodeClient.Transfer(from, to, amount)
res, err := fullnodeClient.Transfer(ctx, from, to, amount)
assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, 1, len(res.RawData.Contract))
Expand Down
5 changes: 3 additions & 2 deletions relayer/gotron-sdk/pkg/http/soliditynode/account.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package soliditynode

import (
"context"
"github.com/fbsobreira/gotron-sdk/pkg/address"
)

Expand Down Expand Up @@ -105,9 +106,9 @@ type GetAccountResponse struct {
FreeAssetNetUsagev2 []Asset `json:"free_asset_net_usageV2"` // <string, int64> The amount of free bandwidth consumed by account transferring TRC10 tokens
}

func (tc *Client) GetAccount(accountAddress address.Address) (*GetAccountResponse, error) {
func (tc *Client) GetAccount(ctx context.Context, accountAddress address.Address) (*GetAccountResponse, error) {
getAccountResponse := GetAccountResponse{}
err := tc.Post("/getaccount", &GetAccountRequest{
err := tc.Post(ctx, "/getaccount", &GetAccountRequest{
Address: accountAddress.String(),
Visible: true,
}, &getAccountResponse)
Expand Down
4 changes: 3 additions & 1 deletion relayer/gotron-sdk/pkg/http/soliditynode/account_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package soliditynode

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand All @@ -18,10 +19,11 @@ func TestGetAccount(t *testing.T) {
}))
defer testServer.Close()

ctx := context.Background()
soliditynodeClient := NewClient(testServer.URL, httpClient)
addr, err := address.StringToAddress("TVSTZkvVosqh4YHLwHmmNuqeyn967aE2iv")
assert.NoError(t, err)
res, err := soliditynodeClient.GetAccount(addr)
res, err := soliditynodeClient.GetAccount(ctx, addr)
assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, "TVSTZkvVosqh4YHLwHmmNuqeyn967aE2iv", res.Address)
Expand Down
9 changes: 5 additions & 4 deletions relayer/gotron-sdk/pkg/http/soliditynode/block.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package soliditynode

import (
"context"
"errors"

"github.com/fbsobreira/gotron-sdk/pkg/http/common"
Expand Down Expand Up @@ -28,9 +29,9 @@ type Block struct {
BlockHeader *BlockHeader `json:"block_header,omitempty"`
}

func (tc *Client) GetNowBlock() (*Block, error) {
func (tc *Client) GetNowBlock(ctx context.Context) (*Block, error) {
block := Block{}
err := tc.Get("/getnowblock", &block)
err := tc.Get(ctx, "/getnowblock", &block)
if err != nil {
return nil, err
}
Expand All @@ -45,9 +46,9 @@ type GetBlockByNumRequest struct {
Num int32 `json:"num"` // defined as int32 in https://developers.tron.network/reference/wallet-getblockbynum
}

func (tc *Client) GetBlockByNum(num int32) (*Block, error) {
func (tc *Client) GetBlockByNum(ctx context.Context, num int32) (*Block, error) {
block := Block{}
err := tc.Post("/getblockbynum",
err := tc.Post(ctx, "/getblockbynum",
&GetBlockByNumRequest{
Num: num,
}, &block)
Expand Down
8 changes: 5 additions & 3 deletions relayer/gotron-sdk/pkg/http/soliditynode/block_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package soliditynode

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -32,7 +33,8 @@ func TestGetNowBlock(t *testing.T) {
defer testServer.Close()

soliditynodeClient := NewClient(testServer.URL, httpClient)
res, err := soliditynodeClient.GetNowBlock()
ctx := context.Background()
res, err := soliditynodeClient.GetNowBlock(ctx)
assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, int64(52799248), res.BlockHeader.RawData.Number)
Expand All @@ -44,9 +46,9 @@ func TestGetBlockByNum(t *testing.T) {
fmt.Fprint(w, blockResponse)
}))
defer testServer.Close()

ctx := context.Background()
soliditynodeClient := NewClient(testServer.URL, httpClient)
res, err := soliditynodeClient.GetBlockByNum(52799248)
res, err := soliditynodeClient.GetBlockByNum(ctx, 52799248)
assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, int64(52799248), res.BlockHeader.RawData.Number)
Expand Down
15 changes: 8 additions & 7 deletions relayer/gotron-sdk/pkg/http/soliditynode/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package soliditynode

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -20,7 +21,7 @@ func NewClient(baseURL string, client *http.Client) *Client {
}
}

func (tc *Client) request(method string, path string, reqBody interface{}, responseBody interface{}) error {
func (tc *Client) request(ctx context.Context, method string, path string, reqBody interface{}, responseBody interface{}) error {
endpoint := tc.BaseURL + path

var req *http.Request
Expand All @@ -33,13 +34,13 @@ func (tc *Client) request(method string, path string, reqBody interface{}, respo
return fmt.Errorf("failed to marshal JSON request body (%s %s): %w", method, endpoint, err)
}

req, err = http.NewRequest(method, endpoint, bytes.NewBuffer(jsonbytes))
req, err = http.NewRequestWithContext(ctx, method, endpoint, bytes.NewBuffer(jsonbytes))
if err != nil {
return fmt.Errorf("failed to create new HTTP request with body (%s %s): %w", method, endpoint, err)
}
} else {
var err error
req, err = http.NewRequest(method, endpoint, nil)
req, err = http.NewRequestWithContext(ctx, method, endpoint, nil)
if err != nil {
return fmt.Errorf("failed to create new HTTP request (%s %s): %w", method, endpoint, err)
}
Expand Down Expand Up @@ -89,10 +90,10 @@ func (tc *Client) request(method string, path string, reqBody interface{}, respo

}

func (tc *Client) Post(endpoint string, reqBody, responseBody interface{}) error {
return tc.request("POST", endpoint, reqBody, responseBody)
func (tc *Client) Post(ctx context.Context, endpoint string, reqBody, responseBody interface{}) error {
return tc.request(ctx, http.MethodPost, endpoint, reqBody, responseBody)
}

func (tc *Client) Get(endpoint string, responseBody interface{}) error {
return tc.request("GET", endpoint, nil, responseBody)
func (tc *Client) Get(ctx context.Context, endpoint string, responseBody interface{}) error {
return tc.request(ctx, http.MethodGet, endpoint, nil, responseBody)
}
9 changes: 5 additions & 4 deletions relayer/gotron-sdk/pkg/http/soliditynode/contract.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package soliditynode

import (
"context"
"encoding/hex"
"fmt"

Expand Down Expand Up @@ -29,7 +30,7 @@ type TriggerConstantContractResponse struct {
Transaction *common.ExecutedTransaction `json:"transaction"` // Transaction information, refer to GetTransactionByID
}

func (tc *Client) TriggerConstantContract(from, contractAddress address.Address, method string, params []any) (*TriggerConstantContractResponse, error) {
func (tc *Client) TriggerConstantContract(ctx context.Context, from, contractAddress address.Address, method string, params []any) (*TriggerConstantContractResponse, error) {
paramBytes, err := abi.GetPaddedParam(params)
if err != nil {
return nil, fmt.Errorf("failed to encode params: %w", err)
Expand All @@ -42,7 +43,7 @@ func (tc *Client) TriggerConstantContract(from, contractAddress address.Address,
Visible: true,
}
response := TriggerConstantContractResponse{}
err = tc.Post("/triggerconstantcontract", tcRequest, &response)
err = tc.Post(ctx, "/triggerconstantcontract", tcRequest, &response)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -76,7 +77,7 @@ type EnergyEstimateResult struct {
EnergyRequired int64 `json:"energy_required"` // Estimated energy to run the contract
}

func (tc *Client) EstimateEnergy(from, contractAddress address.Address, method string, params []any, tAmount int64) (*EnergyEstimateResult, error) {
func (tc *Client) EstimateEnergy(ctx context.Context, from, contractAddress address.Address, method string, params []any, tAmount int64) (*EnergyEstimateResult, error) {
paramBytes, err := abi.GetPaddedParam(params)
if err != nil {
return nil, fmt.Errorf("failed to encode params: %w", err)
Expand All @@ -91,7 +92,7 @@ func (tc *Client) EstimateEnergy(from, contractAddress address.Address, method s
}

response := EnergyEstimateResult{}
err = tc.Post("/estimateenergy", reqBody, &response)
err = tc.Post(ctx, "/estimateenergy", reqBody, &response)
if err != nil {
return nil, err
}
Expand Down
Loading