Last active
March 21, 2023 10:25
-
-
Save tolak/bc8684a6d7b3cc6b54fa5ac7c481335d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ethers } from "ethers"; | |
import { ApiPromise, WsProvider } from "@polkadot/api"; | |
export type Address = string; | |
export enum ChainType { | |
Evm, | |
Sub, | |
} | |
export class Asset { | |
public id: Address; | |
public name: string; | |
public symbol: string; | |
public decimals: number; | |
constructor(id: Address, name: string, symbol: string, decimals: number) { | |
this.id = id; | |
this.name = name; | |
this.symbol = symbol; | |
this.decimals = decimals; | |
} | |
} | |
export interface DepositParams { | |
asset: Asset; | |
amount: number; | |
recipient: Address; | |
request: String; | |
} | |
class Chain { | |
private _name: string; | |
private _chainType: ChainType; | |
private _handler: Address; | |
private _provider: any; | |
constructor(name: string, chainType: ChainType, handler: Address, providerUrl: string) { | |
this._name = name; | |
this._chainType = chainType; | |
this._handler = handler; | |
if (this._chainType === ChainType.Evm) { | |
this._provider = new ethers.providers.JsonRpcProvider(providerUrl); | |
} else if (this._chainType === ChainType.Sub) { | |
this._provider = new WsProvider(providerUrl); | |
} else { | |
throw new Error("Unsupported ChainType"); | |
} | |
} | |
public name(): string { | |
return this._name; | |
} | |
public chainType(): ChainType { | |
return this._chainType; | |
} | |
public handler(): Address { | |
return this._handler; | |
} | |
public async deposit(params: DepositParams): Promise<void> { | |
if (this._chainType === ChainType.Evm) { | |
const handlerAbi = [ | |
"function deposit(address assetId, uint256 amount, address recipient) external", | |
]; | |
const handlerContract = new ethers.Contract(this.handler, handlerAbi, this._provider.getSigner()); | |
const amount = ethers.utils.parseUnits(params.amount.toString(), params.asset.decimals); | |
const tx = await handlerContract.deposit(params.asset.id, amount, params.recipient, params.request); | |
await tx.wait(); | |
} else if (this._chainType === ChainType.Sub) { | |
let api = await ApiPromise.create({ | |
provider: new WsProvider(this._provider), | |
}); | |
const unsub = await api.tx.palletIndex.depositTask( | |
params.asset.id, | |
params.amount, | |
params.recipient, | |
params.request, | |
).signAndSend(this._provider.getSigner(), (result) => { | |
if (result.status.isInBlock) { | |
console.log(`Transaction included at blockHash ${result.status.asInBlock}`); | |
} else if (result.status.isFinalized) { | |
console.log(`Transaction finalized at blockHash ${result.status.asFinalized}`); | |
unsub(); | |
} | |
}); | |
} else { | |
throw Error("Unsupported ChainType"); | |
} | |
console.log(`Deposited ${params.amount} ${params.asset.symbol} to ${params.recipient} on ${this.name} chain`); | |
} | |
} | |
class Ethereum extends Chain { | |
public ETH: Asset; | |
public WETH: Asset; | |
public USDC: Asset; | |
constructor() { | |
super("Ethereum", ChainType.Evm, "", ""); | |
this.ETH = new Asset( | |
"", | |
"Ether", | |
"ETH", | |
18 | |
); | |
this.WETH = new Asset( | |
"", | |
"WrapEther", | |
"WETH", | |
18 | |
); | |
this.USDC = new Asset( | |
"", | |
"USD Coin", | |
"USDT", | |
6 | |
); | |
} | |
} | |
class Moonbeam extends Chain { | |
public GLMR: Asset; | |
public USDC: Asset; | |
constructor() { | |
super("Moonbeam", ChainType.Evm, "", ""); | |
this.GLMR = new Asset( | |
"", | |
"GLMR", | |
"GLMR", | |
18 | |
); | |
this.USDC = new Asset( | |
"", | |
"USD Coin", | |
"USDT", | |
6 | |
); | |
} | |
} | |
class AStar extends Chain { | |
public ASTR: Asset; | |
public USDC: Asset; | |
constructor() { | |
super("AStar", ChainType.Evm, "", ""); | |
this.ASTR = new Asset( | |
"", | |
"ASTR", | |
"ASTR", | |
18 | |
); | |
this.USDC = new Asset( | |
"", | |
"USD Coin", | |
"USDT", | |
6 | |
); | |
} | |
} | |
class Acala extends Chain { | |
public ACA: Asset; | |
constructor() { | |
super("Acala", ChainType.Sub, "", ""); | |
this.ACA = new Asset( | |
"", | |
"Acala Token", | |
"ACA", | |
12 | |
); | |
} | |
} | |
class Phala extends Chain { | |
public PHA: Asset; | |
constructor() { | |
super("Phala", ChainType.Sub, "", ""); | |
this.PHA = new Asset( | |
"", | |
"Phala Token", | |
"PHA", | |
12 | |
); | |
} | |
} | |
export { | |
Ethereum, | |
Moonbeam, | |
AStar, | |
Acala, | |
Phala, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment