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: 83 add cosmos service: add query and tx clients #96

Merged
merged 28 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f2a17a7
Import generated Duality core code (and ignore linting errors)
dib542 Jun 30, 2022
68d7790
Add a generated code update script
dib542 Jun 30, 2022
6252360
Add used dependencies in generated code
dib542 Jun 30, 2022
18efcf0
Downgrade stargate to avoid inconsistent sub-dependencies:
dib542 Jul 8, 2022
fb1cc85
Add @cosmjs node environment dependencies
dib542 Jul 1, 2022
8802a73
Ignore generated TypeScript issues: but it doesn't ignore all of them
dib542 Jul 1, 2022
4f67236
Fix Startport's terrible code with no way to automatically fix
dib542 Jul 1, 2022
88bc2e5
Add potential generated script fix
dib542 Jul 5, 2022
97690e8
Add autogenerated changes about tick query URL path
dib542 Jul 11, 2022
9f10a82
Fix Webpack sourcemap warnings: by disabling sourcemaps
dib542 Jul 21, 2022
6523acc
Fix: align client.signAndBroadcast type options
dib542 Jul 21, 2022
5d0dba6
Change default fee to 'auto'
dib542 Jul 21, 2022
9c32edf
Create custom API file from generated file
dib542 Jul 21, 2022
8e0203e
Move generated files and allow Prettier to format them
dib542 Jul 21, 2022
b218af0
Remove previous rules for not formatting generated code
dib542 Jul 21, 2022
6a65370
Allow update script to be run as an package script
dib542 Jul 21, 2022
145297d
Copy over only generated types and REST file: index.ts is now custom
dib542 Jul 21, 2022
3dd8509
Attempt to add all generated Msgs to txClient dynamically
dib542 Jul 21, 2022
1343390
Revert "Attempt to add all generated Msgs to txClient dynamically"
dib542 Jul 22, 2022
b312a4d
Add environment variables for local chain connecting
dib542 Jul 21, 2022
6daa277
Make indexer environment URLs consistent with prototype listener URLs
dib542 Jul 22, 2022
e438a70
Make Cosmos Query and Tx env variables consistent with Event variables
dib542 Jul 22, 2022
93375f7
Update to my suggested environment variable names
dib542 Jul 22, 2022
66cc6a2
Add comments about updating Msg methods
dib542 Jul 22, 2022
d1729a5
Allow more API config properties to be passed through new queryClient
dib542 Jul 22, 2022
7f08a17
Simplify ApiConfig passing with default and spreading arbitrary params
dib542 Jul 25, 2022
73c5ded
Merge branch 'main' into 83-add-query-and-tx-clients
dib542 Jul 25, 2022
0f54a0f
Align environment variables to main branch
dib542 Jul 25, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
# link: https://create-react-app.dev/docs/adding-custom-environment-variables/#expanding-environment-variables-in-env
REACT_APP__APP_VERSION=$npm_package_version

# disable source-maps. @cosmjs packages do not publish source code on npm
# so attempting to generate them will cause hundreds of errorrs.
GENERATE_SOURCEMAP=false

# Chain Info
REACT_APP__RPC_API=http://localhost:26657
REACT_APP__REST_API=http://localhost:1317
REACT_APP__WEBSOCKET_URL=ws://localhost:26657/websocket

# Contract Info
REACT_APP__CHAIN_ID=31337
REACT_APP__CONTRACT_ADDRESS__DUALITY_CORE=0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9
1,550 changes: 1,498 additions & 52 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
"build": "react-scripts build",
"test": "react-scripts test",
"lint": "eslint .",
"prepare": "husky install"
"prepare": "husky install",
"update": "sh ./scripts/update-generated-files.sh"
},
"engines": {
"node": "^16.14.2",
"npm": "^8.5.0"
},
"dependencies": {
"@cosmjs/launchpad": "^0.27.1",
"@cosmjs/proto-signing": "^0.27.1",
"@cosmjs/stargate": "^0.27.1",
"@floating-ui/react-dom": "^0.6.3",
"@reach/dialog": "^0.17.0",
"buffer": "^6.0.3",
Expand All @@ -25,6 +29,11 @@
"react-router-dom": "^6.3.0",
"web-vitals": "^2.1.4"
},
"peerDependencies": {
"crypto": "npm:crypto-browserify@^3.12.0",
"path": "npm:path-browserify@^1.0.1",
"stream": "npm:stream-browserify@3.0.0"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
Expand Down
20 changes: 20 additions & 0 deletions scripts/update-generated-files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

# use provided source path or a default
DUALITY_CORE_DIRECTORY="${1:-"../duality"}"

# copy type files
cp -r "$DUALITY_CORE_DIRECTORY/vue/src/store/generated/duality/duality.duality/module/types" \
"src/lib/web3/generated/duality/duality.duality/module"

# copy REST API file
cp -r "$DUALITY_CORE_DIRECTORY/vue/src/store/generated/duality/duality.duality/module/rest.ts" \
"src/lib/web3/generated/duality/duality.duality/module/rest.ts"

# copy version info
cp -r "$DUALITY_CORE_DIRECTORY/vue/src/store/generated/duality/duality.duality/package.json" \
"src/lib/web3/generated/duality/duality.duality/package.json"

# copy readme info
cp -r "$DUALITY_CORE_DIRECTORY/vue/src/store/generated/readme.md" \
"src/lib/web3/generated/readme.md"
72 changes: 72 additions & 0 deletions src/lib/web3/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This file is modified from an original generated/duality/duality.duality/module/index.ts file
//
// This file will not auto generate changes:
// if new Msgs are created they should be added to the txClient below

import { StdFee } from '@cosmjs/launchpad';
import { defaultRegistryTypes, SigningStargateClient } from '@cosmjs/stargate';
import { Registry, OfflineSigner, EncodeObject } from '@cosmjs/proto-signing';
import { Api } from './generated/duality/duality.duality/module/rest';
import {
MsgWithdrawShares,
MsgDepositShares,
} from './generated/duality/duality.duality/module/types/duality/tx';

const { REACT_APP__RPC_API = '', REACT_APP__REST_API = '' } = process.env;

export const MissingWalletError = new Error('wallet is required');

export const registry = new Registry(defaultRegistryTypes);

// -----> register our Msgs here
registry.register('/duality.duality.MsgDepositShares', MsgDepositShares);
registry.register('/duality.duality.MsgWithdrawShares', MsgWithdrawShares);

interface TxClientOptions {
addr?: string;
}

interface SignAndBroadcastOptions {
fee?: StdFee | 'auto' | number;
memo?: string;
}

const txClient = async (
wallet: OfflineSigner,
{ addr = REACT_APP__RPC_API }: TxClientOptions = {}
) => {
if (!wallet) throw MissingWalletError;
const client = addr
? await SigningStargateClient.connectWithSigner(addr, wallet, { registry })
: await SigningStargateClient.offline(wallet, { registry });
const { address } = (await wallet.getAccounts())[0];

return {
signAndBroadcast: (
msgs: EncodeObject[],
{ fee = 'auto', memo }: SignAndBroadcastOptions = {}
) => client.signAndBroadcast(address, msgs, fee, memo),

// -----> register our Msg client methods here
msgWithdrawShares: (data: MsgWithdrawShares): EncodeObject => ({
typeUrl: '/duality.duality.MsgWithdrawShares',
value: MsgWithdrawShares.fromPartial(data),
}),
msgDepositShares: (data: MsgDepositShares): EncodeObject => ({
typeUrl: '/duality.duality.MsgDepositShares',
value: MsgDepositShares.fromPartial(data),
}),
};
};

interface QueryClientOptions {
addr?: string;
}

const queryClient = async ({
addr = REACT_APP__REST_API,
}: QueryClientOptions = {}) => {
return new Api({ baseUrl: addr });
};
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels a bit too complicated
If we don't add any more attributes something like would be a lot simpler

Suggested change
interface QueryClientOptions {
addr?: string;
}
const queryClient = async ({
addr = REACT_APP__REST_API,
}: QueryClientOptions = {}) => {
return new Api({ baseUrl: addr });
};
const queryClient = (addr = REACT_APP__REST_API ) => {
return new Api({ baseUrl: addr });
}

If we do, I think somewhat spreading the defaults would make a more readable (maybe a tiny bit)

Suggested change
interface QueryClientOptions {
addr?: string;
}
const queryClient = async ({
addr = REACT_APP__REST_API,
}: QueryClientOptions = {}) => {
return new Api({ baseUrl: addr });
};
const defaultOptions = {
addr: REACT_APP__REST_API
};
const queryClient = (options = defaultOptions) => {
const { addr } = { ...defaultOptions, ...options };
return new Api({ baseUrl: addr });
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Resolved by pulling in the already generated ApiConfig type with the baseUrl already predefined in d1729a5


export { txClient, queryClient };
Loading