Skip to content

Commit

Permalink
Merge branch 'main' into bitSmiley-memo-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Nov 29, 2024
2 parents 0e7e637 + 856177b commit be67ae8
Show file tree
Hide file tree
Showing 8 changed files with 497 additions and 501 deletions.
80 changes: 35 additions & 45 deletions contracts/SwapHelperLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity 0.8.26;
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router01.sol";
import "./shared/interfaces/IZRC20.sol";
import "./SystemContract.sol";
import "./shared/libraries/UniswapV2Library.sol";

library SwapHelperLib {
Expand Down Expand Up @@ -112,41 +111,39 @@ library SwapHelperLib {
}

function swapExactTokensForTokens(
SystemContract systemContract,
address router,
address zrc20,
uint256 amount,
address targetZRC20,
uint256 minAmountOut
) internal returns (uint256) {
address factory = IUniswapV2Router01(router).factory();
address wzeta = IUniswapV2Router01(router).WETH();

address[] memory path;
path = new address[](2);
path[0] = zrc20;
path[1] = targetZRC20;

bool isSufficientLiquidity = _isSufficientLiquidity(
systemContract.uniswapv2FactoryAddress(),
factory,
amount,
minAmountOut,
path
);

bool isZETA = targetZRC20 == systemContract.wZetaContractAddress() ||
zrc20 == systemContract.wZetaContractAddress();
bool isZETA = targetZRC20 == wzeta || zrc20 == wzeta;

if (!isSufficientLiquidity && !isZETA) {
path = new address[](3);
path[0] = zrc20;
path[1] = systemContract.wZetaContractAddress();
path[1] = wzeta;
path[2] = targetZRC20;
}

IZRC20(zrc20).approve(
address(systemContract.uniswapv2Router02Address()),
amount
);
uint256[] memory amounts = IUniswapV2Router01(
systemContract.uniswapv2Router02Address()
).swapExactTokensForTokens(
IZRC20(zrc20).approve(router, amount);
uint256[] memory amounts = IUniswapV2Router01(router)
.swapExactTokensForTokens(
amount,
minAmountOut,
path,
Expand All @@ -157,17 +154,16 @@ library SwapHelperLib {
}

function swapExactTokensForTokensDirectly(
SystemContract systemContract,
address router,
address zrc20,
uint256 amount,
address targetZRC20,
uint256 minAmountOut
) internal returns (uint256) {
bool existsPairPool = _existsPairPool(
systemContract.uniswapv2FactoryAddress(),
zrc20,
targetZRC20
);
address factory = IUniswapV2Router01(router).factory();
address wzeta = IUniswapV2Router01(router).WETH();

bool existsPairPool = _existsPairPool(factory, zrc20, targetZRC20);

address[] memory path;
if (existsPairPool) {
Expand All @@ -177,17 +173,13 @@ library SwapHelperLib {
} else {
path = new address[](3);
path[0] = zrc20;
path[1] = systemContract.wZetaContractAddress();
path[1] = wzeta;
path[2] = targetZRC20;
}

IZRC20(zrc20).approve(
address(systemContract.uniswapv2Router02Address()),
amount
);
uint256[] memory amounts = IUniswapV2Router01(
systemContract.uniswapv2Router02Address()
).swapExactTokensForTokens(
IZRC20(zrc20).approve(router, amount);
uint256[] memory amounts = IUniswapV2Router01(router)
.swapExactTokensForTokens(
amount,
minAmountOut,
path,
Expand All @@ -198,17 +190,16 @@ library SwapHelperLib {
}

function swapTokensForExactTokens(
SystemContract systemContract,
address router,
address zrc20,
uint256 amount,
address targetZRC20,
uint256 amountInMax
) internal returns (uint256) {
bool existsPairPool = _existsPairPool(
systemContract.uniswapv2FactoryAddress(),
zrc20,
targetZRC20
);
address factory = IUniswapV2Router01(router).factory();
address wzeta = IUniswapV2Router01(router).WETH();

bool existsPairPool = _existsPairPool(factory, zrc20, targetZRC20);

address[] memory path;
if (existsPairPool) {
Expand All @@ -218,17 +209,13 @@ library SwapHelperLib {
} else {
path = new address[](3);
path[0] = zrc20;
path[1] = systemContract.wZetaContractAddress();
path[1] = wzeta;
path[2] = targetZRC20;
}

IZRC20(zrc20).approve(
address(systemContract.uniswapv2Router02Address()),
amountInMax
);
uint256[] memory amounts = IUniswapV2Router01(
systemContract.uniswapv2Router02Address()
).swapTokensForExactTokens(
IZRC20(zrc20).approve(router, amountInMax);
uint256[] memory amounts = IUniswapV2Router01(router)
.swapTokensForExactTokens(
amount,
amountInMax,
path,
Expand All @@ -239,28 +226,31 @@ library SwapHelperLib {
}

function getMinOutAmount(
SystemContract systemContract,
address router,
address zrc20,
address target,
uint256 amountIn
) public view returns (uint256 minOutAmount) {
address factory = IUniswapV2Router01(router).factory();
address wzeta = IUniswapV2Router01(router).WETH();

address[] memory path;

path = new address[](2);
path[0] = zrc20;
path[1] = target;
uint[] memory amounts1 = UniswapV2Library.getAmountsOut(
systemContract.uniswapv2FactoryAddress(),
factory,
amountIn,
path
);

path = new address[](3);
path[0] = zrc20;
path[1] = systemContract.wZetaContractAddress();
path[1] = wzeta;
path[2] = target;
uint[] memory amounts2 = UniswapV2Library.getAmountsOut(
systemContract.uniswapv2FactoryAddress(),
factory,
amountIn,
path
);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@
"@nomiclabs/hardhat-ethers": "^2.2.3",
"@openzeppelin/contracts": "^5.0.2",
"@openzeppelin/contracts-upgradeable": "^5.0.2",
"@solana/wallet-adapter-react": "^0.15.35",
"@solana/web3.js": "^1.95.3",
"@uniswap/v2-periphery": "^1.1.0-beta.0",
"@zetachain/faucet-cli": "^4.1.1",
"@zetachain/networks": "10.0.0",
"@zetachain/networks": "v10.0.0-rc1",
"@zetachain/protocol-contracts": "11.0.0-rc3",
"axios": "^1.4.0",
"bech32": "^2.0.0",
Expand Down
52 changes: 49 additions & 3 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { Wallet as SolanaWallet } from "@coral-xyz/anchor";
import type { WalletContextState } from "@solana/wallet-adapter-react";
import { PublicKey } from "@solana/web3.js";
import { networks } from "@zetachain/networks";
import type { Signer, Wallet } from "ethers";
import merge from "lodash/merge";
Expand Down Expand Up @@ -33,16 +36,45 @@ export interface ZetaChainClientParamsBase {

export type ZetaChainClientParams = ZetaChainClientParamsBase &
(
| { signer: Signer; wallet?: never }
| { signer?: never; wallet: Wallet }
| { signer?: undefined; wallet?: undefined }
| {
signer: Signer;
solanaAdapter?: never;
solanaWallet?: never;
wallet?: never;
}
| {
signer?: never;
solanaAdapter: WalletContextState;
solanaWallet?: never;
wallet?: never;
}
| {
signer?: never;
solanaAdapter?: never;
solanaWallet: SolanaWallet;
wallet?: never;
}
| {
signer?: never;
solanaAdapter?: never;
solanaWallet?: never;
wallet: Wallet;
}
| {
signer?: undefined;
solanaAdapter?: undefined;
solanaWallet?: undefined;
wallet?: undefined;
}
);

export class ZetaChainClient {
public chains: { [key: string]: any };
public network: string;
public wallet: Wallet | undefined;
public signer: any | undefined;
public solanaWallet: SolanaWallet | undefined;
public solanaAdapter: WalletContextState | undefined;

/**
* Initializes ZetaChainClient instance.
Expand Down Expand Up @@ -96,6 +128,10 @@ export class ZetaChainClient {
this.wallet = params.wallet;
} else if (params.signer) {
this.signer = params.signer;
} else if (params.solanaWallet) {
this.solanaWallet = params.solanaWallet;
} else if (params.solanaAdapter) {
this.solanaAdapter = params.solanaAdapter;
}
this.chains = { ...networks };
this.network = params.network || "";
Expand All @@ -115,6 +151,16 @@ export class ZetaChainClient {
return this.chains;
}

public isSolanaWalletConnected(): boolean {
return this.solanaAdapter?.connected || this.solanaWallet !== undefined;
}

public getSolanaPublicKey(): PublicKey | null {
return (
this.solanaAdapter?.publicKey || this.solanaWallet?.publicKey || null
);
}

getEndpoint = getEndpoint;
getBalances = getBalances;
getForeignCoins = getForeignCoins;
Expand Down
Loading

0 comments on commit be67ae8

Please sign in to comment.