Skip to content

Commit

Permalink
Minting roundtrip system tests scenario (#497)
Browse files Browse the repository at this point in the history
Here we add a new system test scenario that leverages the TBTCVault contract.
The scenario consists of several steps:

1. Initiate minting of TBTC tokens by creating a deposit pointing to the
   TBTCVault
2. Sweep the deposit on the BTC side and submit the SPV proof of the sweep to
   the Bridge
3. Unmint the minted TBTC tokens and request redemption using the token's
   approve-and-call mechanism
4. Redeem on the BTC side and submit the SPV proof of redemption to the Bridge

Apart from that, this PR:

- Bumps the tbtc-v2.ts dependency to pull recent changes and adjusts the
  existing system tests code accordingly
- Adds the new minting scenario to the existing system tests CI workflow
  • Loading branch information
pdyraga authored Jan 25, 2023
2 parents 44998d5 + eb86680 commit e95ab1f
Show file tree
Hide file tree
Showing 7 changed files with 1,122 additions and 365 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/system-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,59 @@ jobs:
DEPOSITOR_BITCOIN_WIF: ${{ secrets.SYSTEM_TESTS_DEPOSITOR_PRIVATE_KEY_WIF }}
run: |
yarn test ./test/deposit-redemption.test.ts --network $ETH_NETWORK
system-tests-minting-unminting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-node@v3
with:
node-version: "14.x"
cache: "yarn"
cache-dependency-path: system-tests/yarn.lock

- name: Configure git to not use unauthenticated protocol
run: git config --global url."https://".insteadOf git://

- name: Install solidity dependencies
working-directory: ./solidity
run: yarn install

- name: Run ethereum node
working-directory: ./solidity
run: |
npx hardhat node --no-deploy &
HH_NODE_PID=$!
while ! lsof -n -Fn -p $HH_NODE_PID | grep -q '^n.*:8545$'; do sleep 10; done
- name: Deploy contracts
working-directory: ./solidity
env:
ETH_NETWORK: system_tests
run: |
USE_EXTERNAL_DEPLOY=true npx hardhat deploy --network $ETH_NETWORK --export export.json
- name: Setup scenario prerequisites
working-directory: ./solidity
env:
WALLET_PUBLIC_KEY: ${{ secrets.SYSTEM_TESTS_WALLET_PUBLIC_KEY }}
ETH_NETWORK: system_tests
run: |
npx hardhat test-utils:register-operators --network $ETH_NETWORK
npx hardhat test-utils:create-wallet --wallet-public-key $WALLET_PUBLIC_KEY --network $ETH_NETWORK
- name: Install system tests dependencies
working-directory: ./system-tests
run: yarn install

- name: Run scenario
working-directory: ./system-tests
env:
ETH_NETWORK: system_tests
ELECTRUM_URL: ${{ secrets.SYSTEM_TESTS_ELECTRUM_URL }}
CONTRACTS_DEPLOYMENT_EXPORT_FILE_PATH: ../solidity/export.json
WALLET_BITCOIN_WIF: ${{ secrets.SYSTEM_TESTS_WALLET_PRIVATE_KEY_WIF }}
DEPOSITOR_BITCOIN_WIF: ${{ secrets.SYSTEM_TESTS_DEPOSITOR_PRIVATE_KEY_WIF }}
run: |
yarn test ./test/minting-unminting.test.ts --network $ETH_NETWORK
44 changes: 25 additions & 19 deletions system-tests/test/deposit-redemption.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@ import {
SpvMaintainer,
ElectrumClient,
EthereumBridge,
} from "@keep-network/tbtc-v2.ts"
import {
computeHash160,
TransactionHash,
} from "@keep-network/tbtc-v2.ts/dist/bitcoin"
BitcoinTransactionHash,
} from "@keep-network/tbtc-v2.ts/dist/src"
import { computeHash160 } from "@keep-network/tbtc-v2.ts/dist/src/bitcoin"
import { BigNumber, constants, Contract } from "ethers"
import chai, { expect } from "chai"
import { submitDepositTransaction } from "@keep-network/tbtc-v2.ts/dist/deposit"
import { submitDepositSweepTransaction } from "@keep-network/tbtc-v2.ts/dist/deposit-sweep"
import { submitRedemptionTransaction } from "@keep-network/tbtc-v2.ts/dist/redemption"
import { submitDepositTransaction } from "@keep-network/tbtc-v2.ts/dist/src/deposit"
import { submitDepositSweepTransaction } from "@keep-network/tbtc-v2.ts/dist/src/deposit-sweep"
import { submitRedemptionTransaction } from "@keep-network/tbtc-v2.ts/dist/src/redemption"
import chaiAsPromised from "chai-as-promised"

import { setupSystemTestsContext } from "./utils/context"
import { generateDeposit } from "./utils/deposit"
import { fakeRelayDifficulty, waitTransactionConfirmed } from "./utils/bitcoin"

import type { UnspentTransactionOutput } from "@keep-network/tbtc-v2.ts/dist/bitcoin"
import type { UnspentTransactionOutput } from "@keep-network/tbtc-v2.ts/dist/src/bitcoin"
import type { SystemTestsContext } from "./utils/context"
import type { RedemptionRequest } from "@keep-network/tbtc-v2.ts/dist/redemption"
import type { Deposit } from "@keep-network/tbtc-v2.ts/dist/deposit"
import type { RedemptionRequest } from "@keep-network/tbtc-v2.ts/dist/src/redemption"
import type { Deposit } from "@keep-network/tbtc-v2.ts/dist/src/deposit"

chai.use(chaiAsPromised)

Expand Down Expand Up @@ -72,12 +70,12 @@ describe("System Test - Deposit and redemption", () => {

maintainerBridgeHandle = new EthereumBridge({
address: bridgeAddress,
signer: maintainer,
signerOrProvider: maintainer,
})

depositorBridgeHandle = new EthereumBridge({
address: bridgeAddress,
signer: depositor,
signerOrProvider: depositor,
})

const bankDeploymentInfo = deployedContracts.Bank
Expand Down Expand Up @@ -188,7 +186,7 @@ describe("System Test - Deposit and redemption", () => {
// This is the first sweep of the given wallet so there is no main UTXO.
{
// The function expects an unprefixed hash.
transactionHash: TransactionHash.from(constants.HashZero),
transactionHash: BitcoinTransactionHash.from(constants.HashZero),
outputIndex: 0,
value: BigNumber.from(0),
},
Expand Down Expand Up @@ -274,6 +272,16 @@ describe("System Test - Deposit and redemption", () => {
)
})

it("should transfer depositor's bank balance to the Bridge", async () => {
expect(
await bank.balanceOf(systemTestsContext.depositor.address)
).to.be.equal(0)

expect(await bank.balanceOf(bridgeAddress)).to.be.equal(
requestedAmount
)
})

it("should register the redemption request on the bridge", async () => {
expect(redemptionRequest.requestedAt).to.be.greaterThan(0)
expect(redemptionRequest.requestedAmount).to.be.equal(requestedAmount)
Expand All @@ -285,7 +293,7 @@ describe("System Test - Deposit and redemption", () => {
context(
"when redemption is made and redemption proof submitted",
() => {
let redemptionTxHash: TransactionHash
let redemptionTxHash: BitcoinTransactionHash

before(
"make the redemption and submit redemption proof",
Expand Down Expand Up @@ -345,10 +353,8 @@ describe("System Test - Deposit and redemption", () => {
)
})

it("should decrease depositor's balance in the bank", async () => {
const actualBalance = await bank.balanceOf(
systemTestsContext.depositor.address
)
it("should decrease Bridge's balance in the bank", async () => {
const actualBalance = await bank.balanceOf(bridgeAddress)

expect(actualBalance).to.be.equal(0)
})
Expand Down
Loading

0 comments on commit e95ab1f

Please sign in to comment.