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

Updated OSTPrime contract and tests. #533

Merged
merged 18 commits into from
Dec 12, 2018
Merged
2 changes: 1 addition & 1 deletion contracts/gateway/EIP20CoGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ contract EIP20CoGateway is GatewayBase {
redeemer_ = message.sender;
redeemAmount_ = redeems[_messageHash].amount;
// Burn the redeem amount.
UtilityTokenInterface(utilityToken).burn(address(this), redeemAmount_);
UtilityTokenInterface(utilityToken).burn(redeemAmount_);

// Transfer the bounty amount to the facilitator
msg.sender.transfer(redeems[_messageHash].bounty);
Expand Down
128 changes: 85 additions & 43 deletions contracts/gateway/OSTPrime.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ pragma solidity ^0.5.0;
// limitations under the License.
//
// ----------------------------------------------------------------------------
// Auxiliary chain: OSTPrime
//
// http://www.simpletoken.org/
//
// ----------------------------------------------------------------------------

/// Simple Token Prime [OST'] is equivalently staked for with Simple Token
/// on the value chain and is the base token that pays for gas on the utility
/// chain. The gasprice on utility chains is set in [ST'-Wei/gas] (like
/// on the value chain and is the base token that pays for gas on the auxiliary
/// chain. The gasprice on auxiliary chains is set in [OST'-Wei/gas] (like
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
/// Ether pays for gas on Ethereum mainnet) when sending a transaction on
/// the auxiliary chain.

Expand All @@ -34,11 +33,11 @@ import "./OSTPrimeConfig.sol";


/**
* @title OSTPrime contract which implements UtilityToken and
* @title OSTPrime contract implements UtilityToken and
* OSTPrimeConfig.
*
* @notice A freely tradable equivalent representation of Simple Token [ST]
* on Ethereum mainnet on the utility chain.
* @notice A freely tradable equivalent representation of Simple Token [OST]
* on Ethereum mainnet on the auxiliary chain.
*
* @dev OSTPrime functions as the base token to pay for gas consumption on the
* utility chain.
Expand All @@ -47,27 +46,24 @@ contract OSTPrime is UtilityToken, OSTPrimeConfig {

using SafeMath for uint256;

/** Emitted whenever OSTPrime base token is claimed. */
event Claim(
address indexed _beneficiary,
uint256 _amount,
uint256 _totalSupply,
address _utilityToken
/** Emitted whenever OST` EIP20 token is converted to OST` base token. */
event TokenUnwrapped(
address indexed _account,
uint256 _amount
);

/** Emitted whenever basetoken is converted to OSTPrime */
event Redeem(
address indexed _redeemer,
uint256 _amount,
uint256 _totalSupply,
address _utilityToken
/** Emitted whenever OST` base token is converted to OST` EIP20 token. */
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
event TokenWrapped(
address indexed _account,
uint256 _amount
);

/**
* set when OST' has received TOKENS_MAX tokens;
* when uninitialised mint is not allowed
*/
bool private initialized;
bool public initialized;


/** Modifiers */

Expand All @@ -77,11 +73,15 @@ contract OSTPrime is UtilityToken, OSTPrimeConfig {
* @dev Checks if initialized is set to True to proceed.
*/
modifier onlyInitialized() {
require(initialized);
require(
initialized == true,
"Contract is not initialized."
);
_;
}

/* Public functions */

/* Constructor */
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved

/**
* @notice Contract constructor.
Expand All @@ -92,11 +92,18 @@ contract OSTPrime is UtilityToken, OSTPrimeConfig {
*/
constructor(address _valueToken)
public
UtilityToken(TOKEN_SYMBOL, TOKEN_NAME, TOKEN_DECIMALS, _valueToken)
UtilityToken(
TOKEN_SYMBOL,
TOKEN_NAME,
TOKEN_DECIMALS,
_valueToken)
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
{

}


/* Public functions. */

/**
* @notice Public function initialize.
*
Expand All @@ -105,64 +112,99 @@ contract OSTPrime is UtilityToken, OSTPrimeConfig {
* On setup of the auxiliary chain the base tokens need to be
* transferred in full to OSTPrime for the base tokens to be
* minted as OST'
*
* @return success_ `true` if initialize was successful.
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
*/
function initialize()
public
payable
returns (bool success_)
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
{
require(msg.value == TOKENS_MAX);
require(
initialized != true,
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
"Contract is already initialized."
);

require(
msg.value == TOKENS_MAX,
"Payable amount must be equal to total supply of token."
);

initialized = true;

success_ = true;
}

/**
* @notice convert the OST utility token to base token
* @notice convert the OST' EIP-20 token to OST` base token.
*
* @param _amount Amount of basetoken
* @param _amount Amount of EIP-20 to convert to base token.
*
* @return `true` if claim was successfully progressed
* @return success_ `true` if unwrap was successful.
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
*/
function claim(
function unwrap(
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
uint256 _amount
)
public
onlyInitialized
returns (bool /* success */)
returns (bool success_)
{
require(
_amount > 0,
"Amount should not be zero."
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
);

require(
_amount <= balances[msg.sender],
"Insufficient balance"
"Insufficient balance."
);

assert(address(this).balance >= _amount);
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved

transfer(address(this),_amount);

balances[msg.sender] = balances[msg.sender].sub(_amount);
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
balances[address(this)] = balances[address(this)].add(_amount);
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
msg.sender.transfer(_amount);

emit Claim(msg.sender, _amount, totalTokenSupply, address(this));
emit Transfer(msg.sender, address(this), _amount);
emit TokenUnwrapped(msg.sender, _amount);

return true;
success_ = true;
}

/**
* @notice convert the base token to OST utility token
* @notice convert OST` base token to OST EIP-20 token.
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
*
* @param _amount Amount of utility token
*
* @return `true` if claim was successfully progressed
* @return success_ `true` if claim was successfully progressed
*/
function redeem(uint256 _amount)
function wrap()
public
onlyInitialized
payable
returns (bool /** success */)
returns (bool success_)
{
require(msg.value == _amount);
assert(address(this).balance >= _amount);
require(
msg.value > 0,
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
"Payable amount should not be zero."
);

allowed[address(this)][msg.sender] = _amount;
transferFrom(address(this), msg.sender, _amount);
require(
address(msg.sender).balance >= msg.value,
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
"Available balance is less than payable amount."
);

emit Redeem(msg.sender, _amount, totalTokenSupply, address(this));
require(
balances[address(this)] >= msg.value,
"Insufficient EIP-20 token balance."
);

balances[address(this)] = balances[address(this)].sub(msg.value);
balances[msg.sender] = balances[msg.sender].add(msg.value);
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved

emit Transfer(address(this), msg.sender, msg.value);
emit TokenWrapped(msg.sender, msg.value);

success_ = true;
}

}
2 changes: 1 addition & 1 deletion contracts/gateway/UtilityToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ contract UtilityToken is
{
require(
_valueToken != address(0),
"ERC20 token should not be zero"
"ERC20 token should not be zero."
);

valueToken = _valueToken;
Expand Down
2 changes: 0 additions & 2 deletions contracts/gateway/UtilityTokenInterface.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@ contract UtilityTokenInterface {
* @dev only burns the amount from CoGateway address, So to burn
* transfer the amount to CoGateway.
*
* @param _burner Burner address.
* @param _amount Amount of tokens to burn.
*
* @return bool `true` if burn is successful, false otherwise.
*/
function burn(
address _burner,
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
uint256 _amount
)
external
Expand Down
74 changes: 74 additions & 0 deletions contracts/test/gateway/TestOSTPrime.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* solhint-disable-next-line compiler-fixed */
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/
//
// ----------------------------------------------------------------------------

/// Simple Token Prime [OST'] is equivalently staked for with Simple Token
/// on the value chain and is the base token that pays for gas on the auxiliary
/// chain. The gasprice on auxiliary chains is set in [OST'-Wei/gas] (like
/// Ether pays for gas on Ethereum mainnet) when sending a transaction on
/// the auxiliary chain.
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved

import "../../gateway/OSTPrime.sol";

/**
* @title Test OST prime contract.
*/
contract TestOSTPrime is OSTPrime {


/* Constructor */
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved

/**
* @notice Contract constructor.
*
* @dev This contract is used only for testing.
*
* @param _valueToken ERC20 token address in origin chain
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved
*/
constructor(address _valueToken)
public
OSTPrime(_valueToken)
{

}
deepesh-kn marked this conversation as resolved.
Show resolved Hide resolved


/* Public functions. */

/**
* @notice Set the EIP-20 token balance for the given account address.
*
* @dev This is used only for testing.
*
* @param _account Address for which the balance is to be set.
* @param _amount The amount of tokens to be set.
*/
function setTokenBalance(
address _account,
uint256 _amount
)
public
{
balances[_account] = _amount;
}

}
Loading