Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add wormhole contracts #5

Merged
merged 9 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/murky"]
path = lib/murky
url = https://github.com/dmfxyz/murky
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/openzeppelin/openzeppelin-contracts
[submodule "lib/openzeppelin-contracts-upgradeable"]
path = lib/openzeppelin-contracts-upgradeable
url = https://github.com/openzeppelin/openzeppelin-contracts-upgradeable
[submodule "lib/murky"]
path = lib/murky
url = https://github.com/dmfxyz/murky
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ runs = 10000

[rpc_endpoints]
mainnet = "https://ethereum-rpc.publicnode.com"
base = "https://base-rpc.publicnode.com"
sepolia = "https://ethereum-sepolia.publicnode.com"
goerli = "https://ethereum-goerli.publicnode.com"

Expand Down
2 changes: 1 addition & 1 deletion lib/openzeppelin-contracts
2 changes: 1 addition & 1 deletion lib/openzeppelin-contracts-upgradeable
18 changes: 18 additions & 0 deletions script/DeployAvailWormhole.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.25;

import {TransparentUpgradeableProxy} from
"lib/openzeppelin-contracts/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {AvailWormhole} from "src/AvailWormhole.sol";
import {Script} from "forge-std/Script.sol";

contract Deploy is Script {
function run() external {
vm.startBroadcast();
address admin = vm.envAddress("ADMIN");
address impl = address(new AvailWormhole());
AvailWormhole avail = AvailWormhole(address(new TransparentUpgradeableProxy(impl, admin, "")));
avail.initialize(admin);
vm.stopBroadcast();
}
}
36 changes: 36 additions & 0 deletions script/Verify.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// VerificationTester.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

import "forge-std/Script.sol";
import "forge-std/console.sol";

contract VerificationTester is Script {
function run() external view {
// Load environment variables;
address CONTRACT_ADDRESS = 0xEaf1db02ad660f832AAa6F84F2Cb639c0F1cB5C6;

// Get deployed bytecode
bytes memory deployedBytecode =
vm.getDeployedCode(string.concat(vm.projectRoot(), "/src/AvailWormhole.sol:AvailWormhole"));

// Get on-chain bytecode
bytes memory onchainBytecode;
assembly {
let size := extcodesize(CONTRACT_ADDRESS)
onchainBytecode := mload(0x40)
mstore(0x40, add(onchainBytecode, add(size, 0x20)))
mstore(onchainBytecode, size)
extcodecopy(CONTRACT_ADDRESS, add(onchainBytecode, 0x20), 0, size)
}

// Compare bytecode lengths
console.log("Deployed bytecode length:", deployedBytecode.length);
console.log("On-chain bytecode length:", onchainBytecode.length);

console.log(" ############## ");
console.logBytes(deployedBytecode);
console.log(" ############## ");
console.logBytes(onchainBytecode);
}
}
38 changes: 38 additions & 0 deletions src/AvailWormhole.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.25;

import {
ERC20Upgradeable,
ERC20PermitUpgradeable
} from "lib/openzeppelin-contracts-upgradeable/contracts/token/ERC20/extensions/ERC20PermitUpgradeable.sol";
import {AccessControlDefaultAdminRulesUpgradeable} from
"lib/openzeppelin-contracts-upgradeable/contracts/access/extensions/AccessControlDefaultAdminRulesUpgradeable.sol";
import {INttToken} from "src/interfaces/INttToken.sol";

/**
* @author @QEDK (Avail)
* @title Avail ERC20 token with support for Wormhole
* @notice An Avail token implementation for Wormhole-based bridges
* @custom:security [email protected]
*/
contract AvailWormhole is AccessControlDefaultAdminRulesUpgradeable, ERC20PermitUpgradeable, INttToken {
bytes32 private constant MINTER_ROLE = keccak256("MINTER_ROLE");

constructor() {
_disableInitializers();
}

function initialize(address governance) external initializer {
__ERC20Permit_init("Avail (Wormhole)");
__ERC20_init("Avail (Wormhole)", "AVAIL.W");
__AccessControlDefaultAdminRules_init(0, governance);
}

function mint(address account, uint256 amount) external onlyRole(MINTER_ROLE) {
_mint(account, amount);
}

function burn(uint256 amount) external {
_burn(msg.sender, amount);
}
}
12 changes: 12 additions & 0 deletions src/interfaces/INttToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.25;

interface INttToken {
// NOTE: the `mint` method is not present in the standard ERC20 interface.
function mint(address account, uint256 amount) external;

// NOTE: NttTokens in `burn` mode require the `burn` method to be present.
// This method is not present in the standard ERC20 interface, but is
// found in the `ERC20Burnable` interface.
function burn(uint256 amount) external;
}
78 changes: 78 additions & 0 deletions test/AvailWormhole.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.25;

import {AvailWormhole} from "src/AvailWormhole.sol";
import {TransparentUpgradeableProxy} from
"lib/openzeppelin-contracts/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {IAccessControl} from "lib/openzeppelin-contracts/contracts/access/IAccessControl.sol";
import {Vm, Test} from "forge-std/Test.sol";

contract AvailWormholeTest is Test {
AvailWormhole avail;
address owner;
address governance;
address minter;
bytes32 private constant MINTER_ROLE = keccak256("MINTER_ROLE");

function setUp() external {
governance = makeAddr("governance");
minter = makeAddr("minter");
address impl = address(new AvailWormhole());
avail = AvailWormhole(address(new TransparentUpgradeableProxy(impl, msg.sender, "")));
avail.initialize(governance);
vm.prank(governance);
avail.grantRole(MINTER_ROLE, minter);
}

function testRevert_initialize(address rand) external {
vm.expectRevert();
avail.initialize(rand);
}

function test_initialize() external view {
assertEq(avail.totalSupply(), 0);
assertNotEq(avail.owner(), address(0));
assertEq(avail.owner(), governance);
assertNotEq(avail.name(), "");
assertEq(avail.name(), "Avail (Wormhole)");
assertNotEq(avail.symbol(), "");
assertEq(avail.symbol(), "AVAIL.W");
}

function testRevertOnlyMinter_mint(address to, uint256 amount) external {
address rand = makeAddr("rand");
vm.assume(rand != minter);
vm.expectRevert(
abi.encodeWithSelector((IAccessControl.AccessControlUnauthorizedAccount.selector), rand, MINTER_ROLE)
);
vm.prank(rand);
avail.mint(to, amount);
}

function test_mint(address to, uint256 amount) external {
vm.assume(to != address(0));
vm.prank(minter);
avail.mint(to, amount);
assertEq(avail.balanceOf(to), amount);
}

function test_burn(address from, uint256 amount) external {
vm.assume(from != address(0));
vm.prank(minter);
avail.mint(from, amount);
assertEq(avail.balanceOf(from), amount);
vm.prank(from);
avail.burn(amount);
assertEq(avail.balanceOf(from), 0);
}

function test_burn2(address from, uint256 amount, uint256 amount2) external {
vm.assume(from != address(0) && amount2 < amount);
vm.prank(minter);
avail.mint(from, amount);
assertEq(avail.balanceOf(from), amount);
vm.prank(from);
avail.burn(amount2);
assertEq(avail.balanceOf(from), amount - amount2);
}
}
Loading