Skip to content

Commit

Permalink
chore: better formatters and new toSignificant (thorswap#365)
Browse files Browse the repository at this point in the history
* chore: better formatters and new toSignificant

* chore: add more big int tests

* fix: assetValue, node env playground & comsos connections
  • Loading branch information
ice-chillios committed Oct 28, 2023
1 parent 645e7d9 commit 4ad62d8
Show file tree
Hide file tree
Showing 31 changed files with 800 additions and 648 deletions.
21 changes: 21 additions & 0 deletions .changeset/chilly-bears-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@swapkit/wallet-evm-extensions': patch
'@swapkit/toolbox-cosmos': patch
'@swapkit/wallet-keystore': patch
'@swapkit/helpers': patch
'@swapkit/tokens': patch
'@swapkit/toolbox-utxo': patch
'@swapkit/wallet-ledger': patch
'@swapkit/wallet-trezor': patch
'@swapkit/types': patch
'@swapkit/toolbox-evm': patch
'@swapkit/wallet-keplr': patch
'@swapkit/wallet-xdefi': patch
'@swapkit/core': patch
'@swapkit/api': patch
'@swapkit/sdk': patch
'@swapkit/wallet-okx': patch
'@swapkit/wallet-wc': patch
---

fix rpc connection
5 changes: 5 additions & 0 deletions .changeset/funny-poets-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@swapkit/tokens': patch
---

add kuji.usk
1 change: 0 additions & 1 deletion internal/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"eslint-plugin-babel": "5.3.1",
"eslint-plugin-eslint-comments": "3.2.0",
"eslint-plugin-import": "2.28.1",
"eslint-plugin-jest": "27.2.3",
"eslint-plugin-prettier": "5.0.0",
"eslint-plugin-react": "7.33.2",
"eslint-plugin-react-hooks": "4.6.0",
Expand Down
75 changes: 75 additions & 0 deletions internal/node-playground/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { AssetValue, Chain, SwapKitCore } from '@swapkit/core';
import { keystoreWallet } from '@swapkit/wallet-keystore';
let skClient: SwapKitCore | undefined;
const phrase = process.env.PRHASES;

if (!phrase) throw new Error('No phrase found');

const getSwapKitClient = async () => {
try {
if (skClient) return skClient;
const client = new SwapKitCore();
client.extend({ wallets: [keystoreWallet] });

await client.connectKeystore([Chain.THORChain], phrase);
skClient = client;
return client;
} catch (error) {
console.error(error);
throw new Error('Error getting SwapKit client');
}
};
/**
* send an asset from your wallet to another address
*/
export const doSend = async ({
sendAmount,
toAddress,
}: {
sendAmount: number;
toAddress: string;
}) => {
try {
const client = await getSwapKitClient();
const from = client.getAddress(Chain.THORChain);
const balance = await client.getBalance(Chain.THORChain);
console.log(`Balance: ${balance}`);
console.log(`💰 Wallet - ${from} | Balance: ${balance}}`);

const assetValue = await AssetValue.fromString('THOR.RUNE', sendAmount);
console.log(`💰 Sending ${sendAmount} RUNE to ${toAddress}`);
console.log(`💰 Asset value: ${assetValue.assetValue}`);
try {
const connectedWallets = client.connectedWallets;
return connectedWallets.THOR?.transfer({
from,
assetValue,
recipient: toAddress,
memo: '',
})
.then((txHash) => {
console.log(txHash);
return txHash;
})
.catch((err) => {
console.log(err);
return '';
});
} catch (error) {
console.error(error);
}
} catch (error) {
console.error(error);
return '';
}
};

const main = async () => {
const tx = await doSend({
sendAmount: 0.1,
toAddress: 'thor1e9lxzfl7x2zvxnjczf8v0urel8943nesq9c4pk',
});
console.log(tx);
};

main();
25 changes: 25 additions & 0 deletions internal/node-playground/package.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"dependencies": {
"@swapkit/core": "../../packages/swapkit/core/src/index.ts",
"@swapkit/helpers": "../../packages/swapkit/helpers/src/index.ts/index.ts",
"@swapkit/types": "../../packages/swapkit/types/src/index.ts",
"@swapkit/wallet-keystore": "../../packages/wallets/keystore/src/index.ts",
"@swapkit/toolbox-cosmos": "../../packages/toolboxes/cosmos/src/index.ts",
"@swapkit/toolbox-evm": "../../packages/toolboxes/evm/src/index.ts",
"@swapkit/toolbox-utxo": "../../packages/toolboxes/utxo/src/index.ts"
},
"devDependencies": {
"bun-types": "1.0.7"
},
"eslintConfig": {
"extends": "../../internal/eslint-config"
},
"private": true,
"scripts": {
"dev:node": "ts-node ./index.ts",
"dev:bun": "bun run ./index.ts",
"preview-playground": "vite preview"
},
"type": "module"
}

25 changes: 25 additions & 0 deletions internal/node-playground/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"allowJs": false,
"allowSyntheticDefaultImports": true,
"baseUrl": "./src",
"composite": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"jsx": "react-jsx",
"lib": ["dom", "dom.iterable", "ESNext"],
"module": "ESNext",
"moduleResolution": "bundler",
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"strictNullChecks": true,
"target": "ESNext",
}
}
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"eslint-plugin-babel": "5.3.1",
"eslint-plugin-eslint-comments": "3.2.0",
"eslint-plugin-import": "2.28.1",
"eslint-plugin-jest": "27.4.0",
"eslint-plugin-prettier": "5.0.0",
"eslint-plugin-react": "7.33.2",
"eslint-plugin-react-hooks": "4.6.0",
Expand Down Expand Up @@ -60,13 +59,15 @@
"preview:playground": "turbo run preview-playground --token=turbotoken-oss-europe1 --concurrency=30",
"clean": "turbo run clean",
"dev:playground": "turbo run dev --concurrency=30 --filter=\"{./internal/playground}...\"",
"dev:playground:node": "turbo run dev:node --concurrency=30 --filter=\"{./internal/node-playground}...\"",
"dev:playground:bun": "turbo run dev:bun --concurrency=30 --filter=\"{./internal/node-playground}...\"",
"generate": "cd ./internal/generator; pnpm generate; cd ../..",
"generate:tokens": "cd ./packages/swapkit/tokens; pnpm run generate-tokens; cd ../../..; pnpm run lint",
"lint": "turbo run lint --token=turbotoken-oss-europe1 --filter=!./packages/swapkit/swapkit-sdk",
"lint": "turbo run lint --continue --token=turbotoken-oss-europe1 --filter=!./packages/swapkit/swapkit-sdk",
"postinstall": "rm -rf ./.git/hooks/pre-commit ./.git/hooks/prepare-commit-msg",
"publish-packages": "turbo run build lint --token=turbotoken-oss-europe1 && pnpm version-bump && pnpm changeset publish",
"test": "dotenv -- turbo run test --token=turbotoken-oss-europe1",
"test:coverage": "dotenv -- turbo run test:coverage",
"test": "dotenv -- turbo run test --continue --token=turbotoken-oss-europe1",
"test:coverage": "dotenv -- turbo --continue run test:coverage",
"version-bump": "dotenv -- pnpm changeset version"
}
}
40 changes: 0 additions & 40 deletions packages/swapkit/helpers/src/helpers/number.ts

This file was deleted.

59 changes: 34 additions & 25 deletions packages/swapkit/helpers/src/helpers/others.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,28 @@ export const getRequest = async <T>(
url: string,
params?: { [key in string]?: any },
): Promise<T> => {
const queryParams = Object.entries(params || {}).reduce(
(acc, [key, value]) => {
if (value) {
acc[key] = value;
}

return acc;
},
{} as { [key in string]: any },
);

const response = await fetch(
`${url}${params ? `?${new URLSearchParams(queryParams).toString()}` : ''}`,
{ method: 'GET', mode: 'cors', credentials: 'omit', referrer: 'https://sk.thorswap.net' },
);

return response.json();
try {
const queryParams = Object.entries(params || {}).reduce(
(acc, [key, value]) => {
if (value) {
acc[key] = value;
}

return acc;
},
{} as { [key in string]: any },
);

const response = await fetch(
`${url}${params ? `?${new URLSearchParams(queryParams).toString()}` : ''}`,
{ method: 'GET', mode: 'cors', credentials: 'omit', referrer: 'https://sk.thorswap.net' },
);

return response.json();
} catch (error) {
console.error(error);
return {} as T;
}
};

export const postRequest = async <T>(
Expand All @@ -48,12 +53,16 @@ export const postRequest = async <T>(
headers?: Record<string, string>,
parseAsString = false,
): Promise<T> => {
const response = await fetch(`${url}`, {
body,
headers,
method: 'POST',
referrer: 'https://sk.thorswap.net',
});

return parseAsString ? response.text() : response.json();
try {
const response = await fetch(`${url}`, {
body,
headers,
method: 'POST',
referrer: 'https://sk.thorswap.net',
});

return parseAsString ? response.text() : response.json();
} catch (error) {
return {} as T;
}
};
8 changes: 4 additions & 4 deletions packages/swapkit/helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
export * from './helpers/asset.ts';
export * from './helpers/liquidity.ts';
export * from './helpers/memo.ts';
export * from './helpers/number.ts';
export * from './helpers/others.ts';

/**
* Modules
*/
export { AssetValue, getMinAmountByChain } from './modules/assetValue.ts';
export { type Keys, SwapKitError } from './modules/swapKitError.ts';
export { SwapKitNumber } from './modules/swapKitNumber.ts';
export * from './modules/assetValue.ts';
export * from './modules/bigIntArithmetics.ts';
export * from './modules/swapKitError.ts';
export * from './modules/swapKitNumber.ts';
Loading

0 comments on commit 4ad62d8

Please sign in to comment.