Skip to content

Commit

Permalink
Upgrade Viem & fix neutron chainmetadata (#3049)
Browse files Browse the repository at this point in the history
### Description

- Upgrade Viem and wagmi chains versions
- Add optional RestUrl field to ChainMetadata
- Set rest url for Neutron
- Add simple deepCopy function

Prerequisite for hyperlane-xyz/hyperlane-warp-ui-template#80

### Backward compatibility

Yes

### Testing

Tested in Warp UI
  • Loading branch information
jmrossy authored Dec 15, 2023
1 parent dcf8b80 commit fd4fc18
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 102 deletions.
9 changes: 9 additions & 0 deletions .changeset/great-readers-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@hyperlane-xyz/utils': patch
'@hyperlane-xyz/sdk': patch
---

- Upgrade Viem to 1.20.0
- Add optional restUrls field to ChainMetadata
- Add deepCopy util function
- Add support for cosmos factory token addresses
4 changes: 2 additions & 2 deletions typescript/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"@solana/web3.js": "^1.78.0",
"@types/coingecko-api": "^1.0.10",
"@types/debug": "^4.1.7",
"@wagmi/chains": "^0.2.6",
"@wagmi/chains": "^1.8.0",
"bignumber.js": "^9.1.1",
"coingecko-api": "^1.0.10",
"cosmjs-types": "^0.9.0",
"cross-fetch": "^3.1.5",
"debug": "^4.3.4",
"ethers": "^5.7.2",
"viem": "^1.3.1",
"viem": "^1.20.0",
"zod": "^3.21.2"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions typescript/sdk/src/consts/chainMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ export const neutron: ChainMetadata = {
symbol: 'NTRN',
},
protocol: ProtocolType.Cosmos,
restUrls: [{ http: 'grpc-kralum.neutron-1.neutron.org:80' }],
rpcUrls: [{ http: 'https://rpc-kralum.neutron-1.neutron.org' }],
slip44: 118,
};
Expand Down
11 changes: 7 additions & 4 deletions typescript/sdk/src/core/HyperlaneCore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ethers } from 'ethers';
import type { TransactionReceipt as ViemTxReceipt } from 'viem';

import { Mailbox__factory } from '@hyperlane-xyz/core';
import {
Expand Down Expand Up @@ -119,15 +120,15 @@ export class HyperlaneCore extends HyperlaneApp<CoreFactories> {
}

waitForMessageProcessing(
sourceTx: ethers.ContractReceipt,
sourceTx: ethers.ContractReceipt | ViemTxReceipt,
): Promise<ethers.ContractReceipt[]> {
const messages = HyperlaneCore.getDispatchedMessages(sourceTx);
return Promise.all(messages.map((msg) => this.waitForProcessReceipt(msg)));
}

// TODO consider renaming this, all the waitForMessage* methods are confusing
async waitForMessageProcessed(
sourceTx: ethers.ContractReceipt,
sourceTx: ethers.ContractReceipt | ViemTxReceipt,
delay?: number,
maxAttempts?: number,
): Promise<void> {
Expand All @@ -146,12 +147,14 @@ export class HyperlaneCore extends HyperlaneApp<CoreFactories> {
}

// Redundant with static method but keeping for backwards compatibility
getDispatchedMessages(sourceTx: ethers.ContractReceipt): DispatchedMessage[] {
getDispatchedMessages(
sourceTx: ethers.ContractReceipt | ViemTxReceipt,
): DispatchedMessage[] {
return HyperlaneCore.getDispatchedMessages(sourceTx);
}

static getDispatchedMessages(
sourceTx: ethers.ContractReceipt,
sourceTx: ethers.ContractReceipt | ViemTxReceipt,
): DispatchedMessage[] {
const mailbox = Mailbox__factory.createInterface();
const dispatchLogs = sourceTx.logs
Expand Down
4 changes: 4 additions & 0 deletions typescript/sdk/src/metadata/chainMetadataTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ export const ChainMetadataSchemaObject = z.object({
.array(RpcUrlSchema)
.nonempty()
.describe('The list of RPC endpoints for interacting with the chain.'),
restUrls: z
.array(RpcUrlSchema)
.describe('For cosmos chains only, a list of Rest API URLs')
.optional(),
blockExplorers: z
.array(
z.object({
Expand Down
1 change: 1 addition & 0 deletions typescript/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export {
export {
ValueOf,
arrayToObject,
deepCopy,
deepEquals,
invertKeysAndValues,
isObject,
Expand Down
24 changes: 19 additions & 5 deletions typescript/utils/src/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ import { Address, HexString, ProtocolType } from './types';

const EVM_ADDRESS_REGEX = /^0x[a-fA-F0-9]{40}$/;
const SEALEVEL_ADDRESS_REGEX = /^[a-zA-Z0-9]{36,44}$/;
const COSMOS_ADDRESS_REGEX =
/^[a-z]{1,10}1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38,58}$/; // Bech32
export const IBC_DENOM_REGEX = /^ibc\/([A-Fa-f0-9]{64})$/;

// https://github.com/cosmos/cosmos-sdk/blob/84c33215658131d87daf3c629e909e12ed9370fa/types/coin.go#L601C17-L601C44
const COSMOS_DENOM_PATTERN = `[a-zA-Z][a-zA-Z0-9]{2,127}`;
// https://en.bitcoin.it/wiki/BIP_0173
const BECH32_ADDRESS_PATTERN = `[a-zA-Z]{1,83}1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{38,58}`;
const COSMOS_ADDRESS_REGEX = new RegExp(`^${BECH32_ADDRESS_PATTERN}$`);
const IBC_DENOM_REGEX = new RegExp(`^ibc/([A-Fa-f0-9]{64})$`);
const COSMOS_FACTORY_TOKEN_REGEX = new RegExp(
`^factory/(${BECH32_ADDRESS_PATTERN})/${COSMOS_DENOM_PATTERN}$`,
);

const EVM_TX_HASH_REGEX = /^0x([A-Fa-f0-9]{64})$/;
const SEALEVEL_TX_HASH_REGEX = /^[a-zA-Z1-9]{88}$/;
Expand All @@ -26,7 +33,11 @@ export function isAddressSealevel(address: Address) {
}

export function isAddressCosmos(address: Address) {
return COSMOS_ADDRESS_REGEX.test(address) || IBC_DENOM_REGEX.test(address);
return (
COSMOS_ADDRESS_REGEX.test(address) ||
IBC_DENOM_REGEX.test(address) ||
COSMOS_FACTORY_TOKEN_REGEX.test(address)
);
}

export function getAddressProtocolType(address: Address) {
Expand Down Expand Up @@ -79,7 +90,10 @@ export function isValidAddressSealevel(address: Address) {
export function isValidAddressCosmos(address: Address) {
try {
const isValid =
address && (IBC_DENOM_REGEX.test(address) || fromBech32(address));
address &&
(IBC_DENOM_REGEX.test(address) ||
COSMOS_FACTORY_TOKEN_REGEX.test(address) ||
fromBech32(address));
return !!isValid;
} catch (error) {
return false;
Expand Down
4 changes: 4 additions & 0 deletions typescript/utils/src/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export function deepEquals(v1: any, v2: any) {
return JSON.stringify(v1) === JSON.stringify(v2);
}

export function deepCopy(v: any) {
return JSON.parse(JSON.stringify(v));
}

export type ValueOf<T> = T[keyof T];

export function objMapEntries<
Expand Down
Loading

0 comments on commit fd4fc18

Please sign in to comment.