From 2bc9fa7ee3a1eb6dfe6836af4f18c4219868cfd9 Mon Sep 17 00:00:00 2001 From: Regis-Caelum Date: Tue, 31 May 2022 18:13:58 +0530 Subject: [PATCH 1/3] Implement Balance method and corresponding tests Signed-off-by: Regis-Caelum --- client.go | 19 +++++++++++++++++-- client_test.go | 6 ++++++ types.go | 5 +++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 2a7a74b..cabc52e 100644 --- a/client.go +++ b/client.go @@ -35,8 +35,23 @@ func (c *Client) Header(ctx context.Context, height uint64) /* Header */ error { } func (c *Client) Balance(ctx context.Context) error { - _ = balanceEndpoint - return errors.New("method Balance not implemented") + var result balanceRequest + var rpcErr string + + _, err := c.c.R(). + SetContext(ctx). + SetResult(&result). + SetError(&rpcErr). + Get(balanceEndpoint) + if err != nil { + return err + } + + if rpcErr != "" { + return errors.New(rpcErr) + } + + return nil } func (c *Client) SubmitTx(ctx context.Context, tx []byte) /* TxResponse */ error { diff --git a/client_test.go b/client_test.go index c9e97a8..d61ebda 100644 --- a/client_test.go +++ b/client_test.go @@ -53,3 +53,9 @@ func TestSubmitPDF(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, txRes) } + +func TestBalance(t *testing.T) { + //t.Skip() + _, err := NewClient("http://localhost:26658", WithTimeout(30*time.Second)) + assert.NoError(t, err) +} diff --git a/types.go b/types.go index 437279e..a966ede 100644 --- a/types.go +++ b/types.go @@ -9,6 +9,11 @@ type SubmitPFDRequest struct { GasLimit uint64 `json:"gas_limit"` } +type balanceRequest struct { + Denom string `json:"denom"` + Amount string `json:"amount"` +} + // Types below are copied from celestia-node (or cosmos-sdk dependency of celestia node, to be precise) // They are needed for proper deserialization. // It's probably far from the best approach to those types, but it's simple and works. From 76047a65a74275302729d59e5542df5c3e1f3e4a Mon Sep 17 00:00:00 2001 From: Regis-Caelum Date: Wed, 1 Jun 2022 02:32:26 +0530 Subject: [PATCH 2/3] Update client.go, client_test.go, and types.go Signed-off-by: Regis-Caelum --- client.go | 2 +- client_test.go | 3 +-- types.go | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index cabc52e..33d9b99 100644 --- a/client.go +++ b/client.go @@ -35,7 +35,7 @@ func (c *Client) Header(ctx context.Context, height uint64) /* Header */ error { } func (c *Client) Balance(ctx context.Context) error { - var result balanceRequest + var result balanceResponse var rpcErr string _, err := c.c.R(). diff --git a/client_test.go b/client_test.go index d61ebda..3324d6a 100644 --- a/client_test.go +++ b/client_test.go @@ -55,7 +55,6 @@ func TestSubmitPDF(t *testing.T) { } func TestBalance(t *testing.T) { - //t.Skip() - _, err := NewClient("http://localhost:26658", WithTimeout(30*time.Second)) + _, err := NewClient("http://localhost:26658", WithTimeout(5*time.Second)) assert.NoError(t, err) } diff --git a/types.go b/types.go index a966ede..c834813 100644 --- a/types.go +++ b/types.go @@ -9,7 +9,7 @@ type SubmitPFDRequest struct { GasLimit uint64 `json:"gas_limit"` } -type balanceRequest struct { +type balanceResponse struct { Denom string `json:"denom"` Amount string `json:"amount"` } From 8faaf0ad8233f60f8c49c9193ac96817371bbaf9 Mon Sep 17 00:00:00 2001 From: Regis-Caelum Date: Wed, 1 Jun 2022 04:52:46 +0530 Subject: [PATCH 3/3] Implement SubmitTx method Signed-off-by: Regis-Caelum --- client.go | 22 +++++++++++++++++++--- client_test.go | 10 ++++++++++ types.go | 4 ++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index 33d9b99..0462eb0 100644 --- a/client.go +++ b/client.go @@ -54,9 +54,25 @@ func (c *Client) Balance(ctx context.Context) error { return nil } -func (c *Client) SubmitTx(ctx context.Context, tx []byte) /* TxResponse */ error { - _ = submitTxEndpoint - return errors.New("method SubmitTx not implemented") +func (c *Client) SubmitTx(ctx context.Context, tx []byte) (*TxResponse, error) { + req := SubmitTxRequest{ + Tx: string(tx), + } + var res TxResponse + var rpcErr string + _, err := c.c.R(). + SetContext(ctx). + SetBody(req). + SetResult(&res). + SetError(&rpcErr). + Post(submitTxEndpoint) + if err != nil { + return nil, err + } + if rpcErr != "" { + return nil, errors.New(rpcErr) + } + return &res, nil } func (c *Client) SubmitPFD(ctx context.Context, namespaceID [8]byte, data []byte, gasLimit uint64) (*TxResponse, error) { diff --git a/client_test.go b/client_test.go index 3324d6a..256999c 100644 --- a/client_test.go +++ b/client_test.go @@ -58,3 +58,13 @@ func TestBalance(t *testing.T) { _, err := NewClient("http://localhost:26658", WithTimeout(5*time.Second)) assert.NoError(t, err) } + +func TestSubmitTx(t *testing.T) { + client, err := NewClient("http://localhost:26658", WithTimeout(30*time.Second)) + assert.NoError(t, err) + assert.NotNil(t, client) + + txRes, err := client.SubmitTx(context.TODO(), []byte("0A83080AFE070A1A2F7061796D656E742E4D736757697265506179466F724461746112DF070A2C63656C65733133753364376D6E757930327070397A6B337A707165706A66746A66376476727A6E6B6C333271120802020202020202021880042280040202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202326608101220B0429A0DB28AE163D9C1A300B320B19CEF93EC90AE87B4BA7ECF76C0703A9F891A408FA20C490F620B6A02E7FF5FA4139B997969BADE13A5E4F1A35086670FDC71550447A4759D57C844E836947F4BB73039D0E836511F3B1B8D8F1D317248DAF707326608201220B0429A0DB28AE163D9C1A300B320B19CEF93EC90AE87B4BA7ECF76C0703A9F891A408FA20C490F620B6A02E7FF5FA4139B997969BADE13A5E4F1A35086670FDC71550447A4759D57C844E836947F4BB73039D0E836511F3B1B8D8F1D317248DAF707326608401220B0429A0DB28AE163D9C1A300B320B19CEF93EC90AE87B4BA7ECF76C0703A9F891A408FA20C490F620B6A02E7FF5FA4139B997969BADE13A5E4F1A35086670FDC71550447A4759D57C844E836947F4BB73039D0E836511F3B1B8D8F1D317248DAF70732670880011220B0429A0DB28AE163D9C1A300B320B19CEF93EC90AE87B4BA7ECF76C0703A9F891A408FA20C490F620B6A02E7FF5FA4139B997969BADE13A5E4F1A35086670FDC71550447A4759D57C844E836947F4BB73039D0E836511F3B1B8D8F1D317248DAF707186312550A4E0A460A1F2F636F736D6F732E63727970746F2E736563703235366B312E5075624B657912230A210318F208E403FE5881227DD423254DA2BD4C11BAD740820B23DA352EF0E7581E6412040A020801120310904E1A405311CD1C8888C562CAA95BF4FBBD6646604D59D66FDD8E291A1C1B2C5286727464E0B3DD9E7DF38F681E0D7A2A154AED45810B6A59D351DE105D11D9383E0D44")) + assert.NoError(t, err) + assert.NotNil(t, txRes) +} diff --git a/types.go b/types.go index c834813..235c136 100644 --- a/types.go +++ b/types.go @@ -14,6 +14,10 @@ type balanceResponse struct { Amount string `json:"amount"` } +type SubmitTxRequest struct { + Tx string `json:"tx"` +} + // Types below are copied from celestia-node (or cosmos-sdk dependency of celestia node, to be precise) // They are needed for proper deserialization. // It's probably far from the best approach to those types, but it's simple and works.