Skip to content

Commit

Permalink
Merge pull request #559 from sarvesh-ost/mint_ost_prime_as_base_token
Browse files Browse the repository at this point in the history
Mint ost prime as base token
  • Loading branch information
Deepesh Kumar Nath authored Dec 21, 2018
2 parents 287d68f + 8915476 commit 6672548
Show file tree
Hide file tree
Showing 15 changed files with 394 additions and 67 deletions.
10 changes: 6 additions & 4 deletions contracts/gateway/EIP20CoGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ contract EIP20CoGateway is GatewayBase {
uint256 amount;

/** Address for which the utility tokens will be minted */
address beneficiary;
address payable beneficiary;
}

/* public Variables */
Expand Down Expand Up @@ -755,7 +755,9 @@ contract EIP20CoGateway is GatewayBase {
* @param _staker Staker address.
* @param _stakerNonce Nonce of the staker address.
* @param _beneficiary The address in the auxiliary chain where the utility
* tokens will be minted.
* tokens will be minted. This is payable so that it
* provides flexibility of transferring base token
* to account on minting.
* @param _amount Amount of utility token will be minted.
* @param _gasPrice Gas price that staker is ready to pay to get the stake
* and mint process done
Expand All @@ -770,7 +772,7 @@ contract EIP20CoGateway is GatewayBase {
function confirmStakeIntent(
address _staker,
uint256 _stakerNonce,
address _beneficiary,
address payable _beneficiary,
uint256 _amount,
uint256 _gasPrice,
uint256 _gasLimit,
Expand Down Expand Up @@ -1114,7 +1116,7 @@ contract EIP20CoGateway is GatewayBase {

// Mint token after subtracting reward amount.
UtilityTokenInterface(utilityToken).increaseSupply(
beneficiary_,
mint.beneficiary,
mintedAmount_
);

Expand Down
35 changes: 27 additions & 8 deletions contracts/gateway/OSTPrime.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import "../lib/SafeMath.sol";
import "./UtilityToken.sol";
import "./OSTPrimeConfig.sol";
import "../lib/IsMemberInterface.sol";
import "../lib/MutexAddress.sol";

/**
* @title OSTPrime contract implements UtilityToken and
Expand All @@ -42,7 +43,7 @@ import "../lib/IsMemberInterface.sol";
* @dev OSTPrime functions as the base token to pay for gas consumption on the
* utility chain.
*/
contract OSTPrime is UtilityToken, OSTPrimeConfig {
contract OSTPrime is UtilityToken, OSTPrimeConfig, MutexAddress {

/* Usings */

Expand Down Expand Up @@ -217,29 +218,47 @@ contract OSTPrime is UtilityToken, OSTPrimeConfig {
}

/**
* @notice Adds number of OST Prime tokens to account balance and increases
* the total token supply. Can be called only when contract is
* initialized and only by CoGateway address.
* @notice This method performs following operations:
* - Adds number of OST Prime EIP20 tokens to this contract address.
* - Increases the total token supply.
* - Transfers base token to the beneficiary address.
* It can be called by CoGateway address and when contract is
* initialized.
*
* @dev The parameters _account and _amount should not be zero. This check
* @dev The parameter _amount should not be zero. This check
* is added in function increaseSupplyInternal.
*
* @param _account Account address for which the OST Prime balance will be
* increased.
* increased. This is payable so that base token can be
* transferred to the account.
* @param _amount Amount of tokens.
*
* @return success_ `true` if increase supply is successful, false otherwise.
*/
function increaseSupply(
address _account,
address payable _account,
uint256 _amount
)
external
onlyInitialized
onlyCoGateway
returns (bool success_)
{
success_ = increaseSupplyInternal(_account, _amount);
require(
_account != address(0),
"Account address should not be zero."
);

/*
* Acquire lock for msg.sender so that this function can only be
* executed once in a transaction.
*/
MutexAddress.acquire(msg.sender);

success_ = increaseSupplyInternal(address(this), _amount);
_account.transfer(_amount);

MutexAddress.release(msg.sender);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion contracts/gateway/UtilityToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,14 @@ contract UtilityToken is EIP20Token, Organized, UtilityTokenInterface {
* is added in function increaseSupplyInternal.
*
* @param _account Account address for which the balance will be increased.
This is payable so that it provides flexibility of
* transferring base token to account on increase supply.
* @param _amount Amount of tokens.
*
* @return success_ `true` if increase supply is successful, false otherwise.
*/
function increaseSupply(
address _account,
address payable _account,
uint256 _amount
)
external
Expand Down
4 changes: 3 additions & 1 deletion contracts/gateway/UtilityTokenInterface.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ contract UtilityTokenInterface {
* total token supply.
*
* @param _account Account address for which the balance will be increased.
* This is payable so that it provides flexibility of
* transferring base token to account on increase supply.
* @param _amount Amount of tokens.
*
* @return success_ `true` if increase supply is successful, false otherwise.
*/
function increaseSupply(
address _account,
address payable _account,
uint256 _amount
)
external
Expand Down
78 changes: 78 additions & 0 deletions contracts/lib/MutexAddress.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
pragma solidity ^0.5.0;

// Copyright 2018 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/
//
// ----------------------------------------------------------------------------

/** @title MutexAddress contract provide locking mechanism. */
contract MutexAddress {

/* Storage */

/** Mapping of address and lock status. */
mapping(address => bool) private locks;


/* Constructor */

constructor() public
{ }

/**
* @notice This acquires lock for an account if not already acquired.
* This will revert the transaction if lock is already acquired.
*
* @param _account Account for which lock acquisition is required.
*
* @return isAcquired_ `true` on successful lock acquisition, `false` otherwise.
*/
function acquire(address _account)
internal
returns(bool isAcquired_)
{
require(
locks[_account] == false,
"Lock already acquired for the account."
);

locks[_account] = true;
isAcquired_ = true;
}

/**
* @notice This releases lock for an account if lock is acquired.
* This will revert the transaction if lock is not acquired.
*
* @param _account Account for which release lock is required.
*
* @return isReleased_ `true` on successful lock release, `false` otherwise.
*/
function release(address _account)
internal
returns(bool isReleased_)
{
require(
locks[_account] == true,
"Lock is not acquired for the address."
);

locks[_account] = false;
isReleased_ = true;
}
}
2 changes: 1 addition & 1 deletion contracts/test/TestEIP20CoGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ contract TestEIP20CoGateway is EIP20CoGateway {
*/
function setMints(
bytes32 _messageHash,
address _beneficiary,
address payable _beneficiary,
uint256 _amount
)
public
Expand Down
62 changes: 62 additions & 0 deletions contracts/test/TestMutexAddress.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
pragma solidity ^0.5.0;

// Copyright 2018 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/
//
// ----------------------------------------------------------------------------

import "../lib/MutexAddress.sol";

/** @title TestMutexAddress contract to test Mutex. */
contract TestMutexAddress is MutexAddress {

constructor()
MutexAddress()
public
{ }

/**
* @notice This acquires lock for an account if not already acquired.
* This will revert the transaction if lock is already acquired.
*
* @param _account Account for which lock acquisition is required.
*
* @return isAcquired_ `true` on successful lock acquisition, `false` otherwise.
*/
function acquireExternal(address _account)
external
returns(bool success_)
{
success_ = super.acquire(_account);
}

/**
* @notice This releases lock for an account if lock is acquired.
* This will revert the transaction if lock is not acquired.
*
* @param _account Account for which release lock is required.
*
* @return isReleased_ `true` on successful lock release, `false` otherwise.
*/
function releaseExternal(address _account)
external
returns(bool success_)
{
success_ = super.release(_account);
}
}
16 changes: 8 additions & 8 deletions test/gateway/ost_prime/decrease_supply.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract('OSTPrime.decreaseSupply()', function (accounts) {
membersManager,
coGatewayAddress;

async function initialize(){
async function initialize() {
await ostPrime.initialize(
{from: accounts[2], value: TOKENS_MAX}
);
Expand Down Expand Up @@ -71,7 +71,7 @@ contract('OSTPrime.decreaseSupply()', function (accounts) {
amount = new BN(0);

await Utils.expectRevert(
ostPrime.decreaseSupply(amount, { from: coGatewayAddress }),
ostPrime.decreaseSupply(amount, {from: coGatewayAddress}),
'Amount should be greater than zero.',
);

Expand All @@ -82,7 +82,7 @@ contract('OSTPrime.decreaseSupply()', function (accounts) {
await initialize();

await Utils.expectRevert(
ostPrime.decreaseSupply(amount, { from: accounts[5] }),
ostPrime.decreaseSupply(amount, {from: accounts[5]}),
'Only CoGateway can call the function.',
);

Expand All @@ -91,7 +91,7 @@ contract('OSTPrime.decreaseSupply()', function (accounts) {
it('should fail when OST Prime contract is not initialized', async function () {

await Utils.expectRevert(
ostPrime.decreaseSupply(amount, { from: coGatewayAddress }),
ostPrime.decreaseSupply(amount, {from: coGatewayAddress}),
'Contract is not initialized.',
);

Expand All @@ -105,7 +105,7 @@ contract('OSTPrime.decreaseSupply()', function (accounts) {
amount = new BN(2000);

await Utils.expectRevert(
ostPrime.decreaseSupply(amount, { from: coGatewayAddress }),
ostPrime.decreaseSupply(amount, {from: coGatewayAddress}),
'Insufficient balance.',
);

Expand All @@ -118,7 +118,7 @@ contract('OSTPrime.decreaseSupply()', function (accounts) {
amount = new BN(500);

let result = await ostPrime.decreaseSupply.call(
amount, { from: coGatewayAddress }
amount, {from: coGatewayAddress}
);

assert.strictEqual(
Expand All @@ -127,7 +127,7 @@ contract('OSTPrime.decreaseSupply()', function (accounts) {
'Contract should return true.',
);

await ostPrime.decreaseSupply(amount, { from: coGatewayAddress });
await ostPrime.decreaseSupply(amount, {from: coGatewayAddress});

let coGatewayBalance = await ostPrime.balanceOf.call(coGatewayAddress);

Expand All @@ -152,7 +152,7 @@ contract('OSTPrime.decreaseSupply()', function (accounts) {

let tx = await ostPrime.decreaseSupply(
amount,
{ from: coGatewayAddress }
{from: coGatewayAddress}
);

let event = EventDecoder.getEvents(tx, ostPrime);
Expand Down
Loading

0 comments on commit 6672548

Please sign in to comment.