Skip to content
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
27 changes: 13 additions & 14 deletions src/core/ElasticDAO.sol
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ contract ElasticDAO is ReentryProtection {
* Based on the current state of the DAO, capitalDelta, deltaE, mDash are calulated,
* after which _deltaLambda is minted for the address calling the function.
*
* @param _deltaLambda - the amount of lambda minted to the address
*
* @dev documentation and further math regarding capitalDelta, deltaE,
* mDash can be found at ../libraries/ElasticMath.sol
* @dev emits the JoinDAO event
Expand All @@ -212,7 +210,7 @@ contract ElasticDAO is ReentryProtection {
* must be sent in the transaction by the calling address
* The token contract should be successfully be able to mint _deltaLambda
*/
function join(uint256 _deltaLambda)
function join()
external
payable
onlyAfterSummoning
Expand All @@ -221,10 +219,6 @@ contract ElasticDAO is ReentryProtection {
{
Token.Instance memory token = _getToken();

require(
_deltaLambda <= token.maxLambdaPurchase,
'ElasticDAO: Cannot purchase those many lambda at once'
);

ElasticGovernanceToken tokenContract = ElasticGovernanceToken(token.uuid);
uint256 capitalDelta =
Expand All @@ -236,20 +230,18 @@ contract ElasticDAO is ReentryProtection {
);
uint256 deltaE =
ElasticMath.deltaE(
_deltaLambda,
token.maxLambdaPurchase,
capitalDelta,
token.k,
token.elasticity,
token.lambda,
token.m
);

if (deltaE != msg.value) {
revert('ElasticDAO: Incorrect ETH amount');
}
require(msg.value >= deltaE, 'ElasticDAO: Incorrect ETH amount');

// mdash
uint256 lambdaDash = SafeMath.add(_deltaLambda, token.lambda);
uint256 lambdaDash = SafeMath.add(token.maxLambdaPurchase, token.lambda);
uint256 mDash = ElasticMath.mDash(lambdaDash, token.lambda, token.m);

// serialize the token
Expand All @@ -259,9 +251,16 @@ contract ElasticDAO is ReentryProtection {
tokenStorage.serialize(token);

// tokencontract mint shares
bool success = tokenContract.mintShares(msg.sender, _deltaLambda);
bool success = tokenContract.mintShares(msg.sender, token.maxLambdaPurchase);
require(success, 'ElasticDAO: Mint Shares Failed during Join');
emit JoinDAO(msg.sender, _deltaLambda, msg.value);

// return extra ETH
if(success && msg.value > deltaE) {
(success, ) = msg.sender.call{ value: SafeMath.sub(msg.value, deltaE) }('');
require(success, 'ElasticDAO: TransactionFailed');
}

emit JoinDAO(msg.sender, token.maxLambdaPurchase, msg.value);
}

/**
Expand Down
29 changes: 21 additions & 8 deletions test/capitalDeltaTest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { expect } = require('chai');

const { capitalDelta, deltaE, mDash } = require('@elastic-dao/sdk');
const BigNumber = require('bignumber.js');

const { ONE } = require('./constants');
const { ethBalance, signers, summonedDAO } = require('./helpers');
Expand Down Expand Up @@ -30,10 +29,17 @@ describe('ElasticDAO: CapitalDelta value of a token', () => {

// calculate deltaE using capital Delta to buy ONE_TENTH shares
// deltaE = capitalDelta * k * ( (lambdaDash*mDash*revamp) - (lambda*m) )
const dE = deltaE(0.1, cDelta, token.k, token.elasticity, token.lambda, token.m);
const dE = deltaE(
token.maxLambdaPurchase,
cDelta,
token.k,
token.elasticity,
token.lambda,
token.m,
);

// send that value of deltaE to joinDAO to buy ONE_TENTH shares
const tx = dao.elasticDAO.join(0.1, { value: dE });
const tx = dao.elasticDAO.join({ value: dE });

// transaction reverts with 'ElasticDAO: Incorrect ETH amount'
await expect(tx).to.be.revertedWith('ElasticDAO: Incorrect ETH amount');
Expand All @@ -53,13 +59,20 @@ describe('ElasticDAO: CapitalDelta value of a token', () => {

// calculate deltaE using capital Delta to buy ONE_TENTH shares
// deltaE = capitalDelta * k * ( (lambdaDash*mDash*revamp) - (lambda*m) )
const deltaLambda = BigNumber(0.1);
const lambdaDash = token.lambda.plus(deltaLambda);
const dE = deltaE(deltaLambda, cDelta, token.k, token.elasticity, token.lambda, token.m);
const lambdaDash = token.lambda.plus(token.maxLambdaPurchase);
const dE = deltaE(
token.maxLambdaPurchase,
cDelta,
token.k,
token.elasticity,
token.lambda,
token.m,
);

const mD = mDash(lambdaDash, token.lambda, token.m);

// send that value of deltaE to joinDAO to buy ONE_TENTH shares
await dao.elasticDAO.join(deltaLambda, { value: dE });
// send that value of deltaE to joinDAO to buy ONE share
await dao.elasticDAO.join({ value: dE });
await token.refresh();

// post join check the following values:
Expand Down
45 changes: 45 additions & 0 deletions test/elasticDAOTests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { expect } = require('chai');
const { ethers } = require('ethers');
const { capitalDelta, deltaE, mDash } = require('@elastic-dao/sdk');
const { deployments } = require('hardhat');
const hre = require('hardhat').ethers;
const { ethBalance, SDK, signers, summoners, summonedDAO } = require('./helpers');
Expand Down Expand Up @@ -399,5 +400,49 @@ describe('ElasticDAO: Core', () => {
}
}
});

it('Should allow a new member to join as long as they send more ETH than deltaE', async () => {
dao = await summonedDAO();
const token = await dao.token();
// get the eth balance of elasticDAO
const ethBalanceElasticDAOBeforeJoin = await ethBalance(dao.uuid);

// get the T value of the token
const totalSupplyOfToken = await dao.elasticGovernanceToken.totalSupply();

// calculate capital Delta
const cDelta = capitalDelta(ethBalanceElasticDAOBeforeJoin, totalSupplyOfToken);

// calculate deltaE using capital Delta to buy ONE_TENTH shares
// deltaE = capitalDelta * k * ( (lambdaDash*mDash*revamp) - (lambda*m) )
const lambdaDash = token.lambda.plus(token.maxLambdaPurchase);
const dE = deltaE(
token.maxLambdaPurchase,
cDelta,
token.k,
token.elasticity,
token.lambda,
token.m,
);

const mD = mDash(lambdaDash, token.lambda, token.m);

// send that value of deltaE to joinDAO to buy ONE share + 1
await dao.elasticDAO.join({ value: dE.plus(1) });
await token.refresh();

// post join check the following values:
// check the m value- after join,previous mDash should be current m
await expect(token.m.toString()).to.equal(mD.toString());
await expect(token.lambda.toString()).to.equal(lambdaDash.toString());

// check the the total eth - which should be initial eth, plus delta e
const ethBalanceElasticDAOAfterJoin = await ethBalance(dao.uuid);

const expectedEthInElasticDAOAfterJoin = ethBalanceElasticDAOBeforeJoin.plus(dE);
await expect(ethBalanceElasticDAOAfterJoin.toString()).to.equal(
expectedEthInElasticDAOAfterJoin.toString(),
);
});
});
});