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

NEP141 connector: remove proof consumer #240

Merged
merged 1 commit into from
May 16, 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
45 changes: 21 additions & 24 deletions erc20-bridge-token/contracts/BridgeTokenFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import "rainbow-bridge-sol/nearprover/contracts/INearProver.sol";
import "rainbow-bridge-sol/nearprover/contracts/ProofDecoder.sol";
import "rainbow-bridge-sol/nearbridge/contracts/Borsh.sol";

import "./IProofConsumer.sol";
import "./ProofConsumer.sol";
import "./BridgeToken.sol";
import "./ResultsDecoder.sol";
import "./SelectivePausableUpgradable.sol";

contract BridgeTokenFactory is
UUPSUpgradeable,
AccessControlUpgradeable,
SelectivePausableUpgradable
SelectivePausableUpgradable,
ProofConsumer
{
using Borsh for Borsh.Data;
using SafeERC20 for IERC20;
Expand All @@ -39,7 +40,6 @@ contract BridgeTokenFactory is
mapping(bytes => bool) private _whitelistedAccounts;
bool private _isWhitelistModeEnabled;

address public proofConsumerAddress;
address public tokenImplementationAddress;

bytes32 public constant PAUSABLE_ADMIN_ROLE = keccak256("PAUSABLE_ADMIN_ROLE");
Expand All @@ -63,14 +63,17 @@ contract BridgeTokenFactory is
// BridgeTokenFactory is linked to the bridge token factory on NEAR side.
// It also links to the prover that it uses to unlock the tokens.
function initialize(
address _proofConsumerAddress,
address _tokenImplementationAddress
address _tokenImplementationAddress,
bytes memory _nearTokenLocker,
INearProver _prover,
uint64 _minBlockAcceptanceHeight
) external initializer {
require(
_proofConsumerAddress != address(0),
"Proof consumer should not be zero address"
);
proofConsumerAddress = _proofConsumerAddress;
require(_nearTokenLocker.length > 0, "Invalid Near Token Locker address");
require(address(_prover) != address(0), "Invalid Near prover address");

nearTokenLocker = _nearTokenLocker;
prover = _prover;
minBlockAcceptanceHeight = _minBlockAcceptanceHeight;
tokenImplementationAddress = _tokenImplementationAddress;

__UUPSUpgradeable_init();
Expand Down Expand Up @@ -101,8 +104,10 @@ contract BridgeTokenFactory is
) external returns (address) {
require(!_isBridgeToken[_nearToEthToken[nearTokenId]], "ERR_TOKEN_EXIST");

ProofDecoder.ExecutionStatus memory status = IProofConsumer(proofConsumerAddress)
.parseAndConsumeProof(proofData, proofBlockHeight);
ProofDecoder.ExecutionStatus memory status = _parseAndConsumeProof(
proofData,
proofBlockHeight
);
ResultsDecoder.MetadataResult memory result = ResultsDecoder.decodeMetadataResult(
status.successValue
);
Expand Down Expand Up @@ -146,8 +151,10 @@ contract BridgeTokenFactory is
bytes memory proofData,
uint64 proofBlockHeight
) external whenNotPaused(PAUSED_DEPOSIT) {
ProofDecoder.ExecutionStatus memory status = IProofConsumer(proofConsumerAddress)
.parseAndConsumeProof(proofData, proofBlockHeight);
ProofDecoder.ExecutionStatus memory status = _parseAndConsumeProof(
proofData,
proofBlockHeight
);
ResultsDecoder.LockResult memory result = ResultsDecoder.decodeLockResult(
status.successValue
);
Expand Down Expand Up @@ -214,16 +221,6 @@ contract BridgeTokenFactory is
proxy.upgradeToAndCall(implementation, bytes(""));
}

function setProofConsumer(
address newProofConsumerAddress
) external onlyRole(DEFAULT_ADMIN_ROLE) {
require(
newProofConsumerAddress != address(0),
"Proof consumer should not be zero address"
);
proofConsumerAddress = newProofConsumerAddress;
}

function enableWhitelistMode() external onlyRole(DEFAULT_ADMIN_ROLE) {
_isWhitelistModeEnabled = true;
}
Expand Down
11 changes: 0 additions & 11 deletions erc20-bridge-token/contracts/IProofConsumer.sol

This file was deleted.

26 changes: 0 additions & 26 deletions erc20-bridge-token/contracts/Migrations.sol

This file was deleted.

23 changes: 5 additions & 18 deletions erc20-bridge-token/contracts/ProofConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import "rainbow-bridge-sol/nearprover/contracts/INearProver.sol";
import "rainbow-bridge-sol/nearprover/contracts/ProofDecoder.sol";
import "rainbow-bridge-sol/nearbridge/contracts/Borsh.sol";

import "./IProofConsumer.sol";

contract ProofConsumer is Ownable, IProofConsumer {
contract ProofConsumer {
using Borsh for Borsh.Data;
using ProofDecoder for Borsh.Data;

Expand All @@ -23,25 +21,12 @@ contract ProofConsumer is Ownable, IProofConsumer {
// OutcomeReciptId -> Used
mapping(bytes32 => bool) public usedProofs;

constructor(
bytes memory _nearTokenLocker,
INearProver _prover,
uint64 _minBlockAcceptanceHeight
) Ownable(msg.sender) {
require(_nearTokenLocker.length > 0, "Invalid Near Token Locker address");
require(address(_prover) != address(0), "Invalid Near prover address");

nearTokenLocker = _nearTokenLocker;
prover = _prover;
minBlockAcceptanceHeight = _minBlockAcceptanceHeight;
}

/// Parses the provided proof and consumes it if it's not already used.
/// The consumed event cannot be reused for future calls.
function parseAndConsumeProof(
function _parseAndConsumeProof(
bytes memory proofData,
uint64 proofBlockHeight
) external override onlyOwner returns (ProofDecoder.ExecutionStatus memory result) {
) internal returns (ProofDecoder.ExecutionStatus memory result) {
require(
prover.proveOutcome(proofData, proofBlockHeight),
"Proof should be valid"
Expand Down Expand Up @@ -84,4 +69,6 @@ contract ProofConsumer is Ownable, IProofConsumer {
"Can't use unknown execution outcome for unlocking the tokens or set metadata"
);
}

uint256[50] private __gap;
kiseln marked this conversation as resolved.
Show resolved Hide resolved
}
13 changes: 3 additions & 10 deletions erc20-bridge-token/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
/**
* @type import('hardhat/config').HardhatUserConfig
*/
require("@nomiclabs/hardhat-waffle");
require('hardhat-contract-sizer');
require('@openzeppelin/hardhat-upgrades')
require('solidity-coverage')
require("@nomiclabs/hardhat-etherscan");

require('dotenv').config();
require("dotenv").config();
require("@openzeppelin/hardhat-upgrades");
require("@nomicfoundation/hardhat-verify");

const ALCHEMY_API_KEY = process.env.ALCHEMY_API_KEY;
const INFURA_API_KEY = process.env.INFURA_API_KEY;
Expand Down
11 changes: 5 additions & 6 deletions erc20-bridge-token/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
"@openzeppelin/contracts": "^5.0.2",
"@openzeppelin/contracts-upgradeable": "^5.0.2",
"dotenv": "^16.0.1",
"hardhat-contract-sizer": "^2.5.1",
"rainbow-bridge-eth2near-block-relay": "https://gitpkg.now.sh/aurora-is-near/rainbow-bridge/eth2near/eth2near-block-relay?327ff937bda3e3a99386fe488994f3c8e35f8aa0",
"rainbow-bridge-lib": "https://gitpkg.now.sh/aurora-is-near/rainbow-bridge/utils?327ff937bda3e3a99386fe488994f3c8e35f8aa0",
"rainbow-bridge-sol": "https://gitpkg.now.sh/aurora-is-near/rainbow-bridge/contracts/eth?327ff937bda3e3a99386fe488994f3c8e35f8aa0",
"rainbow-bridge-utils": "https://gitpkg.now.sh/aurora-is-near/rainbow-bridge/utils?327ff937bda3e3a99386fe488994f3c8e35f8aa0"
},
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.1.0",
"@nomicfoundation/hardhat-ethers": "^3.0.6",
"@nomiclabs/hardhat-etherscan": "^3.1.0",
"@nomiclabs/hardhat-waffle": "^2.0.3",
"@openzeppelin/hardhat-upgrades": "^1.17.0",
"@nomicfoundation/hardhat-verify": "^2.0.7",
"@openzeppelin/hardhat-upgrades": "^3.1.0",
"@openzeppelin/test-helpers": "^0.5.5",
"chai": "^4.3.6",
"coveralls": "^3.1.0",
Expand All @@ -30,8 +29,8 @@
"eslint-plugin-standard": "^4.0.0",
"eth-gas-reporter": "^0.2.17",
"ethereum-waffle": "^3.4.4",
"ethers": "^5.6.6",
"hardhat": "^2.10.1",
"ethers": "^6.12.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to updates the tests as well according to the new ethers version. It's a lot of code but should be straightforward

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, let's fix it in the follow-up PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rather do it here but up to you

"hardhat": "^2.22.4",
"prettier": "^3.2.5",
"prettier-plugin-solidity": "^1.3.1",
"solc": "^0.6",
Expand Down
49 changes: 32 additions & 17 deletions erc20-bridge-token/scripts/deploy-factory.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
require('dotenv').config();
require("dotenv").config();
const { ethers, upgrades } = require("hardhat");

const PROOF_CONSUMER_ADDRESS = process.env.PROOF_CONSUMER_ADDRESS;
const BRIDGE_TOKEN_IMPL_ADDRESS = process.env.BRIDGE_TOKEN_IMPL_ADDRESS;
const NEAR_TOKEN_LOCKER = Buffer.from(process.env.NEAR_TOKEN_LOCKER, "utf-8");
const PROVER_ADDRESS = process.env.PROVER_ADDRESS;
const MIN_BLOCK_ACCEPTANCE_HEIGHT = parseInt(
process.env.MIN_BLOCK_ACCEPTANCE_HEIGHT,
);

async function main() {
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with the account:", deployer.address);
console.log("Account balance:", (await deployer.getBalance()).toString());
console.log("ProofConsumer address: ", PROOF_CONSUMER_ADDRESS);
const accountBalance = await deployer.provider.getBalance(deployer.address);
console.log("Account balance:", accountBalance.toString());
console.log("Bridge token impl address: ", BRIDGE_TOKEN_IMPL_ADDRESS);
console.log("Prover address:", PROVER_ADDRESS);
console.log("Near token locker:", NEAR_TOKEN_LOCKER.toString());
console.log("Min block acceptance height:", MIN_BLOCK_ACCEPTANCE_HEIGHT);

const BridgeTokenFactoryContract = await ethers.getContractFactory("BridgeTokenFactory");
const BridgeTokenFactoryContract =
await ethers.getContractFactory("BridgeTokenFactory");
const BridgeTokenFactory = await upgrades.deployProxy(
BridgeTokenFactoryContract,
[PROOF_CONSUMER_ADDRESS, BRIDGE_TOKEN_IMPL_ADDRESS],
[
BRIDGE_TOKEN_IMPL_ADDRESS,
NEAR_TOKEN_LOCKER,
PROVER_ADDRESS,
MIN_BLOCK_ACCEPTANCE_HEIGHT,
],
{
initializer: 'initialize',
timeout: 0
}
initializer: "initialize",
timeout: 0,
},
);
await BridgeTokenFactory.waitForDeployment();
console.log(
`BridgeTokenFactory deployed at ${await BridgeTokenFactory.getAddress()}`,
);
console.log(
"Implementation address:",
await upgrades.erc1967.getImplementationAddress(
await BridgeTokenFactory.getAddress(),
),
);
await BridgeTokenFactory.deployed();
console.log(`BridgeTokenFactory deployed at ${BridgeTokenFactory.address}`);
console.log("Implementation address:", await upgrades.erc1967.getImplementationAddress(BridgeTokenFactory.address));

console.log("Transfer ProofConsumer's ownership to: ", BridgeTokenFactory.address);
const ProofConsumerContract = await ethers.getContractFactory("ProofConsumer");
const proofConsumer = ProofConsumerContract.attach(PROOF_CONSUMER_ADDRESS);
await proofConsumer.transferOwnership(BridgeTokenFactory.address);
}

main()
Expand Down
28 changes: 0 additions & 28 deletions erc20-bridge-token/scripts/deploy-proof-consumer.js

This file was deleted.

12 changes: 7 additions & 5 deletions erc20-bridge-token/scripts/deploy-token-impl.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
require('dotenv').config();
require("dotenv").config();
const { ethers } = require("hardhat");

async function main() {
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with the account:", deployer.address);
console.log("Account balance:", (await deployer.getBalance()).toString());
const accountBalance = await deployer.provider.getBalance(deployer.address);
console.log("Account balance:", accountBalance.toString());

const BridgeTokenContractFactory = await ethers.getContractFactory("BridgeToken");
const BridgeTokenContractFactory =
await ethers.getContractFactory("BridgeToken");
const BridgeTokenContract = await BridgeTokenContractFactory.deploy();
await BridgeTokenContract.deployed();
console.log(`BridgeTokenContract deployed at ${BridgeTokenContract.address}`);
await BridgeTokenContract.waitForDeployment();
console.log(`BridgeTokenContract deployed at ${await BridgeTokenContract.getAddress()}`);
}

main()
Expand Down
Loading
Loading