lbry-desktop/ui/js/rewards.js

94 lines
2.7 KiB
JavaScript
Raw Normal View History

import lbry from './lbry.js';
import lbryio from './lbryio.js';
2017-04-10 14:32:40 +02:00
function rewardMessage(type, amount) {
return {
2017-04-12 22:23:20 +02:00
new_developer: "You received ${amount} for registering as a new developer.",
2017-04-10 14:32:40 +02:00
new_user: `You received ${amount} LBC new user reward.`,
2017-04-12 22:23:20 +02:00
confirm_email: "You received ${amount} LBC for verifying your email address.",
first_channel: "You received ${amount} LBC for creating a publisher identity.",
first_purchase: "You received ${amount} LBC for making your first purchase.",
first_publish: "You received ${amount} LBC for making your first publication.",
2017-04-10 14:32:40 +02:00
}[type];
}
const rewards = {};
2017-04-12 22:23:20 +02:00
rewards.TYPE_NEW_DEVELOPER = "new_developer",
rewards.TYPE_NEW_USER = "new_user",
rewards.TYPE_CONFIRM_EMAIL = "confirm_email",
rewards.TYPE_FIRST_CHANNEL = "first_channel",
rewards.TYPE_FIRST_PURCHASE = "first_purchase",
rewards.TYPE_FIRST_PUBLISH = "first_publish";
rewards.claimReward = function (type) {
2017-04-13 20:52:26 +02:00
function requestReward(resolve, reject, params) {
if (!lbryio.enabled) {
reject(new Error("Rewards are not enabled."))
return;
}
2017-04-13 20:52:26 +02:00
lbryio.call('reward', 'new', params, 'post').then(({RewardAmount}) => {
const
message = rewardMessage(type, RewardAmount),
result = {
type: type,
amount: RewardAmount,
message: message
};
// Display global notice
document.dispatchEvent(new CustomEvent('globalNotice', {
detail: {
message: message,
linkText: "Show All",
linkTarget: "?rewards",
isError: false,
},
}));
// Add more events here to display other places
resolve(result);
}, reject);
}
return new Promise((resolve, reject) => {
lbry.get_new_address().then((address) => {
const params = {
reward_type: type,
wallet_address: address,
};
2017-04-13 20:52:26 +02:00
2017-04-12 22:23:20 +02:00
switch (type) {
2017-04-13 20:52:26 +02:00
case rewards.TYPE_FIRST_CHANNEL:
lbry.claim_list_mine().then(function(channels) {
if (channels.length) {
params.transaction_id = channels[0].txid;
requestReward(resolve, reject, params)
} else {
reject(new Error("Please create a channel identity first."))
}
}).catch(reject)
2017-04-12 22:23:20 +02:00
break;
case 'first_purchase':
2017-04-13 20:52:26 +02:00
// lbry.claim_list_mine().then(function(channels) {
// if (channels.length) {
// requestReward(resolve, reject, {transaction_id: channels[0].txid})
// }
// }).catch(reject)
2017-04-12 22:23:20 +02:00
break;
case 'first_channel':
//params.transaction_id = RelevantTransactionID;
break;
2017-04-13 20:52:26 +02:00
default:
requestReward(resolve, reject, params);
}
});
});
}
export default rewards;