Skip to content

Commit

Permalink
feat: connect core backend (#102)
Browse files Browse the repository at this point in the history
* Generalise script for reading from production repo

* Fix autogenerated code files with update script

* Add auto-generated code from production repo

* Add local dev coins from backend repo

* Swap cosmos mock backend logic with cosmos core logic

* Fix searching of state for tick pairs: frontend has no sorting context

* Remove cosmos-mock generated (and semi-generated code)

* Add sorted Tick arrays and keyed Ticks by ID structures both:

    - to handle different scenarios better:
    - Keyed Ticks by ID are better for isolated context updates better
    - Sorted arrays allow sorting only once when needed

* Stop storing TickInfo indexes as they need to be updated on new info

    - when events come through with updates to certain ticks, indexes of all ticks will need to change

* Optimise finding state pairs without knowing the sorting order

* Subscribe to core backend NewDeposit and NewWithdraw shapes

* Make event new totalShares calculation more understandable

* Fix Query transformation of tick pools to be transformed independently

* Ensure Msg requests in token are rounded to minimum denom integers

* Add note about unhandled pool arrays

* Handle sorting of new tick pools

* Remove unused mock data of outdated format

* Add JSDoc types to helper script

Co-authored-by: Nick Z <nzoumis@outlook.com>

* Add note about price type

* Remove explicit \n chars from autogenerated code changes

* Update generated code with new changes

* Do not check for pool length in one direction:

    - pools may be empty in one direction if there is no liquidity of one of the tokens available

* Fix tickState sorted pools shape on handling Events

* Revert "Update generated code with new changes"

This reverts commit cc08afe.

* Abstract out combining old tick state new tick data

* Abstract out combining old tick state new tick data, part 2: move

* Rename message handler for dex module messages

* Add update to tick state from NewSwap messages

* Fix unsurfaced possible value is undefined error

* Remove previous commit issue by replacing tick objects completely

* Add PoolTicks type

* Add note about possible future backend provision of totalShares

* Add note about tick state optimization

Co-authored-by: Nick Z <nzoumis@outlook.com>
  • Loading branch information
dib542 and nickzoum authored Aug 26, 2022
1 parent 254056b commit 7ca9e8c
Show file tree
Hide file tree
Showing 45 changed files with 9,320 additions and 2,431 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ REACT_APP__CHAIN_ID=duality
REACT_APP__CHAIN_NAME=Duality testnet
REACT_APP__COIN_DENOM=COSMOS
REACT_APP__COIN_MIN_DENOM=token
REACT_APP__COIN_MIN_DENOM_EXP=18
REACT_APP__REST_API=http://localhost:1317
REACT_APP__RPC_API=http://localhost:26657
REACT_APP__WEBSOCKET_URL=ws://localhost:26657/websocket
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@

# ignore change log, it is auto-formatted by semantic release
CHANGELOG.md

# autogenerated code
/src/lib/web3/generated
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
"typescript": "^4.6.3"
},
"lint-staged": {
"**/*.{js,jsx,ts,tsx,json,css,scss,html,md,yaml,yml}": [
"**/*.{mjs,js,jsx,ts,tsx,json,css,scss,html,md,yaml,yml}": [
"prettier --write"
],
"**/*.{js,jsx,ts,tsx}": [
"**/*.{mjs,js,jsx,ts,tsx}": [
"eslint --max-warnings 0"
]
},
Expand Down
90 changes: 90 additions & 0 deletions scripts/update-generated-files.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import fs from 'fs';

/**
* @param {string} directory
* @param {{ recursive: boolean }} opts
*/
function getDirFilenames(directory, opts = {}) {
return fs
.readdirSync(directory, { withFileTypes: true })
.reduce((result, file) => {
const path = `${directory}/${file.name}`;
if (opts.recursive && file.isDirectory()) {
result.push(...getDirFilenames(path, opts));
} else {
result.push(path);
}
return result;
}, []);
}

const files = getDirFilenames('./src/lib/web3/generated', { recursive: true });

// fix ESLint and TypeScript warnings
files
.filter((file) => file.endsWith('/index.ts'))
.forEach((file) => {
const data = fs.readFileSync(file, { encoding: 'utf8' });
fs.writeFileSync(
file,
`
/* eslint-disable */
/* tslint:disable */
${data}`.trimStart()
);
});

// fix TypeScript error
files
.filter((file) => file.endsWith('/index.ts'))
.forEach((file) => {
const data = fs.readFileSync(file, { encoding: 'utf8' });
const get = `
let client;
if (addr) {
client = await SigningStargateClient.connectWithSigner(addr, wallet, { registry });
}else{
client = await SigningStargateClient.offline( wallet, { registry });
}
`;
const set = `
const client = addr
? await SigningStargateClient.connectWithSigner(addr, wallet, { registry })
: await SigningStargateClient.offline( wallet, { registry });
`;
const replaced = data.replace(get, set);
fs.writeFileSync(file, replaced);
});

// fix hardcoded URLs
files
.filter((file) => file.endsWith('/index.ts'))
.forEach((file) => {
const replaced = fs
.readFileSync(file, { encoding: 'utf8' })
.replace(
'"http://localhost:26657"',
'process.env.REACT_APP__RPC_API || ""'
)
.replace(
'"http://localhost:1317"',
'process.env.REACT_APP__REST_API || ""'
);
fs.writeFileSync(file, replaced);
});

// remove non-module index files (eg. vuex index.ts files)
files
.filter(
(file) => file.endsWith('/index.ts') && !file.endsWith('/module/index.ts')
)
.forEach((file) => {
fs.rmSync(file);
});

// remove vuex-root files
files
.filter((file) => file.endsWith('/vuex-root'))
.forEach((file) => {
fs.rmSync(file);
});
23 changes: 11 additions & 12 deletions scripts/update-generated-files.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#!/bin/sh

# use provided source path or a default
DUALITY_CORE_DIRECTORY="${1:-"../duality"}"
CHAIN_REPO="${1:-"duality"}"
CHAIN_REPO_USER_NAME=$(echo $CHAIN_REPO | cut -d '/' -f 1) # get name part
CHAIN_REPO_PROJECT_NAME=$(echo "$CHAIN_REPO" | cut -d '/' -f 2) # get name part
CHAIN_REPO_PROJECT_NAME="${CHAIN_REPO_PROJECT_NAME:-$CHAIN_REPO_USER_NAME}" # default project name to copy of username if not specified
CHAIN_REPO_MODULE_NAME=$(echo $CHAIN_REPO_USER_NAME | tr '[:upper:]' '[:lower:]') # get name part as lowercase of username
DUALITY_CORE_DIRECTORY="${2:-"../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 module files
cp -r "$DUALITY_CORE_DIRECTORY/vue/src/store/generated/$CHAIN_REPO" \
"src/lib/web3/generated"

# copy readme info
cp -r "$DUALITY_CORE_DIRECTORY/vue/src/store/generated/readme.md" \
"src/lib/web3/generated/readme.md"

node ./scripts/update-generated-files.mjs
2 changes: 2 additions & 0 deletions src/components/TokenPicker/mockHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export interface SwapRequest {
}

const tokens: Array<Token> = [
{ logo: null, symbol: 'TKN', name: 'TokenCoin', address: 'token' },
{ logo: null, symbol: 'STK', name: 'StakeCoin', address: 'stake' },
{ logo: null, symbol: 'Eth', name: 'Ether', address: 'ETH' },
{
logo: null,
Expand Down
79 changes: 0 additions & 79 deletions src/lib/web3/api.ts

This file was deleted.

Loading

0 comments on commit 7ca9e8c

Please sign in to comment.