Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #69 from synapsecns/revert-67-dev
Browse files Browse the repository at this point in the history
Revert "Dev to master: add new Metis/Cronos bridge and zap addresses, add UST everywhere"
  • Loading branch information
caesar0x committed Mar 3, 2022
2 parents 6c654e5 + 767aa33 commit 67e8bcd
Show file tree
Hide file tree
Showing 20 changed files with 378 additions and 325 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"ts-node": "10.5.0",
"ts-patch": "2.0.1",
"typechain": "7.0.0",
"typescript": "4.6.2",
"typescript": "4.5.5",
"typescript-transform-paths": "3.3.1"
}
}
95 changes: 66 additions & 29 deletions src/bridge/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
rejectPromise,
} from "@common/utils";

import type {ID} from "@internal/distinct";
import type {ID} from "@internal/entity";
import {SwapType} from "@internal/swaptype";
import {rpcProviderForChain} from "@internal/rpcproviders";
import {tokenSwitch} from "@internal/utils";
Expand Down Expand Up @@ -47,7 +47,6 @@ import type {
ContractTransaction,
PopulatedTransaction,
} from "@ethersproject/contracts";
import {SynapseContracts} from "@common/synapse_contracts";

/**
* Bridge provides a wrapper around common Synapse Bridge interactions, such as output estimation, checking supported swaps/bridges,
Expand All @@ -57,7 +56,7 @@ export namespace Bridge {
type CanBridgeResult = [boolean, Error|string];
export type CheckCanBridgeResult = [boolean, BigNumber];

export type BridgeOutputEstimate = {
export interface BridgeOutputEstimate {
amountToReceive: BigNumber,
bridgeFee: BigNumber,
}
Expand Down Expand Up @@ -87,13 +86,13 @@ export namespace Bridge {
addressTo?: string
}

type EasyArgsCheck = {
interface EasyArgsCheck {
isEasy: boolean,
castArgs: BridgeUtils.BridgeTxParams,
txn?: Promise<PopulatedTransaction>,
}

type BridgeTokenArgs = {
interface BridgeTokenArgs {
fromChainTokens: Token[],
toChainTokens: Token[],
tokenFrom: Token,
Expand All @@ -102,7 +101,7 @@ export namespace Bridge {
tokenIndexTo: number,
}

type CheckCanBridgeParams = {
interface CheckCanBridgeParams {
address: string,
token: Token,
amount: BigNumberish,
Expand Down Expand Up @@ -150,18 +149,14 @@ export namespace Bridge {
this.isL2Zap = this.network.zapIsL2BridgeZap;
this.isL2ETHChain = BridgeUtils.isL2ETHChain(this.chainId);

const
factoryParams = {chainId: this.chainId, signerOrProvider: this.provider},
contractAddrs = SynapseContracts.contractsForChainId(this.chainId);

this.bridgeAddress = contractAddrs.bridge_address;
this.zapBridgeAddress = contractAddrs.bridge_zap_address;
let factoryParams = {chainId: this.chainId, signerOrProvider: this.provider};

this.bridgeInstance = SynapseEntities.synapseBridge(factoryParams);
this.bridgeAddress = contractAddressFor(this.chainId, "bridge");

if (this.zapBridgeAddress && this.zapBridgeAddress !== "") {
this.networkZapBridgeInstance = SynapseEntities.zapBridge(factoryParams);
}
this.networkZapBridgeInstance = SynapseEntities.zapBridge({ chainId: this.chainId, signerOrProvider: this.provider })

this.zapBridgeAddress = this.networkZapBridgeInstance.address;
}

bridgeVersion(): Promise<BigNumber> {
Expand Down Expand Up @@ -461,7 +456,7 @@ export namespace Bridge {
let {intermediateToken, bridgeConfigIntermediateToken} = TokenSwap.intermediateTokens(chainIdTo, tokenFrom);

const
intermediateTokenAddr = bridgeConfigIntermediateToken.address(chainIdTo).toLowerCase(),
intermediateTokenAddr = bridgeConfigIntermediateToken.address(chainIdTo),
multiplier = BigNumber.from(10).pow(18-tokenFrom.decimals(this.chainId)),
feeRequestAmountFrom = amountFrom.mul(multiplier);

Expand Down Expand Up @@ -603,7 +598,7 @@ export namespace Bridge {
});

let
easyRedeems: ID[] = [Tokens.SYN.id, Tokens.UST.id],
easyRedeems: ID[] = [Tokens.SYN.id],
easyDeposits: ID[] = [Tokens.HIGH.id, Tokens.DOG.id, Tokens.FRAX.id],
easyDepositETH: ID[] = [Tokens.NETH.id];

Expand Down Expand Up @@ -703,12 +698,8 @@ export namespace Bridge {

let
easyDeposits: ID[] = [],
easyDepositETH: ID[] = [],
easyRedeems: ID[] = [
Tokens.SYN.id, Tokens.HIGH.id,
Tokens.DOG.id, Tokens.FRAX.id,
Tokens.UST.id
];
easyRedeems: ID[] = [Tokens.SYN.id, Tokens.HIGH.id, Tokens.DOG.id, Tokens.FRAX.id],
easyDepositETH: ID[] = [];

BridgeUtils.DepositIfChainTokens.forEach((args) => {
let {chainId, tokens, depositEth, altChainId} = args;
Expand Down Expand Up @@ -907,27 +898,69 @@ export namespace Bridge {
private makeBridgeTokenArgs(args: BridgeParams): BridgeTokenArgs {
let {tokenFrom, tokenTo, chainIdTo} = args;

const
checkAndChangeToken = (
t: Token,
check: Token,
swappy: Token
): Token => t.isEqual(check) ? swappy : t,
checkAndChangeTokens = (
check: Token,
swappy: Token
): ((t1: Token, t2: Token) => [Token, Token]) =>
(t1: Token, t2: Token) => [
checkAndChangeToken(t1, check, swappy),
checkAndChangeToken(t2, check, swappy)
];

let bridgeTokens: (t1: Token, t2: Token) => [Token, Token];

switch (tokenFrom.swapType) {
case SwapType.ETH:
bridgeTokens = BridgeUtils.checkReplaceTokens(Tokens.ETH, Tokens.WETH);
bridgeTokens = checkAndChangeTokens(Tokens.ETH, Tokens.WETH);
break;
case SwapType.AVAX:
bridgeTokens = BridgeUtils.checkReplaceTokens(Tokens.AVAX, Tokens.WAVAX);
bridgeTokens = checkAndChangeTokens(Tokens.AVAX, Tokens.WAVAX);
break;
case SwapType.MOVR:
bridgeTokens = BridgeUtils.checkReplaceTokens(Tokens.MOVR, Tokens.WMOVR);
bridgeTokens = checkAndChangeTokens(Tokens.MOVR, Tokens.WMOVR);
break;
default:
bridgeTokens = (t1: Token, t2: Token) => [t1, t2];
}

[tokenFrom, tokenTo] = bridgeTokens(tokenFrom, tokenTo);

const findSymbol = (tokA: Token, tokB: Token): boolean => {
let compareTok: Token = tokB;
switch (tokenSwitch(tokB)) {
case Tokens.WETH_E:
compareTok = Tokens.AVWETH;
break;
case Tokens.WETH:
compareTok = Tokens.WETH;
break;
default:
if (tokB.isWrappedToken) {
compareTok = tokB.underlyingToken;
}
break;
}

return tokA.isEqual(compareTok);
}

const makeTokenArgs = (chainId: number, t: Token): [Token[], number] => {
let
toks = SwapPools.bridgeSwappableTypePoolsByChain[chainId]?.[t.swapType]?.poolTokens,
idx = toks.findIndex((tok: Token) => findSymbol(tok, t));

return [toks, idx]
}

const
[fromChainTokens, tokenIndexFrom] = BridgeUtils.makeTokenArgs(this.chainId, tokenFrom),
[toChainTokens, tokenIndexTo] = BridgeUtils.makeTokenArgs(chainIdTo, tokenTo);
[fromChainTokens, tokenIndexFrom] = makeTokenArgs(this.chainId, tokenFrom),
[toChainTokens, tokenIndexTo] = makeTokenArgs(chainIdTo, tokenTo);

return {
fromChainTokens,
Expand Down Expand Up @@ -960,7 +993,11 @@ export namespace Bridge {
return REQUIRED_CONFS[chainId] ?? -1
}


interface EasyArgsCheck {
isEasy: boolean,
castArgs: BridgeUtils.BridgeTxParams,
txn?: Promise<PopulatedTransaction>,
}

export function bridgeSwapSupported(args: TokenSwap.BridgeSwapSupportedParams): TokenSwap.SwapSupportedResult {
return TokenSwap.bridgeSwapSupported(args)
Expand Down
51 changes: 4 additions & 47 deletions src/bridge/bridgeutils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import {Tokens} from "@tokens";
import {Slippages} from "./slippages";

import {Tokens} from "@tokens";
import {ChainId} from "@chainid";
import {SwapPools} from "@swappools";
import {ChainId} from "@chainid";

import type {Token} from "@token";
import type {GenericZapBridgeContract, L2BridgeZapContract} from "@contracts";

import {Zero} from "@ethersproject/constants";
import {BigNumber} from "@ethersproject/bignumber";
import {tokenSwitch} from "@internal/utils";


export namespace BridgeUtils {
Expand All @@ -30,7 +27,8 @@ export namespace BridgeUtils {
ChainId.ARBITRUM,
];

export const isL2ETHChain = (chainId: number): boolean => L2_ETH_CHAINS.includes(chainId);
export const isL2ETHChain = (chainId: number): boolean => L2_ETH_CHAINS.includes(chainId);

export const chainSupportsGasToken = (chainId: number): boolean => GAS_TOKEN_CHAINS.includes(chainId);

interface DepositIfChainArgs {
Expand Down Expand Up @@ -170,45 +168,4 @@ export namespace BridgeUtils {
t.isEqual(Tokens.WETH_E) || t.isEqual(Tokens.ONE_ETH) || t.isEqual(Tokens.FTM_ETH)

export const makeOverrides = (value: BigNumber, withValue: boolean): any => withValue ? {value} : {};

/**
* Switch t1 with t3 is t1 is t2
* @param {Token} t1 token being checked
* @param {Token} t2 token to check t1 against
* @param {Token} t3 token to return instead of t1 if t1 equals t2
*/
const checkReplaceToken = (t1: Token, t2: Token, t3: Token): Token => t1.isEqual(t2) ? t3 : t1;
export const checkReplaceTokens = (
check: Token,
replace: Token
): ((t1: Token, t2: Token) => [Token, Token]) =>
(t1: Token, t2: Token) => [
checkReplaceToken(t1, check, replace),
checkReplaceToken(t2, check, replace)
];

const findSymbol = (t1: Token, t2: Token): boolean => {
let compare: Token = t2;
switch (tokenSwitch(t2)) {
case Tokens.WETH_E:
compare = Tokens.AVWETH;
break;
case Tokens.WETH:
compare = Tokens.WETH;
break;
default:
compare = t2.isWrappedToken ? t2.underlyingToken : compare;
break;
}

return t1.isEqual(compare);
}

export const makeTokenArgs = (chainId: number, t: Token): [Token[], number] => {
let
toks: Token[] = SwapPools.bridgeSwappableMap[chainId].swappableSwapGroups[t.swapType].poolTokens,
idx = toks.findIndex((tok: Token) => findSymbol(tok, t));

return [toks, idx]
}
}
4 changes: 2 additions & 2 deletions src/bridge/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ export const MAX_APPROVAL_AMOUNT = BigNumber.from("0xfffffffffffffffffffffffffff


export namespace ERC20 {
export type ApproveArgs = {
export interface ApproveArgs {
spender: string,
amount?: BigNumberish
}

export type ERC20TokenParams = {
export interface ERC20TokenParams {
tokenAddress: string,
chainId: number,
}
Expand Down
2 changes: 1 addition & 1 deletion src/bridge/gasutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {BigNumber} from "@ethersproject/bignumber";
import type {PopulatedTransaction} from "@ethersproject/contracts";

export namespace GasUtils {
type GasParams = {
export interface GasParams {
maxFeePerGas?: BigNumber,
maxPriorityFee?: BigNumber,
gasPrice?: BigNumber,
Expand Down
10 changes: 5 additions & 5 deletions src/common/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import {SwapPools} from "@swappools";

import type {
ID,
Distinct,
} from "@internal/distinct";
Entity,
} from "@internal/entity";

import {BridgeUtils} from "@bridge/bridgeutils";

import type {ChainIdTypeMap} from "./types";

export namespace Networks {
type supportedTokenEdgeCase = {
interface SupportsTokenChecks {
chainId: ChainId,
token: Token,
}

const tokenSupportChecks: supportedTokenEdgeCase[] = [
const tokenSupportChecks: SupportsTokenChecks[] = [
{chainId: ChainId.ETH, token: Tokens.WETH},
{chainId: ChainId.ETH, token: Tokens.NETH},
{chainId: ChainId.AVALANCHE, token: Tokens.AVAX},
Expand All @@ -42,7 +42,7 @@ export namespace Networks {
chainCurrency: string,
}

export class Network implements Distinct {
export class Network implements Entity {
readonly id: ID;
readonly name: string;
readonly chainCurrency: string;
Expand Down
14 changes: 10 additions & 4 deletions src/common/synapse_contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {ContractInterface} from "@ethersproject/contracts";


export namespace SynapseContracts {
type abiAndAddress = {
interface abiAndAddress {
address: string,
abi: ContractInterface,
}
Expand All @@ -34,9 +34,17 @@ export namespace SynapseContracts {
return this.bridge.address
}

get bridge_abi(): ContractInterface {
return this.bridge.abi
}

get bridge_zap_address(): string {
return this.bridge_zap.address
}

get bridge_zap_abi(): ContractInterface {
return this.bridge_zap.abi
}
}

export const Ethereum = new SynapseContract({
Expand All @@ -52,7 +60,6 @@ export namespace SynapseContracts {

export const Cronos = new SynapseContract({
bridge: "0xE27BFf97CE92C3e1Ff7AA9f86781FDd6D48F5eE9",
bridge_zap: "0x88E7af57270F70BCF32CD61fff0Ff635775C8f7c",
});

export const BSC = new SynapseContract({
Expand All @@ -76,8 +83,7 @@ export namespace SynapseContracts {
});

export const Metis = new SynapseContract({
bridge: "0x06Fea8513FF03a0d3f61324da709D4cf06F42A5c",
bridge_zap: "0x244268b9082E05a8BcF18b3b0e83999EA4Fc9fCf",
bridge: "0xAf41a65F786339e7911F4acDAD6BD49426F2Dc6b",
});

export const Moonbeam = new SynapseContract(({
Expand Down
2 changes: 1 addition & 1 deletion src/internal/distinct.ts → src/internal/entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type ID = symbol;

export interface Distinct {
export interface Entity {
readonly id: ID,
}
1 change: 0 additions & 1 deletion src/internal/swaptype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ export enum SwapType {
SOLAR = "SOLAR",
AVAX = "AVAX",
MOVR = "MOVR",
UST = "UST",
}
Loading

0 comments on commit 67e8bcd

Please sign in to comment.