Skip to content

Commit

Permalink
feat(factory): configure liquidty pools
Browse files Browse the repository at this point in the history
  • Loading branch information
rsbkmr committed Jul 21, 2024
1 parent 66e40d6 commit 78ba435
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 41 deletions.
49 changes: 25 additions & 24 deletions src/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface SendInstallment<Signer, Ret, GasArgs> {
tokenSymbol: string,
destAddress: string,
fee?: bigint,
gasArgs?: GasArgs
gasArgs?: GasArgs,
) => Promise<{ hash: string; tx: Ret }>;
}

Expand All @@ -72,21 +72,22 @@ export interface GetTokenBalance {
}

export type AddressBookKeys =
| 'GasFees'
| 'EmmetTokenVault'
| 'EmmetData'
| 'CCTPHelper'
| 'HashHelper'
| 'SignatureVerifier'
| 'LiquidityPoolHelper'
| 'EmmetBridge'
| 'AddressStorageHelper'
| 'WTON' // Wrapped Token
| 'EMMET' // Token
| 'TON/USD' // price feed
| 'BNB/USD' // price feed
| 'MATIC/USD' // Price Feed
| 'EmmetMultisig';
| "GasFees"
| "EmmetTokenVault"
| "EmmetData"
| "CCTPHelper"
| "HashHelper"
| "SignatureVerifier"
| "LiquidityPoolHelper"
| "EmmetBridge"
| "AddressStorageHelper"
| "WTON" // Wrapped Token
| "EMMET" // Token
| "TON/USD" // price feed
| "BNB/USD" // price feed
| "MATIC/USD" // Price Feed
| "EmmetMultisig"
| `elp${string}`;

export interface AddressBook {
address: (contr: AddressBookKeys) => Promise<string>;
Expand Down Expand Up @@ -132,7 +133,7 @@ export interface PreTransfer<Signer, GasArgs> {
token: string,
spender: string,
amount: bigint,
gasArgs: GasArgs
gasArgs: GasArgs,
) => Promise<string>;
}

Expand All @@ -157,15 +158,15 @@ export interface GetApprovedTokenAmount {
getApprovedAmount: (
token: string,
owner: string,
spender: string
spender: string,
) => Promise<bigint>;
}

export interface GetTxFee {
txFee: (
targetChain: bigint,
fromToken: string,
targetToken: string
targetToken: string,
) => Promise<bigint>;
}

Expand Down Expand Up @@ -198,7 +199,7 @@ export interface GetEstimatedTime {
estimateTime(
targetChain: bigint,
fromToken: string,
targetToken: string
targetToken: string,
): Promise<bigint | undefined>;
}

Expand All @@ -211,17 +212,17 @@ export interface StakeLiquidity<Signer, RetTx, GasArgs> {
signer: Signer,
pool: string,
amount: bigint,
ga: GasArgs
) => Promise<RetTx>;
ga: GasArgs | undefined,
) => Promise<{ hash: string; tx: RetTx }>;
}

export interface WithdrawLiquidity<Signer, RetTx, GasArgs> {
withdrawLiquidity: (
signer: Signer,
pool: string,
amount: bigint,
ga: GasArgs
) => Promise<RetTx>;
ga: GasArgs | undefined,
) => Promise<{ hash: string; tx: RetTx }>;
}

export interface GetLpCurrentAPY {
Expand Down
40 changes: 23 additions & 17 deletions src/chains/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Overrides,
type Provider,
type Signer,
} from 'ethers';
} from "ethers";
import type {
AddressBook,
ChainID,
Expand Down Expand Up @@ -33,15 +33,15 @@ import type {
GetLpTokenFee,
GetLpProtocolFee,
GetLpProtocolFeeAmount,
} from '.';
} from ".";
import {
EmmetAddressBook__factory,
EmmetBridge__factory,
EmmetData__factory,
EmmetLPV2__factory,
WrappedERC20__factory,
} from '@emmet-contracts/web3';
import type { PayableOverrides } from '@emmet-contracts/web3/dist/common';
} from "@emmet-contracts/web3";
import type { PayableOverrides } from "@emmet-contracts/web3/dist/common";

export type Web3Helper = GetBalance &
GetProvider<Provider> &
Expand Down Expand Up @@ -85,21 +85,27 @@ export async function web3Helper({
nativeCoin,
}: Web3Params): Promise<Web3Helper> {
const addrBook = EmmetAddressBook__factory.connect(addressBook, provider);
const bridgeAddr = await addrBook.get('EmmetBridge');
const emmetData = await addrBook.get('EmmetData');
const bridgeAddr = await addrBook.get("EmmetBridge");
const emmetData = await addrBook.get("EmmetData");
const bridge = EmmetBridge__factory.connect(bridgeAddr, provider);
const data = EmmetData__factory.connect(emmetData, provider);
return {
id: async () => (await provider.getNetwork()).chainId,
stakeLiquidity: async (signer, pool, amt, ga) => {
const lp = EmmetLPV2__factory.connect(pool, signer);
const deposit = await lp.deposit(amt, ga);
return deposit;
const deposit = await lp.deposit(amt, { ...ga });
return {
hash: deposit.hash,
tx: deposit,
};
},
withdrawLiquidity: async (signer, pool, amt, ga) => {
const lp = EmmetLPV2__factory.connect(pool, signer);
const withdraw = await lp.withdrawTokens(amt, ga);
return withdraw;
const withdraw = await lp.withdrawTokens(amt, { ...ga });
return {
hash: withdraw.hash,
tx: withdraw,
};
},
getLpCurrentAPY: async (pool) => {
const lp = EmmetLPV2__factory.connect(pool);
Expand Down Expand Up @@ -137,18 +143,18 @@ export async function web3Helper({
const ffc = await data.getForeignFeeCompensation(
targetChainId,
fromToken,
targetToken
targetToken,
);
return protocolFee + ffc;
},
async txInfo(hash) {
if (hash === '') {
if (hash === "") {
return {
timestamp: 0n,
value: 0n,
};
}
if (!hash.startsWith('0x')) {
if (!hash.startsWith("0x")) {
//biome-ignore lint/style/noParameterAssign: ignore
hash = `0x${hash}`;
}
Expand All @@ -173,8 +179,8 @@ export async function web3Helper({
if (!receipt) throw new Error(`No receipt found for tx hash: ${hash}`);
const log = receipt.logs.find((e) =>
e.topics.includes(
bridge.interface.getEvent('SendInstallment').topicHash
)
bridge.interface.getEvent("SendInstallment").topicHash,
),
);
if (!log)
throw new Error(`No send installment log found for tx hash: ${hash}`);
Expand Down Expand Up @@ -212,15 +218,15 @@ export async function web3Helper({
getApprovedAmount: async (tid, owner, spender) =>
await WrappedERC20__factory.connect(tid, provider).allowance(
owner,
spender
spender,
),
balance: (addr) => provider.getBalance(addr),
provider: () => provider,
async estimateTime(targetChain, fromToken, targetToken) {
const ts = await data.getCrossChainTokenStrategy(
targetChain,
fromToken,
targetToken
targetToken,
);
const localSteps = ts[0];
const foreignSteps = ts[1];
Expand Down
11 changes: 11 additions & 0 deletions src/factory/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
ParamMap,
} from "./types";
import { ChainIDToDomain, type SupportedChainID } from "../explorer-utils";
import { AddressBookKeys } from "../chains";

function mapNonceToParams(chainParams: Partial<ChainParams>): ParamMap {
const cToP: ParamMap = new Map();
Expand Down Expand Up @@ -103,6 +104,16 @@ export async function ChainFactoryBuilder(

return {
inner,
async stakeLiqiduity(chain, signer, token, amount, ga) {
const lp = await chain.address(`elp${token}`);
const response = chain.stakeLiquidity(signer, lp, amount, ga);
return response;
},
async withdrawLiqiduity(chain, signer, token, amount, ga) {
const lp = await chain.address(`elp${token}`);
const response = chain.withdrawLiquidity(signer, lp, amount, ga);
return response;
},
preTransfer: async (chain, signer, tid, spender, amt, ga) => {
const pt = await chain.preTransfer(signer, tid, spender, amt, ga);
return pt;
Expand Down
17 changes: 17 additions & 0 deletions src/factory/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { Web3Helper, Web3Params } from "../../chains/web3";
import type { TonHelper, TonParams } from "../../chains/ton";
import type {
AddressBook,
Decimals,
GetTxFee,
NativeCoinName,
PreTransfer,
ProtocolFee,
SendInstallment,
StakeLiquidity,
WithdrawLiquidity,
} from "../../chains";
import type { JsonRpcProvider } from "ethers";

Expand Down Expand Up @@ -85,6 +88,20 @@ export type HelperMap<K extends ChainNonce> = Map<
>;

export interface ChainFactory {
stakeLiqiduity: <Signer, RetTx, GasArgs>(
chain: StakeLiquidity<Signer, RetTx, GasArgs> & AddressBook,
signer: Signer,
token: string,
amount: bigint,
ga: GasArgs | undefined,
) => Promise<{ hash: string; tx: RetTx }>;
withdrawLiqiduity: <Signer, RetTx, GasArgs>(
chain: WithdrawLiquidity<Signer, RetTx, GasArgs> & AddressBook,
signer: Signer,
token: string,
amount: bigint,
ga: GasArgs | undefined,
) => Promise<{ hash: string; tx: RetTx }>;
inner: <T extends ChainNonce>(chain: T) => Promise<InferChainH<T>>;
sendInstallment: <Signer, RetTx, GasArgs>(
chain: SendInstallment<Signer, RetTx, GasArgs> & GetTxFee,
Expand Down

0 comments on commit 78ba435

Please sign in to comment.