forked from jordanteague/SimpleBondingCurve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearBondingCurve.sol
88 lines (57 loc) · 2.23 KB
/
LinearBondingCurve.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.8.0;
import "./ERC20.sol";
contract LinearBondingCurve is ERC20 {
uint256 public startingPrice; // wei
uint256 public blockSize; // wei
uint256 public blockPriceIncrement;
constructor() ERC20('MyDAO', 'TEST', 18) {
startingPrice = 100; // hardcoded values for ease of test deployment
blockSize = 10;
blockPriceIncrement = 10;
}
function buy(uint256 amount_) public payable {
uint256 estPrice = estimatePrice(amount_);
require(msg.value >= estPrice, 'INSUFFICIENT_FUNDS');
_mint(msg.sender, amount_);
}
// separate external functions for testing purposes only
function getCurrentBlock() public view returns (uint256) {
return (totalSupply / blockSize);
}
function getRemainingInBlock() public view returns (uint256) {
uint256 used = totalSupply % blockSize;
uint256 remaining = blockSize - used;
return remaining;
}
function getCurrentPrice() public view returns (uint256) {
uint256 currentPrice = startingPrice;
uint256 currentBlock = getCurrentBlock();
for (uint256 i = 0; i < currentBlock; i++) {
currentPrice += blockPriceIncrement;
}
return currentPrice;
}
function estimatePrice(uint256 amount_) public view returns (uint256) {
uint256 remainingInBlock = getRemainingInBlock();
uint256 currentPrice = getCurrentPrice();
uint256 estTotal;
if (amount_ <= remainingInBlock) {
estTotal = amount_ * currentPrice;
} else {
estTotal += remainingInBlock * currentPrice;
currentPrice += blockPriceIncrement;
uint256 remainingAmount = amount_ - remainingInBlock;
uint256 remainder = remainingAmount % blockSize;
uint256 blocksRemaining = remainingAmount / blockSize;
for (uint256 i = 0; i < blocksRemaining; i++) {
estTotal += currentPrice * blockSize;
currentPrice += blockPriceIncrement;
}
if (remainder != 0) {
estTotal += remainder * currentPrice;
}
}
return estTotal;
}
}