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

Stake and mint integration test #634

Merged
merged 14 commits into from
Feb 4, 2019
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
48 changes: 38 additions & 10 deletions test_integration/01_deployment/01_deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

const chai = require('chai');
const Web3 = require('web3');
const BN = require('bn.js');
const docker = require('../docker');
const shared = require('../shared');
const {
Expand All @@ -31,6 +32,9 @@ const {

const { assert } = chai;

// Max ost prime supply.
const TOKENS_MAX = new BN('800000000000000000000000000');

describe('Deploy', async () => {
let rpcEndpointOrigin;
let web3Origin;
Expand All @@ -56,22 +60,14 @@ describe('Deploy', async () => {
shared.auxiliary.web3 = web3Auxiliary;
shared.origin.deployerAddress = deployerAddressOrigin;
shared.auxiliary.deployerAddress = deployerAddressAuxiliary;
shared.origin.accounts = accountsOrigin;
shared.auxiliary.accounts = accountsAuxiliary;

// FIXME: #623
shared.origin.organizationAddress = deployerAddressOrigin;
shared.auxiliary.organizationAddress = deployerAddressAuxiliary;
});

after(async () => {
await shared.origin.addContract('EIP20Gateway');
await shared.origin.addContract('Anchor');
await shared.origin.addContract('EIP20StandardToken', 'Token');
await shared.origin.addContract('EIP20StandardToken', 'BaseToken');

await shared.auxiliary.addContract('EIP20CoGateway');
await shared.auxiliary.addContract('Anchor');
});

let tokenAddressOrigin;
let baseTokenAddressOrigin;
it('correctly deploys branded token and base token on Origin', async () => {
Expand All @@ -98,6 +94,8 @@ describe('Deploy', async () => {
/* Note that they are called Token and BaseToken! */
shared.origin.contractAddresses.Token = tokenAddressOrigin;
shared.origin.contractAddresses.BaseToken = baseTokenAddressOrigin;
await shared.origin.addContract('EIP20StandardToken', 'Token');
await shared.origin.addContract('EIP20StandardToken', 'BaseToken');
});

it('correctly deploys Gateway and CoGateway', async () => {
Expand Down Expand Up @@ -138,5 +136,35 @@ describe('Deploy', async () => {
...shared.auxiliary.contractAddresses,
...auxiliaryAddresses,
};

await shared.origin.addContract('EIP20Gateway');
0xsarvesh marked this conversation as resolved.
Show resolved Hide resolved
await shared.origin.addContract('Anchor');

await shared.auxiliary.addContract('EIP20CoGateway');
await shared.auxiliary.addContract('Anchor');
await shared.auxiliary.addContract('OSTPrime');
});

it('activates the gateway ', async () => {
const gateway = shared.origin.contracts.EIP20Gateway;

await gateway.activateGateway(
shared.auxiliary.contractAddresses.EIP20CoGateway,
{ from: shared.origin.organizationAddress },
);
});

it('initializes and sets co-gateway in ost prime', async () => {
const ostPrime = shared.auxiliary.contracts.OSTPrime;

await ostPrime.initialize({
from: shared.auxiliary.organizationAddress,
value: TOKENS_MAX,
});

await ostPrime.setCoGateway(
shared.auxiliary.contractAddresses.EIP20CoGateway,
{ from: shared.auxiliary.organizationAddress },
);
});
});
42 changes: 0 additions & 42 deletions test_integration/02_stake_and_mint/01_stake.js

This file was deleted.

224 changes: 224 additions & 0 deletions test_integration/02_stake_and_mint/01_stake_and_mint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// Copyright 2019 OpenST Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ----------------------------------------------------------------------------
//
// http://www.simpletoken.org/
//
// ----------------------------------------------------------------------------

const BN = require('bn.js');
const shared = require('../shared');
const Utils = require('../../test/test_lib/utils');
const EventDecoder = require('../../test/test_lib/event_decoder');

const StakeAssertion = require('./utils/stake_assertion');
const ProveGatewayAssertion = require('./utils/prove_gateway_assertion');
const ConfirmStakeIntentAssertion = require('./utils/confirm_stake_intent_assertion');
const ProgressStakeAssertion = require('./utils/progress_stake_assertion');
const ProgressMintAssertion = require('./utils/progress_mint_assertion');
const ProofUtils = require('./utils/proof_utils');
const Anchor = require('./utils/anchor');

describe('Stake and mint', async () => {
let assertStake;
let assertProgressStake;
let assertProgressMint;
let proofUtils;

let originAccounts;
let auxiliaryAccounts;
let originWeb3;
let auxiliaryWeb3;

let gateway;
let cogateway;
let token;
let baseToken;
let auxiliaryAnchor;
let ostPrime;
let stakeRequest;

before(async () => {
originWeb3 = shared.origin.web3;
auxiliaryWeb3 = shared.auxiliary.web3;
originAccounts = shared.origin.accounts;
auxiliaryAccounts = shared.origin.accounts;
token = shared.origin.contracts.Token;
baseToken = shared.origin.contracts.BaseToken;
gateway = shared.origin.contracts.EIP20Gateway;
cogateway = shared.auxiliary.contracts.EIP20CoGateway;
ostPrime = shared.auxiliary.contracts.OSTPrime;

const hasher = Utils.generateHashLock();
stakeRequest = {
amount: new BN(200),
gasPrice: new BN(1),
gasLimit: new BN(100),
staker: originAccounts[0],
bounty: await gateway.bounty.call(),
nonce: await gateway.getNonce.call(originAccounts[0]),
beneficiary: auxiliaryAccounts[2],
hashLock: hasher.l,
unlockSecret: hasher.s,
};

assertStake = new StakeAssertion(gateway, token, baseToken);
assertProgressStake = new ProgressStakeAssertion(gateway, token, baseToken);
assertProgressMint = new ProgressMintAssertion(auxiliaryWeb3, cogateway, ostPrime);
proofUtils = new ProofUtils(originWeb3, auxiliaryWeb3);
auxiliaryAnchor = new Anchor(
originWeb3,
shared.auxiliary.contracts.Anchor,
shared.auxiliary.organizationAddress,
);
});

it('stakes', async () => {
// Capture initial token and base token balance of staker and gateway.
const initialBalances = await assertStake.captureBalances(stakeRequest.staker);

await approveGatewayForStakeAmount(token, gateway, stakeRequest);
await approveGatewayForBounty(baseToken, gateway, stakeRequest);

const tx = await gateway.stake(
stakeRequest.amount,
stakeRequest.beneficiary,
stakeRequest.gasPrice,
stakeRequest.gasLimit,
stakeRequest.nonce,
stakeRequest.hashLock,
{ from: stakeRequest.staker },
);

const event = EventDecoder.getEvents(tx, gateway);
// Assert event and balances.
await assertStake.verify(event, stakeRequest, initialBalances);

stakeRequest.messageHash = event.StakeIntentDeclared._messageHash;
});

it('confirms stake', async () => {
// Anchor state root.
const blockNumber = await auxiliaryAnchor.anchorStateRoot(
'latest',
);
// Generate outbox proof for block height for which state root is
// anchored.
const outboxProof = await proofUtils.getOutboxProof(
gateway.address,
[stakeRequest.messageHash],
originWeb3.utils.toHex(blockNumber),
);

stakeRequest.blockHeight = new BN(blockNumber);
// Prove gateway.
let tx = await cogateway.proveGateway(
stakeRequest.blockHeight,
outboxProof.encodedAccountValue,
outboxProof.serializedAccountProof,
{ from: auxiliaryAccounts[0] },
);

let event = EventDecoder.getEvents(tx, cogateway);
ProveGatewayAssertion.verify(
event,
stakeRequest.blockHeight,
outboxProof.storageHash,
gateway.address,
);

tx = await cogateway.confirmStakeIntent(
stakeRequest.staker,
stakeRequest.nonce,
stakeRequest.beneficiary,
stakeRequest.amount,
stakeRequest.gasPrice,
stakeRequest.gasLimit,
stakeRequest.hashLock,
stakeRequest.blockHeight,
outboxProof.storageProof[0].serializedProof,
{ from: auxiliaryAccounts[0] },
);

event = EventDecoder.getEvents(tx, cogateway);
// Assert event.
ConfirmStakeIntentAssertion.verify(event, stakeRequest);
});

it('progresses stake', async () => {
// Capture initial token and base token balance of staker and gateway.
const initialBalancesBeforeProgress = await assertProgressStake.captureBalances(
stakeRequest.staker,
);

const tx = await gateway.progressStake(
stakeRequest.messageHash,
stakeRequest.unlockSecret,
{ from: originAccounts[0] },
);

const event = EventDecoder.getEvents(tx, gateway);
// Assert event and balances.
await assertProgressStake.verify(event, stakeRequest, initialBalancesBeforeProgress);
});

it('progresses mint', async () => {
// Capture initial OST prime ERC20 and base token balance of
// beneficiary, OST prime contract address and gateway.
const initialBalancesBeforeMint = await assertProgressMint.captureBalances(
stakeRequest.beneficiary,
);

const tx = await cogateway.progressMint(
stakeRequest.messageHash,
stakeRequest.unlockSecret,
{ from: auxiliaryAccounts[0] },
);
const event = EventDecoder.getEvents(tx, cogateway);

// Assert event and balances.
await assertProgressMint.verify(event, stakeRequest, initialBalancesBeforeMint);
});
});

/**
* Approve Gateway for stake amount.
* @param {Object }token Token contract instance.
* @param {Object} gateway Gateway contract instance.
* @param {Object} stakeRequest stake request.
* @return {Promise<void>}
*/
async function approveGatewayForStakeAmount(token, gateway, stakeRequest) {
await token.approve(
gateway.address,
stakeRequest.amount,
{ from: stakeRequest.staker },
);
}

/**
* Approve gateway for bounty.
* @param {Object} baseToken Base token contract instance.
* @param {Object} gateway Gateway contract instance.
* @param {Object} stakeRequest stake request.
* @return {Promise<void>}
*/
async function approveGatewayForBounty(baseToken, gateway, stakeRequest) {
await baseToken.approve(
gateway.address,
stakeRequest.bounty,
{ from: stakeRequest.staker },
);
}
Loading