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

Fix native deposit amount size on wrapAndDeposit #52

Merged
merged 3 commits into from
Nov 1, 2021
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
1 change: 1 addition & 0 deletions contracts/anchors/bridged/Anchor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ contract Anchor is LinkableAnchor {
require(!commitments[_commitment], "The commitment has been submitted");
// wrap into the token and send directly to this contract
if (tokenAddress == address(0)) {
require(msg.value == denomination);
ITokenWrapper(token).wrapForAndSendTo{value: msg.value}(
msg.sender,
tokenAddress,
Expand Down
1 change: 1 addition & 0 deletions lib/darkwebb/Anchor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class Anchor {
const treeHeight = await anchor.levels();
const createdAnchor = new Anchor(anchor, signer, treeHeight, maxEdges);
createdAnchor.token = await anchor.token();
createdAnchor.denomination = (await anchor.denomination()).toString();
return createdAnchor;
}

Expand Down
6 changes: 6 additions & 0 deletions lib/darkwebb/GovernedTokenWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class GovernedTokenWrapper {
return handler;
}

public static connect(address: string, signer: ethers.Signer) {
const contract = GovernedTokenWrapper__factory.connect(address, signer);
const tokenWrapper = new GovernedTokenWrapper(contract);
return tokenWrapper;
}

public grantMinterRole(address: string) {
const MINTER_ROLE = ethers.utils.keccak256(ethers.utils.toUtf8Bytes('MINTER_ROLE'));
return this.contract.grantRole(MINTER_ROLE, address);
Expand Down
63 changes: 63 additions & 0 deletions test/anchor/anchor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,69 @@ describe('Anchor for 2 max edges', () => {
const balTokenAfterWithdrawAndUnwrapSender = await token.balanceOf(sender.address);
assert.strictEqual(balTokenBeforeDepositSender.toString(), balTokenAfterWithdrawAndUnwrapSender.toString());
});

it('should native deposit appropriate amount after connection', async () => {
const signers = await ethers.getSigners();
const wallet = signers[0];
const sender = wallet;
// create wrapped token
const name = 'webbETH';
const symbol = 'webbETH';
const wrappedTokenFactory = new WrappedTokenFactory(wallet);
wrappedToken = await wrappedTokenFactory.deploy(name, symbol, sender.address, '10000000000000000000000000', true);
await wrappedToken.deployed();
await wrappedToken.add(token.address);

// create Anchor for wrapped token
const wrappedAnchor = await Anchor.createAnchor(
verifier.contract.address,
hasherInstance.address,
tokenDenomination,
levels,
wrappedToken.address,
sender.address,
sender.address,
sender.address,
MAX_EDGES,
sender
);

const MINTER_ROLE = ethers.utils.keccak256(ethers.utils.toUtf8Bytes('MINTER_ROLE'));
await wrappedToken.grantRole(MINTER_ROLE, wrappedAnchor.contract.address);

await token.approve(wrappedToken.address, '1000000000000000000');
const balTokenBeforeDepositSender = await token.balanceOf(sender.address);

const anchorUnderTest = await Anchor.connect(wrappedAnchor.contract.address, wallet);

// create a deposit on the anchor already setup
const { deposit, index, originChainId } = await anchorUnderTest.wrapAndDeposit(
token.address,
);

// Check that the anchor has the appropriate amount of wrapped token balance
const anchorWrappedTokenBalance = await wrappedToken.balanceOf(anchorUnderTest.contract.address);
assert.deepStrictEqual(anchorWrappedTokenBalance.toString(), tokenDenomination);

// Check that the anchor's token wrapper has the appropriate amount of token balance
const tokenWrapper = await anchorUnderTest.contract.token();
const tokenWrapperBalanceOfToken = await token.balanceOf(tokenWrapper);
assert.deepStrictEqual(tokenWrapperBalanceOfToken.toString(), tokenDenomination);

await TruffleAssert.passes(anchorUnderTest.withdrawAndUnwrap(
deposit,
originChainId,
index,
sender.address,
signers[1].address,
bigInt(0),
bigInt(0),
token.address
));

const balTokenAfterWithdrawAndUnwrapSender = await token.balanceOf(sender.address);
assert.strictEqual(balTokenBeforeDepositSender.toString(), balTokenAfterWithdrawAndUnwrapSender.toString());
});
});
});

Expand Down
43 changes: 14 additions & 29 deletions test/anchorbridge/nativeBridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import MintableToken from '../../lib/darkwebb/MintableToken';
import { toFixedHex } from '../../lib/darkwebb/utils';
import { BigNumber } from '@ethersproject/bignumber';
import { Signer } from '@ethersproject/abstract-signer';
import GovernedTokenWrapper from '../../lib/darkwebb/GovernedTokenWrapper';

function startGanacheServer(port: number, networkId: number, mnemonic: string) {
const ganacheServer = ganache.server({
Expand Down Expand Up @@ -61,7 +62,7 @@ describe('multichain tests', () => {
describe('BridgeConstruction', () => {
let bridge2WebbEthInput: BridgeInput;

it.only('create 2 side bridge for native token', async () => {
it('create 2 side bridge for native token', async () => {
bridge2WebbEthInput = {
anchorInputs: {
asset: {
Expand All @@ -88,31 +89,6 @@ describe('multichain tests', () => {
const anchor1: Anchor = bridge.getAnchor(chainId1, anchorSize)!;
const anchor2: Anchor = bridge.getAnchor(chainId2, anchorSize)!;

// Should be able to retrieve the token address (so we can mint tokens for test scenario)
const webbTokenAddress = bridge.getWebbTokenAddress(chainId1);
const webbToken = await MintableToken.tokenFromAddress(webbTokenAddress!, signers[1]);
const tx = await webbToken.mintTokens(signers[2].address, '100000000000000000000000');
const wrappedStartingBalance = await webbToken.getBalance(signers[2].address);

// get the state of anchors before deposit
const sourceAnchorRootBefore = await anchor1.contract.getLastRoot();

// Deposit on the bridge
const depositNote = await bridge.deposit(chainId2, anchorSize, signers[2]);

const wrappedEndingBalance = await webbToken.getBalance(signers[2].address);
assert.deepStrictEqual(wrappedEndingBalance, wrappedStartingBalance.sub(anchorSize));

// Check the state of anchors after deposit
let edgeIndex = await anchor2.contract.edgeIndex(chainId1);

const sourceAnchorRootAfter = await anchor1.contract.getLastRoot();
let destAnchorEdgeAfter = await anchor2.contract.edgeList(edgeIndex);

// make sure the edge exists on the anchor
assert.notEqual(sourceAnchorRootAfter, sourceAnchorRootBefore);
assert.deepStrictEqual(ethers.BigNumber.from(0), destAnchorEdgeAfter.latestLeafIndex);

// get the balance of native token for the signer
const nativeStartingBalance = await signers[2].getBalance();

Expand All @@ -123,8 +99,17 @@ describe('multichain tests', () => {
const nativeEndingBalance = await signers[2].getBalance();
assert.equal(nativeEndingBalance.lt(nativeStartingBalance.sub(anchorSize)), true);

destAnchorEdgeAfter = await anchor2.contract.edgeList(edgeIndex);
assert.deepStrictEqual(ethers.BigNumber.from(1), destAnchorEdgeAfter.latestLeafIndex);
// Check the edge of the linked anchor is updated
let edgeIndex = await anchor2.contract.edgeIndex(chainId1);
let destAnchorEdgeAfter = await anchor2.contract.edgeList(edgeIndex);
assert.deepStrictEqual(ethers.BigNumber.from(0), destAnchorEdgeAfter.latestLeafIndex);

// Check the wrapped token has been added to the anchor's account
const wrappedTokenAddress = bridge.getWebbTokenAddress(chainId1);
const wrappedToken = GovernedTokenWrapper.connect(wrappedTokenAddress!, signers[2]);
const wrappedTokenAnchorBalance = await wrappedToken.contract.balanceOf(anchor1.contract.address);
console.log(`wrappedTokenAnchorBalance: ${wrappedTokenAnchorBalance}`);
assert.equal(wrappedTokenAnchorBalance.eq(anchorSize), true);

// deposit on the other side of the bridge
const depositNativeOther = await bridge.wrapAndDeposit(chainId1, '0x0000000000000000000000000000000000000000', anchorSize, ganacheWallet2);
Expand All @@ -137,7 +122,7 @@ describe('multichain tests', () => {
})
})

// describe('2 sided bridge native only use', () => {
// describe.only('2 sided bridge native only use', () => {

// let bridge2WebbEthInput = {
// anchorInputs: {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('multichain tests', () => {
await tokenInstance1.mintTokens(signers[1].address, '100000000000000000000000000');
});

it.only('create 2 side bridge for one token', async () => {
it('create 2 side bridge for one token', async () => {
bridge2WebbEthInput = {
anchorInputs: {
asset: {
Expand Down