-
Notifications
You must be signed in to change notification settings - Fork 15
Refactor and renames #61
base: main
Are you sure you want to change the base?
Changes from all commits
7de92cd
7eb849d
6fda2e0
3c032b1
306b107
ab9c023
fc668c4
4dfad6e
f2bc058
21d4075
62408e3
2832bc6
74a0259
5af73e5
f1fa841
52acb95
47e655a
9c6819a
76f4865
a0ffe74
d907d40
d07bcb5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,27 +3,33 @@ import { BigNumberish } from 'ethers'; | |
import { ChainName, HyperlaneContracts, RouterApp } from '@hyperlane-xyz/sdk'; | ||
import { types } from '@hyperlane-xyz/utils'; | ||
|
||
import { TokenType } from './config'; | ||
import { | ||
HypERC20Factories, | ||
HypERC721Factories, | ||
TokenFactories, | ||
} from './contracts'; | ||
import { TokenRouter } from './types'; | ||
import { | ||
HypERC20, | ||
HypERC20Collateral, | ||
HypERC721, | ||
HypERC721Collateral, | ||
HypNativeCollateral, | ||
TokenRouter, | ||
} from './types'; | ||
|
||
class HyperlaneTokenApp< | ||
abstract class HyperlaneTokenApp< | ||
Factories extends TokenFactories, | ||
> extends RouterApp<Factories> { | ||
router(contracts: HyperlaneContracts<TokenFactories>): TokenRouter { | ||
return contracts.router; | ||
} | ||
abstract router(contracts: HyperlaneContracts<Factories>): TokenRouter; | ||
|
||
async transfer( | ||
origin: ChainName, | ||
destination: ChainName, | ||
recipient: types.Address, | ||
amountOrId: BigNumberish, | ||
) { | ||
const originRouter = this.getContracts(origin).router; | ||
const originRouter = this.router(this.getContracts(origin)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fn does not support native collateral, since value should be gasPayment + amountOrId |
||
const destProvider = this.multiProvider.getProvider(destination); | ||
const destinationNetwork = await destProvider.getNetwork(); | ||
const gasPayment = await originRouter.quoteGasPayment( | ||
|
@@ -44,13 +50,23 @@ class HyperlaneTokenApp< | |
} | ||
|
||
export class HypERC20App extends HyperlaneTokenApp<HypERC20Factories> { | ||
router( | ||
contracts: HyperlaneContracts<HypERC20Factories>, | ||
): HypERC20 | HypERC20Collateral | HypNativeCollateral { | ||
return ( | ||
contracts[TokenType.synthetic] || | ||
contracts[TokenType.collateral] || | ||
contracts[TokenType.native] | ||
); | ||
} | ||
|
||
async transfer( | ||
origin: ChainName, | ||
destination: ChainName, | ||
recipient: types.Address, | ||
amount: BigNumberish, | ||
) { | ||
const originRouter = this.getContracts(origin).router; | ||
const originRouter = this.router(this.getContracts(origin)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would really like us to detect the contract type and:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably best for a follow up PR that covers what we discussed yesterday (i.e. passing chain/tokenType to the app constructor) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another request I'd love to have in that PR is a |
||
const signerAddress = await this.multiProvider.getSignerAddress(origin); | ||
const balance = await originRouter.balanceOf(signerAddress); | ||
if (balance.lt(amount)) | ||
|
@@ -62,13 +78,19 @@ export class HypERC20App extends HyperlaneTokenApp<HypERC20Factories> { | |
} | ||
|
||
export class HypERC721App extends HyperlaneTokenApp<HypERC721Factories> { | ||
router( | ||
contracts: HyperlaneContracts<HypERC721Factories>, | ||
): HypERC721 | HypERC721Collateral { | ||
return contracts[TokenType.synthetic] || contracts[TokenType.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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,45 +26,30 @@ export const isTokenMetadata = (metadata: any): metadata is TokenMetadata => | |
export const isErc20Metadata = (metadata: any): metadata is ERC20Metadata => | ||
metadata.decimals && isTokenMetadata(metadata); | ||
|
||
export type SyntheticConfig = TokenMetadata & { | ||
export type SyntheticConfig = Partial<TokenMetadata> & { | ||
type: TokenType.synthetic | TokenType.syntheticUri; | ||
}; | ||
export type CollateralConfig = { | ||
type: TokenType.collateral | TokenType.collateralUri; | ||
token: string; | ||
}; | ||
export type NativeConfig = { | ||
type: TokenType.native; | ||
type: TokenType.collateral | TokenType.collateralUri | TokenType.native; | ||
token?: string; // no token implies native collateral | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Maybe Just b/c the type is not implied by the presence/absence of token, it's explicit in the type |
||
}; | ||
|
||
export type TokenConfig = SyntheticConfig | CollateralConfig | NativeConfig; | ||
export type TokenConfig = SyntheticConfig | CollateralConfig; | ||
|
||
export const isCollateralConfig = ( | ||
config: TokenConfig, | ||
): config is CollateralConfig => | ||
config.type === TokenType.collateral || | ||
config.type === TokenType.collateralUri; | ||
config.type === TokenType.collateralUri || | ||
config.type === TokenType.native; | ||
|
||
export const isSyntheticConfig = ( | ||
config: TokenConfig, | ||
): 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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,33 @@ | ||
import { HyperlaneFactories } from '@hyperlane-xyz/sdk'; | ||
import { TokenType } from './config'; | ||
import { | ||
HypERC20Collateral__factory, | ||
HypERC20__factory, | ||
HypERC721Collateral__factory, | ||
HypERC721URICollateral__factory, | ||
HypERC721URIStorage__factory, | ||
HypERC721__factory, | ||
HypNative__factory, | ||
HypNativeCollateral__factory, | ||
TokenRouter, | ||
} from './types'; | ||
|
||
export type HypERC20Factories = { | ||
router: HypERC20__factory | HypERC20Collateral__factory | HypNative__factory; | ||
type TokenRouterFactory = { deploy(...args: any[]): Promise<TokenRouter>; }; | ||
|
||
export type TokenFactories = Partial<Record<TokenType, TokenRouterFactory>> & HyperlaneFactories; | ||
|
||
export const hypErc20Factories = { | ||
[TokenType.synthetic]: new HypERC20__factory(), | ||
[TokenType.collateral]: new HypERC20Collateral__factory(), | ||
[TokenType.native]: new HypNativeCollateral__factory(), | ||
}; | ||
export type HypERC721Factories = { | ||
router: | ||
| HypERC721__factory | ||
| HypERC721Collateral__factory | ||
| HypERC721URICollateral__factory; | ||
|
||
export type HypERC20Factories = typeof hypErc20Factories; | ||
|
||
export const hypErc721Factories = { | ||
[TokenType.synthetic]: new HypERC721__factory(), | ||
[TokenType.syntheticUri]: new HypERC721URIStorage__factory(), | ||
[TokenType.collateral]: new HypERC721Collateral__factory(), | ||
[TokenType.collateralUri]: new HypERC721URICollateral__factory(), | ||
}; | ||
|
||
export type TokenFactories = HypERC20Factories | HypERC721Factories; | ||
export type HypERC721Factories = typeof hypErc721Factories; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better name