Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
10fda21
remove admin from proover contract
idirall22 Dec 1, 2021
e24efc4
convert the nearBridge contract to upgradable
idirall22 Dec 1, 2021
9eb0024
added rainbow bridge deployer config file
idirall22 Dec 1, 2021
0648675
update the cli to deploy eth client proxy
idirall22 Dec 1, 2021
bd235ee
added Makefile to run the bridge locally
idirall22 Dec 1, 2021
e26b43a
change the contract version to 0.8.7
idirall22 Dec 1, 2021
d13202f
added cmd to verify contracts on etherscan
idirall22 Dec 1, 2021
49efa84
fix makefile
idirall22 Dec 1, 2021
8d764e8
update the admin contract ACL
idirall22 Dec 3, 2021
2bc8f96
added admin control to the prover contract
idirall22 Dec 3, 2021
a7c7bdd
update the eth prover cli deployment to proxy
idirall22 Dec 3, 2021
ba451e7
fix review 1
idirall22 Dec 28, 2021
26492dc
[WIP] added trasnsferOwnership task and gas comparison
UrAvgDeveloper Apr 6, 2023
ee9745d
[WIP] task args camel_case -> lower_case
UrAvgDeveloper Apr 6, 2023
a34c5b0
added feature: contract upgrade through multi-sig
UrAvgDeveloper Apr 10, 2023
7c9fe5e
added trasnferOwnership, deployNearProver hardhat-task for near-prove…
UrAvgDeveloper Apr 11, 2023
ac355a6
Merge branch 'master' into upgradable_feature
UrAvgDeveloper Apr 12, 2023
047b400
Fixed existing tests and added new
UrAvgDeveloper Apr 12, 2023
c12c7f9
added readme.md and did some code cleanup
UrAvgDeveloper Apr 18, 2023
c42fa85
minor change in readme.md
UrAvgDeveloper Apr 18, 2023
8fb15a0
minor change in readme.md
UrAvgDeveloper Apr 18, 2023
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
103 changes: 103 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
help:
@echo ======================================Local dev=====================================
@echo 1 run "make init-yarn" install node packages.
@echo 2 run "make gen-contarcts" generate ethereum contracts.
@echo 3 run "make setup-clean-and-prepare" clean and prepare local env.
@echo 4 run "make start-local-near-and-ganache-nodes" start nearup and ganache.
@echo 5 run "make deploy-full-contracts" deploy near and eth contracts.
@echo 6 run "make start-relayer" start relayers.
@echo 7 run "make stop-all" stop relayers.
@echo
@echo ======================================Build Near Contrats=====================================
@echo "make eth-build-client" build eth client near contract.
@echo "make eth-build-prover" build eth prover near contract.
@echo
@echo ======================================Run Near Tests=====================================
@echo "make eth-test-client" run tests eth client
@echo "make eth-test-prover" run tests eth prover
@echo


# ===============================Init==============================

init-yarn:
yarn
yarn install

# ===============================Local==============================

# generate ether contracts
gen-contracts:
cd contracts/eth/nearbridge/ && yarn && yarn build
cd contracts/eth/nearprover/ && yarn && yarn build

setup-clean-and-prepare:
cli/index.js clean
cli/index.js prepare

# start near blockchain and connect with ganache.
start-local-near-and-ganache-nodes:
cli/index.js start near-node
cli/index.js start ganache

# ===============================Deploy contracts localy==============================

# deploy contracts
deploy-full-contracts:
cli/index.js init-near-contracts
cli/index.js init-eth-ed25519
cli/index.js init-eth-client
cli/index.js init-eth-prover
cli/index.js init-eth-erc20
cli/index.js init-eth-locker
cli/index.js init-near-token-factory

# Verify ethereum contract, example: make verify address=0x01...
verify:
cli/index.js verify-address ${address}

# ===============================Relayers==============================

# start relayers
start-relayer:
cli/index.js start eth2near-relay
cli/index.js start near2eth-relay
cli/index.js start bridge-watchdog
pm2 logs

# stop relayers
stop-all:
cli/index.js stop all

# ===============================Build NEAR Contracts==============================

# build eth near client
eth-build-client:
cd contracts/near/eth-client && sudo ./build.sh

# build eth near prover
eth-build-prover:
cd contracts/near/eth-prover && sudo ./build.sh

# ===============================Run tests==============================

# test eth near client
eth-test-client:
cd contracts/near/eth-client && ./test.sh

# test eth near prover
eth-test-prover:
cd contracts/near/eth-prover && ./test.sh

.PHONY: help \
init-yarn \
gen-contracts \
setup-clean-and-prepare \
start-local-near-and-ganache-nodes \
deploy-full-contracts \
start-relayer \
stop-all \
eth-build-client \
eth-build-prover \
eth-test-client \
eth-test-prover
13 changes: 12 additions & 1 deletion cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const {
InitEthErc20,
InitEthLocker,
InitEthClient,
InitEthProver
InitEthProver,
VerifyAddress
} = require('./init')

// Source dir or where rainbow cli is installed (when install with npm)
Expand Down Expand Up @@ -637,6 +638,16 @@ RainbowConfig.addOptions(
]
)

RainbowConfig.addOptions(
program
.command('verify-address <address>')
.description('Verify address on etherscan.'),
async (address, args) => {
await VerifyAddress.execute(address)
},
[]
)

RainbowConfig.addOptions(
program
.command('eth-to-near-find-proof <locked_event>')
Expand Down
136 changes: 42 additions & 94 deletions cli/init/eth-contracts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const BN = require('bn.js')
const fs = require('fs')
const { Web3, normalizeEthKey, sleep } = require('rainbow-bridge-utils')
const { Web3, normalizeEthKey, sleep, execAsync } = require('rainbow-bridge-utils')

const RETRY_SEND_TX = 15

Expand Down Expand Up @@ -181,112 +181,59 @@ class InitEthLocker {

class InitEthClient {
static async execute ({
ethNodeUrl,
ethMasterSk,
ethClientLockEthAmount,
ethClientLockDuration,
ethClientReplaceDuration,
ethEd25519Address,
ethClientArtifactPath,
ethAdminAddress,
ethGasMultiplier
ethEd25519Address
}) {
if (ethAdminAddress === '') {
const web3 = new Web3('')
ethAdminAddress = web3.eth.accounts.privateKeyToAccount(ethMasterSk)
.address
}

ethClientLockDuration = Number(ethClientLockDuration)
ethClientReplaceDuration = Number(ethClientReplaceDuration)
console.log('Start deploy ETH client proxy')

const cmd = `
cd ./contracts/eth/nearbridge && \\
npx hardhat deployNearBridgeProxy \\
--ed25519 ${ethEd25519Address} \\
--eth-client-lock-eth-amount ${ethClientLockEthAmount} \\
--eth-client-lock-duration ${ethClientLockDuration} \\
--eth-client-replace-duration ${ethClientReplaceDuration} \\
--paused-flags 0 \\
--config rainbowBridgeConfig.js \\
--network rainbowBridge
`
await execAsync(cmd)

console.log('ETH client proxy deployed!')
}
}

// replace duration should be at least twice as long as lock duration or 20 minutes longer
const minAllowedReplaceDuration = Math.min(
ethClientLockDuration + 20 * 60,
2 * ethClientLockDuration
)
class VerifyAddress {
static async execute (address) {
console.log(`Start verify contract address ${address}`)

if (ethClientReplaceDuration < minAllowedReplaceDuration) {
throw new Error(
`Invalid parameters ${JSON.stringify({
ethClientLockDuration,
ethClientReplaceDuration,
minAllowedReplaceDuration
})}`
)
}
const cmd = `cd ./contracts/eth/nearbridge && npx hardhat verify ${address} \\
--config rainbowBridgeConfig.js --network rainbowBridge
`
await execAsync(cmd)

const ethContractInitializer = new EthContractInitializer()
const web3 = new Web3(ethNodeUrl)
const lockEthAmount = web3.utils.toBN(ethClientLockEthAmount)
const lockDuration = web3.utils.toBN(ethClientLockDuration)
const replaceDuration = web3.utils
.toBN(ethClientReplaceDuration)
.mul(new web3.utils.BN(1e9))
try {
// Only WebSocket provider can close.
web3.currentProvider.connection.close()
} catch (e) {}
const success = await ethContractInitializer.execute(
{
args: [
ethEd25519Address,
lockEthAmount,
lockDuration,
replaceDuration,
ethAdminAddress,
0
],
gas: 5000000,
ethContractArtifactPath: ethClientArtifactPath,
ethNodeUrl,
ethMasterSk,
ethGasMultiplier
}
)
if (!success) {
console.log("Can't deploy", ethClientArtifactPath)
process.exit(1)
}
return {
ethClientAddress: success.ethContractAddress
}
console.log(`Contract address ${address} verified!`)
}
}

class InitEthProver {
static async execute ({
ethNodeUrl,
ethMasterSk,
ethClientAddress,
ethProverArtifactPath,
ethAdminAddress,
ethGasMultiplier
ethClientAddress
}) {
if (ethAdminAddress === '') {
const web3 = new Web3('')
ethAdminAddress = web3.eth.accounts.privateKeyToAccount(ethMasterSk)
.address
}

const ethContractInitializer = new EthContractInitializer()
const success = await ethContractInitializer.execute(
{
args: [ethClientAddress, ethAdminAddress, 0],
gas: 3000000,
ethContractArtifactPath: ethProverArtifactPath,
ethNodeUrl,
ethMasterSk,
ethGasMultiplier
}
)
if (!success) {
console.log("Can't deploy", ethProverArtifactPath)
process.exit(1)
}
return {
ethProverAddress: success.ethContractAddress
}
console.log('Start deploy ETH prover proxy')

const cmd = `
cd ./contracts/eth/nearprover && npx hardhat deployNearProverProxy \\
--eth-client-address ${ethClientAddress} \\
--paused-flags 0 \\
--config rainbowBridgeConfig.js \\
--network rainbowBridge
`
await execAsync(cmd)

console.log('ETH prover proxy deployed!')
}
}

Expand All @@ -295,3 +242,4 @@ exports.InitEthErc20 = InitEthErc20
exports.InitEthLocker = InitEthLocker
exports.InitEthClient = InitEthClient
exports.InitEthProver = InitEthProver
exports.VerifyAddress = VerifyAddress
4 changes: 3 additions & 1 deletion cli/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const {
InitEthEd25519,
InitEthErc20,
InitEthLocker,
InitEthProver
InitEthProver,
VerifyAddress
} = require('./eth-contracts')
const { InitNearContracts } = require('./near-contracts')
const { InitNearTokenFactory } = require('./near-token-factory')
Expand All @@ -15,3 +16,4 @@ exports.InitEthClient = InitEthClient
exports.InitEthProver = InitEthProver
exports.InitNearContracts = InitNearContracts
exports.InitNearTokenFactory = InitNearTokenFactory
exports.VerifyAddress = VerifyAddress
63 changes: 63 additions & 0 deletions contracts/eth/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# **_Near Bridge Upgradable_**
---

#### Near Bridge stores the block header from Near chain on ethereum side in trustless manner.

## To deploy Near-Bridge Upgradable:question:
* Setup `.env` file configurations as follows:-
* `DEFENDER_TEAM_API_KEY`:- Your's hardhat defender API key.
* `DEFENDER_TEAM_API_SECRET_KEY`: Your;s hardhat defender API secret key, obtained from defender account.
* `ETHERSCAN_API_KEY`:- Etherscan API key to verify the contract automatically after deployments.
* **NOTE:** While deploying the network node URL is picked up from `'home/.rainbow/config.json'` directory, therefore this config must be setup before executing deployments.
* For example: in config.json
* "`ethNodeUrl`" : "https://eth-goerli.g.alchemy.com/v2/RZeKGX8HziWu*******3GxgOMG1Zr2Yb",
* "`ethMasterSk`" : "0xac0974bec39a17e36ba4a6b*******************************d7bf4f2ff80"
* run this command after all above setup:-
* ```npx hardhat deployNearBridgeProxy --ethclientlockethamount <lock_eth_amount> --ethclientlockduration <lock_duration_in_seconds> --ethclientreplaceduration <replace_duration_in_nanoseconds> --ed25519 <ed25519_address> --pausedflags <pausedFlag (0 or 1)> --upgrader <gnosis_safe_address> --config rainbowBridgeConfig.js --network <network_name>```

## To transfer ownership of Near-Bridge Upgradable:question:
* **Note:**
* Current owner is the address who initially deployed the upgradable contract.
* Only present owner of bridge can transfer their ownership to new owner. ie. `ethMasterSk` in `'home/.rainbow/config.json'` must be the pvt key of present owner.
* To transfer ownership run the following command:-
* ```npx hardhat transferOwnership --currentadmin <present_owner_address> --newadmin <new_owner_address> --bridgeaddress <bridge_proxy_address> --config rainbowBridgeConfig.js --network <network_name>```

## To upgrade Near-Bridge-Upgradable from Multi-Sig safe:question:

* **Note:** Near-bridge can be upgraded by only multi-sig safe address provided as `upgrader` while deployment.
* A proposal is created for upgradation through hardhat-defender where owners of safe can pass by signing threshold txn.
* All m of n owner's in multi-sig must be present to sign txn from hardhat defender to upgrade the contract.
* txn can be signed from defender after running the below command.
* new implementation contract must be present in `./nearbridge/contracts` directory before running the command.
* Run this command to upgrade the Near_Bridge upgradable contract:-
* ```npx hardhat proposeUpgrade --proxyaddress <near_bridge_proxy_address> --newcontractname <new_implementation_contract_name> --upgrader <multi_sig_address> --config rainbowBridgeConfig.js --network <network_name>```
---
# **_Near Prover Upgradable_**

#### Near Prover checks the integrity of Near blocks in Near bridge.

## To deploy Near-Bridge Upgradable❓

* Setup .env file configurations same as for Near bridge.
* Run this command after setup:-
* ```npx hardhat deployNearProverProxy --ethclientaddress <near-bridge-eth-address> --pausedflags <paused Flag> --upgrader <multi-sig safe address>```

## To transfer ownership of Near-Prover Upgradable:question:
* **Note:**
* Current owner is the address who initially deployed the upgradable contract.
* Only present owner of near prover can transfer their ownership to new owner. ie. `ethMasterSk` in `'home/.rainbow/config.json'` must be the pvt key of present owner.
* To transfer ownership run the following command:-
* ```npx hardhat transferOwnership --currentadmin <present_owner_address> --newadmin <new_owner_address> --proveraddress <near_prover_proxy_address> --config rainbowBridgeConfig.js --network <network_name>```

## To upgrade Near-Prover-Upgradable from Multi-Sig safe:question:

* **Note:** Near-prover can be upgraded by only multi-sig safe address provided as `upgrader` while deployment.
* A proposal is created for upgradation through hardhat-defender where owners of safe can pass by signing threshold txn.
* All m of n owner's in multi-sig must be present to sign txn from hardhat defender to upgrade the contract.
* txn can be signed from defender after running the below command.
* new implementation contract must be present in `./nearprover/contracts` directory before running the command.
* Run this command to upgrade the Near_Prover upgradable contract:-
* ```npx hardhat proposeUpgrade --proxyaddress <near_prover_proxy_address> --newcontractname <new_implementation_contract_name> --upgrader <multi_sig_address> --config rainbowBridgeConfig.js --network <network_name>```
---


3 changes: 3 additions & 0 deletions contracts/eth/nearbridge/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DEFENDER_TEAM_API_KEY=
DEFENDER_TEAM_API_SECRET_KEY=
ETHERSCAN_API_KEY=
Loading