Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: core refactor #30

Merged
merged 26 commits into from
Jan 22, 2024
Merged

feat: core refactor #30

merged 26 commits into from
Jan 22, 2024

Conversation

dalechyn
Copy link
Collaborator

Problem: The current state of the core is not reliable for use. Some of the parameters of the functions are simply ignored (faced that with apiRequest previously), some are counter-intuitive and use repetitive naming. Matter fact, ~30% code of the classes is its initialization, and sometimes the actual business logic are just two or even a single function.

Solution:

  • Move away from writing custom classes on top of the other wherever possible and rely on the @abstract-money/cli generated modules as much as possible.

  • Provide intuitive and declarative naming to the methods to be used.

  • De-classify classes that should not have been classes and reduce complexity.

  • To eliminate confusion between AnsAsset and Asset (which has an amount field), introduce Token type.

    The list will be populated as the refactor goes.

Problem: The current state of the core is not reliable for use.
Some of the parameters of the functions are simply ignored (faced that
with `apiRequest` previously), some are counter-intuitive and use
repetitive naming. Matter fact, ~30% code of the classes is its
initialization, and sometimes the actual business logic are just two or
even a single function.

Solution:
- Move away from writing custom classes on top of the other wherever
  possible and rely on the `@abstract-money/cli` generated modules as
  much as possible.
- Provide intuitive and declarative naming to the methods to be used.
- De-classify classes that should not have been classes and reduce
  complexity.
- To eliminate confusion between `AnsAsset` and `Asset` (which has an
  `amount` field), introduce `Token` type.

  The list will be populated as the refactor goes.
@dalechyn dalechyn self-assigned this Jan 11, 2024
Copy link

changeset-bot bot commented Jan 11, 2024

🦋 Changeset detected

Latest commit: 95664f6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
wagemos-cosmoskit-nextjs Minor
wagemos-graz-nextjs Minor
@abstract-money/cli Minor
@abstract-money/core Minor
@abstract-money/react Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@dalechyn dalechyn marked this pull request as draft January 15, 2024 17:52
@dalechyn dalechyn changed the title feat: core refactor [draft] feat: core refactor Jan 15, 2024
I figured out that we actually have 5 clients:
- base client – knows only the `apiUrl`
- public client - knows the `apiUrl` and `CosmWasmClient`
- wallet client - knows the `apiUrl` and `SigningCosmWasmClient`
- account-public client - knows it's accountId, `apiUrl` and `CosmWasmClient`
- account-wallet client - knows it's accountId, `apiUrl` and `SigningCosmWasmClient`

The commit is still WIP, yet mostly it's just the functionality of the abstract
account that is left to be implemented.

Following folders were created to manage the codebase effectively:
- `actions` – contains all actions that clients are able to perform
|  - `public` – contains all public client actions
|  - `wallet` – contains all wallet client actions
|  - `account-public` – contains all account-public client actions
|  - `account-wallet` – contains all account-wallet client actions
|  ... - base client actions
|  Some of them are grouped by module groups, such as `ans`, `account-factory`, `manager` etc.
- `utils` – contains all utility data-transformation, formatting functions
– `clients` – contains the clients implementations
|  - `decorators` – contains the decorators to inject the client data and bind those to clients
- `types` – contains types

With such a structure, we're clearly separating the business logic from the utilitary logic that
mostly concerns different data transormations.

The focus here is also to keep the functions with the most minimal API as possible, so that hardly
re-uses `ts-codegen` generated types whenever possible.

The API might not look user-friendly at the moment, but later on we will provide aliases for more
user-friendly function names.
Copy link
Contributor

@adairrr adairrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, the changes look really good. I didn't like them at first, but came to enjoy the way that the code read when diving deeper.

Left quite a few comments / questions, though the most major is that we need to add doc comments for all the public-facing api at least. And secondly, we should ensure we're not requesting too much info from the API when unnecessary.

The refactor of the API / Console to use this is going to be quite a PITA... but good that it'll be done at once.

packages/core/src/utils/tokens/get-tokens-from-ans.ts Outdated Show resolved Hide resolved
packages/core/src/utils/tokens/get-tokens-from-ans.ts Outdated Show resolved Hide resolved
packages/core/src/utils/tokens/get-token-from-ans.ts Outdated Show resolved Hide resolved
packages/core/src/utils/tokens/get-token-from-ans.ts Outdated Show resolved Hide resolved
`
query AssetsQuery($chain: ID!, $filter: IdsFilter!) {
ans(chain: $chain) {
assets(filter: $filter) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make a task to deprecate the assets api to be replaced with tokens.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can improve it at the same time to return different types based on cw20 or native, getting rid of the overridden "address"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adairrr
Copy link
Contributor

adairrr commented Jan 16, 2024

We removed AnsAsset which actually was not just a difference with the amount field, but rather the difference that it has a name included. This was useful because sometimes you work with the ans assets (like calling into the dex adapter) but also need the resolved ones, like when sending coins along with that message.

Something we'd considered in the past is allowing for passing AnsAssets types in our calls in addition to Coin that would do the resolution and transfer message on behalf of the dev.

- To eliminate confusion between `AnsAsset` and `Asset` (which has an
  `amount` field), introduce `Token` type.

Comment on lines 12 to 19
cosmWasmClient: CosmWasmClient,
versionControlAddress: string,
version?: string,
) {
const registryClient = new VersionControlQueryClient(
cosmWasmClient,
versionControlAddress,
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cosmWasmClient is propagated by decorators therefore it simplifies the overall API, yet if we would pass such an entity explicitly the developer would have to create one himself.

@dalechyn
Copy link
Collaborator Author

We removed AnsAsset which actually was not just a difference with the amount field, but rather the difference that it has a name included. This was useful because sometimes you work with the ans assets (like calling into the dex adapter) but also need the resolved ones, like when sending coins along with that message.

Something we'd considered in the past is allowing for passing AnsAssets types in our calls in addition to Coin that would do the resolution and transfer message on behalf of the dev.

- To eliminate confusion between `AnsAsset` and `Asset` (which has an
  `amount` field), introduce `Token` type.

I addressed that issue, and now AnsAsset extends Asset by introducing the name property.

Copy link
Contributor

github-actions bot commented Jan 18, 2024

Size Change: +428 B (0%)

Total Size: 91.2 kB

Filename Size Change
packages/cli/dist/plugins 5.36 kB -10 B (0%)
packages/cli/dist/plugins/index.d.ts 280 B -31.8 kB (-99%) 🏆
packages/cli/dist/plugins/index.js 671 B +671 B (new file) 🆕
packages/core/dist 8.15 kB +7.99 kB (+4961%) 🆘
packages/core/dist/_tsup-dts-rollup.d.ts 0 B -83 B (removed) 🏆
packages/core/dist/api/index.d.ts 0 B -115 B (removed) 🏆
packages/core/dist/api/index.js 0 B -138 B (removed) 🏆
packages/core/dist/chain-registry 0 B -20 B (removed) 🏆
packages/core/dist/chain-registry/index.d.ts 0 B -1.35 kB (removed) 🏆
packages/core/dist/chain-registry/index.js 0 B -801 B (removed) 🏆
packages/core/dist/chunk-AER7UDD4.js 0 B -147 B (removed) 🏆
packages/core/dist/chunk-AXK7NDCY.js 0 B -5.14 kB (removed) 🏆
packages/core/dist/chunk-F67FMWMH.js 0 B -21.1 kB (removed) 🏆
packages/core/dist/chunk-PGE32UO3.js 0 B -602 B (removed) 🏆
packages/core/dist/chunk-VCUCCWJK.js 0 B -361 B (removed) 🏆
packages/core/dist/clients 7.37 kB +7.37 kB (new file) 🆕
packages/core/dist/clients/index.d.ts 894 B +894 B (new file) 🆕
packages/core/dist/clients/index.js 663 B +323 B (+95%) 🆘
packages/core/dist/codegen 0 B -324 B (removed) 🏆
packages/core/dist/codegen/abstract 0 B -719 B (removed) 🏆
packages/core/dist/codegen/abstract/index.d.ts 0 B -559 B (removed) 🏆
packages/core/dist/index.d.ts 13.6 kB +13.4 kB (+9252%) 🆘
packages/core/dist/index.js 17.7 kB +17.6 kB (+16483%) 🆘
packages/core/dist/tools/index.d.ts 0 B -345 B (removed) 🏆
packages/core/dist/tools/index.js 0 B -163 B (removed) 🏆
packages/cosmwasm-utils/dist/index.js 590 B -157 B (-21%) 🎉
packages/cosmwasm-utils/dist/query/index.js 700 B +217 B (+45%) 🚨
packages/react/dist/chunk-6IDD2WC3.js 0 B -696 B (removed) 🏆
packages/react/dist/chunk-PLZFQS37.js 0 B -167 B (removed) 🏆
packages/react/dist/hooks 0 B -540 B (removed) 🏆
packages/react/dist/hooks/index.d.ts 3.36 kB +3.36 kB (new file) 🆕
packages/react/dist/hooks/index.js 1.79 kB +1.03 kB (+134%) 🆘
packages/react/dist/index.d.ts 0 B -1.45 kB (removed) 🏆
packages/core/dist/asset-uMBPbAJ3.d.ts 5.83 kB +5.83 kB (new file) 🆕
packages/core/dist/chunk-3HT4SBSY.js 2.41 kB +2.41 kB (new file) 🆕
packages/core/dist/chunk-7SSMFNLJ.js 131 B +131 B (new file) 🆕
packages/core/dist/legacy 1.66 kB +1.66 kB (new file) 🆕
packages/core/dist/legacy/index.js 2.83 kB +2.83 kB (new file) 🆕
packages/core/dist/messages-xtaSCPLq.d.ts 610 B +610 B (new file) 🆕
packages/react/dist/chunk-FD3TJZWK.js 687 B +687 B (new file) 🆕
packages/react/dist/chunk-NJTJO346.js 174 B +174 B (new file) 🆕
ℹ️ View Unchanged
Filename Size Change
packages/bundle-require/dist 1.11 kB 0 B
packages/bundle-require/dist/index.d.ts 2 kB 0 B
packages/bundle-require/dist/index.js 2.62 kB 0 B
packages/cli/dist 150 B 0 B
packages/cli/dist/_tsup-dts-rollup.d.ts 379 B 0 B
packages/cli/dist/chunk-6C3VEZWH.js 153 B 0 B
packages/cli/dist/chunk-AOQH5JQO.js 1.4 kB 0 B
packages/cli/dist/chunk-B7QC3SVE.js 31 B 0 B
packages/cli/dist/chunk-ELQZQKSN.js 1.37 kB 0 B
packages/cli/dist/cli.d.ts 2.48 kB 0 B
packages/cli/dist/cli.js 113 B 0 B
packages/cli/dist/commands-RWJT77ZY.js 104 B 0 B
packages/cli/dist/config.d.ts 131 B 0 B
packages/cli/dist/config.js 150 B 0 B
packages/cli/dist/index.d.ts 0 B 0 B 🆕
packages/cli/dist/index.js 95 B 0 B
packages/core/dist/api 0 B 0 B 🆕
packages/core/dist/chunk-IZT4EWJL.js 0 B 0 B 🆕
packages/core/dist/codegen/abstract/index.js 0 B 0 B 🆕
packages/core/dist/tools 0 B 0 B 🆕
packages/core/dist/utils 829 B 0 B
packages/core/dist/utils/index.d.ts 591 B 0 B
packages/core/dist/utils/index.js 310 B 0 B
packages/cosmwasm-utils/dist 0 B 0 B 🆕
packages/cosmwasm-utils/dist/_tsup-dts-rollup.d.ts 189 B 0 B
packages/cosmwasm-utils/dist/chunk-3ETX6U3V.js 156 B 0 B
packages/cosmwasm-utils/dist/chunk-KTEV5PS3.js 170 B 0 B
packages/cosmwasm-utils/dist/client 182 B 0 B
packages/cosmwasm-utils/dist/client/index.d.ts 0 B 0 B 🆕
packages/cosmwasm-utils/dist/client/index.js 81 B 0 B
packages/cosmwasm-utils/dist/index.d.ts 77 B 0 B
packages/cosmwasm-utils/dist/query 795 B 0 B
packages/cosmwasm-utils/dist/query/index.d.ts 0 B 0 B 🆕
packages/react/dist 120 B 0 B
packages/react/dist/index.js 0 B 0 B 🆕
packages/react/dist/use-withdraw-5n4FqUnJ.d.ts 0 B 0 B 🆕
packages/react/dist/utils 0 B 0 B 🆕
packages/react/dist/utils/index.d.ts 0 B 0 B 🆕
packages/react/dist/utils/index.js 0 B 0 B 🆕
packages/core/dist/chains-XsgjNPOH.d.ts 0 B 0 B 🆕
packages/core/dist/create-public-client-3G5TCsJR.d.ts 0 B 0 B 🆕
packages/core/dist/legacy/index.d.ts 0 B 0 B 🆕

compressed-size-action

@dalechyn dalechyn requested a review from adairrr January 18, 2024 17:19
@dalechyn dalechyn marked this pull request as ready for review January 18, 2024 17:19
Copy link
Contributor

@adairrr adairrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall comment: we should ensure that we have type the returns of actions to be ExecuteResult as well as the query results - which should be typed to what the user would expect.

@dalechyn
Copy link
Collaborator Author

Overall comment: we should ensure that we have type the returns of actions to be ExecuteResult as well as the query results - which should be typed to what the user would expect.

All the mutating actions return ExecuteResult except for:

  • deposit: DeliverTxResponse
  • execute: DeliverTxResponse
  • withdraw: DeliverTxResponse
  • createAccount: returns the resolved addresses for a created account

@dalechyn dalechyn requested a review from adairrr January 19, 2024 17:16
packages/core/src/actions/account/wallet/withdraw.ts Outdated Show resolved Hide resolved
packages/core/src/actions/get-ans-host-address-from-api.ts Outdated Show resolved Hide resolved
packages/core/src/actions/wallet/create-account.ts Outdated Show resolved Hide resolved
Comment on lines 32 to 41
getAccountFactoryAddressFromApi: (...args) =>
getAccountFactoryAddressFromApi(apiUrl, ...args),
getAnsHostAddressFromApi: (...args) =>
getAnsHostAddressFromApi(apiUrl, ...args),
getAccountsByOwnerFromApi: (...args) =>
getAccountsByOwnerFromApi(apiUrl, ...args),
getAnsTokenFromApi: (...args) => getAnsTokenFromApi(apiUrl, ...args),
getAnsTokensFromApi: (...args) => getAnsTokensFromApi(apiUrl, ...args),
getVersionControlAddressFromApi: (...args) =>
getVersionControlAddressFromApi(apiUrl, ...args),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the api client wouldn't it make sense to name them without the FromApi postfix?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could rename them here but overall it's better to keep the postfix

Comment on lines +3 to +5
export function accountIdToParameter(id: VersionControlTypes.AccountId) {
return { seq: id.seq, trace: id.trace }
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter should be the AccountId type rather than VersionControlTypes.AccountId,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless it's meant just to truncate the chainName?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method does not need chainName as it doesn't use one in the response

@adairrr
Copy link
Contributor

adairrr commented Jan 19, 2024

Overall comment: we should ensure that we have type the returns of actions to be ExecuteResult as well as the query results - which should be typed to what the user would expect.

All the mutating actions return ExecuteResult except for:

  • deposit: DeliverTxResponse
  • execute: DeliverTxResponse
  • withdraw: DeliverTxResponse
  • createAccount: returns the resolved addresses for a created account

Ah yes, forgot about DeliverTxResponse... this is because of the use of signAndBroadcast if I'm not mistaken. What's interesting is that the ExecuteResult doesn't extend DeliverTxResponse because the following logic just "removes" fields from it.

    async executeMultiple(senderAddress, instructions, fee, memo = "") {
...
        const result = await this.signAndBroadcast(senderAddress, msgs, fee, memo);
        if ((0, stargate_1.isDeliverTxFailure)(result)) {
            throw new Error(createDeliverTxResponseErrorMessage(result));
        }
        return {
            logs: stargate_1.logs.parseRawLog(result.rawLog),
            height: result.height,
            transactionHash: result.transactionHash,
            events: result.events,
            gasWanted: result.gasWanted,
            gasUsed: result.gasUsed,
        };
        ```

@dalechyn dalechyn requested a review from adairrr January 22, 2024 17:28
@dalechyn dalechyn merged commit 6dd5eab into mainline Jan 22, 2024
2 checks passed
@github-actions github-actions bot mentioned this pull request Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants