Skip to content

Commit 0c35be1

Browse files
committed
[interfaces] Fix client interfaces to be consistent and enforced
1 parent 3a8f90b commit 0c35be1

6 files changed

+185
-104
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ adheres to the format set out by [Keep a Changelog](https://keepachangelog.com/e
1313
- [`Fix`] Fix a self calling function in the client
1414
- [`Fix`/`Breaking`] Fix fungible asset API calls that didn't return a value
1515
- [`Fix`] Fix errors from failed transactions in concurrent submission
16+
- [`Fix`] Fix AptosRpcClient, AptosFaucetClient, AptosIndexerClient interfaces to be enforced on concrete types properly
1617

1718
# v1.5.0 (2/10/2024)
1819

client.go

+62
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,43 @@ func init() {
7878
setNN(MainnetConfig)
7979
}
8080

81+
// PollPeriod is an option to PollForTransactions
82+
type PollPeriod time.Duration
83+
84+
// PollTimeout is an option to PollForTransactions
85+
type PollTimeout time.Duration
86+
87+
// EstimateGasUnitPrice estimates the gas unit price for a transaction
88+
type EstimateGasUnitPrice bool
89+
90+
// EstimateMaxGasAmount estimates the max gas amount for a transaction
91+
type EstimateMaxGasAmount bool
92+
93+
// EstimatePrioritizedGasUnitPrice estimates the prioritized gas unit price for a transaction
94+
type EstimatePrioritizedGasUnitPrice bool
95+
96+
// MaxGasAmount will set the max gas amount in gas units for a transaction
97+
type MaxGasAmount uint64
98+
99+
// GasUnitPrice will set the gas unit price in octas (1/10^8 APT) for a transaction
100+
type GasUnitPrice uint64
101+
102+
// ExpirationSeconds will set the number of seconds from the current time to expire a transaction
103+
type ExpirationSeconds uint64
104+
105+
// FeePayer will set the fee payer for a transaction
106+
type FeePayer *AccountAddress
107+
108+
// AdditionalSigners will set the additional signers for a transaction
109+
type AdditionalSigners []AccountAddress
110+
111+
// SequenceNumber will set the sequence number for a transaction
112+
type SequenceNumber uint64
113+
114+
// ChainIdOption will set the chain ID for a transaction
115+
// TODO: This one may want to be removed / renamed?
116+
type ChainIdOption uint8
117+
81118
// AptosClient is an interface for all functionality on the Client.
82119
// It is a combination of [AptosRpcClient], [AptosIndexerClient], and [AptosFaucetClient] for the purposes
83120
// of mocking and convenience.
@@ -137,6 +174,12 @@ type AptosRpcClient interface {
137174
// AccountResourcesBCS fetches account resources as raw Move struct BCS blobs in AccountResourceRecord.Data []byte
138175
AccountResourcesBCS(address AccountAddress, ledgerVersion ...uint64) (resources []AccountResourceRecord, err error)
139176

177+
// AccountModule fetches a single account module's bytecode and ABI from on-chain state.
178+
AccountModule(address AccountAddress, moduleName string, ledgerVersion ...uint64) (data *api.MoveBytecode, err error)
179+
180+
// EntryFunctionWithArgs generates an EntryFunction from on-chain Module ABI, and converts simple inputs to BCS encoded ones.
181+
EntryFunctionWithArgs(moduleAddress AccountAddress, moduleName string, functionName string, typeArgs []any, args []any) (entry *EntryFunction, err error)
182+
140183
// BlockByHeight fetches a block by height
141184
//
142185
// block, _ := client.BlockByHeight(1, false)
@@ -172,6 +215,10 @@ type AptosRpcClient interface {
172215
// }
173216
TransactionByHash(txnHash string) (data *api.Transaction, err error)
174217

218+
// WaitTransactionByHash waits for a transaction to be confirmed by its hash.
219+
// This function allows you to monitor the status of a transaction until it is finalized.
220+
WaitTransactionByHash(txnHash string) (data *api.Transaction, err error)
221+
175222
// TransactionByVersion gets info on a transaction from its LedgerVersion. It must have been
176223
// committed to have a ledger version
177224
//
@@ -185,6 +232,11 @@ type AptosRpcClient interface {
185232
// }
186233
TransactionByVersion(version uint64) (data *api.CommittedTransaction, err error)
187234

235+
// PollForTransaction waits up to 10 seconds for a transaction to be done, polling at 10Hz
236+
// Accepts options PollPeriod and PollTimeout which should wrap time.Duration values.
237+
// Not just a degenerate case of PollForTransactions, it may return additional information for the single transaction polled.
238+
PollForTransaction(hash string, options ...any) (*api.UserTransaction, error)
239+
188240
// PollForTransactions Waits up to 10 seconds for transactions to be done, polling at 10Hz
189241
// Accepts options PollPeriod and PollTimeout which should wrap time.Duration values.
190242
//
@@ -609,6 +661,11 @@ func (client *Client) TransactionByHash(txnHash string) (data *api.Transaction,
609661
return client.nodeClient.TransactionByHash(txnHash)
610662
}
611663

664+
// WaitTransactionByHash waits for a transaction to complete and returns it's data when finished.
665+
func (client *Client) WaitTransactionByHash(txnHash string) (data *api.Transaction, err error) {
666+
return client.nodeClient.WaitTransactionByHash(txnHash)
667+
}
668+
612669
// TransactionByVersion gets info on a transaction from its LedgerVersion. It must have been
613670
// committed to have a ledger version
614671
//
@@ -624,6 +681,11 @@ func (client *Client) TransactionByVersion(version uint64) (data *api.CommittedT
624681
return client.nodeClient.TransactionByVersion(version)
625682
}
626683

684+
// PollForTransaction Waits up to 10 seconds for as single transaction to be done, polling at 10Hz
685+
func (client *Client) PollForTransaction(hash string, options ...any) (*api.UserTransaction, error) {
686+
return client.nodeClient.PollForTransaction(hash, options...)
687+
}
688+
627689
// PollForTransactions Waits up to 10 seconds for transactions to be done, polling at 10Hz
628690
// Accepts options PollPeriod and PollTimeout which should wrap time.Duration values.
629691
//

client_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -773,3 +773,36 @@ func TestClient_EntryFunctionWithArgs(t *testing.T) {
773773

774774
assert.True(t, txn.Success)
775775
}
776+
777+
func TestEnsureClientInterfacesHold(t *testing.T) {
778+
t.Parallel()
779+
var err error
780+
var c AptosClient
781+
c, err = NewClient(LocalnetConfig)
782+
require.NoError(t, err)
783+
require.NotNil(t, c)
784+
var rc AptosRpcClient
785+
rc, err = NewClient(LocalnetConfig)
786+
require.NoError(t, err)
787+
require.NotNil(t, rc)
788+
var fc AptosFaucetClient
789+
fc, err = NewClient(LocalnetConfig)
790+
require.NoError(t, err)
791+
require.NotNil(t, fc)
792+
var ic AptosIndexerClient
793+
ic, err = NewClient(LocalnetConfig)
794+
require.NoError(t, err)
795+
require.NotNil(t, ic)
796+
797+
rc, err = NewNodeClient(LocalnetConfig.NodeUrl, 4)
798+
require.NoError(t, err)
799+
require.NotNil(t, rc)
800+
nc, err := NewNodeClient(LocalnetConfig.NodeUrl, 4)
801+
require.NoError(t, err)
802+
fc, err = NewFaucetClient(nc, LocalnetConfig.FaucetUrl)
803+
require.NoError(t, err)
804+
require.NotNil(t, fc)
805+
ic = NewIndexerClient(nc.client, LocalnetConfig.IndexerUrl)
806+
require.NoError(t, err)
807+
require.NotNil(t, ic)
808+
}

indexerClient.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@ func NewIndexerClient(httpClient *http.Client, url string) *IndexerClient {
2525
}
2626
}
2727

28+
// QueryIndexer is a generic function for making any GraphQL query against the indexer
29+
func (ic *IndexerClient) QueryIndexer(query any, variables map[string]any, options ...graphql.Option) error {
30+
return ic.inner.Query(context.Background(), query, variables, options...)
31+
}
32+
2833
// Query is a generic function for making any GraphQL query against the indexer
34+
//
35+
// Deprecated, please use QueryIndexer as it matches the interface
2936
func (ic *IndexerClient) Query(query any, variables map[string]any, options ...graphql.Option) error {
30-
return ic.inner.Query(context.Background(), query, variables, options...)
37+
return ic.QueryIndexer(query, variables, options...)
3138
}
3239

3340
type CoinBalance struct {

0 commit comments

Comments
 (0)