Skip to content

Commit

Permalink
update getSaverPosition() (#871)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorian1te authored Oct 23, 2023
1 parent 7b755eb commit dec9bda
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 45 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"e2e": "lerna run e2e",
"lint": "lerna run lint",
"updateDeps": " ts-node scripts/updateDeps.ts",
"updatePackages": "python3 scripts/updatePackageVersion.py ./packages"
"updatePackages": "python3 scripts/updatePackageVersion.py ./packages",
"checkVersion": "ts-node scripts/checkVersionPublished.ts ./packages"
},
"husky": {
"hooks": {
Expand Down
4 changes: 2 additions & 2 deletions packages/xchain-thorchain-amm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@xchainjs/xchain-midgard": "^0.5.2",
"@xchainjs/xchain-thorchain": "^0.28.8",
"@xchainjs/xchain-thornode": "^0.3.8",
"@xchainjs/xchain-thorchain-query": "^0.6.4",
"@xchainjs/xchain-thorchain-query": "^0.6.5",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.5",
"axios": "^1.3.6",
Expand Down Expand Up @@ -91,7 +91,7 @@
"@xchainjs/xchain-midgard": "^0.5.2",
"@xchainjs/xchain-thornode": "^0.3.8",
"@xchainjs/xchain-thorchain": "^0.28.8",
"@xchainjs/xchain-thorchain-query": "^0.6.4",
"@xchainjs/xchain-thorchain-query": "^0.6.5",
"@xchainjs/xchain-util": "^0.13.1",
"@xchainjs/xchain-utxo-providers": "^0.2.5",
"axios": "^1.3.6",
Expand Down
6 changes: 6 additions & 0 deletions packages/xchain-thorchain-query/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.6.5 (2023-10-22)

## Update

- address comparion fix, previously failing case matching

# v0.6.4 (2023-10-19)

## Update
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Thorchain-query thorname Integration Tests', () => {
})
it('Estimate not registered thorname without expirity', async () => {
const thorname = await thorchainQuery.estimateThorname({
thorname: 'hippo-2',
thorname: 'asgardex',
chain: BTCChain,
chainAddress: btcAddress,
owner: owner,
Expand Down
4 changes: 2 additions & 2 deletions packages/xchain-thorchain-query/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-thorchain-query",
"version": "0.6.4",
"version": "0.6.5",
"license": "MIT",
"description": "Thorchain query module that is resposible for estimating swap calculations and add/remove liquidity for thorchain ",
"keywords": [
Expand Down Expand Up @@ -53,4 +53,4 @@
"publishConfig": {
"access": "public"
}
}
}
69 changes: 40 additions & 29 deletions packages/xchain-thorchain-query/src/thorchain-query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LastBlock, Thorname } from '@xchainjs/xchain-thornode'
import { LastBlock } from '@xchainjs/xchain-thornode'
import {
Address,
Asset,
Expand Down Expand Up @@ -770,8 +770,9 @@ export class ThorchainQuery {
const blockData = (await this.thorchainCache.thornode.getLastBlock()).find(
(item: LastBlock) => item.chain === params.asset.chain,
)
// address comparison is done after conversion to lower case
const savers = (await this.thorchainCache.thornode.getSavers(`${params.asset.chain}.${params.asset.symbol}`)).find(
(item) => item.asset_address === params.address,
(item) => item.asset_address.toLowerCase() === params.address.toLowerCase(),
)

const pool = (await this.thorchainCache.getPoolForAsset(params.asset)).thornodeDetails
Expand Down Expand Up @@ -1004,10 +1005,38 @@ export class ThorchainQuery {
public async getThornameDetails(thorname: string, height?: number): Promise<ThornameDetails> {
const errors: string[] = []

const thornameResp = await this.thorchainCache.thornode.getThornameDetails(thorname, height)
const response: { error?: string } = JSON.parse(JSON.stringify(thornameResp))
if (response.error) errors.push(`Thornode request quote failed: ${response.error}`)
if (errors.length > 0) {
try {
const thornameResp = await this.thorchainCache.thornode.getThornameDetails(thorname, height)
const response: { error?: string } = JSON.parse(JSON.stringify(thornameResp))
if (response.error) errors.push(`Thornode request quote failed: ${response.error}`)
if (errors.length > 0) {
const errorResp: ThornameDetails = {
name: '',
expireBlockHeight: 0,
owner: '',
preferredAsset: '',
affiliateCollectorRune: '',
aliases: [],
error: errors,
}
return errorResp
}

const thornameAliases: ThornameAlias[] = thornameResp.aliases.map((alias) => ({
chain: alias.chain as Chain,
address: alias.address as Address,
}))

const thornameDetails: ThornameDetails = {
name: thornameResp.name || '',
expireBlockHeight: thornameResp.expire_block_height || 0,
owner: thornameResp.owner || '',
preferredAsset: thornameResp.preferred_asset || '',
affiliateCollectorRune: thornameResp.affiliate_collector_rune || '',
aliases: thornameAliases || [],
}
return thornameDetails
} catch (e) {
const errorResp: ThornameDetails = {
name: '',
expireBlockHeight: 0,
Expand All @@ -1019,22 +1048,6 @@ export class ThorchainQuery {
}
return errorResp
}

const thornameAliases: ThornameAlias[] = thornameResp.aliases.map((alias) => ({
chain: alias.chain as Chain,
address: alias.address as Address,
}))

const thornameDetails: ThornameDetails = {
name: thornameResp.name || '',
expireBlockHeight: thornameResp.expire_block_height || 0,
owner: thornameResp.owner || '',
preferredAsset: thornameResp.preferred_asset || '',
affiliateCollectorRune: thornameResp.affiliate_collector_rune || '',
aliases: thornameAliases || [],
}

return thornameDetails
}

/**
Expand All @@ -1050,18 +1063,16 @@ export class ThorchainQuery {
*/
public async estimateThorname(params: QuoteThornameParams) {
// CHECK IF ALREADY EXISTS
const thornameDetails = (await this.thorchainCache.thornode.getThornameDetails(
params.thorname,
)) as unknown as Thorname // TODO: Until integrate THORNode PR
const thornameDetails = await this.getThornameDetails(params.thorname)

if (thornameDetails && !params.isUpdate) {
if (thornameDetails.owner !== '' && !params.isUpdate) {
throw Error('Thorname already registered')
}

const blockData = await this.thorchainCache.thornode.getLastBlock()
const currentThorchainHeight = blockData[0].thorchain
const currentHeightForExpirity = params.isUpdate
? (thornameDetails?.expire_block_height as number)
? (thornameDetails?.expireBlockHeight as number)
: currentThorchainHeight

// DEFAULT EXPIRITY
Expand All @@ -1074,8 +1085,8 @@ export class ThorchainQuery {
const numberOfSecondsToExpire = expirityTimestamp - currentTimestamp
const numberOfBlocks = Math.round(numberOfSecondsToExpire / 6)
const newHeightExpirity = currentThorchainHeight + numberOfBlocks
numberOfBlocksToAddToExpirity = thornameDetails?.expire_block_height
? newHeightExpirity - thornameDetails?.expire_block_height
numberOfBlocksToAddToExpirity = thornameDetails?.expireBlockHeight
? newHeightExpirity - thornameDetails?.expireBlockHeight
: numberOfBlocks
}
// COMPUTE VALUE
Expand Down
6 changes: 1 addition & 5 deletions packages/xchain-thorchain-query/src/utils/thornode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,7 @@ export class Thornode {
try {
const resp = (await api.thorname(thorname, height)).data
return resp
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
if (e.response.status == 404) {
}
}
} catch (e) {}
}
throw new Error(`THORNode is not responding`)
}
Expand Down
12 changes: 7 additions & 5 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

### Examples


### Using python scripting update package version Minor or Patch

It will only bump version number by 1. i.e for patch > 0.1.1 -> 0.1.2 for Minor > 0.1.1 -> 0.2.0
It will only bump version number by 1. i.e for patch > 0.1.1 -> 0.1.2 for Minor > 0.1.1 -> 0.2.0

For the whole library.

Expand All @@ -26,8 +25,7 @@ For just one package.
yarn updatePackages minor Update "update rollup config and axios to the latest" avax
```


## Using Typescript to update package dependencies in other library packages.
## Using Typescript to update package dependencies in other library packages.

// For xchainjs packages, will search the library for matching args and update
yarn updateDeps <packageName> <packageVersion>
Expand All @@ -38,12 +36,16 @@ yarn updateDeps client 0.13.7

### For other packages

For all other package deps flag <true> last arg for script to search for non xchainjs packages
For all other package deps flag <true> last arg for script to search for non xchainjs packages

yarn updateDeps <fullpackageName> <packageVersion> <boolean>

```
yarn updateDeps @psf/bitcoincashjs-lib 4.0.3 true
```

### For version comparison between published version & current xchainjs Master version

```
yarn checkVersion
```
75 changes: 75 additions & 0 deletions scripts/checkVersionPublished.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const { execSync } = require('child_process')
const fs = require('fs')
const path = require('path')

interface SimplePackageJson {
name: string
version: string
}

function getLatestVersion(packageName: string): string {
try {
return execSync(`npm show ${packageName} version`, { encoding: 'utf-8' }).trim()
} catch (e) {
return 'unavailable'
}
}

const publishCommands: string[] = []

function compareVersions(packagePath: string, packageName: string, currentVersion: string): void {
const latestVersion = getLatestVersion(packageName)
if (latestVersion === 'unavailable') {
console.log(`Unable to fetch latest version for ${packageName}.`)
return
}

if (currentVersion !== latestVersion) {
const relativePath = path.relative(process.cwd(), packagePath)
const publishCommand = `(cd ${relativePath} && npm publish)`

console.log(
`Publish new version for ${packageName} in ${packagePath} from npm version ${latestVersion} to ${currentVersion}`,
)
publishCommands.push(publishCommand)
}
}
function checkPackageJson(packagePath: string): void {
const packageJsonPath = path.join(packagePath, 'package.json')
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) as SimplePackageJson

const { name, version } = packageJson
compareVersions(packagePath, name, version)
}

function getPackagePaths(directoryPath: string): string[] {
const files = fs.readdirSync(directoryPath)
const packagePaths: string[] = []

for (const file of files) {
const filePath = path.join(directoryPath, file)
const fileStat = fs.statSync(filePath)
if (fileStat.isDirectory() && fs.existsSync(path.join(filePath, 'package.json'))) {
packagePaths.push(filePath)
} else if (fileStat.isDirectory()) {
packagePaths.push(...getPackagePaths(filePath))
}
}

return packagePaths
}

function main(): void {
const packagesPath = path.join(__dirname, '..', 'packages')
const packagePaths = getPackagePaths(packagesPath)

for (const packagePath of packagePaths) {
checkPackageJson(packagePath)
}
if (publishCommands.length > 0) {
console.log('\nPublish Commands:')
console.log(publishCommands.join('\n'))
}
}

main()

0 comments on commit dec9bda

Please sign in to comment.