Skip to content

Commit

Permalink
upgradeable
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Dec 6, 2024
1 parent 0ee8a64 commit 354868e
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 42 deletions.
33 changes: 27 additions & 6 deletions examples/swap/contracts/Swap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ import "@zetachain/protocol-contracts/contracts/zevm/interfaces/IGatewayZEVM.sol
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/IWZETA.sol";
import {GatewayZEVM} from "@zetachain/protocol-contracts/contracts/zevm/GatewayZEVM.sol";

contract Swap is UniversalContract {
address public immutable uniswapRouter;
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";

contract Swap is
UniversalContract,
Initializable,
UUPSUpgradeable,
OwnableUpgradeable
{
address public uniswapRouter;
GatewayZEVM public gateway;
uint256 constant BITCOIN = 18332;
uint256 public immutable gasLimit;
uint256 public gasLimit;

error InvalidAddress();
error Unauthorized();
Expand All @@ -28,13 +37,21 @@ contract Swap is UniversalContract {
_;
}

constructor(
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}

function initialize(
address payable gatewayAddress,
address uniswapRouterAddress,
uint256 gasLimitAmount
) {
uint256 gasLimitAmount,
address owner
) public initializer {
if (gatewayAddress == address(0) || uniswapRouterAddress == address(0))
revert InvalidAddress();
__UUPSUpgradeable_init();
__Ownable_init(owner);
uniswapRouter = uniswapRouterAddress;
gateway = GatewayZEVM(gatewayAddress);
gasLimit = gasLimitAmount;
Expand Down Expand Up @@ -228,4 +245,8 @@ contract Swap is UniversalContract {
fallback() external payable {}

receive() external payable {}

function _authorizeUpgrade(
address newImplementation
) internal override onlyOwner {}
}
3 changes: 3 additions & 0 deletions examples/swap/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import "@zetachain/toolkit/tasks";
import { getHardhatConfigNetworks } from "@zetachain/networks";
import { HardhatUserConfig } from "hardhat/config";

import "@openzeppelin/hardhat-upgrades";
import "@nomiclabs/hardhat-ethers";

const config: HardhatUserConfig = {
networks: {
...getHardhatConfigNetworks(),
Expand Down
1 change: 1 addition & 0 deletions examples/swap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@nomicfoundation/hardhat-toolbox": "^2.0.0",
"@nomiclabs/hardhat-ethers": "^2.0.0",
"@nomiclabs/hardhat-etherscan": "^3.0.0",
"@openzeppelin/hardhat-upgrades": "1.28.0",
"@typechain/ethers-v5": "^10.1.0",
"@typechain/hardhat": "^6.1.2",
"@types/chai": "^4.2.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/swap/scripts/localnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ GATEWAY_ZETACHAIN=$(jq -r '.addresses[] | select(.type=="gatewayZEVM" and .chain
UNISWAP_ROUTER=$(jq -r '.addresses[] | select(.type=="uniswapRouterInstance" and .chain=="zetachain") | .address' localnet.json)
SENDER=0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

CONTRACT_SWAP=$(npx hardhat deploy --name Swap --network localhost --gateway "$GATEWAY_ZETACHAIN" --uniswap-router "$UNISWAP_ROUTER" --json | jq -r '.contractAddress')
CONTRACT_SWAP=$(npx hardhat deploy --name Swap --network localhost --gateway "$GATEWAY_ZETACHAIN" --uniswap-router "$UNISWAP_ROUTER" | jq -r '.contractAddress')
echo -e "\n🚀 Deployed Swap contract on ZetaChain: $CONTRACT_SWAP"

npx hardhat swap-from-evm \
Expand Down
34 changes: 12 additions & 22 deletions examples/swap/tasks/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,23 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
}

const factory = await hre.ethers.getContractFactory(args.name);
const contract = await (factory as any).deploy(
args.gateway,
args.uniswapRouter,
args.gasLimit
);
await contract.deployed();

if (args.json) {
console.log(
JSON.stringify({
contractAddress: contract.address,
deployer: signer.address,
network: network,
transactionHash: contract.deployTransaction.hash,
})
);
} else {
console.log(`🔑 Using account: ${signer.address}
const contract = await hre.upgrades.deployProxy(
factory,
[args.gateway, args.uniswapRouter, args.gasLimit, signer.address],
{ kind: "uups" }
);

🚀 Successfully deployed contract on ${network}.
📜 Contract address: ${contract.address}
`);
}
console.log(
JSON.stringify({
contractAddress: contract.address,
deployer: signer.address,
network: network,
})
);
};

task("deploy", "Deploy the contract", main)
.addFlag("json", "Output in JSON")
.addOptionalParam("name", "Contract to deploy", "Swap")
.addOptionalParam("uniswapRouter", "Uniswap v2 Router address")
.addOptionalParam(
Expand Down
Loading

0 comments on commit 354868e

Please sign in to comment.