Skip to content

Commit

Permalink
Merge branch 'main' into deployment-test
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuba authored Apr 26, 2024
2 parents 5b9aefd + b9f9936 commit 86e3258
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/solidity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ jobs:

- name: Deploy contracts
env:
ACCOUNTS_PRIVATE_KEYS: ${{ secrets.TESTNET_ETH_CONTRACT_OWNER_PRIVATE_KEY }}
CHAIN_API_URL: ${{ secrets.SEPOLIA_CHAIN_API_URL }}
SEPOLIA_PRIVATE_KEY: ${{ secrets.TESTNET_ETH_CONTRACT_OWNER_PRIVATE_KEY }}
SEPOLIA_RPC_URL: ${{ secrets.SEPOLIA_CHAIN_API_URL }}
ETHERSCAN_API_KEY: ${{ secrets.ETHERSCAN_API_KEY }}
run: |
pnpm run deploy --network ${{ github.event.inputs.environment }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ tmp/

# Environment configuration files
.envrc
.env
.env.*
!.env.example
!.env.ci.example
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 16 additions & 9 deletions sdk/src/lib/ethereum/bitcoin-depositor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,28 @@ class EthereumBitcoinDepositor
}

const bridgeAddress = await this.instance.bridge()
const bridge = new Contract(bridgeAddress, [
"function depositsParameters()",
])
const depositsParameters =
(await bridge.depositsParameters()) as TbtcDepositParameters
const bridge = new Contract(
bridgeAddress,
[
"function depositParameters() view returns (uint64 depositDustThreshold, uint64 depositTreasuryFeeDivisor, uint64 depositTxMaxFee, uint32 depositRevealAheadPeriod)",
],
this.instance.runner,
)
const { depositTreasuryFeeDivisor, depositTxMaxFee } =
(await bridge.depositParameters()) as TbtcDepositParameters

const vaultAddress = await this.getTbtcVaultChainIdentifier()
const vault = new Contract(`0x${vaultAddress.identifierHex}`, [
"function optimisticMintingFeeDivisor()",
])
const vault = new Contract(
`0x${vaultAddress.identifierHex}`,
["function optimisticMintingFeeDivisor() view returns (uint32)"],
this.instance.runner,
)
const optimisticMintingFeeDivisor =
(await vault.optimisticMintingFeeDivisor()) as bigint

this.#cache.tbtcBridgeMintingParameters = {
...depositsParameters,
depositTreasuryFeeDivisor,
depositTxMaxFee,
optimisticMintingFeeDivisor,
}
return this.#cache.tbtcBridgeMintingParameters
Expand Down
24 changes: 15 additions & 9 deletions sdk/test/lib/ethereum/tbtc-depositor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const testData = {
describe("BitcoinDepositor", () => {
const spyOnEthersDataSlice = jest.spyOn(ethers, "dataSlice")
const spyOnEthersContract = jest.spyOn(ethers, "Contract")
const signer = {} as EthereumSigner

const vaultAddress = EthereumAddress.from(
ethers.Wallet.createRandom().address,
Expand All @@ -45,6 +46,7 @@ describe("BitcoinDepositor", () => {
.fn()
.mockResolvedValue(testData.depositorFeeDivisor),
minDepositAmount: jest.fn().mockImplementation(() => minDepositAmount),
runner: signer,
}

let depositor: EthereumBitcoinDepositor
Expand All @@ -62,7 +64,7 @@ describe("BitcoinDepositor", () => {

depositor = new EthereumBitcoinDepositor(
{
signer: {} as EthereumSigner,
signer,
address: depositorAddress.identifierHex,
},
"sepolia",
Expand Down Expand Up @@ -268,9 +270,9 @@ describe("BitcoinDepositor", () => {
)
})

describe("estimateDepositFees", () => {
describe("calculateDepositFee", () => {
const mockedBridgeContractInstance = {
depositsParameters: jest
depositParameters: jest
.fn()
.mockResolvedValue(testData.depositParameters),
}
Expand Down Expand Up @@ -337,25 +339,29 @@ describe("BitcoinDepositor", () => {
expect(Contract).toHaveBeenNthCalledWith(
1,
`0x${bridgeAddress.identifierHex}`,
["function depositsParameters()"],
[
"function depositParameters() view returns (uint64 depositDustThreshold, uint64 depositTreasuryFeeDivisor, uint64 depositTxMaxFee, uint32 depositRevealAheadPeriod)",
],
mockedContractInstance.runner,
)
})

it("should get the deposit parameters from chain", () => {
expect(
mockedBridgeContractInstance.depositsParameters,
mockedBridgeContractInstance.depositParameters,
).toHaveBeenCalled()
})

it("should get the vault contract address", () => {
expect(mockedContractInstance.tbtcVault).toHaveBeenCalled()
})

it("should create the ethers Contract instance of the Bridge contract", () => {
it("should create the ethers Contract instance of the tBTC Vault contract", () => {
expect(Contract).toHaveBeenNthCalledWith(
2,
`0x${vaultAddress.identifierHex}`,
["function optimisticMintingFeeDivisor()"],
["function optimisticMintingFeeDivisor() view returns (uint32)"],
mockedContractInstance.runner,
)
})

Expand All @@ -381,7 +387,7 @@ describe("BitcoinDepositor", () => {
mockedContractInstance.bridge.mockClear()
mockedContractInstance.tbtcVault.mockClear()
mockedContractInstance.depositorFeeDivisor.mockClear()
mockedBridgeContractInstance.depositsParameters.mockClear()
mockedBridgeContractInstance.depositParameters.mockClear()
mockedVaultContractInstance.optimisticMintingFeeDivisor.mockClear()

result2 = await depositor.calculateDepositFee(amountToStake)
Expand All @@ -390,7 +396,7 @@ describe("BitcoinDepositor", () => {
it("should get the deposit parameters from cache", () => {
expect(mockedContractInstance.bridge).toHaveBeenCalledTimes(0)
expect(
mockedBridgeContractInstance.depositsParameters,
mockedBridgeContractInstance.depositParameters,
).toHaveBeenCalledTimes(0)
})

Expand Down
4 changes: 0 additions & 4 deletions solidity/.env

This file was deleted.

1 change: 1 addition & 0 deletions solidity/.env.ci.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

9 changes: 7 additions & 2 deletions solidity/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
CHAIN_API_URL=
ACCOUNTS_PRIVATE_KEYS=
MAINNET_RPC_URL=
MAINNET_PRIVATE_KEY=

SEPOLIA_RPC_URL=
SEPOLIA_PRIVATE_KEY=

ETHERSCAN_API_KEY=

COINMARKETCAP_API_KEY=
6 changes: 4 additions & 2 deletions solidity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ $ pnpm test

To run the integration tests follow these steps:

- Run the Hardhat Node locally forking Mainnet at block `19680873`:
- Define `MAINNET_RPC_URL` environment variable pointing to Ethereum Mainnet RPC URL.

- Run the Hardhat Node locally forking Mainnet:

```
$ npx hardhat node --no-deploy --fork https://eth-mainnet.g.alchemy.com/v2/<key> --fork-block-number 19680873
$ pnpm run node:forking
```

- Once the local node is running you can execute the integration tests:
Expand Down
46 changes: 35 additions & 11 deletions solidity/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ import "hardhat-contract-sizer"
import "hardhat-deploy"
import "solidity-docgen"
import "@keep-network/hardhat-helpers"
import dotenv from "dotenv-safer"

dotenv.config({
allowEmptyValues: true,
example: process.env.CI ? ".env.ci.example" : ".env.example",
})

const MAINNET_RPC_URL = process.env.MAINNET_RPC_URL ?? ""

const MAINNET_PRIVATE_KEY = process.env.MAINNET_PRIVATE_KEY
? [process.env.MAINNET_PRIVATE_KEY]
: []

const SEPOLIA_RPC_URL = process.env.SEPOLIA_RPC_URL ?? ""

const SEPOLIA_PRIVATE_KEY = process.env.SEPOLIA_PRIVATE_KEY
? [process.env.SEPOLIA_PRIVATE_KEY]
: []

const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY ?? ""

const COINMARKETCAP_API_KEY = process.env.COINMARKETCAP_API_KEY ?? ""

const config: HardhatUserConfig = {
solidity: {
Expand All @@ -26,23 +48,25 @@ const config: HardhatUserConfig = {
},

networks: {
hardhat: {
forking:
process.env.FORKING === "true"
? { url: MAINNET_RPC_URL, blockNumber: 19680873 }
: undefined,
},
integration: {
url: "http://localhost:8545",
},
sepolia: {
url: process.env.CHAIN_API_URL || "",
url: SEPOLIA_RPC_URL,
chainId: 11155111,
accounts: process.env.ACCOUNTS_PRIVATE_KEYS
? process.env.ACCOUNTS_PRIVATE_KEYS.split(",")
: undefined,
accounts: SEPOLIA_PRIVATE_KEY,
tags: ["etherscan"],
},
mainnet: {
url: process.env.CHAIN_API_URL || "",
url: MAINNET_RPC_URL,
chainId: 1,
accounts: process.env.ACCOUNTS_PRIVATE_KEYS
? process.env.ACCOUNTS_PRIVATE_KEYS.split(",")
: undefined,
accounts: MAINNET_PRIVATE_KEY,
tags: ["etherscan"],
},
},
Expand All @@ -57,8 +81,8 @@ const config: HardhatUserConfig = {

etherscan: {
apiKey: {
sepolia: process.env.ETHERSCAN_API_KEY,
mainnet: process.env.ETHERSCAN_API_KEY,
sepolia: ETHERSCAN_API_KEY,
mainnet: ETHERSCAN_API_KEY,
},
},

Expand Down Expand Up @@ -102,7 +126,7 @@ const config: HardhatUserConfig = {

gasReporter: {
enabled: true,
coinmarketcap: process.env.COINMARKETCAP_API_KEY,
coinmarketcap: COINMARKETCAP_API_KEY,
},

typechain: {
Expand Down
11 changes: 7 additions & 4 deletions solidity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
],
"scripts": {
"clean": "hardhat clean && rm -rf cache/ export/ gen/ export.json",
"build": "hardhat compile",
"deploy": "hardhat deploy --export export.json",
"docs": "hardhat docgen",
"prepare:env": "cp -n .env.example .env || true",
"build": "npm run prepare:env && hardhat compile",
"deploy": "npm run prepare:env && hardhat deploy --export export.json",
"docs": "npm run prepare:env && hardhat docgen",
"format": "npm run lint:js && npm run lint:sol && npm run lint:config",
"format:fix": "npm run lint:js:fix && npm run lint:sol:fix && npm run lint:config:fix",
"lint:js": "eslint .",
Expand All @@ -26,7 +27,8 @@
"lint:sol:fix": "solhint 'contracts/**/*.sol' --fix && prettier --write 'contracts/**/*.sol'",
"lint:config": "prettier --check '**/*.@(json)'",
"lint:config:fix": "prettier --write '**/*.@(json)'",
"test": "hardhat test ./test/*.test.ts",
"node:forking": "FORKING=true hardhat node --no-deploy",
"test": "npm run prepare:env && hardhat test ./test/*.test.ts",
"test:integration": "hardhat test --deploy-fixture ./test/integration/*.test.ts --network integration"
},
"devDependencies": {
Expand All @@ -45,6 +47,7 @@
"@types/mocha": "^10.0.6",
"@types/node": "^20.9.4",
"chai": "^4.3.10",
"dotenv-safer": "^1.0.0",
"eslint": "^8.54.0",
"ethers": "^6.8.1",
"hardhat": "^2.19.1",
Expand Down
4 changes: 4 additions & 0 deletions solidity/types/dotenv-safer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "dotenv-safer" {
// eslint-disable-next-line import/prefer-default-export
export function config(options = {})
}

0 comments on commit 86e3258

Please sign in to comment.