Skip to content

Commit

Permalink
cosmic-proto without cosmjs (#9691)
Browse files Browse the repository at this point in the history
refs: #9408

## Description
cosmjs deps add a lot of size and complicates our builds (#9583) At runtime we only need `Decimal` so this vendors that (just the subset necessary) and drops all other runtime deps.

### Security Considerations
less supply chain

### Scaling Considerations
none

### Documentation Considerations
Maybe get this upstream to reduce maintenance.

### Testing Considerations
CI suffices?

### Upgrade Considerations
None, not on chain.
  • Loading branch information
mergify[bot] authored Jul 12, 2024
2 parents 3915e4c + cb232ef commit 5d18974
Show file tree
Hide file tree
Showing 14 changed files with 699 additions and 146 deletions.
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

0 comments on commit 5d18974

Please sign in to comment.