Skip to content

Commit 34b76f1

Browse files
committed
add http client sdk support for scheduled txs
1 parent 24accde commit 34b76f1

6 files changed

Lines changed: 191 additions & 2 deletions

File tree

access/http/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ func (c *Client) GetTransactionResultsByBlockID(ctx context.Context, blockID flo
189189
}
190190

191191
func (c *Client) GetScheduledTransaction(ctx context.Context, scheduledTxID uint64) (*flow.Transaction, error) {
192-
return nil, fmt.Errorf("not implemented")
192+
return c.httpClient.GetScheduledTransaction(ctx, scheduledTxID)
193193
}
194194

195195
func (c *Client) GetScheduledTransactionResult(ctx context.Context, scheduledTxID uint64) (*flow.TransactionResult, error) {
196-
return nil, fmt.Errorf("not implemented")
196+
return c.httpClient.GetScheduledTransactionResult(ctx, scheduledTxID)
197197
}
198198

199199
func (c *Client) GetSystemTransaction(ctx context.Context, blockID flow.Identifier) (*flow.Transaction, error) {

access/http/client_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,74 @@ func TestBaseClient_GetTransactionResult(t *testing.T) {
416416
}))
417417
}
418418

419+
func TestBaseClient_GetScheduledTransaction(t *testing.T) {
420+
const handlerName = "getScheduledTransaction"
421+
422+
t.Run("Success", clientTest(func(ctx context.Context, t *testing.T, handler *mockHandler, client *Client) {
423+
httpTx := unittest.TransactionFlowFixture()
424+
expectedTx, err := convert.ToTransaction(&httpTx)
425+
assert.NoError(t, err)
426+
427+
var scheduledTxID uint64 = 42
428+
handler.
429+
On(handlerName, mock.Anything, scheduledTxID, false).
430+
Return(&httpTx, nil)
431+
432+
tx, err := client.GetScheduledTransaction(ctx, scheduledTxID)
433+
assert.NoError(t, err)
434+
assert.Equal(t, tx, expectedTx)
435+
}))
436+
437+
t.Run("Not Found", clientTest(func(ctx context.Context, t *testing.T, handler *mockHandler, client *Client) {
438+
handler.On(handlerName, mock.Anything, mock.Anything, mock.Anything).Return(nil, HTTPError{
439+
Url: "/",
440+
Code: 404,
441+
Message: "scheduled tx not found",
442+
})
443+
444+
tx, err := client.GetScheduledTransaction(ctx, uint64(99))
445+
assert.EqualError(t, err, "scheduled tx not found")
446+
assert.Nil(t, tx)
447+
}))
448+
}
449+
450+
func TestBaseClient_GetScheduledTransactionResult(t *testing.T) {
451+
const handlerName = "getScheduledTransaction"
452+
453+
t.Run("Success", clientTest(func(ctx context.Context, t *testing.T, handler *mockHandler, client *Client) {
454+
httpTx := unittest.TransactionFlowFixture()
455+
httpTxRes := unittest.TransactionResultFlowFixture(flow.EventEncodingVersionJSONCDC)
456+
httpTx.Result = &httpTxRes
457+
expectedTx, err := convert.ToTransaction(&httpTx)
458+
assert.NoError(t, err)
459+
460+
expectedTxRes, err := convert.ToTransactionResult(&httpTxRes, nil)
461+
assert.NoError(t, err)
462+
463+
var scheduledTxID uint64 = 42
464+
handler.
465+
On(handlerName, mock.Anything, scheduledTxID, true).
466+
Return(&httpTx, nil)
467+
468+
txRes, err := client.GetScheduledTransactionResult(ctx, scheduledTxID)
469+
assert.NoError(t, err)
470+
assert.Equal(t, txRes, expectedTxRes)
471+
_ = expectedTx // suppress unused
472+
}))
473+
474+
t.Run("Not Found", clientTest(func(ctx context.Context, t *testing.T, handler *mockHandler, client *Client) {
475+
handler.On(handlerName, mock.Anything, mock.Anything, true).Return(nil, HTTPError{
476+
Url: "/",
477+
Code: 404,
478+
Message: "scheduled tx result not found",
479+
})
480+
481+
tx, err := client.GetScheduledTransactionResult(ctx, uint64(99))
482+
assert.EqualError(t, err, "scheduled tx result not found")
483+
assert.Nil(t, tx)
484+
}))
485+
}
486+
419487
func TestBaseClient_GetAccount(t *testing.T) {
420488
const handlerName = "getAccount"
421489

access/http/handler.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,29 @@ func (h *httpHandler) getTransaction(
363363
return &transaction, nil
364364
}
365365

366+
func (h *httpHandler) getScheduledTransaction(
367+
ctx context.Context,
368+
scheduledTxID uint64,
369+
includeResult bool,
370+
opts ...queryOpts,
371+
) (*models.Transaction, error) {
372+
var transaction models.Transaction
373+
u := h.mustBuildURL(fmt.Sprintf("/transactions/%d", scheduledTxID), opts...)
374+
375+
if includeResult {
376+
q := u.Query()
377+
q.Add("expand", "result")
378+
u.RawQuery = q.Encode()
379+
}
380+
381+
err := h.get(ctx, u, &transaction)
382+
if err != nil {
383+
return nil, errors.Wrap(err, fmt.Sprintf("get scheduled transaction ID %d failed", scheduledTxID))
384+
}
385+
386+
return &transaction, nil
387+
}
388+
366389
func (h *httpHandler) sendTransaction(ctx context.Context, transaction []byte, opts ...queryOpts) error {
367390
var tx models.Transaction
368391
return h.post(ctx, h.mustBuildURL("/transactions", opts...), transaction, &tx)

access/http/handler_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,40 @@ func TestHandler_GetTransaction(t *testing.T) {
461461
}))
462462
}
463463

464+
func newScheduledTransactionURL(scheduledTxID uint64, query map[string]string) url.URL {
465+
u, _ := url.Parse(fmt.Sprintf("/transactions/%d", scheduledTxID))
466+
return addQuery(u, query)
467+
}
468+
469+
func TestHandler_GetScheduledTransaction(t *testing.T) {
470+
471+
t.Run("Success", handlerTest(func(ctx context.Context, t *testing.T, handler httpHandler, req *testRequest) {
472+
httpTx := unittest.TransactionFlowFixture()
473+
var scheduledTxID uint64 = 42
474+
475+
txURL := newScheduledTransactionURL(scheduledTxID, nil)
476+
req.SetData(txURL, httpTx)
477+
478+
tx, err := handler.getScheduledTransaction(ctx, scheduledTxID, false)
479+
assert.NoError(t, err)
480+
assert.Equal(t, *tx, httpTx)
481+
}))
482+
483+
t.Run("Success With Results", handlerTest(func(ctx context.Context, t *testing.T, handler httpHandler, req *testRequest) {
484+
httpTx := unittest.TransactionFlowFixture()
485+
var scheduledTxID uint64 = 42
486+
487+
txURL := newScheduledTransactionURL(scheduledTxID, map[string]string{
488+
"expand": "result",
489+
})
490+
req.SetData(txURL, httpTx)
491+
492+
tx, err := handler.getScheduledTransaction(ctx, scheduledTxID, true)
493+
assert.NoError(t, err)
494+
assert.Equal(t, *tx, httpTx)
495+
}))
496+
}
497+
464498
func newEventsURL(query map[string]string, ids []string) url.URL {
465499
u, _ := url.Parse("/events")
466500
if query == nil {

access/http/http.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type handler interface {
4848
executeScriptAtBlockHeight(ctx context.Context, height string, script string, arguments []string, opts ...queryOpts) (string, error)
4949
executeScriptAtBlockID(ctx context.Context, ID string, script string, arguments []string, opts ...queryOpts) (string, error)
5050
getTransaction(ctx context.Context, ID string, includeResult bool, opts ...queryOpts) (*models.Transaction, error)
51+
getScheduledTransaction(ctx context.Context, scheduledTxID uint64, includeResult bool, opts ...queryOpts) (*models.Transaction, error)
5152
sendTransaction(ctx context.Context, transaction []byte, opts ...queryOpts) error
5253
getEvents(ctx context.Context, eventType string, start string, end string, blockIDs []string, opts ...queryOpts) ([]models.BlockEvents, error)
5354
getExecutionResultByID(ctx context.Context, id string, opts ...queryOpts) (*models.ExecutionResult, error)
@@ -299,6 +300,32 @@ func (c *BaseClient) GetTransactionResult(
299300
return convert.ToTransactionResult(tx.Result, c.jsonOptions)
300301
}
301302

303+
func (c *BaseClient) GetScheduledTransaction(
304+
ctx context.Context,
305+
scheduledTxID uint64,
306+
opts ...queryOpts,
307+
) (*flow.Transaction, error) {
308+
tx, err := c.handler.getScheduledTransaction(ctx, scheduledTxID, false, opts...)
309+
if err != nil {
310+
return nil, err
311+
}
312+
313+
return convert.ToTransaction(tx)
314+
}
315+
316+
func (c *BaseClient) GetScheduledTransactionResult(
317+
ctx context.Context,
318+
scheduledTxID uint64,
319+
opts ...queryOpts,
320+
) (*flow.TransactionResult, error) {
321+
tx, err := c.handler.getScheduledTransaction(ctx, scheduledTxID, true, opts...)
322+
if err != nil {
323+
return nil, err
324+
}
325+
326+
return convert.ToTransactionResult(tx.Result, c.jsonOptions)
327+
}
328+
302329
func (c *BaseClient) GetAccountAtBlockHeight(
303330
ctx context.Context,
304331
address flow.Address,

access/http/mock_handler.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)