Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andresaiello committed Nov 16, 2023
1 parent c7df370 commit 93de470
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 68 deletions.
2 changes: 1 addition & 1 deletion packages/zeta-app-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {
"@openzeppelin/contracts": "^4.8.3",
"@zetachain/networks": "^2.4.3",
"@zetachain/protocol-contracts": "^3.0.0",
"@zetachain/protocol-contracts": "^3.0.1",
"ethers": "5.6.8"
}
}
2 changes: 1 addition & 1 deletion packages/zeta-app-contracts/scripts/address.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const saveAddress = (name: string, address: string) => {

console.log(`Updating ${name} address on ${networkName}.`);

const filename = join(__dirname, `./data/addresses.json`);
const filename = join(__dirname, `../data/addresses.json`);

const newAddresses = JSON.parse(readFileSync(filename, "utf8"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ async function main() {
await contract.deployed();

console.log("MultiChainValue deployed to:", contract.address);
saveAddress("multiChainValue", contract.address);

console.log("MultiChainValue post rutine...");

networkName !== "goerli_testnet" &&
(await (await contract.addAvailableChainId(getChainId("goerli_testnet")))
Expand All @@ -34,7 +37,7 @@ async function main() {
networkName !== "zeta_testnet" &&
(await (await contract.addAvailableChainId(getChainId("zeta_testnet"))).wait().catch((e: any) => console.error(e)));

saveAddress("multiChainValue", contract.address);
console.log("MultiChainValue post rutine finish");
}

main().catch(error => {
Expand Down
2 changes: 1 addition & 1 deletion packages/zeta-app-contracts/test/MultiChainValue.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { ZetaEth } from "@zetachain/interfaces/typechain-types";
import { ZetaEth } from "@zetachain/protocol-contracts/dist/typechain-types";
import { expect } from "chai";
import { parseEther } from "ethers/lib/utils";
import { ethers } from "hardhat";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ contract StakingRewards is RewardsDistributionRecipient, ReentrancyGuard, Pausab
uint256 internal _totalSupply;
mapping(address => uint256) internal _balances;

/* ========== CUSTOM ERRORS ========== */

error CannotStakeZero();
error CannotWithdrawZero();
error PreviousRewardsPeriodNotComplete();
error ProvidedRewardTooHigh();
error CannotWithdrawStakingToken();
error RewardIsNotAWrappedAsset();
error TransferFailed();

/* ========== CONSTRUCTOR ========== */

constructor(
Expand Down Expand Up @@ -82,7 +92,7 @@ contract StakingRewards is RewardsDistributionRecipient, ReentrancyGuard, Pausab
/* ========== MUTATIVE FUNCTIONS ========== */

function stake(uint256 amount) public virtual nonReentrant notPaused updateReward(msg.sender) {
require(amount > 0, "Cannot stake 0");
if (amount == 0) revert CannotStakeZero();
_totalSupply = _totalSupply.add(amount);
_balances[msg.sender] = _balances[msg.sender].add(amount);
stakingToken.safeTransferFrom(msg.sender, address(this), amount);
Expand All @@ -91,14 +101,14 @@ contract StakingRewards is RewardsDistributionRecipient, ReentrancyGuard, Pausab

///@dev This function is added to support staking from the same contract without the need of an extra transfer
function _stakeFromContract(uint256 amount) internal notPaused updateReward(msg.sender) {
require(amount > 0, "Cannot stake 0");
if (amount == 0) revert CannotStakeZero();
_totalSupply = _totalSupply.add(amount);
_balances[msg.sender] = _balances[msg.sender].add(amount);
emit Staked(msg.sender, amount);
}

function withdraw(uint256 amount) public virtual nonReentrant updateReward(msg.sender) {
require(amount > 0, "Cannot withdraw 0");
if (amount == 0) revert CannotWithdrawZero();
_totalSupply = _totalSupply.sub(amount);
_balances[msg.sender] = _balances[msg.sender].sub(amount);
stakingToken.safeTransfer(msg.sender, amount);
Expand All @@ -120,10 +130,11 @@ contract StakingRewards is RewardsDistributionRecipient, ReentrancyGuard, Pausab

// Make the low-level call
(bool success, ) = address(rewardsToken).call(data);
require(success, "Reward is not a wrapped asset");

if (!success) revert RewardIsNotAWrappedAsset();

(success, ) = msg.sender.call{value: reward}("");
require(success, "Transfer failed");
if (!success) revert TransferFailed();
} else rewardsToken.safeTransfer(msg.sender, reward);
emit RewardPaid(msg.sender, reward);
}
Expand All @@ -150,7 +161,7 @@ contract StakingRewards is RewardsDistributionRecipient, ReentrancyGuard, Pausab
// very high values of rewardRate in the earned and rewardsPerToken functions;
// Reward + leftover must be less than 2^256 / 10^18 to avoid overflow.
uint balance = rewardsToken.balanceOf(address(this));
require(rewardRate <= balance.div(rewardsDuration), "Provided reward too high");
if (rewardRate > balance.div(rewardsDuration)) revert ProvidedRewardTooHigh();

lastUpdateTime = block.timestamp;
periodFinish = block.timestamp.add(rewardsDuration);
Expand All @@ -159,16 +170,13 @@ contract StakingRewards is RewardsDistributionRecipient, ReentrancyGuard, Pausab

// Added to support recovering LP Rewards from other systems such as BAL to be distributed to holders
function recoverERC20(address tokenAddress, uint256 tokenAmount) external onlyOwner {
require(tokenAddress != address(stakingToken), "Cannot withdraw the staking token");
if (tokenAddress == address(stakingToken)) revert CannotWithdrawStakingToken();
IERC20(tokenAddress).safeTransfer(owner, tokenAmount);
emit Recovered(tokenAddress, tokenAmount);
}

function setRewardsDuration(uint256 _rewardsDuration) external onlyOwner {
require(
block.timestamp > periodFinish,
"Previous rewards period must be complete before changing the duration for the new period"
);
if (block.timestamp <= periodFinish) revert PreviousRewardsPeriodNotComplete();
rewardsDuration = _rewardsDuration;
emit RewardsDurationUpdated(rewardsDuration);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/zevm-app-contracts/data/addresses.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"disperse": "0x1E0F767F48Fb10FcF820703f116E9B0F87319d63",
"rewardDistributorFactory": "0x667e4C493d40015256BDC89E3ba750B2F90359E1",
"zetaSwap": "0x44D1F1f9289DBA1Cf5824bd667184cEBE020aA1c",
"zetaSwapBtcInbound": "0x008b393933D5CA2457Df570CA5D628380FFf6da4"
"zetaSwapBtcInbound": "0x008b393933D5CA2457Df570CA5D628380FFf6da4",
"invitationManager": "0xF4cF881A3d23936e3710ef2Cbbe93f71C4389918"
}
}
}
2 changes: 1 addition & 1 deletion packages/zevm-app-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@openzeppelin/contracts": "4.8.3",
"@uniswap/v2-periphery": "1.1.0-beta.0",
"@zetachain/networks": "^2.4.3",
"@zetachain/protocol-contracts": "^3.0.0",
"@zetachain/protocol-contracts": "^3.0.1",
"ethers": "5.6.8"
}
}
4 changes: 2 additions & 2 deletions packages/zevm-app-contracts/scripts/address.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { networks } from "@zetachain/networks";
import { isProtocolNetworkName, ZetaProtocolNetwork } from "@zetachain/protocol-contracts";
import { ZetaProtocolNetwork } from "@zetachain/protocol-contracts";
import protocolAddresses from "@zetachain/protocol-contracts/dist/data/addresses.json";
import { readFileSync, writeFileSync } from "fs";
import { join } from "path";
Expand Down Expand Up @@ -29,7 +29,7 @@ export const saveAddress = (name: string, address: string) => {

console.log(`Updating ${name} address on ${networkName}.`);

const filename = join(__dirname, `./data/addresses.json`);
const filename = join(__dirname, `../data/addresses.json`);

const newAddresses = JSON.parse(readFileSync(filename, "utf8"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { formatUnits, parseUnits } from "ethers/lib/utils";
import { ethers } from "hardhat";

import { IUniswapV2Factory__factory } from "../../../zevm-example-contracts/typechain-types";
import { IUniswapV2Factory__factory } from "../../typechain-types";
import { ERC20, IUniswapV2Pair__factory } from "../../typechain-types";

export interface Pair {
Expand Down
2 changes: 0 additions & 2 deletions packages/zevm-app-contracts/scripts/zeta-points/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ const networkName = network.name;

const invitationManager = async () => {
const InvitationManagerFactory = (await ethers.getContractFactory("InvitationManager")) as InvitationManager__factory;

const invitationManager = await InvitationManagerFactory.deploy();
await invitationManager.deployed();

console.log("InvitationManager deployed to:", invitationManager.address);
saveAddress("invitationManager", invitationManager.address);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ async function main() {
console.log(`WZETA:`, WZETAAddress);
await getZRC20Address(systemContract, "btc_testnet");
await getZRC20Address(systemContract, "goerli_testnet");
// await getZRC20Address(systemContract, "klaytn-baobab");
await getZRC20Address(systemContract, "bsc_testnet");
await getZRC20Address(systemContract, "mumbai_testnet");
}
Expand Down
37 changes: 17 additions & 20 deletions packages/zevm-app-contracts/scripts/zeta-swap/stress-swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
import { BigNumber } from "@ethersproject/bignumber";
import { formatEther, parseEther } from "@ethersproject/units";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { getAddress } from "@zetachain/addresses";
import { ChainToZRC20, getZRC20Address, isSwappableNetwork, SwappableNetwork } from "@zetachain/addresses-tools";
import {
getAddress,
getZRC20Address,
isProtocolNetworkName,
ZetaProtocolNetwork,
zetaProtocolNetworks
} from "@zetachain/protocol-contracts";
import { ethers, network } from "hardhat";

import { getZEVMAppAddress } from "../address.helpers";
import { getSwapData } from "./helpers";

const ZRC20Addresses = getZRC20Address();
const networkName = network.name;

interface SwapToChainParams {
destinationNetwork: SwappableNetwork;
destinationNetwork: ZetaProtocolNetwork;
nonce: number;
signer: SignerWithAddress;
tssAddress: string;
Expand All @@ -35,8 +41,7 @@ const swapToChain = async ({
value,
nonce
}: SwapToChainParams) => {
const zrc20 = ChainToZRC20[destinationNetwork];
const data = getSwapData(zetaSwapAddress, signer.address, ZRC20Addresses[zrc20], BigNumber.from("0"));
const data = getSwapData(zetaSwapAddress, signer.address, getZRC20Address(destinationNetwork), BigNumber.from("0"));
const tx = await signer.sendTransaction({
data,
nonce,
Expand All @@ -47,30 +52,22 @@ const swapToChain = async ({
};

const main = async () => {
if (!isSwappableNetwork(network.name) || !network.name) throw new Error("Invalid network name");
const swappableNetwork: SwappableNetwork = network.name;
if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name");
const swappableNetwork: ZetaProtocolNetwork = networkName;

// @dev: bitcoin is invalid as destination
const invalidDestinations: SwappableNetwork[] = [swappableNetwork, "btc_testnet"];
const networks = Object.keys(ChainToZRC20).map(c => c as SwappableNetwork);
const invalidDestinations: ZetaProtocolNetwork[] = [swappableNetwork, "btc_testnet"];
const networks = zetaProtocolNetworks.map(c => c as ZetaProtocolNetwork);

const destinationNetworks = networks.filter(e => !invalidDestinations.includes(e));

console.log(`Swapping native token...`);

const [signer] = await ethers.getSigners();

const zetaSwapAddress = getAddress({
address: "zetaSwap",
networkName: "athens",
zetaNetwork: "athens"
});
const zetaSwapAddress = getZEVMAppAddress("zetaSwap");

const tssAddress = getAddress({
address: "tss",
networkName: network.name,
zetaNetwork: "athens"
});
const tssAddress = getAddress("tss", swappableNetwork);

const nonce = await signer.getTransactionCount();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ const main = async () => {
params = getSwapParams(signer.address, getZRC20Address("goerli_testnet"), BigNumber.from("0"));
zetaSwapContract = ZetaSwap__factory.connect(zetaSwap, signer);
}
const tx1 = await zetaSwapContract.onCrossChainCall(sourceToken, amount, params);
const zContextStruct = {
chainID: ethers.BigNumber.from("0"),
origin: ethers.constants.HashZero,
sender: ethers.constants.AddressZero
};
const tx1 = await zetaSwapContract.onCrossChainCall(zContextStruct, sourceToken, amount, params);
await tx1.wait();

console.log("tx:", tx1.hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("LiquidityIncentives tests", () => {

const uniswapRouterAddr = getNonZetaAddress("uniswapV2Router02", "eth_mainnet");

const uniswapFactoryAddr = "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"; // getNonZetaAddress("uniswapV2Factory02", "etherum_mainnet");
const uniswapFactoryAddr = getNonZetaAddress("uniswapV2Factory", "eth_mainnet");

const wGasToken = getNonZetaAddress("weth9", "eth_mainnet");

Expand Down
2 changes: 1 addition & 1 deletion packages/zevm-app-contracts/test/MultipleOutput.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("ZetaSwap tests", () => {

const uniswapRouterAddr = getNonZetaAddress("uniswapV2Router02", "eth_mainnet");

const uniswapFactoryAddr = "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"; // getNonZetaAddress("uniswapV2Factory02", "etherum_mainnet");
const uniswapFactoryAddr = getNonZetaAddress("uniswapV2Factory", "eth_mainnet");

const wGasToken = getNonZetaAddress("weth9", "eth_mainnet");

Expand Down
2 changes: 1 addition & 1 deletion packages/zevm-app-contracts/test/Swap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe("ZetaSwap tests", () => {

const uniswapRouterAddr = getNonZetaAddress("uniswapV2Router02", "eth_mainnet");

const uniswapFactoryAddr = "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"; // getNonZetaAddress("uniswapV2Factory02", "etherum_mainnet");
const uniswapFactoryAddr = getNonZetaAddress("uniswapV2Factory", "eth_mainnet");

const wGasToken = getNonZetaAddress("weth9", "eth_mainnet");

Expand Down
27 changes: 6 additions & 21 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2542,10 +2542,10 @@ __metadata:
languageName: node
linkType: hard

"@zetachain/protocol-contracts@npm:^3.0.0":
version: 3.0.0
resolution: "@zetachain/protocol-contracts@npm:3.0.0"
checksum: 2bfc8fe05e5e5145cecfb76dd7fe72c22388f7303ebab6039bfa11c92fe89d1bcbe562741dc3d7b3e3819393515c641c4034ebce55bed11f4667be384dddb652
"@zetachain/protocol-contracts@npm:^3.0.1":
version: 3.0.1
resolution: "@zetachain/protocol-contracts@npm:3.0.1"
checksum: 5a833dc9f59b69b83654754f4587aad57f4c9859908bc3fd9b5f3f6329cf803bf68e66aff4f355a99fb7e23726526196610e43987d15ec60208cc5564bc94259
languageName: node
linkType: hard

Expand All @@ -2556,7 +2556,7 @@ __metadata:
"@defi-wonderland/smock": ^2.3.4
"@openzeppelin/contracts": ^4.8.3
"@zetachain/networks": ^2.4.3
"@zetachain/protocol-contracts": ^3.0.0
"@zetachain/protocol-contracts": ^3.0.1
ethers: 5.6.8
hardhat-gas-reporter: ^1.0.8
solidity-coverage: ^0.7.20
Expand All @@ -2571,22 +2571,7 @@ __metadata:
"@openzeppelin/contracts": 4.8.3
"@uniswap/v2-periphery": 1.1.0-beta.0
"@zetachain/networks": ^2.4.3
"@zetachain/protocol-contracts": ^3.0.0
ethers: 5.6.8
hardhat-gas-reporter: ^1.0.8
solidity-coverage: ^0.7.20
tsconfig-paths: ^3.14.1
languageName: unknown
linkType: soft

"@zetachain/zevm-example-contracts@workspace:packages/zevm-example-contracts":
version: 0.0.0-use.local
resolution: "@zetachain/zevm-example-contracts@workspace:packages/zevm-example-contracts"
dependencies:
"@openzeppelin/contracts": ^4.8.3
"@uniswap/v2-periphery": 1.1.0-beta.0
"@zetachain/networks": ^2.4.3
"@zetachain/protocol-contracts": ^3.0.0
"@zetachain/protocol-contracts": ^3.0.1
ethers: 5.6.8
hardhat-gas-reporter: ^1.0.8
solidity-coverage: ^0.7.20
Expand Down

0 comments on commit 93de470

Please sign in to comment.