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

Fix Bug with Wallet Address, Update Wasm Command, and Update Denom Utils #120

Merged
merged 8 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"changesets": [
"blue-geese-change",
"shaggy-carrots-serve",
"warm-oranges-attack"
"warm-oranges-attack",
"wild-oranges-give"
]
}
7 changes: 7 additions & 0 deletions .changeset/wild-oranges-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@andromedaprotocol/andromeda.js": patch
"@andromedaprotocol/cli": patch
"@andromedaprotocol/gql": patch
---

NPM package updates and Wallet Address cache in CLI
24 changes: 24 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test Packages
on:
pull_request:
types:
- opened
- synchronize
branches:
- "main"
- "development"
paths:
- "packages/**"
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "20.x"
registry-url: "https://registry.npmjs.org"
- run: npm install -g pnpm
- run: pnpm install
- run: pnpm run build
- run: pnpm run test
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"scripts": {
"build": "pnpm --filter \"./packages/*\" run build",
"test": "pnpm --filter \"./packages/*\" run test",
"prepack": "rimraf packages/*/andromedaprotocol-*.tgz",
"pack": "npm run build && pnpm --filter \"./packages/*\" exec pnpm pack",
"postpack": "npm run collect",
Expand All @@ -30,7 +31,8 @@
"follow-redirects@<=1.15.5": ">=1.15.6",
"braces@<3.0.3": ">=3.0.3",
"ws@>=7.0.0 <7.5.10": ">=7.5.10",
"micromatch@<4.0.8": ">=4.0.8"
"micromatch@<4.0.8": ">=4.0.8",
"dset@<3.1.4": ">=3.1.4"
}
}
}
12 changes: 11 additions & 1 deletion packages/andrjs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
# @andromedaprotocol/andromeda.js

## 2.0.0-beta.3

### Patch Changes

- NPM package updates
- Fix: Denom utilities now return the correct amount of decimal places
- Tests: Added tests for denom utilities
- Fix: Query Contract Raw supports Uint8Array keys now
- Updated dependencies
- @andromedaprotocol/[email protected]

## 2.0.0-beta.2

### Patch Changes

Feat - Endpoint can now be rpc string or RPC Client
Feat - Added simulateRaw command


## 2.0.0-beta.1

### Patch Changes
Expand Down
6 changes: 6 additions & 0 deletions packages/andrjs/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
roots: ["src"],
};
7 changes: 5 additions & 2 deletions packages/andrjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andromedaprotocol/andromeda.js",
"version": "2.0.0-beta.2",
"version": "2.0.0-beta.3",
"repository": {
"type": "git",
"url": "git+https://github.com/andromedaprotocol/andromeda-cli.git"
Expand Down Expand Up @@ -47,11 +47,13 @@
},
"devDependencies": {
"@types/crypto-js": "^4.2.2",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.17.7",
"@types/pako": "^2.0.3",
"depcheck": "^1.4.7",
"jest": "^29.7.0",
"nodemon": "^2.0.22",
"ts-jest": "^29.2.5",
"tsc-alias": "^1.8.10",
"typescript": "^5.5.4"
},
Expand All @@ -66,7 +68,8 @@
"follow-redirects@<=1.15.5": ">=1.15.6",
"braces@<3.0.3": ">=3.0.3",
"ws@>=7.0.0 <7.5.10": ">=7.5.10",
"micromatch@<4.0.8": ">=4.0.8"
"micromatch@<4.0.8": ">=4.0.8",
"dset@<3.1.4": ">=3.1.4"
}
}
}
6 changes: 3 additions & 3 deletions packages/andrjs/src/AndromedaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ export default class AndromedaClient {
* @param query
* @returns
*/
async queryContractRaw<T = any>(address: string, key: string) {
async queryContractRaw<T = any>(address: string, key: string | Uint8Array) {
this.preMessage();
const result = await this.chainClient!.queryClient!.queryContractRaw(
address,
Buffer.from(key)
typeof key === 'string' ? Buffer.from(key, 'utf8') : key
);
if (!result || result.length === 0) return null;
return JSON.parse(Buffer.from(result).toString('utf8')) as T
Expand All @@ -221,7 +221,7 @@ export default class AndromedaClient {
* @param query
* @returns
*/
async queryContractRawAll(address: string, pagination: Partial<PageRequest>) {
async queryContractStates(address: string, pagination: Partial<PageRequest>) {
this.preMessage();
const rpcClient = createProtobufRpcClient(this.chainClient!.rawQueryClient!);
const wasmQueryClient = new WasmQueryClientImpl(rpcClient);
Expand Down
51 changes: 51 additions & 0 deletions packages/andrjs/src/utils/denom.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { convertMicroToMacro, convertMacroToMicro, formatAmountToInternational, DENOM_EXPONENTS, getUnitsConfigFromDenom } from './denom';

describe('Denom Utility Functions', () => {
test('converts micro to macro correctly', () => {
expect(convertMicroToMacro('1000000', 'u')).toBe('1.0');
expect(convertMicroToMacro('1000000', 6)).toBe('1.0');
expect(convertMicroToMacro('500000', 'm')).toBe('500.0');
expect(convertMicroToMacro('1234567.89', 'u')).toBe('1.23456789');
expect(convertMicroToMacro('1000000.123456', 'm')).toBe('1000.000123456');
expect(convertMicroToMacro('1', 'u')).toBe('0.000001');
expect(convertMicroToMacro('0', 'u')).toBe('0.0');
expect(convertMicroToMacro('0.0', 'u')).toBe('0.0');
});

test('converts macro to micro correctly', () => {
expect(convertMacroToMicro('1.000000', 'u')).toBe('1000000');
expect(convertMacroToMicro('1.000000', 6)).toBe('1000000');
expect(convertMacroToMicro('0.500000', 'm')).toBe('500');
expect(convertMacroToMicro('1.23456789', 'u')).toBe('1234567.89');
expect(convertMacroToMicro('1000.000123456', 'm')).toBe('1000000.123456');
expect(convertMacroToMicro('1', 'u')).toBe('1000000');
expect(convertMacroToMicro('0', 'u')).toBe('0');
expect(convertMacroToMicro('0.0', 'u')).toBe('0');
});

test('formatAmountToInternational formats numbers correctly', () => {
expect(formatAmountToInternational(1234567.89, 2)).toBe('1,234,567.89');
expect(formatAmountToInternational(1000, 0)).toBe('1,000');
expect(formatAmountToInternational(1234.5678, 3)).toBe('1,234.567');
});

test('DENOM_EXPONENTS contains correct values', () => {
expect(DENOM_EXPONENTS).toEqual({
'd': 1,
'c': 2,
'm': 3,
'u': 6,
'n': 9,
'p': 12,
'f': 15,
'a': 18,
'z': 21,
'y': 24,
});
});

test('getUnitsConfigFromDenom returns correct values', () => {
expect(getUnitsConfigFromDenom('inj')).toEqual({ units: 18, microDenom: 'inj', macroDenom: 'inj' });
expect(getUnitsConfigFromDenom('uatom')).toEqual({ units: 6, microDenom: 'uatom', macroDenom: 'ATOM' });
});
});
13 changes: 9 additions & 4 deletions packages/andrjs/src/utils/denom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export const convertMicroToMacro = (amount: string, unit: UNITS) => {
let numZeroes = typeof unit === 'number' ? unit : DENOM_EXPONENTS[unit];
amount = amount.padStart(numZeroes + 1, '0');
const fixed = amount.substring(0, amount.length - numZeroes);
const decimals = (amount.substring(amount.length - numZeroes)).replace(/0+$/, '').padEnd(1, '0');
return fixed.concat('.').concat(decimals).concat(subDecimals.join());
const decimals = (amount.substring(amount.length - numZeroes)).concat(subDecimals.join()).replace(/0+$/, '').padEnd(1, '0');
return fixed.concat('.').concat(decimals);
}

/**
Expand All @@ -49,8 +49,13 @@ export const convertMicroToMacro = (amount: string, unit: UNITS) => {
export const convertMacroToMicro = (amount: string, unit: UNITS) => {
const numZeroes = typeof unit === 'number' ? unit : DENOM_EXPONENTS[unit];
let [result, decimals = ''] = amount.split('.');
decimals = decimals.substring(0, numZeroes).padEnd(numZeroes, '0');
return result.concat(decimals).replace(/^0+/, '');
const shiftedDecimals = decimals.substring(0, numZeroes).padEnd(numZeroes, '0');
const remainingDecimals = decimals.substring(numZeroes).replace(/0+$/, '');;
result = result.concat(shiftedDecimals).replace(/^0+/, '').padEnd(1, '0');
if (remainingDecimals) {
result = result.concat('.').concat(remainingDecimals);
}
return result;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/andrjs/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"exclude": [
"node_modules",
"dist",
"test/**/*.ts"
"test/**/*.ts",
"**/*.spec.ts"
]
}
13 changes: 12 additions & 1 deletion packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
# @andromedaprotocol/cli

## 2.0.0-beta.2
## 2.0.0-beta.3

### Patch Changes

- NPM package updates
- Wallet Address cache
- GQL Assets default to app-contract
- wasm query-raw command changed to query raw key
- wasm contract-state command now list all contract keys (previously knows as wasm query-raw command)
- Updated dependencies
- @andromedaprotocol/[email protected]

## 2.0.0-beta.2

### Patch Changes

Feat - wasm query-raw and info commands
Feat - Legacy wallet can be imported in any environment
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andromedaprotocol/cli",
"version": "2.0.0-beta.2",
"version": "2.0.0-beta.3",
"repository": {
"type": "git",
"url": "git+https://github.com/andromedaprotocol/andromeda-cli.git"
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export async function title() {
// const versionMsg = "CLI version - v" + version;
console.log(versionMsg);
if (version !== latest) {
console.log(pc.bold(pc.green("Update available")) + ", update using the following command: " + pc.bgBlack(" npm update -g @andromeda-protocol/cli "));
console.log(pc.bold(pc.green("Update available")) + ", update using the following command: " + pc.bgBlack(pc.white(" npm update -g @andromeda-protocol/cli ")));
}
const envMsg = pc.gray("Environment - " + envConfig.get('name') + " , " + `Chain Config - ${config.get('chain.name')}` + " ");
console.log(envMsg);
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/handlers/gql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ const commands: Commands = {
"Queries details about your deployed apps and ADOs for the current chain",
flags: {
type: {
description: "Filter assets by ADO type",
description: "Filter assets by ADO type. Default: app-contract",
usage: "--type cw721",
},
search: {
description: "Filter assets by name",
usage: "--search name",
},
limit: {
description: "Paginate response, 0 mean no limit",
description: "Paginate response, 0 mean no limit. Default: 10",
usage: "--limit 10",
},
offset: {
description: "Paginate response",
description: "Paginate response. Default: 0",
usage: "--offset 0",
},
},
Expand Down Expand Up @@ -96,7 +96,7 @@ async function appHandler(input: string[]) {
async function assetsHandler(_input: string[], flags: Flags) {
const walletAddr = await State.wallets.currentWalletAddress();
if (!walletAddr) throw new Error("No wallet currently assigned");
const { type, search } = flags;
const { type = "app-contract", search } = flags;

const limit = parseInt(flags.limit ?? '10');
const offset = parseInt(flags.offset ?? '0');
Expand Down
53 changes: 48 additions & 5 deletions packages/cli/src/handlers/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ export const commands: Commands = {
color: pc.green,
description: "Queries a contract key",
usage: "wasm query-raw <contract address> <key>",
inputs: [
{
requestMessage: "Input Contract Address:",
validate: validateAddressInput,
},
{
requestMessage: "Input Key:",
},
],
flags: {
encoded: {
description: "Key provided is already encoded as hex",
usage: "--encoded",
}
}
},
'contract-state': {
handler: queryStatesHandler,
color: pc.green,
description: "Queries a contract states",
usage: "wasm contract-state <contract address>",
inputs: [
{
requestMessage: "Input Contract Address:",
Expand Down Expand Up @@ -263,6 +284,30 @@ async function queryHandler(input: string[]) {
* @param input
*/
async function queryRawHandler(input: string[], flags: Flags) {
const [contractAddr, key] = input;
const encodedKey = !!flags.encoded;
const keyBytes = encodedKey ? Uint8Array.from(Buffer.from(key, 'hex')) : Uint8Array.from(Buffer.from(key, 'utf8'));


const value = await displaySpinnerAsync(
"Querying contrac key...",
async () => await State.client.queryContractRaw(contractAddr, keyBytes)
);
console.log();


console.log("Key:", pc.blue(key));
console.log("Value:");
console.log(JSON.stringify(value, null, 2));

console.log()
}

/**
* Queries a contract given a query message and address
* @param input
*/
async function queryStatesHandler(input: string[], flags: Flags) {
const [contractAddr] = input;
const limit = BigInt(flags.limit ?? '10');
const offset = BigInt(flags.offset ?? '0');
Expand All @@ -276,14 +321,14 @@ async function queryRawHandler(input: string[], flags: Flags) {
const key = nextKeyBytes ? Uint8Array.from(Buffer.from(nextKeyBytes, 'hex')) : nextKey ? Uint8Array.from(Buffer.from(nextKey, 'utf8')) : undefined;

const states = await displaySpinnerAsync(
"Querying contrac key...",
async () => await State.client.queryContractRawAll(contractAddr, { limit, offset, key })
"Querying contract states...",
async () => await State.client.queryContractStates(contractAddr, { limit, offset, key })
);
console.log();


for (const state of states.states) {
console.log(pc.blue(state.key));
console.log(pc.blue(state.key), pc.gray(`(hex: ${Buffer.from(state.key).toString('hex')})`));
console.log(state.value);
console.log();
}
Expand All @@ -295,8 +340,6 @@ async function queryRawHandler(input: string[], flags: Flags) {
console.log(pc.gray(`Total - ${states.pagination.total.toString()}`));
}
console.log()


}

/**
Expand Down
Loading
Loading