lbry-desktop/src/renderer/rewards.js

102 lines
3 KiB
JavaScript
Raw Normal View History

2018-04-19 20:49:47 +02:00
import { Lbry, doNotify } from 'lbry-redux';
import Lbryio from 'lbryio';
const rewards = {};
rewards.claimReward = type => {
2017-06-08 23:15:34 +02:00
function requestReward(resolve, reject, params) {
if (!Lbryio.enabled) {
reject(new Error(__('Rewards are not enabled.')));
2017-06-08 23:15:34 +02:00
return;
}
Lbryio.call('reward', 'new', params, 'post').then(reward => {
const message =
reward.reward_notification || `You have claimed a ${reward.reward_amount} LBC reward.`;
2017-06-08 23:15:34 +02:00
// Display global notice
2018-04-19 20:49:47 +02:00
const action = doNotify({
2017-06-08 23:15:34 +02:00
message,
linkText: __('Show All'),
linkTarget: '/rewards',
2017-06-08 23:15:34 +02:00
isError: false,
displayType: ['snackbar'],
2017-06-08 23:15:34 +02:00
});
window.app.store.dispatch(action);
// Add more events here to display other places
resolve(reward);
}, reject);
}
2017-06-08 23:15:34 +02:00
return new Promise((resolve, reject) => {
Lbry.wallet_unused_address().then(address => {
2017-06-08 23:15:34 +02:00
const params = {
reward_type: type,
wallet_address: address,
};
switch (type) {
case rewards.TYPE_FIRST_CHANNEL:
Lbry.claim_list_mine()
2017-12-13 22:36:30 +01:00
.then(claims => {
const claim = claims
.reverse()
.find(
foundClaim =>
foundClaim.name.length &&
foundClaim.name[0] === '@' &&
foundClaim.txid.length &&
foundClaim.category === 'claim'
2017-06-08 23:15:34 +02:00
);
if (claim) {
params.transaction_id = claim.txid;
requestReward(resolve, reject, params);
} else {
reject(new Error(__('Please create a channel identity first.')));
2017-06-08 23:15:34 +02:00
}
})
.catch(reject);
break;
case rewards.TYPE_FIRST_PUBLISH:
Lbry.claim_list_mine()
2017-06-08 23:15:34 +02:00
.then(claims => {
2017-12-13 22:36:30 +01:00
const claim = claims
.reverse()
.find(
foundClaim =>
foundClaim.name.length &&
foundClaim.name[0] !== '@' &&
foundClaim.txid.length &&
foundClaim.category === 'claim'
2017-06-08 23:15:34 +02:00
);
if (claim) {
params.transaction_id = claim.txid;
requestReward(resolve, reject, params);
} else {
reject(
claims.length
? new Error(
__(
'Please publish something and wait for confirmation by the network to claim this reward.'
2017-06-08 23:15:34 +02:00
)
)
: new Error(__('Please publish something to claim this reward.'))
2017-06-08 23:15:34 +02:00
);
}
})
.catch(reject);
break;
case rewards.TYPE_FIRST_STREAM:
case rewards.TYPE_NEW_USER:
default:
requestReward(resolve, reject, params);
}
});
});
2017-06-06 06:21:55 +02:00
};
export default rewards;