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

cosmic-proto without cosmjs #9691

Merged
merged 3 commits into from
Jul 12, 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
5 changes: 0 additions & 5 deletions packages/cosmic-proto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@
"typescript": "^5.5.3"
},
"dependencies": {
"@cosmjs/amino": "^0.32.3",
"@cosmjs/math": "^0.32.3",
"@cosmjs/proto-signing": "^0.32.3",
"@cosmjs/stargate": "^0.32.3",
"@cosmjs/tendermint-rpc": "^0.32.3",
"@endo/base64": "^1.0.5",
"@endo/init": "^1.1.2"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/cosmic-proto/src/codegen/agoric/vbank/vbank.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//@ts-nocheck
import { Coin, CoinSDKType } from '../../cosmos/base/v1beta1/coin.js';
import { BinaryReader, BinaryWriter } from '../../binary.js';
import { Decimal } from '@cosmjs/math';
import { isSet } from '../../helpers.js';
import { Decimal, isSet } from '../../helpers.js';
import { JsonSafe } from '../../json-safe.js';
/** The module governance/configuration parameters. */
export interface Params {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
CoinSDKType,
} from '../../base/v1beta1/coin.js';
import { BinaryReader, BinaryWriter } from '../../../binary.js';
import { Decimal } from '@cosmjs/math';
import { isSet } from '../../../helpers.js';
import { Decimal, isSet } from '../../../helpers.js';
import { JsonSafe } from '../../../json-safe.js';
/** Params defines the set of params for the distribution module. */
export interface Params {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
DurationSDKType,
} from '../../../google/protobuf/duration.js';
import { BinaryReader, BinaryWriter } from '../../../binary.js';
import { Decimal } from '@cosmjs/math';
import {
Decimal,
isSet,
fromJsonTimestamp,
fromTimestamp,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ts-nocheck
import { BinaryReader, BinaryWriter } from '../../../binary.js';
import { Decimal } from '@cosmjs/math';
import { isSet } from '../../../helpers.js';
import { Decimal, isSet } from '../../../helpers.js';
import { JsonSafe } from '../../../json-safe.js';
/** Minter represents the minting state. */
export interface Minter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import {
} from '../../../google/protobuf/duration.js';
import { Coin, CoinSDKType } from '../../base/v1beta1/coin.js';
import { BinaryReader, BinaryWriter } from '../../../binary.js';
import { isSet, fromJsonTimestamp, fromTimestamp } from '../../../helpers.js';
import {
isSet,
Decimal,
fromJsonTimestamp,
fromTimestamp,
} from '../../../helpers.js';
import { JsonSafe } from '../../../json-safe.js';
import { Decimal } from '@cosmjs/math';
/** BondStatus is the status of a validator. */
export enum BondStatus {
/** BOND_STATUS_UNSPECIFIED - UNSPECIFIED defines an invalid validator status. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ import {
TimestampSDKType,
} from '../../../google/protobuf/timestamp.js';
import { BinaryReader, BinaryWriter } from '../../../binary.js';
import { isSet, fromJsonTimestamp, fromTimestamp } from '../../../helpers.js';
import {
isSet,
Decimal,
fromJsonTimestamp,
fromTimestamp,
} from '../../../helpers.js';
import { JsonSafe } from '../../../json-safe.js';
import { Decimal } from '@cosmjs/math';
/** MsgCreateValidator defines a SDK message for creating a new validator. */
export interface MsgCreateValidator {
description: Description;
Expand Down
81 changes: 81 additions & 0 deletions packages/cosmic-proto/src/codegen/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,84 @@ export function fromJsonTimestamp(o: any): Timestamp {
function numberToLong(number: number) {
return BigInt(Math.trunc(number));
}

// The largest value we need is 18 (Ether).
const maxFractionalDigits = 30;
// Subset of Decimal in @cosmjs/math
/**
* A type for arbitrary precision, non-negative decimals.
*
* Instances of this class are immutable.
*/
export class Decimal {
public static fromUserInput(
input: string,
fractionalDigits: number,
): Decimal {
Decimal.verifyFractionalDigits(fractionalDigits);

const badCharacter = input.match(/[^0-9.]/);
if (badCharacter) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
throw new Error(
`Invalid character at position ${badCharacter.index! + 1}`,
);
}

let whole: string;
let fractional: string;

if (input === '') {
whole = '0';
fractional = '';
} else if (input.search(/./) === -1) {
// integer format, no separator
whole = input;
fractional = '';
} else {
const parts = input.split('.');
switch (parts.length) {
case 0:
case 1:
throw new Error(
'Fewer than two elements in split result. This must not happen here.',
);
case 2:
if (!parts[1]) throw new Error('Fractional part missing');
whole = parts[0];
fractional = parts[1].replace(/0+$/, '');
break;
default:
throw new Error('More than one separator found');
}
}

if (fractional.length > fractionalDigits) {
throw new Error('Got more fractional digits than supported');
}

const quantity = `${whole}${fractional.padEnd(fractionalDigits, '0')}`;

return new Decimal(quantity, fractionalDigits);
}

public static fromAtomics(
atomics: string,
fractionalDigits: number,
): Decimal {
Decimal.verifyFractionalDigits(fractionalDigits);
return new Decimal(atomics, fractionalDigits);
}

private static verifyFractionalDigits(fractionalDigits: number): void {
if (!Number.isInteger(fractionalDigits))
throw new Error('Fractional digits is not an integer');
if (fractionalDigits < 0)
throw new Error('Fractional digits must not be negative');
if (fractionalDigits > maxFractionalDigits) {
throw new Error(
`Fractional digits must not exceed ${maxFractionalDigits}`,
);
}
}
}
1 change: 0 additions & 1 deletion packages/cosmic-proto/test/bundle-cosmjs-math.js

This file was deleted.

18 changes: 0 additions & 18 deletions packages/cosmic-proto/test/bundle-cosmjs-math.test.js

This file was deleted.

25 changes: 0 additions & 25 deletions patches/@cosmjs+math+0.32.3.patch

This file was deleted.

Loading
Loading