Skip to content

Commit

Permalink
Merge pull request #10180 from clockworkgr/feat/add-atomone-support
Browse files Browse the repository at this point in the history
  • Loading branch information
timolegros authored Dec 12, 2024
2 parents c453812 + 15ff8fc commit 5e8152a
Show file tree
Hide file tree
Showing 22 changed files with 1,049 additions and 108 deletions.
1 change: 1 addition & 0 deletions libs/chains/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"protobufjs": "^6.1.13"
},
"devDependencies": {
"@atomone/atomone-types-long": "^1.0.3",
"tsx": "^4.7.2"
}
}
3 changes: 3 additions & 0 deletions libs/chains/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export {
type QueryVotesResponseSDKType,
} from './cosmos-ts/src/codegen/cosmos/gov/v1/query';
export { LCDQueryClient as GovV1Client } from './cosmos-ts/src/codegen/cosmos/gov/v1/query.lcd';

export { LCDQueryClient as GovV1AtomOneClient } from '@atomone/atomone-types-long/atomone/gov/v1/query.lcd';
export { createLCDClient as createAtomOneLCDClient } from '@atomone/atomone-types-long/atomone/lcd';
export { createLCDClient } from './cosmos-ts/src/codegen/cosmos/lcd';
export * from './cosmos-ts/src/codegen/google/protobuf/any';
export * from './cosmos-ts/src/codegen/helpers';
1 change: 1 addition & 0 deletions libs/shared/src/types/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export enum ChainNetwork {
*/
export enum CosmosGovernanceVersion {
v1 = 'v1',
v1atomone = 'v1atomone',
v1beta1govgen = 'v1beta1govgen',
v1beta1 = 'v1beta1',
v1beta1Failed = 'v1beta1-attempt-failed',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import IChainAdapter from '../../../models/IChainAdapter';
import type CosmosAccount from './account';
import CosmosAccounts from './accounts';
import CosmosChain from './chain';
import CosmosGovernanceV1AtomOne from './gov/atomone/governance-v1';
import CosmosGovernanceGovgen from './gov/govgen/governance-v1beta1';
import CosmosGovernanceV1 from './gov/v1/governance-v1';
import CosmosGovernance from './gov/v1beta1/governance-v1beta1';
Expand All @@ -17,7 +18,8 @@ class Cosmos extends IChainAdapter<CosmosToken, CosmosAccount> {
public governance:
| CosmosGovernance
| CosmosGovernanceV1
| CosmosGovernanceGovgen;
| CosmosGovernanceGovgen
| CosmosGovernanceV1AtomOne;

public readonly base = ChainBase.CosmosSDK;

Expand All @@ -28,9 +30,11 @@ class Cosmos extends IChainAdapter<CosmosToken, CosmosAccount> {
this.governance =
meta?.ChainNode?.cosmos_gov_version === 'v1beta1govgen'
? new CosmosGovernanceGovgen(this.app)
: meta?.ChainNode?.cosmos_gov_version === 'v1'
? new CosmosGovernanceV1(this.app)
: new CosmosGovernance(this.app);
: meta?.ChainNode?.cosmos_gov_version === 'v1atomone'
? new CosmosGovernanceV1AtomOne(this.app)
: meta?.ChainNode?.cosmos_gov_version === 'v1'
? new CosmosGovernanceV1(this.app)
: new CosmosGovernance(this.app);
}

public async initApi() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import BN from 'bn.js';
import { CosmosToken } from 'controllers/chain/cosmos/types';
import moment from 'moment';
import { LCD } from 'shared/chain/types/cosmos';
import { AtomOneLCD, LCD } from 'shared/chain/types/cosmos';
import type { IApp } from 'state';
import { ApiStatus } from 'state';
import { SERVER_URL } from 'state/api/config';
Expand All @@ -36,12 +36,14 @@ import { getCosmosChains } from '../../app/webWallets/utils';
import WebWalletController from '../../app/web_wallets';
import type CosmosAccount from './account';
import {
getAtomOneLCDClient,
getLCDClient,
getRPCClient,
getSigningClient,
getTMClient,
} from './chain.utils';
import EthSigningClient from './eth_signing_client';
import type { AtomOneGovExtension } from './gov/atomone/queries-v1';
import type { GovgenGovExtension } from './gov/govgen/queries-v1beta1';

/* eslint-disable @typescript-eslint/no-unused-vars */
Expand All @@ -54,16 +56,19 @@ export interface ICosmosTXData extends ITXData {
// skip simulating the tx twice by saving the original estimated gas
gas: number;
}

export const isAtomoneLCD = (lcd: LCD | AtomOneLCD): lcd is AtomOneLCD => {
return (lcd as AtomOneLCD).atomone !== undefined;
};
export type CosmosApiType = QueryClient &
StakingExtension &
GovExtension &
GovgenGovExtension &
AtomOneGovExtension &
BankExtension;

class CosmosChain implements IChainModule<CosmosToken, CosmosAccount> {
private _api: CosmosApiType;
private _lcd: LCD;
private _lcd: LCD | AtomOneLCD;

public get api() {
return this._api;
Expand Down Expand Up @@ -136,7 +141,18 @@ class CosmosChain implements IChainModule<CosmosToken, CosmosAccount> {
console.error('Error starting LCD client: ', e);
}
}

if (
chain?.ChainNode?.cosmos_gov_version === CosmosGovernanceVersion.v1atomone
) {
try {
const lcdUrl = `${window.location.origin}${SERVER_URL}/cosmosProxy/v1/${chain.id}`;
console.log(`Starting LCD API at ${lcdUrl}...`);
const lcd = await getAtomOneLCDClient(lcdUrl);
this._lcd = lcd;
} catch (e) {
console.error('Error starting LCD client: ', e);
}
}
await this.fetchBlock(); // Poll for new block immediately
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { OfflineSigner } from '@cosmjs/proto-signing';
import { registry as atomoneRegistry } from '@atomone/atomone-types-long/atomone/gov/v1/tx.registry';
import { registry as govgenRegistry } from '@atomone/govgen-types-long/govgen/gov/v1beta1/tx.registry';
import { OfflineSigner, Registry } from '@cosmjs/proto-signing';
import {
AminoTypes,
SigningStargateClient,
createDefaultAminoConverters,
defaultRegistryTypes,
} from '@cosmjs/stargate';
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import { LCD } from '../../../../../shared/chain/types/cosmos';
import { AtomOneLCD, LCD } from '../../../../../shared/chain/types/cosmos';
import { CosmosApiType } from './chain';
import {
createAltGovAminoConverters,
createAtomoneGovAminoConverters,
createGovgenGovAminoConverters,
} from './gov/aminomessages';
import { setupAtomOneExtension } from './gov/atomone/queries-v1';
import { setupGovgenExtension } from './gov/govgen/queries-v1beta1';

export const getTMClient = async (
Expand All @@ -29,6 +34,7 @@ export const getRPCClient = async (
cosm.setupGovExtension,
cosm.setupStakingExtension,
setupGovgenExtension,
setupAtomOneExtension,
cosm.setupBankExtension,
);
return client;
Expand All @@ -42,6 +48,15 @@ export const getLCDClient = async (lcdUrl: string): Promise<LCD> => {
});
};

export const getAtomOneLCDClient = async (
lcdUrl: string,
): Promise<AtomOneLCD> => {
const { createAtomOneLCDClient } = await import('@hicommonwealth/chains');

return await createAtomOneLCDClient({
restEndpoint: lcdUrl,
});
};
export const getSigningClient = async (
url: string,
signer: OfflineSigner,
Expand All @@ -50,9 +65,15 @@ export const getSigningClient = async (
...createDefaultAminoConverters(),
...createAltGovAminoConverters(),
...createGovgenGovAminoConverters(),
...createAtomoneGovAminoConverters(),
});

return await SigningStargateClient.connectWithSigner(url, signer, {
registry: new Registry([
...defaultRegistryTypes,
...atomoneRegistry,
...govgenRegistry,
]),
aminoTypes,
});
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AminoConverter as AtomOneAminoConverter } from '@atomone/atomone-types-long/atomone/gov/v1beta1/tx.amino';
import { AminoConverter } from '@atomone/govgen-types-long/govgen/gov/v1beta1/tx.amino';
import { AminoMsg } from '@cosmjs/amino';
import { AminoMsgSubmitProposal } from '@cosmjs/stargate';
Expand All @@ -17,6 +18,9 @@ export function isAminoMsgSubmitProposal(
return msg.type === 'cosmos-sdk/MsgSubmitProposal';
}
export function createGovgenGovAminoConverters(): AminoConverters {
return AtomOneAminoConverter;
}
export function createAtomoneGovAminoConverters(): AminoConverters {
return AminoConverter;
}
export function createAltGovAminoConverters(): AminoConverters {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { Any, numberToLong } from '@hicommonwealth/chains';
import type {
CosmosToken,
ICosmosProposal,
} from 'controllers/chain/cosmos/types';
import ProposalModule from 'models/ProposalModule';
import { ITXModalData } from 'models/interfaces';
import type CosmosAccount from '../../account';
import type CosmosAccounts from '../../accounts';
import type CosmosChain from '../../chain';
import { isAtomoneLCD, type CosmosApiType } from '../../chain';
import { CosmosProposalV1AtomOne } from './proposal-v1';
import { encodeMsgSubmitProposalAtomOne, propToIProposal } from './utils-v1';

/** This file is a copy of controllers/chain/cosmos/governance.ts, modified for
* gov module version v1. This is considered a patch to make sure v1-enabled chains
* load proposals. Eventually we will ideally move back to one governance.ts file.
* Patch state:
*
* - governance.ts uses cosmJS v1beta1 gov
* - governance-v1.ts uses telescope-generated v1 gov */
class CosmosGovernanceV1AtomOne extends ProposalModule<
CosmosApiType,
ICosmosProposal,
// @ts-expect-error StrictNullChecks
CosmosProposalV1AtomOne
> {
private _minDeposit: CosmosToken;

public get minDeposit() {
return this._minDeposit;
}

public setMinDeposit(minDeposit: CosmosToken) {
this._minDeposit = minDeposit;
}

private _Chain: CosmosChain;
private _Accounts: CosmosAccounts;

/* eslint-disable-next-line @typescript-eslint/require-await */
public async init(
ChainInfo: CosmosChain,
Accounts: CosmosAccounts,
): Promise<void> {
this._Chain = ChainInfo;
this._Accounts = Accounts;
this._initialized = true;
}

public async getProposal(
proposalId: number,
): Promise<CosmosProposalV1AtomOne> {
const existingProposal = this.store.getByIdentifier(proposalId);
if (existingProposal) {
return existingProposal;
}
return this._initProposal(proposalId);
}

private async _initProposal(
proposalId: number,
// @ts-expect-error StrictNullChecks
): Promise<CosmosProposalV1AtomOne> {
try {
// @ts-expect-error StrictNullChecks
if (!proposalId) return;
// @ts-expect-error StrictNullChecks
if (!isAtomoneLCD(this._Chain.lcd)) return;
const { proposal } = await this._Chain.lcd.atomone.gov.v1.proposal({
proposalId: numberToLong(proposalId),
});
const cosmosProposal = new CosmosProposalV1AtomOne(
this._Chain,
this._Accounts,
this,
// @ts-expect-error StrictNullChecks
propToIProposal(proposal),
);
await cosmosProposal.init();
return cosmosProposal;
} catch (error) {
console.error('Error fetching proposal: ', error);
}
}

public createTx(
sender: CosmosAccount,
title: string,
description: string,
initialDeposit: CosmosToken,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
memo = '',
): ITXModalData {
throw new Error('unsupported');
}

// TODO: support multiple deposit types
public async submitProposalTx(
sender: CosmosAccount,
initialDeposit: CosmosToken,
content: Any,
): Promise<number> {
const msg = encodeMsgSubmitProposalAtomOne(
sender.address,
initialDeposit,
content,
);

// fetch completed proposal from returned events
const events = await this._Chain.sendTx(sender, msg);
console.log(events);
const submitEvent = events?.find((e) => e.type === 'submit_proposal');
const cosm = await import('@cosmjs/encoding');
const idAttribute = submitEvent?.attributes.find(
({ key }) => key && cosm.fromAscii(key) === 'proposal_id',
);
// @ts-expect-error StrictNullChecks
const id = +cosm.fromAscii(idAttribute.value);
await this._initProposal(id);
return id;
}
}

export default CosmosGovernanceV1AtomOne;
Loading

0 comments on commit 5e8152a

Please sign in to comment.