Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Refactor and renames
Browse files Browse the repository at this point in the history
  • Loading branch information
yorhodes committed Apr 6, 2023
1 parent 52acb95 commit 9a095c8
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 320 deletions.
4 changes: 2 additions & 2 deletions contracts/HypNative.sol → contracts/HypNativeCollateral.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {Message} from "./libs/Message.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";

/**
* @title Hyperlane Native Token Router that extends ERC20 with remote transfer functionality.
* @title Hyperlane Native Collateral Token Router that extends native asset with remote transfer functionality.
* @author Abacus Works
* @dev Supply on each chain is not constant but the aggregate supply across all chains is.
*/
contract HypNative is TokenRouter {
contract HypNativeCollateral is TokenRouter {
/**
* @notice Initializes the Hyperlane router, ERC20 metadata, and mints initial supply to deployer.
* @param _mailbox The address of the mailbox contract.
Expand Down
20 changes: 14 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import {
HypERC721Factories,
TokenFactories,
} from './contracts';
import { TokenRouter } from './types';
import { HypERC20, HypERC20Collateral, HypERC721, HypERC721Collateral, HypNativeCollateral, TokenRouter } from './types';

class HyperlaneTokenApp<
Factories extends TokenFactories,
> extends RouterApp<Factories> {
router(contracts: HyperlaneContracts<TokenFactories>): TokenRouter {
return contracts.router;
router(contracts: HyperlaneContracts<Factories>): TokenRouter {
return contracts['synthetic'] || contracts['collateral'];
}

async transfer(
Expand All @@ -23,7 +23,7 @@ class HyperlaneTokenApp<
recipient: types.Address,
amountOrId: BigNumberish,
) {
const originRouter = this.getContracts(origin).router;
const originRouter = this.router(this.getContracts(origin));
const destProvider = this.multiProvider.getProvider(destination);
const destinationNetwork = await destProvider.getNetwork();
const gasPayment = await originRouter.quoteGasPayment(
Expand All @@ -44,13 +44,17 @@ class HyperlaneTokenApp<
}

export class HypERC20App extends HyperlaneTokenApp<HypERC20Factories> {
router(contracts: HyperlaneContracts<HypERC20Factories>): HypERC20 | HypERC20Collateral | HypNativeCollateral {
return contracts['synthetic'] || contracts['collateral'];
}

async transfer(
origin: ChainName,
destination: ChainName,
recipient: types.Address,
amount: BigNumberish,
) {
const originRouter = this.getContracts(origin).router;
const originRouter = this.router(this.getContracts(origin));
const signerAddress = await this.multiProvider.getSignerAddress(origin);
const balance = await originRouter.balanceOf(signerAddress);
if (balance.lt(amount))
Expand All @@ -62,13 +66,17 @@ export class HypERC20App extends HyperlaneTokenApp<HypERC20Factories> {
}

export class HypERC721App extends HyperlaneTokenApp<HypERC721Factories> {
router(contracts: HyperlaneContracts<HypERC721Factories>): HypERC721 | HypERC721Collateral {
return contracts['synthetic'] || contracts['collateral'];
}

async transfer(
origin: ChainName,
destination: ChainName,
recipient: types.Address,
tokenId: BigNumberish,
) {
const originRouter = this.getContracts(origin).router;
const originRouter = this.router(this.getContracts(origin));
const signerAddress = await this.multiProvider.getSignerAddress(origin);
const owner = await originRouter.ownerOf(tokenId);
if (signerAddress != owner)
Expand Down
25 changes: 3 additions & 22 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export enum TokenType {
syntheticUri = 'syntheticUri',
collateral = 'collateral',
collateralUri = 'collateralUri',
native = 'native',
}

export type TokenMetadata = {
Expand All @@ -31,17 +30,12 @@ export type SyntheticConfig = TokenMetadata & {
};
export type CollateralConfig = {
type: TokenType.collateral | TokenType.collateralUri;
token: string;
};
export type NativeConfig = {
type: TokenType.native;
token?: string; // no token implies native collateral
};

export type TokenConfig = { type: TokenType } & (
export type TokenConfig =
| SyntheticConfig
| CollateralConfig
| NativeConfig
);

export const isCollateralConfig = (
config: TokenConfig,
Expand All @@ -54,21 +48,8 @@ export const isSyntheticConfig = (
): config is SyntheticConfig =>
config.type === TokenType.synthetic || config.type === TokenType.syntheticUri;

export const isNativeConfig = (config: TokenConfig): config is NativeConfig =>
config.type === TokenType.native;

export const isUriConfig = (config: TokenConfig) =>
config.type === TokenType.syntheticUri ||
config.type === TokenType.collateralUri;

export type HypERC20Config = GasRouterConfig & SyntheticConfig & ERC20Metadata;
export type HypERC20CollateralConfig = GasRouterConfig & CollateralConfig;
export type HypNativeConfig = GasRouterConfig & NativeConfig;
export type ERC20RouterConfig =
| HypERC20Config
| HypERC20CollateralConfig
| HypNativeConfig;

export type HypERC721Config = GasRouterConfig & SyntheticConfig;
export type HypERC721CollateralConfig = GasRouterConfig & CollateralConfig;
export type ERC721RouterConfig = HypERC721Config | HypERC721CollateralConfig;
export type TokenRouterConfig = TokenConfig & GasRouterConfig;
31 changes: 20 additions & 11 deletions src/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { ethers } from 'ethers';
import {
HypERC20Collateral__factory,
HypERC20__factory,
HypERC721Collateral__factory,
HypERC721URICollateral__factory,
HypERC721URIStorage__factory,
HypERC721__factory,
HypNative__factory,
HypNativeCollateral__factory,
TokenRouter__factory,
} from './types';

export type HypERC20Factories = {
router: HypERC20__factory | HypERC20Collateral__factory | HypNative__factory;
};
export type HypERC721Factories = {
router:
| HypERC721__factory
| HypERC721Collateral__factory
| HypERC721URICollateral__factory;
};
export type TokenFactories = { synthetic: TokenRouter__factory & ethers.ContractFactory } | { collateral: TokenRouter__factory & ethers.ContractFactory };

export type TokenFactories = HypERC20Factories | HypERC721Factories;
export type HypERC20Factories =
| {
synthetic: HypERC20__factory;
}
| { collateral: HypERC20Collateral__factory | HypNativeCollateral__factory };

export type HypERC721Factories =
| {
synthetic: HypERC721__factory | HypERC721URIStorage__factory;
}
| {
collateral:
| HypERC721Collateral__factory
| HypERC721URICollateral__factory;
};
Loading

0 comments on commit 9a095c8

Please sign in to comment.