From 8b1da81b07bdc6674f03a3bddb569ef05ea61569 Mon Sep 17 00:00:00 2001 From: lzx Date: Mon, 14 Nov 2022 12:23:37 +0800 Subject: [PATCH] GetAccountResources and GetResourceByAccountAddressAndResourceType allow to provide a version --- client/accounts.go | 26 ++++++++++++++++++-------- client/mock_client.go | 36 ++++++++++++++++++------------------ client/token.go | 6 +++--- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/client/accounts.go b/client/accounts.go index 663b8fd..0867265 100644 --- a/client/accounts.go +++ b/client/accounts.go @@ -8,8 +8,8 @@ import ( type Accounts interface { GetAccount(ctx context.Context, address string, opts ...interface{}) (*AccountInfo, error) - GetAccountResources(ctx context.Context, address string, opts ...interface{}) ([]AccountResource, error) - GetResourceByAccountAddressAndResourceType(ctx context.Context, address, resourceType string, opts ...interface{}) (*AccountResource, error) + GetAccountResources(ctx context.Context, address string, version int64, opts ...interface{}) ([]AccountResource, error) + GetResourceByAccountAddressAndResourceType(ctx context.Context, address, resourceType string, version int64, opts ...interface{}) (*AccountResource, error) GetAccountModules(ctx context.Context, address string, opts ...interface{}) ([]AccountModule, error) GetModuleByModuleID(ctx context.Context, address, moduleID string, opts ...interface{}) (*AccountModule, error) @@ -86,11 +86,16 @@ type TokenStoreResource struct { MutateTokenPropertyEvents EventHandle `json:"mutate_token_property_events"` } -func (impl AccountsImpl) GetAccountResources(ctx context.Context, address string, opts ...interface{}) ([]AccountResource, error) { +func (impl AccountsImpl) GetAccountResources(ctx context.Context, address string, version int64, opts ...interface{}) ([]AccountResource, error) { var rspJSON []AccountResource + var url string + if version < 0 { + url = fmt.Sprintf("/v1/accounts/%s/resources", address) + } else { + url = fmt.Sprintf("/v1/accounts/%s/resources?ledger_version=%d", address, version) + } err := request(ctx, http.MethodGet, - impl.Base.Endpoint()+fmt.Sprintf("/v1/accounts/%s/resources", address), - nil, &rspJSON, nil, requestOptions(opts...)) + impl.Base.Endpoint()+url, nil, &rspJSON, nil, requestOptions(opts...)) if err != nil { return nil, err } @@ -98,11 +103,16 @@ func (impl AccountsImpl) GetAccountResources(ctx context.Context, address string return rspJSON, nil } -func (impl AccountsImpl) GetResourceByAccountAddressAndResourceType(ctx context.Context, address, resourceType string, opts ...interface{}) (*AccountResource, error) { +func (impl AccountsImpl) GetResourceByAccountAddressAndResourceType(ctx context.Context, address, resourceType string, version int64, opts ...interface{}) (*AccountResource, error) { var rspJSON AccountResource + var url string + if version < 0 { + url = fmt.Sprintf("/v1/accounts/%s/resource/%s", address, resourceType) + } else { + url = fmt.Sprintf("/v1/accounts/%s/resource/%s?ledger_version=%d", address, resourceType, version) + } err := request(ctx, http.MethodGet, - impl.Base.Endpoint()+fmt.Sprintf("/v1/accounts/%s/resource/%s", address, resourceType), - nil, &rspJSON, nil, requestOptions(opts...)) + impl.Base.Endpoint()+url, nil, &rspJSON, nil, requestOptions(opts...)) if err != nil { return nil, err } diff --git a/client/mock_client.go b/client/mock_client.go index 027b997..7ff97e3 100644 --- a/client/mock_client.go +++ b/client/mock_client.go @@ -117,15 +117,15 @@ func (_m *MockAptosClient) GetAccountModules(ctx context.Context, address string } // GetAccountResources provides a mock function with given fields: ctx, address, opts -func (_m *MockAptosClient) GetAccountResources(ctx context.Context, address string, opts ...interface{}) ([]AccountResource, error) { +func (_m *MockAptosClient) GetAccountResources(ctx context.Context, address string, version int64, opts ...interface{}) ([]AccountResource, error) { var _ca []interface{} - _ca = append(_ca, ctx, address) + _ca = append(_ca, ctx, address, version) _ca = append(_ca, opts...) ret := _m.Called(_ca...) var r0 []AccountResource - if rf, ok := ret.Get(0).(func(context.Context, string, ...interface{}) []AccountResource); ok { - r0 = rf(ctx, address, opts...) + if rf, ok := ret.Get(0).(func(context.Context, string, int64, ...interface{}) []AccountResource); ok { + r0 = rf(ctx, address, version, opts...) } else { if ret.Get(0) != nil { r0 = ret.Get(0).([]AccountResource) @@ -133,8 +133,8 @@ func (_m *MockAptosClient) GetAccountResources(ctx context.Context, address stri } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, string, ...interface{}) error); ok { - r1 = rf(ctx, address, opts...) + if rf, ok := ret.Get(1).(func(context.Context, string, int64, ...interface{}) error); ok { + r1 = rf(ctx, address, version, opts...) } else { r1 = ret.Error(1) } @@ -143,14 +143,14 @@ func (_m *MockAptosClient) GetAccountResources(ctx context.Context, address stri } // GetAccountTransactions provides a mock function with given fields: ctx, address, start, limit, opts -func (_m *MockAptosClient) GetAccountTransactions(ctx context.Context, address string, start int, limit int, opts ...interface{}) ([]TransactionResp, error) { +func (_m *MockAptosClient) GetAccountTransactions(ctx context.Context, address string, start uint64, limit int, opts ...interface{}) ([]TransactionResp, error) { var _ca []interface{} _ca = append(_ca, ctx, address, start, limit) _ca = append(_ca, opts...) ret := _m.Called(_ca...) var r0 []TransactionResp - if rf, ok := ret.Get(0).(func(context.Context, string, int, int, ...interface{}) []TransactionResp); ok { + if rf, ok := ret.Get(0).(func(context.Context, string, uint64, int, ...interface{}) []TransactionResp); ok { r0 = rf(ctx, address, start, limit, opts...) } else { if ret.Get(0) != nil { @@ -159,7 +159,7 @@ func (_m *MockAptosClient) GetAccountTransactions(ctx context.Context, address s } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, string, int, int, ...interface{}) error); ok { + if rf, ok := ret.Get(1).(func(context.Context, string, uint64, int, ...interface{}) error); ok { r1 = rf(ctx, address, start, limit, opts...) } else { r1 = ret.Error(1) @@ -299,15 +299,15 @@ func (_m *MockAptosClient) GetModuleByModuleID(ctx context.Context, address stri } // GetResourceByAccountAddressAndResourceType provides a mock function with given fields: ctx, address, resourceType, opts -func (_m *MockAptosClient) GetResourceByAccountAddressAndResourceType(ctx context.Context, address string, resourceType string, opts ...interface{}) (*AccountResource, error) { +func (_m *MockAptosClient) GetResourceByAccountAddressAndResourceType(ctx context.Context, address string, resourceType string, version int64, opts ...interface{}) (*AccountResource, error) { var _ca []interface{} - _ca = append(_ca, ctx, address, resourceType) + _ca = append(_ca, ctx, address, resourceType, version) _ca = append(_ca, opts...) ret := _m.Called(_ca...) var r0 *AccountResource - if rf, ok := ret.Get(0).(func(context.Context, string, string, ...interface{}) *AccountResource); ok { - r0 = rf(ctx, address, resourceType, opts...) + if rf, ok := ret.Get(0).(func(context.Context, string, string, int64, ...interface{}) *AccountResource); ok { + r0 = rf(ctx, address, resourceType, version, opts...) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(*AccountResource) @@ -315,8 +315,8 @@ func (_m *MockAptosClient) GetResourceByAccountAddressAndResourceType(ctx contex } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, string, string, ...interface{}) error); ok { - r1 = rf(ctx, address, resourceType, opts...) + if rf, ok := ret.Get(1).(func(context.Context, string, string, int64, ...interface{}) error); ok { + r1 = rf(ctx, address, resourceType, version, opts...) } else { r1 = ret.Error(1) } @@ -411,14 +411,14 @@ func (_m *MockAptosClient) GetTransactionByVersion(ctx context.Context, version } // GetTransactions provides a mock function with given fields: ctx, start, limit, opts -func (_m *MockAptosClient) GetTransactions(ctx context.Context, start int, limit int, opts ...interface{}) ([]TransactionResp, error) { +func (_m *MockAptosClient) GetTransactions(ctx context.Context, start uint64, limit int, opts ...interface{}) ([]TransactionResp, error) { var _ca []interface{} _ca = append(_ca, ctx, start, limit) _ca = append(_ca, opts...) ret := _m.Called(_ca...) var r0 []TransactionResp - if rf, ok := ret.Get(0).(func(context.Context, int, int, ...interface{}) []TransactionResp); ok { + if rf, ok := ret.Get(0).(func(context.Context, uint64, int, ...interface{}) []TransactionResp); ok { r0 = rf(ctx, start, limit, opts...) } else { if ret.Get(0) != nil { @@ -427,7 +427,7 @@ func (_m *MockAptosClient) GetTransactions(ctx context.Context, start int, limit } var r1 error - if rf, ok := ret.Get(1).(func(context.Context, int, int, ...interface{}) error); ok { + if rf, ok := ret.Get(1).(func(context.Context, uint64, int, ...interface{}) error); ok { r1 = rf(ctx, start, limit, opts...) } else { r1 = ret.Error(1) diff --git a/client/token.go b/client/token.go index ea5a5ad..084c2ea 100644 --- a/client/token.go +++ b/client/token.go @@ -349,7 +349,7 @@ func (impl *TokenClientImpl) ClaimToken(ctx context.Context, receiver models.Sin func (impl *TokenClientImpl) GetCollectionData(ctx context.Context, creator models.AccountAddress, collectionName string) (*models.CollectionData, error) { resource, err := impl.client.GetResourceByAccountAddressAndResourceType( - ctx, creator.PrefixZeroTrimmedHex(), "0x3::token::Collections", + ctx, creator.PrefixZeroTrimmedHex(), "0x3::token::Collections", -1, ) if err != nil { return nil, fmt.Errorf("client.GetResourceByAccountAddressAndResourceType error: %w", err) @@ -377,7 +377,7 @@ func (impl *TokenClientImpl) GetCollectionData(ctx context.Context, creator mode func (impl *TokenClientImpl) GetTokenData(ctx context.Context, creator models.AccountAddress, collectionName, tokenName string) (*models.TokenData, error) { resource, err := impl.client.GetResourceByAccountAddressAndResourceType( - ctx, creator.PrefixZeroTrimmedHex(), "0x3::token::Collections", + ctx, creator.PrefixZeroTrimmedHex(), "0x3::token::Collections", -1, ) if err != nil { return nil, fmt.Errorf("client.GetResourceByAccountAddressAndResourceType error: %w", err) @@ -411,7 +411,7 @@ const tokenStoreType = "0x3::token::TokenStore" func (impl *TokenClientImpl) GetToken(ctx context.Context, owner models.AccountAddress, tokenID models.TokenID) (*models.Token, error) { resource, err := impl.client.GetResourceByAccountAddressAndResourceType( - ctx, owner.PrefixZeroTrimmedHex(), tokenStoreType, + ctx, owner.PrefixZeroTrimmedHex(), tokenStoreType, -1, ) if err != nil { return nil, fmt.Errorf("client.GetResourceByAccountAddressAndResourceType error: %w", err)