-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusicPlatformContract.sol
385 lines (334 loc) · 13.6 KB
/
MusicPlatformContract.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
pragma solidity ^0.4.11;
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
function mul(uint a, uint b) internal pure returns (uint) {
uint c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function div(uint a, uint b) internal pure returns (uint) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
function sub(uint a, uint b) internal pure returns (uint) {
assert(b <= a);
return a - b;
}
function add(uint a, uint b) internal pure returns (uint) {
uint c = a + b;
assert(c >= a);
return c;
}
}
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
function Ownable() public {
owner = msg.sender;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) onlyOwner public {
require(newOwner != address(0));
OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
/**
* @title ERC20Basic
* @dev Simpler version of ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/179
*/
contract ERC20Interface {
function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner) public constant returns (uint balance);
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
function mint(address from, address to, uint tokens) public;
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
contract MusicContract {
using SafeMath for uint256;
struct Music {
address musician;
uint musicId;
string musicLink;
bool marketType; //false for sponsor, true for votes
uint totalAmountForUnlock;
uint totalEarning;
uint amountLeftForUnlock;
uint amountToBePaid;
bool isUnlocked;
}
struct Voter {
address publicKey;
uint amountEarned;
}
struct Sponsor {
address publicKey;
uint amountEarned;
uint amountPaid;
}
struct VoteMusicPayoutScheme {
uint musicianPercentage; //57
uint voterPercentage;// 35
uint systemPercentage;// 8
}
struct SponsorPayoutScheme {
uint sponsorPercentage;// 45
uint musicianPercentage;// 37
uint voterPercentage; // 10
uint systemPercentage; // 8
}
// The token that would be sold using this contract
ERC20Interface public token;
//Objects for use within program
VoteMusicPayoutScheme voteMusicPayoutSchemeObj;
SponsorPayoutScheme sponsorPayoutSchemeObj;
Music music;
Sponsor sponsor;
Voter voter;
uint counter = 0;
address public wallet;
mapping (uint=>Voter[]) musicVoterList;
mapping (uint=>Sponsor[]) musicSponsorList;
mapping (uint=>Music) musicList;
uint localIntAsPerNeed;
address localAddressAsPerNeed;
Voter[] voters;
Sponsor[] sponsors;
function MusicContract(address _wallet,address _tokenAddress) public {
wallet = _wallet;
token = ERC20Interface(_tokenAddress);
setup();
}
// fallback function can be used to buy tokens
function () public payable {
revert();
}
function setup() internal {
voteMusicPayoutSchemeObj = VoteMusicPayoutScheme({musicianPercentage:57, voterPercentage:35, systemPercentage:8});
sponsorPayoutSchemeObj = SponsorPayoutScheme({sponsorPercentage:45, musicianPercentage: 37, voterPercentage:10, systemPercentage:8});
}
function UploadMusic(uint muId, string lnk, address muPublicKey,bool unlocktype,uint amount, uint uploadTokenAmount) public
{
require(msg.sender == wallet);
token.mint(muPublicKey,wallet,uploadTokenAmount*10**18); //tokens deducted from advertiser's wallet
require(musicList[muId].musicId == 0);
//Add to music struct
music = Music ({
musician : muPublicKey,
musicId : muId,
musicLink : lnk,
marketType : unlocktype,
totalEarning : 0,
totalAmountForUnlock : amount * 10 ** 18,
amountLeftForUnlock : amount * 10 ** 18,
amountToBePaid : uploadTokenAmount * 10 **18,
isUnlocked : false
});
musicList[muId] = music;
}
function DownloadMusic(uint musId, address senderId, uint tokenAmount) public returns (bool goAhead)
{
require(msg.sender == wallet);
require(musicList[musId].musicId == musId);
require(musicList[musId].isUnlocked == true);
token.mint(senderId,wallet,tokenAmount*10**18);
musicList[musId].totalEarning = musicList[musId].totalEarning.add(tokenAmount);
musicList[musId].amountToBePaid = musicList[musId].amountToBePaid.add(tokenAmount);
goAhead = true;
}
function DoSponsorPayout(Music musicObj) private
{
//logString("In sponsor payout function");
//sponsor
localIntAsPerNeed = musicObj.musicId;
sponsors = musicSponsorList[localIntAsPerNeed];
//calculating sponsor payout
localIntAsPerNeed = sponsorPayoutSchemeObj.sponsorPercentage;
uint sponsorPayout = musicObj.amountToBePaid.mul(localIntAsPerNeed);
sponsorPayout = sponsorPayout.div(100);
//calculating voter payout
voters = musicVoterList[musicObj.musicId];
localIntAsPerNeed = sponsorPayoutSchemeObj.voterPercentage;
uint voterPayout = musicObj.amountToBePaid.mul(localIntAsPerNeed);
voterPayout = voterPayout.div(100);
//calculating musician payout
localIntAsPerNeed = sponsorPayoutSchemeObj.musicianPercentage;
uint musicianPayout = musicObj.amountToBePaid.mul(localIntAsPerNeed);
musicianPayout = musicianPayout.div(100);
//calculating system payout
localIntAsPerNeed = sponsorPayoutSchemeObj.systemPercentage;
uint systemPayout = musicObj.amountToBePaid.mul(localIntAsPerNeed);
systemPayout = systemPayout.div(100);
//doing sponsor payout
for (counter=0;counter<sponsors.length;counter++)
{
//Find the percentage
localIntAsPerNeed = sponsors[counter].amountPaid.mul(100);
localIntAsPerNeed = localIntAsPerNeed.div(musicObj.totalAmountForUnlock);
uint amtToSend = sponsorPayout.mul(localIntAsPerNeed);
amtToSend = amtToSend.div(100);
token.mint(wallet, sponsors[counter].publicKey, amtToSend);
sponsors[counter].amountEarned = sponsors[counter].amountEarned.add(amtToSend);
musicObj.amountToBePaid = musicObj.amountToBePaid.sub(amtToSend);
}
//logString("paid out to sponsors");
//doing voter payout
if (voters.length>0)
{
uint perVoterPayout = voterPayout.div(voters.length);
for (counter=0;counter<voters.length;counter++)
{
token.mint(wallet, voters[counter].publicKey, perVoterPayout);
voters[counter].amountEarned = voters[counter].amountEarned.add(perVoterPayout);
musicObj.amountToBePaid = musicObj.amountToBePaid.sub(perVoterPayout);
}
//logString("paid out to voters");
}
else
{
musicObj.amountToBePaid = musicObj.amountToBePaid.sub(voterPayout);
}
//doing musician payout
localAddressAsPerNeed = musicObj.musician;
token.mint(wallet,localAddressAsPerNeed,musicianPayout);
musicObj.amountToBePaid = musicObj.amountToBePaid.sub(musicianPayout);
//logString("paid out to musicians");
//catering for system payout - no token transfers as the tokens are already in the owner wallet
musicObj.amountToBePaid = musicObj.amountToBePaid.sub(systemPayout);
//logString("paid out to system");
require(musicObj.amountToBePaid == 0);
}
function DoVoterPayout(Music musicObj) private
{
uint j = 0;
//sponsor
//logString("In voter payout function");
//calculating voter payout
voters = musicVoterList[musicObj.musicId];
localIntAsPerNeed = voteMusicPayoutSchemeObj.voterPercentage;
uint voterPayout = musicObj.amountToBePaid.mul(localIntAsPerNeed);
voterPayout = voterPayout.div(100);
uint perVoterPayout = voterPayout.div(voters.length);
//calculating musician payout
localIntAsPerNeed = voteMusicPayoutSchemeObj.musicianPercentage;
uint musicianPayout = musicObj.amountToBePaid.mul(localIntAsPerNeed);
musicianPayout = musicianPayout.div(100);
//calculating system payout
localIntAsPerNeed = voteMusicPayoutSchemeObj.systemPercentage;
uint systemPayout = musicObj.amountToBePaid.mul(localIntAsPerNeed);
systemPayout = systemPayout.div(100);
//doing voter payout
for (j=0;j<voters.length;j++)
{
token.mint(wallet,voters[j].publicKey, perVoterPayout);
voters[j].amountEarned = voters[j].amountEarned.add(perVoterPayout);
musicObj.amountToBePaid = musicObj.amountToBePaid.sub(perVoterPayout);
}
//logString("voter payout done");
//doing musician payout
token.mint(wallet,musicObj.musician,musicianPayout);
musicObj.amountToBePaid = musicObj.amountToBePaid.sub(musicianPayout);
//logString("musician payout done");
//catering for system payout - not doing manual transfer as all the tokens are already in the wallet
musicObj.amountToBePaid = musicObj.amountToBePaid.sub(systemPayout);
//logString("system payout done");
require(musicObj.amountToBePaid == 0);
}
function DoMusicPayout (uint musId) public
{
require(msg.sender == wallet);
require(musicList[musId].musicId == musId);
require(musicList[musId].isUnlocked == true);
require(musicList[musId].amountToBePaid > 0);
require(token.balanceOf(wallet)>=musicList[musId].amountToBePaid);
//logString("In music payout");
bool unlock = musicList[musId].marketType;
if (unlock == false)
{
//unlock type is sponsor
//logString("Unlock type is sponsor");
DoSponsorPayout(musicList[musId]);
musicList[musId].amountToBePaid = 0;
}
else
{
//unlock type is voter
//logString("Unlock type is voter");
DoVoterPayout(musicList[musId]);
musicList[musId].amountToBePaid = 0;
}
}
function SponsorMusic(uint musId, uint sponsorAmount, address sponsorAddress) public
{
//msg.sender is the sponsor
sponsorAmount = sponsorAmount * 10 ** 18;
require(token.balanceOf(sponsorAddress) > sponsorAmount);
require (musicList[musId].musicId == musId);
require (musicList[musId].isUnlocked == false);
require(musicList[musId].marketType == false);
require (musicList[musId].amountLeftForUnlock>=sponsorAmount);
token.mint(sponsorAddress,wallet,sponsorAmount);
//logString("sufficient payment done");
musicList[musId].amountLeftForUnlock = musicList[musId].amountLeftForUnlock.sub(sponsorAmount);
musicList[musId].amountToBePaid = musicList[musId].amountToBePaid.add(sponsorAmount);
sponsor = Sponsor({
publicKey : msg.sender,
amountEarned : 0,
amountPaid : sponsorAmount
});
musicSponsorList[musId].push(sponsor);
//logString("sponsor added");
if (musicList[musId].amountLeftForUnlock == 0)
{
musicList[musId].isUnlocked = true;
//logString("music unlocked");
}
}
function VoteMusic(uint musId, address voterPublicKey) public
{
require(musicList[musId].musicId == musId);
require(musicList[musId].isUnlocked == false);
//logString("music found");
voter = Voter({publicKey: voterPublicKey, amountEarned : 0});
musicVoterList[musId].push(voter);
//logString("voter added");
}
function unlockVoterMusic(uint musId) public
{
require(msg.sender == wallet);
require(musicList[musId].musicId == musId);
musicList[musId].isUnlocked = true;
}
function getTokenBalance() public constant returns (uint) {
return token.balanceOf(msg.sender);
}
function changeWalletAddress(address newWallet) public
{
require(msg.sender == wallet);
wallet = newWallet;
}
}