Shorten Line Length
This commit is contained in:
parent
456f46a5ab
commit
5cebf72fb3
|
@ -1,6 +1,6 @@
|
|||
pragma solidity ^0.4.11;
|
||||
/*
|
||||
Copyright 2016, Jordi Baylina
|
||||
Copyright 2017, Jordi Baylina
|
||||
Contributor: Adrià Massanet <adria@codecontext.io>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
|
@ -28,7 +28,10 @@ contract LiquidPledging is LiquidPledgingBase {
|
|||
// Constructor
|
||||
//////
|
||||
|
||||
// This constructor also calls the constructor for `LiquidPledgingBase`
|
||||
/// @notice Basic constructor for LiquidPleding, also calls the
|
||||
/// LiquidPledgingBase contract
|
||||
/// @dev This constructor also calls the constructor
|
||||
/// for `LiquidPledgingBase`
|
||||
function LiquidPledging(address _vault) LiquidPledgingBase(_vault) {
|
||||
}
|
||||
|
||||
|
@ -37,12 +40,12 @@ contract LiquidPledging is LiquidPledgingBase {
|
|||
/// relevant to this Giver without delegates is increased, and a normal
|
||||
/// transfer is done to the idReceiver
|
||||
/// @param idGiver Identifier of the giver thats donating.
|
||||
/// @param idReceiver To whom it's transfered. Can be the same giver, another
|
||||
/// giver, a delegate or a project
|
||||
|
||||
function donate(uint64 idGiver, uint64 idReceiver) payable {
|
||||
/// @param idReceiver To whom it's transfered. Can be the same giver,
|
||||
/// another giver, a delegate or a project.
|
||||
function donate(uint64 idGiver, uint64 idReceiver) payable {
|
||||
if (idGiver == 0) {
|
||||
idGiver = addGiver('', '', 259200, ILiquidPledgingPlugin(0x0)); // default to 3 day commitTime
|
||||
// default to 3 day commitTime
|
||||
idGiver = addGiver("", "", 259200, ILiquidPledgingPlugin(0x0));
|
||||
}
|
||||
|
||||
PledgeAdmin storage sender = findAdmin(idGiver);
|
||||
|
@ -62,7 +65,8 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
0,
|
||||
0,
|
||||
0,
|
||||
PaymentState.Pledged);
|
||||
PaymentState.Pledged
|
||||
);
|
||||
|
||||
|
||||
Pledge storage nTo = findPledge(idPledge);
|
||||
|
@ -75,13 +79,21 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
|
||||
|
||||
/// @notice Moves value between pledges
|
||||
/// @param idSender ID of the giver, delegate or project admin that is transferring
|
||||
/// the funds from Pledge to Pledge; this admin must have permissions to move the value
|
||||
/// @param idSender ID of the giver, delegate or project admin that is
|
||||
/// transferring the funds from Pledge to Pledge; this admin must have
|
||||
/// permissions to move the value
|
||||
/// @param idPledge Id of the pledge that's moving the value
|
||||
/// @param amount Quantity of value that's being moved
|
||||
/// @param idReceiver Destination of the value, can be a giver sending to a giver or
|
||||
/// a delegate, a delegate to another delegate or a project to precommit it to that project
|
||||
function transfer(uint64 idSender, uint64 idPledge, uint amount, uint64 idReceiver) {
|
||||
/// @param idReceiver Destination of the value, can be a giver sending to
|
||||
/// a giver or a delegate, a delegate to another delegate or a project
|
||||
/// to precommit it to that project
|
||||
function transfer(
|
||||
uint64 idSender,
|
||||
uint64 idPledge,
|
||||
uint amount,
|
||||
uint64 idReceiver
|
||||
)
|
||||
{
|
||||
|
||||
idPledge = normalizePledge(idPledge);
|
||||
|
||||
|
@ -99,7 +111,11 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
} else if (receiver.adminType == PledgeAdminType.Project) {
|
||||
transferOwnershipToProject(idPledge, amount, idReceiver);
|
||||
} else if (receiver.adminType == PledgeAdminType.Delegate) {
|
||||
idPledge = undelegate(idPledge, amount, n.delegationChain.length);
|
||||
idPledge = undelegate(
|
||||
idPledge,
|
||||
amount,
|
||||
n.delegationChain.length
|
||||
);
|
||||
appendDelegate(idPledge, amount, idReceiver);
|
||||
} else {
|
||||
assert(false);
|
||||
|
@ -113,7 +129,8 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
|
||||
// If the receiver is another giver
|
||||
if (receiver.adminType == PledgeAdminType.Giver) {
|
||||
// Only accept to change to the original giver to remove all delegates
|
||||
// Only accept to change to the original giver to
|
||||
// remove all delegates
|
||||
assert(n.owner == idReceiver);
|
||||
undelegate(idPledge, amount, n.delegationChain.length);
|
||||
return;
|
||||
|
@ -125,23 +142,37 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
|
||||
// If the receiver is not in the delegate list
|
||||
if (receiverDIdx == NOTFOUND) {
|
||||
idPledge = undelegate(idPledge, amount, n.delegationChain.length - senderDIdx - 1);
|
||||
idPledge = undelegate(
|
||||
idPledge,
|
||||
amount,
|
||||
n.delegationChain.length - senderDIdx - 1
|
||||
);
|
||||
appendDelegate(idPledge, amount, idReceiver);
|
||||
|
||||
// If the receiver is already part of the delegate chain and is
|
||||
// after the sender, then all of the other delegates after the sender are
|
||||
// removed and the receiver is appended at the end of the delegation chain
|
||||
// after the sender, then all of the other delegates after the
|
||||
// sender are removed and the receiver is appended at the
|
||||
// end of the delegation chain
|
||||
} else if (receiverDIdx > senderDIdx) {
|
||||
idPledge = undelegate(idPledge, amount, n.delegationChain.length - senderDIdx - 1);
|
||||
idPledge = undelegate(
|
||||
idPledge,
|
||||
amount,
|
||||
n.delegationChain.length - senderDIdx - 1
|
||||
);
|
||||
appendDelegate(idPledge, amount, idReceiver);
|
||||
|
||||
// If the receiver is already part of the delegate chain and is
|
||||
// before the sender, then the sender and all of the other
|
||||
// delegates after the RECEIVER are revomved from the chain,
|
||||
// this is interesting because the delegate undelegates from the
|
||||
// delegates that delegated to this delegate... game theory issues? should this be allowed
|
||||
// delegates that delegated to this delegate... game theory
|
||||
// issues? should this be allowed
|
||||
} else if (receiverDIdx <= senderDIdx) {
|
||||
undelegate(idPledge, amount, n.delegationChain.length - receiverDIdx -1);
|
||||
undelegate(
|
||||
idPledge,
|
||||
amount,
|
||||
n.delegationChain.length - receiverDIdx - 1
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -149,7 +180,11 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
// If the delegate wants to support a project, they undelegate all
|
||||
// the delegates after them in the chain and choose a project
|
||||
if (receiver.adminType == PledgeAdminType.Project) {
|
||||
idPledge = undelegate(idPledge, amount, n.delegationChain.length - senderDIdx - 1);
|
||||
idPledge = undelegate(
|
||||
idPledge,
|
||||
amount,
|
||||
n.delegationChain.length - senderDIdx - 1
|
||||
);
|
||||
proposeAssignProject(idPledge, amount, idReceiver);
|
||||
return;
|
||||
}
|
||||
|
@ -158,9 +193,9 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
}
|
||||
|
||||
|
||||
/// @notice This method is used to withdraw value from the system. This can be used
|
||||
/// by the givers to avoid committing the donation or by project admin to use
|
||||
/// the Ether.
|
||||
/// @notice This method is used to withdraw value from the system.
|
||||
/// This can be used by the givers to avoid committing the donation
|
||||
/// or by project admin to use the Ether.
|
||||
/// @param idPledge Id of the pledge that wants to be withdrawn.
|
||||
/// @param amount Quantity of Ether that wants to be withdrawn.
|
||||
function withdraw(uint64 idPledge, uint amount) {
|
||||
|
@ -247,6 +282,7 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
|
||||
/// @notice Method called to cancel specfic pledge.
|
||||
/// @param idPledge Id of the pledge that should be canceled.
|
||||
/// @param amount Quantity of Ether that wants to be rolled back.
|
||||
function cancelPledge(uint64 idPledge, uint amount) {
|
||||
idPledge = normalizePledge(idPledge);
|
||||
|
||||
|
@ -270,13 +306,21 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
/// Bit mask used for dividing pledge amounts in Multi pledge methods
|
||||
uint constant D64 = 0x10000000000000000;
|
||||
|
||||
/// @notice `mTransfer` allows for multiple pledges to be transferred efficiently
|
||||
/// @param idSender ID of the giver, delegate or project admin that is transferring
|
||||
/// the funds from Pledge to Pledge. This admin must have permissions to move the value
|
||||
/// @param amount An array of pledge amounts and IDs which are extrapolated using the D64 bitmask
|
||||
/// @param idReceiver Destination of the value, can be a giver sending to a giver or
|
||||
/// a delegate, a delegate to another delegate or a project to precommit it to that project
|
||||
function mTransfer(uint64 idSender, uint[] pledgesAmounts, uint64 idReceiver) {
|
||||
/// @notice `mTransfer` allows for multiple pledges to be transferred
|
||||
/// efficiently
|
||||
/// @param idSender ID of the giver, delegate or project admin that is
|
||||
/// transferring the funds from Pledge to Pledge. This admin must have
|
||||
/// permissionsto move the value
|
||||
/// @param amount An array of pledge amounts and IDs which are extrapolated
|
||||
/// using the D64 bitmask
|
||||
/// @param idReceiver Destination of the value, can be a giver sending
|
||||
/// to a giver or a delegate or a delegate to another delegate or a
|
||||
/// project to precommit it to that project
|
||||
function mTransfer(
|
||||
uint64 idSender,
|
||||
uint[] pledgesAmounts,
|
||||
uint64 idReceiver
|
||||
) {
|
||||
for (uint i = 0; i < pledgesAmounts.length; i++ ) {
|
||||
uint64 idPledge = uint64( pledgesAmounts[i] & (D64-1) );
|
||||
uint amount = pledgesAmounts[i] / D64;
|
||||
|
@ -285,8 +329,10 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
}
|
||||
}
|
||||
|
||||
/// @notice `mWithdraw` allows for multiple pledges to be withdrawn efficiently
|
||||
/// @param amount An array of pledge amounts and IDs which are extrapolated using the D64 bitmask
|
||||
/// @notice `mWithdraw` allows for multiple pledges to be
|
||||
/// withdrawn efficiently
|
||||
/// @param amount An array of pledge amounts and IDs which are
|
||||
/// extrapolated using the D64 bitmask
|
||||
function mWithdraw(uint[] pledgesAmounts) {
|
||||
for (uint i = 0; i < pledgesAmounts.length; i++ ) {
|
||||
uint64 idPledge = uint64( pledgesAmounts[i] & (D64-1) );
|
||||
|
@ -296,8 +342,10 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
}
|
||||
}
|
||||
|
||||
/// @notice `mConfirmPayment` allows for multiple pledges to be confirmed efficiently
|
||||
/// @param amount An array of pledge amounts and IDs which are extrapolated using the D64 bitmask
|
||||
/// @notice `mConfirmPayment` allows for multiple pledges to be confirmed
|
||||
/// efficiently
|
||||
/// @param amount An array of pledge amounts and IDs which are extrapolated
|
||||
/// using the D64 bitmask
|
||||
function mConfirmPayment(uint[] pledgesAmounts) {
|
||||
for (uint i = 0; i < pledgesAmounts.length; i++ ) {
|
||||
uint64 idPledge = uint64( pledgesAmounts[i] & (D64-1) );
|
||||
|
@ -307,8 +355,10 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
}
|
||||
}
|
||||
|
||||
/// @notice `mCancelPayment` allows for multiple pledges to be cancelled efficiently
|
||||
/// @param amount An array of pledge amounts and IDs which are extrapolated using the D64 bitmask
|
||||
/// @notice `mCancelPayment` allows for multiple pledges to be cancelled
|
||||
/// efficiently
|
||||
/// @param amount An array of pledge amounts and IDs which are extrapolated
|
||||
/// using the D64 bitmask
|
||||
function mCancelPayment(uint[] pledgesAmounts) {
|
||||
for (uint i = 0; i < pledgesAmounts.length; i++ ) {
|
||||
uint64 idPledge = uint64( pledgesAmounts[i] & (D64-1) );
|
||||
|
@ -318,8 +368,10 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
}
|
||||
}
|
||||
|
||||
/// @notice `mNormalizePledge` allows for multiple pledges to be normalized efficiently
|
||||
/// @param amount An array of pledge IDs which are extrapolated using the D64 bitmask
|
||||
/// @notice `mNormalizePledge` allows for multiple pledges to be
|
||||
/// normalized efficiently
|
||||
/// @param amount An array of pledge IDs which are extrapolated using
|
||||
/// the D64 bitmask
|
||||
function mNormalizePledge(uint[] pledges) returns(uint64) {
|
||||
for (uint i = 0; i < pledges.length; i++ ) {
|
||||
uint64 idPledge = uint64( pledges[i] & (D64-1) );
|
||||
|
@ -332,9 +384,13 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
// Private methods
|
||||
///////
|
||||
|
||||
// this function is obvious, but it can also be called to undelegate everyone
|
||||
// by setting yourself as the idReceiver
|
||||
function transferOwnershipToProject(uint64 idPledge, uint amount, uint64 idReceiver) internal {
|
||||
// this function is obvious, but it can also be called to undelegate
|
||||
// everyone by setting yourself as the idReceiver
|
||||
function transferOwnershipToProject(
|
||||
uint64 idPledge,
|
||||
uint amount,
|
||||
uint64 idReceiver
|
||||
) internal {
|
||||
Pledge storage n = findPledge(idPledge);
|
||||
|
||||
require(getPledgeLevel(n) < MAX_INTERPROJECT_LEVEL);
|
||||
|
@ -355,9 +411,13 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
oldPledge,
|
||||
PaymentState.Pledged);
|
||||
doTransfer(idPledge, toPledge, amount);
|
||||
}
|
||||
}
|
||||
|
||||
function transferOwnershipToGiver(uint64 idPledge, uint amount, uint64 idReceiver) internal {
|
||||
function transferOwnershipToGiver(
|
||||
uint64 idPledge,
|
||||
uint amount,
|
||||
uint64 idReceiver
|
||||
) internal {
|
||||
uint64 toPledge = findOrCreatePledge(
|
||||
idReceiver,
|
||||
new uint64[](0),
|
||||
|
@ -368,10 +428,14 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
doTransfer(idPledge, toPledge, amount);
|
||||
}
|
||||
|
||||
function appendDelegate(uint64 idPledge, uint amount, uint64 idReceiver) internal {
|
||||
function appendDelegate(
|
||||
uint64 idPledge,
|
||||
uint amount,
|
||||
uint64 idReceiver
|
||||
) internal {
|
||||
Pledge storage n= findPledge(idPledge);
|
||||
|
||||
require(n.delegationChain.length < MAX_DELEGATES); //TODO change to revert and say the error
|
||||
require(n.delegationChain.length < MAX_DELEGATES);
|
||||
uint64[] memory newDelegationChain = new uint64[](n.delegationChain.length + 1);
|
||||
for (uint i=0; i<n.delegationChain.length; i++) {
|
||||
newDelegationChain[i] = n.delegationChain[i];
|
||||
|
@ -391,7 +455,11 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
}
|
||||
|
||||
/// @param q Number of undelegations
|
||||
function undelegate(uint64 idPledge, uint amount, uint q) internal returns (uint64){
|
||||
function undelegate(
|
||||
uint64 idPledge,
|
||||
uint amount,
|
||||
uint q
|
||||
) internal returns (uint64){
|
||||
Pledge storage n = findPledge(idPledge);
|
||||
uint64[] memory newDelegationChain = new uint64[](n.delegationChain.length - q);
|
||||
for (uint i=0; i<n.delegationChain.length - q; i++) {
|
||||
|
@ -410,7 +478,11 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
}
|
||||
|
||||
|
||||
function proposeAssignProject(uint64 idPledge, uint amount, uint64 idReceiver) internal {// Todo rename
|
||||
function proposeAssignProject(
|
||||
uint64 idPledge,
|
||||
uint amount,
|
||||
uint64 idReceiver
|
||||
) internal {// Todo rename
|
||||
Pledge storage n = findPledge(idPledge);
|
||||
|
||||
require(getPledgeLevel(n) < MAX_SUBPROJECT_LEVEL);
|
||||
|
@ -487,42 +559,105 @@ function donate(uint64 idGiver, uint64 idReceiver) payable {
|
|||
// Plugins
|
||||
/////////////
|
||||
|
||||
function callPlugin(bool before, uint64 adminId, uint64 fromPledge, uint64 toPledge, uint64 context, uint amount) internal returns (uint allowedAmount) {
|
||||
function callPlugin(
|
||||
bool before,
|
||||
uint64 adminId,
|
||||
uint64 fromPledge,
|
||||
uint64 toPledge,
|
||||
uint64 context,
|
||||
uint amount
|
||||
) internal returns (uint allowedAmount) {
|
||||
uint newAmount;
|
||||
allowedAmount = amount;
|
||||
PledgeAdmin storage admin = findAdmin(adminId);
|
||||
if ((address(admin.plugin) != 0) && (allowedAmount > 0)) {
|
||||
if (before) {
|
||||
newAmount = admin.plugin.beforeTransfer(adminId, fromPledge, toPledge, context, amount);
|
||||
newAmount = admin.plugin.beforeTransfer(
|
||||
adminId,
|
||||
fromPledge,
|
||||
toPledge,
|
||||
context,
|
||||
amount
|
||||
);
|
||||
require(newAmount <= allowedAmount);
|
||||
allowedAmount = newAmount;
|
||||
} else {
|
||||
admin.plugin.afterTransfer(adminId, fromPledge, toPledge, context, amount);
|
||||
admin.plugin.afterTransfer(
|
||||
adminId,
|
||||
fromPledge,
|
||||
toPledge,
|
||||
context,
|
||||
amount
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function callPluginsPledge(bool before, uint64 idPledge, uint64 fromPledge, uint64 toPledge, uint amount) internal returns (uint allowedAmount) {
|
||||
function callPluginsPledge(
|
||||
bool before,
|
||||
uint64 idPledge,
|
||||
uint64 fromPledge,
|
||||
uint64 toPledge,
|
||||
uint amount
|
||||
) internal returns (uint allowedAmount) {
|
||||
uint64 offset = idPledge == fromPledge ? 0 : 256;
|
||||
allowedAmount = amount;
|
||||
Pledge storage n = findPledge(idPledge);
|
||||
|
||||
allowedAmount = callPlugin(before, n.owner, fromPledge, toPledge, offset, allowedAmount);
|
||||
allowedAmount = callPlugin(
|
||||
before,
|
||||
n.owner,
|
||||
fromPledge,
|
||||
toPledge,
|
||||
offset,
|
||||
allowedAmount
|
||||
);
|
||||
|
||||
for (uint64 i=0; i<n.delegationChain.length; i++) {
|
||||
allowedAmount = callPlugin(before, n.delegationChain[i], fromPledge, toPledge, offset + i+1, allowedAmount);
|
||||
allowedAmount = callPlugin(
|
||||
before,
|
||||
n.delegationChain[i],
|
||||
fromPledge,
|
||||
toPledge,
|
||||
offset + i+1,
|
||||
allowedAmount
|
||||
);
|
||||
}
|
||||
|
||||
if (n.intendedProject > 0) {
|
||||
allowedAmount = callPlugin(before, n.intendedProject, fromPledge, toPledge, offset + 255, allowedAmount);
|
||||
allowedAmount = callPlugin(
|
||||
before,
|
||||
n.intendedProject,
|
||||
fromPledge,
|
||||
toPledge,
|
||||
offset + 255,
|
||||
allowedAmount
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function callPlugins(bool before, uint64 fromPledge, uint64 toPledge, uint amount) internal returns (uint allowedAmount) {
|
||||
function callPlugins(
|
||||
bool before,
|
||||
uint64 fromPledge,
|
||||
uint64 toPledge,
|
||||
uint amount
|
||||
) internal returns (uint allowedAmount) {
|
||||
allowedAmount = amount;
|
||||
|
||||
allowedAmount = callPluginsPledge(before, fromPledge, fromPledge, toPledge, allowedAmount);
|
||||
allowedAmount = callPluginsPledge(before, toPledge, fromPledge, toPledge, allowedAmount);
|
||||
allowedAmount = callPluginsPledge(
|
||||
before,
|
||||
fromPledge,
|
||||
fromPledge,
|
||||
toPledge,
|
||||
allowedAmount
|
||||
);
|
||||
allowedAmount = callPluginsPledge(
|
||||
before,
|
||||
toPledge,
|
||||
fromPledge,
|
||||
toPledge,
|
||||
allowedAmount
|
||||
);
|
||||
}
|
||||
|
||||
/////////////
|
||||
|
|
Loading…
Reference in New Issue