/
dgdSwapContract.sol
141 lines (120 loc) · 5.95 KB
/
dgdSwapContract.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Version 0.21
// This swap contract was created by Attores and released under a GPL license.
// Please note that this contract is still in beta. Hence use only the amounts that you can afford to lose in case of an unforeseen bug.
// Visit attores.com for more contracts and Smart contract as a Service
// This is the standard token interface
contract TokenInterface {
struct User {
bool locked;
uint256 balance;
uint256 badges;
mapping (address => uint256) allowed;
}
mapping (address => User) users;
mapping (address => uint256) balances;
mapping (address => mapping (address => uint256)) allowed;
mapping (address => bool) seller;
address config;
address owner;
address dao;
bool locked;
/// @return total amount of tokens
uint256 public totalSupply;
uint256 public totalBadges;
/// @param _owner The address from which the balance will be retrieved
/// @return The balance
function balanceOf(address _owner) constant returns (uint256 balance);
/// @param _owner The address from which the badge count will be retrieved
/// @return The badges count
function badgesOf(address _owner) constant returns (uint256 badge);
/// @notice send `_value` tokens to `_to` from `msg.sender`
/// @param _to The address of the recipient
/// @param _value The amount of tokens to be transfered
/// @return Whether the transfer was successful or not
function transfer(address _to, uint256 _value) returns (bool success);
/// @notice send `_value` badges to `_to` from `msg.sender`
/// @param _to The address of the recipient
/// @param _value The amount of tokens to be transfered
/// @return Whether the transfer was successful or not
function sendBadge(address _to, uint256 _value) returns (bool success);
/// @notice send `_value` tokens to `_to` from `_from` on the condition it is approved by `_from`
/// @param _from The address of the sender
/// @param _to The address of the recipient
/// @param _value The amount of tokens to be transfered
/// @return Whether the transfer was successful or not
function transferFrom(address _from, address _to, uint256 _value) returns (bool success);
/// @notice `msg.sender` approves `_spender` to spend `_value` tokens on its behalf
/// @param _spender The address of the account able to transfer the tokens
/// @param _value The amount of tokens to be approved for transfer
/// @return Whether the approval was successful or not
function approve(address _spender, uint256 _value) returns (bool success);
/// @param _owner The address of the account owning tokens
/// @param _spender The address of the account able to transfer the tokens
/// @return Amount of remaining tokens of _owner that _spender is allowed to spend
function allowance(address _owner, address _spender) constant returns (uint256 remaining);
/// @notice mint `_amount` of tokens to `_owner`
/// @param _owner The address of the account receiving the tokens
/// @param _amount The amount of tokens to mint
/// @return Whether or not minting was successful
function mint(address _owner, uint256 _amount) returns (bool success);
function calculateTxFee(uint256 _value, address _user) public returns (uint256);
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event SendBadge(address indexed _from, address indexed _to, uint256 _amount);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
contract swap{
address public beneficiary;
TokenInterface public tokenObj;
uint public price_token;
uint256 public WEI_PER_FINNEY = 1000000000000000;
uint public BILLION = 1000000000;
uint public expiryDate;
uint public balance;
// Constructor function for this contract. Called during contract creation
function swap(address sendEtherTo, address adddressOfToken, uint tokenPriceInFinney_1000FinneyIs_1Ether, uint durationInDays){
beneficiary = sendEtherTo;
tokenObj = TokenInterface(adddressOfToken);
price_token = tokenPriceInFinney_1000FinneyIs_1Ether * WEI_PER_FINNEY;
expiryDate = now + durationInDays * 1 days;
}
function(){
if (now >= expiryDate) throw;
// Dividing by Billion here to cater for the decimal places
var tokens_to_send = (msg.value * BILLION) / price_token;
balance = tokenObj.allowance(beneficiary, this);
address payee = msg.sender;
if (balance >= tokens_to_send){
tokenObj.transferFrom(beneficiary, msg.sender, tokens_to_send);
beneficiary.send(msg.value);
} else {
tokenObj.transferFrom(beneficiary, msg.sender, balance);
uint amountReturned = ((tokens_to_send - balance) * price_token) / BILLION;
payee.send(amountReturned);
beneficiary.send(msg.value - amountReturned);
}
}
modifier afterExpiry() { if (now >= expiryDate) _ }
modifier ifBeneficiary() {
if (beneficiary != msg.sender) {
throw;
} else {
_
}
}
// To kill the swap after expiry date
function killSwap() ifBeneficiary afterExpiry{
suicide(beneficiary);
}
// This function is a fail-safe in case someone "sends" the tokens to this contract instead of "approving" them
function emergencyWithdrawal(address token) ifBeneficiary{
uint balance = TokenInterface(token).balanceOf(this);
// In case of DGX, there is a 0.13% TXN fee, hence we need to do this
if (token == 0x55b9a11c2e8351b4ffc7b11561148bfac9977855){
uint txnFee = TokenInterface(token).calculateTxFee(balance, this);
uint amountReturned = balance - txnFee;
TokenInterface(token).transfer(beneficiary, amountReturned);
} else {
TokenInterface(token).transfer(beneficiary, balance);
}
}
}