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

Beneficiary Support For Redeem and UnstaKe #101

Merged
merged 7 commits into from
Feb 2, 2018
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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ node_js:
- "8"
before_install:
- sudo apt-get update
- sudo apt-get install nodejs
- sudo apt-get install nodejs
- sudo apt-get install npm
install:
install:
- npm install
before_script:
- nohup ./tools/runTestRpc.sh </dev/null >/dev/null 2>&1 &
Expand Down
2 changes: 2 additions & 0 deletions contracts/Hasher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ contract Hasher {
bytes32 _uuid,
address _account,
uint256 _accountNonce,
address _beneficiary,
uint256 _amountUT,
uint256 _escrowUnlockHeight)
public
Expand All @@ -82,6 +83,7 @@ contract Hasher {
_uuid,
_account,
_accountNonce,
_beneficiary,
_amountUT,
_escrowUnlockHeight);
}
Expand Down
44 changes: 28 additions & 16 deletions contracts/OpenSTUtility.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pragma solidity ^0.4.17;
import "./SafeMath.sol";
import "./Hasher.sol";
import "./OpsManaged.sol";
// import "./CoreInterface.sol";

// utility chain contracts
import "./STPrime.sol";
Expand Down Expand Up @@ -58,6 +57,7 @@ contract OpenSTUtility is Hasher, OpsManaged {
struct Redemption {
bytes32 uuid;
address redeemer;
address beneficiary;
uint256 amountUT;
uint256 unlockHeight;
}
Expand All @@ -81,14 +81,14 @@ contract OpenSTUtility is Hasher, OpsManaged {
address _beneficiary, uint256 _amountUT);

event RedemptionIntentDeclared(bytes32 indexed _uuid, bytes32 indexed _redemptionIntentHash,
address _token, address _redeemer, uint256 _nonce, uint256 _amount, uint256 _unlockHeight,
address _token, address _redeemer, uint256 _nonce, address _beneficiary, uint256 _amount, uint256 _unlockHeight,
uint256 _chainIdValue);

event ProcessedRedemption(bytes32 indexed _uuid, bytes32 indexed _redemptionIntentHash, address _token,
address _redeemer, uint256 _amount);
address _redeemer, address _beneficiary, uint256 _amount);

event RevertedRedemption(bytes32 indexed _uuid, bytes32 indexed _redemptionIntentHash,
address _redeemer, uint256 _amountUT);
address _redeemer, address _beneficiary, uint256 _amountUT);

/*
* Constants
Expand All @@ -114,7 +114,7 @@ contract OpenSTUtility is Hasher, OpsManaged {
/// chainId of the current utility chain
uint256 public chainIdUtility;
address public registrar;
/// registered branded tokens
/// registered branded tokens
mapping(bytes32 /* uuid */ => RegisteredToken) internal registeredTokens;
/// name reservation is first come, first serve
mapping(bytes32 /* hashName */ => address /* requester */) public nameReservation;
Expand Down Expand Up @@ -299,17 +299,20 @@ contract OpenSTUtility is Hasher, OpsManaged {
/// as the spender so that the branded token can be taken into escrow by OpenSTUtility
/// note: for STPrime, call OpenSTUtility.redeemSTPrime as a payable function
/// note: nonce must be queried from OpenSTValue contract
/// note: Redemption will be done to beneficiary address
function redeem(
bytes32 _uuid,
uint256 _amountBT,
uint256 _nonce)
uint256 _nonce,
address _beneficiary)
external
returns (
uint256 unlockHeight,
bytes32 redemptionIntentHash)
{
require(_uuid != "");
require(_amountBT > 0);
require(_beneficiary != address(0));
// on redemption allow the nonce to be re-used to cover for an unsuccessful
// previous redemption previously; as the nonce is strictly increasing plus
// one on the value chain; there is no gain on redeeming with the same nonce,
Expand All @@ -332,27 +335,31 @@ contract OpenSTUtility is Hasher, OpsManaged {
_uuid,
msg.sender,
_nonce,
_beneficiary,
_amountBT,
unlockHeight
);

redemptions[redemptionIntentHash] = Redemption({
uuid: _uuid,
redeemer: msg.sender,
beneficiary: _beneficiary,
amountUT: _amountBT,
unlockHeight: unlockHeight
});

RedemptionIntentDeclared(_uuid, redemptionIntentHash, address(token),
msg.sender, _nonce, _amountBT, unlockHeight, chainIdValue);
msg.sender, _nonce, _beneficiary, _amountBT, unlockHeight, chainIdValue);

return (unlockHeight, redemptionIntentHash);
}

/// @dev redeemer must send as value the amount STP to redeem
/// note: nonce must be queried from OpenSTValue contract
/// note: redemption will be done to beneficiary address
function redeemSTPrime(
uint256 _nonce)
uint256 _nonce,
address _beneficiary)
external
payable
returns (
Expand All @@ -361,6 +368,7 @@ contract OpenSTUtility is Hasher, OpsManaged {
bytes32 redemptionIntentHash)
{
require(msg.value > 0);
require(_beneficiary != address(0));
// on redemption allow the nonce to be re-used to cover for an unsuccessful
// previous redemption previously; as the nonce is strictly increasing plus
// one on the value chain; there is no gain on redeeming with the same nonce,
Expand All @@ -375,19 +383,21 @@ contract OpenSTUtility is Hasher, OpsManaged {
uuidSTPrime,
msg.sender,
_nonce,
_beneficiary,
amountSTP,
unlockHeight
);

redemptions[redemptionIntentHash] = Redemption({
uuid: uuidSTPrime,
redeemer: msg.sender,
beneficiary: _beneficiary,
amountUT: amountSTP,
unlockHeight: unlockHeight
});

RedemptionIntentDeclared(uuidSTPrime, redemptionIntentHash, simpleTokenPrime,
msg.sender, _nonce, amountSTP, unlockHeight, chainIdValue);
msg.sender, _nonce, _beneficiary, amountSTP, unlockHeight, chainIdValue);

return (amountSTP, unlockHeight, redemptionIntentHash);
}
Expand Down Expand Up @@ -424,7 +434,7 @@ contract OpenSTUtility is Hasher, OpsManaged {
require(token.burn.value(value)(redemption.redeemer, redemption.amountUT));

ProcessedRedemption(redemption.uuid, _redemptionIntentHash, token,
redemption.redeemer, redemption.amountUT);
redemption.redeemer, redemption.beneficiary, redemption.amountUT);

delete redemptions[_redemptionIntentHash];

Expand All @@ -437,6 +447,7 @@ contract OpenSTUtility is Hasher, OpsManaged {
returns (
bytes32 uuid,
address redeemer,
address beneficiary,
uint256 amountUT)
{
require(_redemptionIntentHash != "");
Expand All @@ -450,6 +461,7 @@ contract OpenSTUtility is Hasher, OpsManaged {
uuid = redemption.uuid;
amountUT = redemption.amountUT;
redeemer = redemption.redeemer;
beneficiary = redemption.beneficiary;

if (redemption.uuid == uuidSTPrime) {
// transfer throws if insufficient funds
Expand All @@ -463,9 +475,9 @@ contract OpenSTUtility is Hasher, OpsManaged {
delete redemptions[_redemptionIntentHash];

// fire event
RevertedRedemption(uuid, _redemptionIntentHash, redeemer, amountUT);
RevertedRedemption(uuid, _redemptionIntentHash, redeemer, beneficiary, amountUT);

return (uuid, redeemer, amountUT);
return (uuid, redeemer, beneficiary, amountUT);
}

/*
Expand Down Expand Up @@ -551,7 +563,7 @@ contract OpenSTUtility is Hasher, OpsManaged {
// in which case must come from same address
// otherwise proposals are first come, first serve
// under opt-in discretion of registrar
address requester = nameReservation[_hashName];
address requester = nameReservation[_hashName];
if ((requester == address(0) ||
requester == _requester)) {
return true;
Expand Down Expand Up @@ -616,8 +628,8 @@ contract OpenSTUtility is Hasher, OpsManaged {
require(registeredUuid == _checkUuid);
require(_brandedToken.uuid() == _checkUuid);

assert(address(registeredTokens[registeredUuid].token) == address(0));
assert(address(registeredTokens[registeredUuid].token) == address(0));

registeredTokens[registeredUuid] = RegisteredToken({
token: _brandedToken,
registrar: registrar
Expand All @@ -630,7 +642,7 @@ contract OpenSTUtility is Hasher, OpsManaged {

RegisteredBrandedToken(registrar, _brandedToken, registeredUuid, _symbol, _name,
_conversionRate, _requester);

return registeredUuid;
}

Expand Down
29 changes: 18 additions & 11 deletions contracts/OpenSTValue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import "./ProtocolVersioned.sol";
import "./SimpleStake.sol";


/// @title OpenSTValue - value staking contract for OpenST
/// @title OpenSTValue - value staking contract for OpenST
contract OpenSTValue is OpsManaged, Hasher {
using SafeMath for uint256;

Expand All @@ -56,13 +56,13 @@ contract OpenSTValue is OpsManaged, Hasher {
address _staker, uint256 _amountST, uint256 _amountUT);

event RedemptionIntentConfirmed(bytes32 indexed _uuid, bytes32 _redemptionIntentHash,
address _redeemer, uint256 _amountST, uint256 _amountUT, uint256 _expirationHeight);
address _redeemer, address _beneficiary, uint256 _amountST, uint256 _amountUT, uint256 _expirationHeight);

event ProcessedUnstake(bytes32 indexed _uuid, bytes32 indexed _redemptionIntentHash,
address stake, address _redeemer, uint256 _amountST);
address stake, address _redeemer, address _beneficiary, uint256 _amountST);

event RevertedUnstake(bytes32 indexed _uuid, bytes32 indexed _redemptionIntentHash,
address _redeemer, uint256 _amountST);
address _redeemer, address _beneficiary, uint256 _amountST);

/*
* Constants
Expand Down Expand Up @@ -100,6 +100,7 @@ contract OpenSTValue is OpsManaged, Hasher {
struct Unstake {
bytes32 uuid;
address redeemer;
address beneficiary;
uint256 amountST;
// @dev consider removal of amountUT
uint256 amountUT;
Expand Down Expand Up @@ -295,6 +296,7 @@ contract OpenSTValue is OpsManaged, Hasher {
bytes32 _uuid,
address _redeemer,
uint256 _redeemerNonce,
address _beneficiary,
uint256 _amountUT,
uint256 _redemptionUnlockHeight,
bytes32 _redemptionIntentHash)
Expand All @@ -306,6 +308,7 @@ contract OpenSTValue is OpsManaged, Hasher {
{
require(utilityTokens[_uuid].simpleStake != address(0));
require(_amountUT > 0);
require(_beneficiary != address(0));
// later core will provide a view on the block height of the
// utility chain
require(_redemptionUnlockHeight > 0);
Expand All @@ -318,6 +321,7 @@ contract OpenSTValue is OpsManaged, Hasher {
_uuid,
_redeemer,
nonces[_redeemer],
_beneficiary,
_amountUT,
_redemptionUnlockHeight
);
Expand All @@ -336,13 +340,14 @@ contract OpenSTValue is OpsManaged, Hasher {
unstakes[redemptionIntentHash] = Unstake({
uuid: _uuid,
redeemer: _redeemer,
beneficiary: _beneficiary,
amountUT: _amountUT,
amountST: amountST,
expirationHeight: expirationHeight
});

RedemptionIntentConfirmed(_uuid, redemptionIntentHash, _redeemer,
amountST, _amountUT, expirationHeight);
_beneficiary, amountST, _amountUT, expirationHeight);

return (amountST, expirationHeight);
}
Expand All @@ -367,10 +372,10 @@ contract OpenSTValue is OpsManaged, Hasher {
stakeAddress = address(utilityToken.simpleStake);
require(stakeAddress != address(0));

require(utilityToken.simpleStake.releaseTo(unstake.redeemer, unstake.amountST));
require(utilityToken.simpleStake.releaseTo(unstake.beneficiary, unstake.amountST));

ProcessedUnstake(unstake.uuid, _redemptionIntentHash, stakeAddress,
unstake.redeemer, unstake.amountST);
ProcessedUnstake(unstake.uuid, _redemptionIntentHash, stakeAddress,
unstake.redeemer, unstake.beneficiary, unstake.amountST);

delete unstakes[_redemptionIntentHash];

Expand All @@ -383,6 +388,7 @@ contract OpenSTValue is OpsManaged, Hasher {
returns (
bytes32 uuid,
address redeemer,
address beneficiary,
uint256 amountST)
{
require(_redemptionIntentHash != "");
Expand All @@ -396,13 +402,14 @@ contract OpenSTValue is OpsManaged, Hasher {

uuid = unstake.uuid;
redeemer = unstake.redeemer;
beneficiary = unstake.beneficiary;
amountST = unstake.amountST;

delete unstakes[_redemptionIntentHash];

RevertedUnstake(uuid, _redemptionIntentHash, redeemer, amountST);
RevertedUnstake(uuid, _redemptionIntentHash, redeemer, beneficiary, amountST);

return (uuid, redeemer, amountST);
return (uuid, redeemer, beneficiary, amountST);
}

function core(
Expand Down Expand Up @@ -528,7 +535,7 @@ contract OpenSTValue is OpsManaged, Hasher {
stakingAccount: _stakingAccount
});

UtilityTokenRegistered(uuid, address(simpleStake), _symbol, _name,
UtilityTokenRegistered(uuid, address(simpleStake), _symbol, _name,
TOKEN_DECIMALS, _conversionRate, _chainIdUtility, _stakingAccount);

return uuid;
Expand Down
1 change: 1 addition & 0 deletions contracts/OpenSTValueInterface.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ contract OpenSTValueInterface {
bytes32 _uuid,
address _redeemer,
uint256 _redeemerNonce,
address _beneficiary,
uint256 _amountUT,
uint256 _redemptionUnlockHeight,
bytes32 _redemptionIntentHash)
Expand Down
2 changes: 2 additions & 0 deletions contracts/Registrar.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ contract Registrar is OpsManaged {
bytes32 _uuid,
address _redeemer,
uint256 _redeemerNonce,
address _beneficiary,
uint256 _amountUT,
uint256 _redemptionUnlockHeight,
bytes32 _redemptionIntentHash)
Expand All @@ -65,6 +66,7 @@ contract Registrar is OpsManaged {
_uuid,
_redeemer,
_redeemerNonce,
_beneficiary,
_amountUT,
_redemptionUnlockHeight,
_redemptionIntentHash);
Expand Down
Loading