Skip to content

Commit

Permalink
refactor: platform address constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
anondev2323 committed Oct 17, 2023
1 parent bf921b5 commit 7125678
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 50 deletions.
6 changes: 5 additions & 1 deletion platforms/cosmwasm/src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@wormhole-foundation/connect-sdk";
import { CosmwasmPlatform } from "./platform";
import { nativeDenomToChain } from "./constants";
import { AnyCosmwasmAddress } from "./types";

declare global {
namespace Wormhole {
Expand Down Expand Up @@ -113,7 +114,10 @@ export class CosmwasmAddress implements Address {
// The denomType is "native", "ibc", or "factory"
private readonly denomType?: string;

constructor(address: string | Uint8Array | UniversalAddress) {
constructor(address: AnyCosmwasmAddress) {
if (address instanceof CosmwasmAddress) {
Object.assign(this, address);
}
if (typeof address === "string") {
// A native denom like "uatom"
if (nativeDenomToChain.has(CosmwasmPlatform.network, address)) {
Expand Down
12 changes: 7 additions & 5 deletions platforms/cosmwasm/src/platformUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { CosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import { CosmwasmPlatform } from "./platform";
import { CosmwasmAddress } from "./address";
import { BankExtension, IbcExtension, QueryClient, setupIbcExtension } from "@cosmjs/stargate";
import { AnyCosmwasmAddress, toCosmwasmAddrString } from "./types";
import { AnyCosmwasmAddress } from "./types";

// forces CosmwasmUtils to implement PlatformUtils
var _: PlatformUtils<"Cosmwasm"> = CosmwasmUtils;
Expand Down Expand Up @@ -59,7 +59,8 @@ export module CosmwasmUtils {
): Promise<bigint> {
if (token === "native") return nativeDecimals(CosmwasmPlatform.platform);

const { decimals } = await rpc.queryContractSmart(toCosmwasmAddrString(token), {
const addrStr = new CosmwasmAddress(token).toString();
const { decimals } = await rpc.queryContractSmart(addrStr, {
token_info: {},
});
return decimals;
Expand All @@ -79,7 +80,8 @@ export module CosmwasmUtils {
return BigInt(amount);
}

const { amount } = await rpc.getBalance(walletAddress, toCosmwasmAddrString(token));
const addrStr = new CosmwasmAddress(token).toString();
const { amount } = await rpc.getBalance(walletAddress, addrStr);
return BigInt(amount);
}

Expand All @@ -90,8 +92,8 @@ export module CosmwasmUtils {
tokens: (AnyCosmwasmAddress | "native")[],
): Promise<Balances> {
const allBalances = await rpc.bank.allBalances(walletAddress);
const balancesArr = tokens.map((t) => {
const address = t === 'native' ? getNativeDenom(chain) : toCosmwasmAddrString(t);
const balancesArr = tokens.map((token) => {
const address = token === 'native' ? getNativeDenom(chain) : new CosmwasmAddress(token).toString();
const balance = allBalances.find(
(balance) => balance.denom === address,
);
Expand Down
10 changes: 1 addition & 9 deletions platforms/cosmwasm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,12 @@ import { logs as cosmosLogs } from "@cosmjs/stargate";

export type CosmwasmChainName = PlatformToChains<"Cosmwasm">;
export type UniversalOrCosmwasm = UniversalOrNative<"Cosmwasm">;
export type AnyCosmwasmAddress = UniversalOrCosmwasm | string;
export type AnyCosmwasmAddress = UniversalOrCosmwasm | string | Uint8Array;

export interface WrappedRegistryResponse {
address: string;
}

export const toCosmwasmAddrString = (addr: AnyCosmwasmAddress) =>
typeof addr === "string"
? addr
: (addr instanceof UniversalAddress
? addr.toNative("Cosmwasm")
: addr
).unwrap();

// TODO: do >1 key at a time
export const searchCosmosLogs = (
key: string,
Expand Down
6 changes: 5 additions & 1 deletion platforms/evm/src/address.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Address, UniversalAddress } from '@wormhole-foundation/connect-sdk';

import { ethers } from 'ethers';
import { AnyEvmAddress } from './types';

declare global {
namespace Wormhole {
Expand All @@ -19,7 +20,10 @@ export class EvmAddress implements Address {
// stored as checksum address
private readonly address: string;

constructor(address: string | Uint8Array | UniversalAddress) {
constructor(address: AnyEvmAddress) {
if (address instanceof EvmAddress) {
Object.assign(this, address);
}
if (typeof address === 'string') {
if (!EvmAddress.isValidAddress(address))
throw new Error(
Expand Down
12 changes: 6 additions & 6 deletions platforms/evm/src/platformUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { evmChainIdToNetworkChainPair } from './constants';
import { EvmAddress, EvmZeroAddress } from './address';
import { EvmContracts } from './contracts';
import { EvmPlatform } from './platform';
import { AnyEvmAddress, toEvmAddrString } from './types';
import { AnyEvmAddress } from './types';

// forces EvmUtils to implement PlatformUtils
var _: PlatformUtils<'Evm'> = EvmUtils;
Expand Down Expand Up @@ -55,7 +55,7 @@ export module EvmUtils {

const tokenContract = EvmContracts.getTokenImplementation(
rpc,
toEvmAddrString(token),
new EvmAddress(token).toString(),
);
return tokenContract.decimals();
}
Expand All @@ -70,7 +70,7 @@ export module EvmUtils {

const tokenImpl = EvmContracts.getTokenImplementation(
rpc,
toEvmAddrString(token),
new EvmAddress(token).toString(),
);
return tokenImpl.balanceOf(walletAddr);
}
Expand All @@ -81,9 +81,9 @@ export module EvmUtils {
walletAddr: string,
tokens: (AnyEvmAddress | 'native')[],
): Promise<Balances> {
const balancesArr = await Promise.all(tokens.map(async (a) => {
const balance = await getBalance(chain, rpc, walletAddr, a);
const address = toEvmAddrString(a);
const balancesArr = await Promise.all(tokens.map(async (token) => {
const balance = await getBalance(chain, rpc, walletAddr, token);
const address = token === 'native' ? 'native' : new EvmAddress(token).toString();
return { [address]: balance }
}))
return balancesArr.reduce(
Expand Down
7 changes: 1 addition & 6 deletions platforms/evm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ export const unusedArbiterFee = 0n;

export type EvmChainName = PlatformToChains<'Evm'>;
export type UniversalOrEvm = UniversalOrNative<'Evm'>;
export type AnyEvmAddress = UniversalOrEvm | string;

export const toEvmAddrString = (addr: AnyEvmAddress) =>
typeof addr === 'string'
? addr
: (addr instanceof UniversalAddress ? addr.toNative('Evm') : addr).unwrap();
export type AnyEvmAddress = UniversalOrEvm | string | Uint8Array;

export const addFrom = (txReq: TransactionRequest, from: string) => ({
...txReq,
Expand Down
8 changes: 6 additions & 2 deletions platforms/solana/src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
UniversalAddress,
} from '@wormhole-foundation/connect-sdk';

import { PublicKey, PublicKeyInitData } from '@solana/web3.js';
import { PublicKey } from '@solana/web3.js';
import { AnySolanaAddress } from './types';

declare global {
namespace Wormhole {
Expand All @@ -24,7 +25,10 @@ export class SolanaAddress implements Address {

private readonly address: PublicKey;

constructor(address: PublicKeyInitData | UniversalAddress) {
constructor(address: AnySolanaAddress) {
if (address instanceof SolanaAddress) {
Object.assign(this, address);
}
if (address instanceof UniversalAddress)
this.address = new PublicKey(address.toUint8Array());
if (typeof address === 'string' && isHexByteString(address))
Expand Down
12 changes: 6 additions & 6 deletions platforms/solana/src/platformUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Connection, ParsedAccountData, PublicKey } from '@solana/web3.js';
import { solGenesisHashToNetworkChainPair } from './constants';
import { SolanaPlatform } from './platform';
import { SolanaAddress, SolanaZeroAddress } from './address';
import { AnySolanaAddress, toSolanaAddrPublicKey, toSolanaAddrString } from './types';
import { AnySolanaAddress } from './types';
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';

// forces SolanaUtils to implement PlatformUtils
Expand Down Expand Up @@ -79,7 +79,7 @@ export module SolanaUtils {

const splToken = await rpc.getTokenAccountsByOwner(
new PublicKey(walletAddress),
{ mint: toSolanaAddrPublicKey(token) },
{ mint: new SolanaAddress(token).unwrap() },
);
if (!splToken.value[0]) return null;

Expand All @@ -106,13 +106,13 @@ export module SolanaUtils {
},
);

const balancesArr = tokens.map((address) => {
if (address === 'native') {
const balancesArr = tokens.map((token) => {
if (token === 'native') {
return { ['native']: native };
}
const addrString = toSolanaAddrString(address);
const addrString = new SolanaAddress(token).toString();
const amount = splParsedTokenAccounts.value.find(
(v) => v?.account.data.parsed?.info?.mint === address,
(v) => v?.account.data.parsed?.info?.mint === token,
)?.account.data.parsed?.info?.tokenAmount?.amount;
if (!amount) return { [addrString]: null };
return { [addrString]: BigInt(amount) };
Expand Down
14 changes: 0 additions & 14 deletions platforms/solana/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,3 @@ registerNative('Solana', SolanaAddress);
export type SolanaChainName = PlatformToChains<'Solana'>;
export type UniversalOrSolana = UniversalOrNative<'Solana'>;
export type AnySolanaAddress = UniversalOrSolana | PublicKeyInitData;

export const toSolanaAddrPublicKey = (addr: AnySolanaAddress) => {
if (addr instanceof SolanaAddress) {
return addr.unwrap();
}
return new SolanaAddress(addr).unwrap()
}

export const toSolanaAddrString = (addr: AnySolanaAddress) => {
if (addr instanceof SolanaAddress) {
return addr.toString();
}
return new SolanaAddress(addr).toString()
}

0 comments on commit 7125678

Please sign in to comment.