Skip to content

Commit

Permalink
#96
Browse files Browse the repository at this point in the history
  • Loading branch information
dib542 committed Jul 22, 2022
1 parent 5ca004f commit 5c73c16
Show file tree
Hide file tree
Showing 20 changed files with 10,910 additions and 54 deletions.
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
Expand Down
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",
"bignumber.js": "^9.0.2",
Expand All @@ -26,6 +30,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 });
};

export { txClient, queryClient };
Loading

0 comments on commit 5c73c16

Please sign in to comment.