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

One-transaction TBTC to BTC #302

Merged
merged 7 commits into from
May 18, 2022
Merged

One-transaction TBTC to BTC #302

merged 7 commits into from
May 18, 2022

Conversation

pdyraga
Copy link
Member

@pdyraga pdyraga commented May 18, 2022

Closes #256

Modified TBTCVault.receiveApproval function to allow unminting TBTC and
requesting Bank balance redemption in the Bridge in a single transaction via
TBTC.approveAndCall. Also, added TBTCVault.redeem function allowing to
unmint TBTC and request redemption in the Bridge on an already approved TBTC
balance.

During the work on this feature, I have been exploring different options,
including exposing IBridge interface that would be called from Bank or
TBTCVault to redeem balance. The current version won because it does not
tightly-couple vault implementations with the specific Bridge implementation.
For example, the second version of the Bridge may not even have a concept of
a wallet or a main wallet UTXO. Passing parameters as bytes is more flexible and
uses an existing Bank mechanism of receiveBalanceApproval.

The reasoning behind the fix in 9738385 is explained in the commit message.
I have also captured the potential investigation work in #303.

Modified `TBTCVault.receiveApproval` function to allow unminting TBTC and
requesting Bank balance redemption in the Bridge in a single transaction via
`TBTC.approveAndCall`. Also, added `TBTCVault.redeem` function allowing to
unmint TBTC and request redemption in the `Bridge` on an already approved TBTC
balance.

During the work on this feature, I have been exploring different options,
including exposing `IBridge` interface that would be called from `Bank` or
`TBTCVault` to redeem balance. The current version won because it does not
tightly-couple vault implementations with the specific `Bridge` implementation.
For example, the second version of the `Bridge` may not even have a concept of
a wallet or a main wallet UTXO. Passing parameters as bytes is more flexible and
uses an existing `Bank` mechanism of `receiveBalanceApproval`.
@pdyraga pdyraga self-assigned this May 18, 2022
@pdyraga pdyraga added the ⛓️ solidity Solidity contracts label May 18, 2022
@pdyraga pdyraga added this to the solidity/v1.0.0 milestone May 18, 2022
This change fixes mysterious problem in unit tests.

Let's say there is a unit test calling `await waffle.loadFixture(bridgeFixture)`
in the setup. For example, `Bridge.Wallets.test.ts`.

Then, `VendingMachine.test.ts` loads `await waffle.loadFixture(vendingMachineFixture)`
and modifies the state of `VendingMachine` by `transferVendingMachineUpgradeInitiatorRole`
to some address.

Finally, some other test starts and that test calls
`await waffle.loadFixture(bridgeFixture)`. For example,
`TBTCVault.Redemption.test.ts`.
THIS TEST WILL OBSERVE CHANGES DONE BY `VendingMachine.test.ts`. For example,
the upgrade initiator will be the address set by `VendingMachine.test.ts`.

This is not happening if we use `await waffle.loadFixture(bridgeFixture)` in
all three tests. The root cause is unkown, we suspect it is some clash between
plugins. There will be a separate issue opened to investigate it. For now we fix
it by loading `bridgeFixture` which makes sense for all tests - this is our full
system deployment.
This change allows to lower the gas consumption. Before/after:
|  Bank       ·  approveBalanceAndCall  ·   33039  ·  120387  ·   78764  │
|  Bank       ·  approveBalanceAndCall  ·   32769  ·  120113  ·   78535  │
|  TBTCVault  ·  redeem                 ·  161704  ·  178643  ·  168753  │
|  TBTCVault  ·  redeem                 ·  161704  ·  178643  ·  168753  │
@pdyraga pdyraga marked this pull request as ready for review May 18, 2022 11:37
@pdyraga pdyraga mentioned this pull request May 18, 2022
solidity/contracts/vault/TBTCVault.sol Outdated Show resolved Hide resolved
solidity/contracts/vault/TBTCVault.sol Outdated Show resolved Hide resolved
solidity/test/fixtures/bridge.ts Show resolved Hide resolved
Added @dev section making clear what are the requirements in regards to
approved amount of TBTC when calling `TBTCVault.redeem` function.
Improved unit tets for `redeem` and `receiveApproval` functions adding
case for P2SH redeemer script.
*Unminting* is when TBTC is transformed to Bank balance.
*Redemption* is when Bank balance is transformed to BTC.

Having this in mind, former `redeem` function should be called
`unmintAndRedeem` because it transforms TBTC to BTC.
@lukasz-zimnoch lukasz-zimnoch merged commit a75bdec into main May 18, 2022
@lukasz-zimnoch lukasz-zimnoch deleted the generalized-approval-6 branch May 18, 2022 14:22
@pdyraga pdyraga removed their assignment Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⛓️ solidity Solidity contracts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

One-transaction TBTC to BTC
2 participants