Skip to content

Commit

Permalink
refactor: on staking reward (#1103)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivalaakam authored Jun 19, 2023
1 parent 118a56f commit 9eac49c
Show file tree
Hide file tree
Showing 9 changed files with 637 additions and 40,837 deletions.
41,055 changes: 481 additions & 40,574 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ newArchEnabled=false

# Use this property to enable or disable the Hermes JS engine.
# If set to false, you will be using JSC instead.
hermesEnabled=false
hermesEnabled=true
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"react-native-camera": "4.2.1",
"react-native-crypto": "2.2.0",
"react-native-detector": "0.2.3",
"react-native-dotenv": "3.4.8",
"react-native-dotenv": "3.4.9",
"react-native-draggable-flatlist": "^4.0.1",
"react-native-encrypted-storage": "4.0.3",
"react-native-fs": "^2.20.0",
Expand Down Expand Up @@ -109,7 +109,7 @@
"react-native-touch-id": "4.4.1",
"react-native-webview": "11.13.0",
"readable-stream": "4.4.0",
"realm": "11.10.0",
"realm": "11.10.1",
"rive-react-native": "^4.1.1",
"rxjs": "7.8.1",
"sha.js": "2.4.11",
Expand Down Expand Up @@ -145,8 +145,8 @@
"babel-jest": "29.5.0",
"babel-plugin-module-resolver": "5.0.0",
"detox": "20.9.1",
"dotenv": "16.2.0",
"eslint": "8.42.0",
"dotenv": "16.3.1",
"eslint": "8.43.0",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-jest": "27.2.1",
"eslint-plugin-prettier": "4.2.1",
Expand Down
2 changes: 1 addition & 1 deletion src/components/home-earn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ const styles = createTheme({
},
container: {
flex: 1,
marginHorizontal: 20,
paddingHorizontal: 20,
},
stakingCard: {
borderRadius: 12,
Expand Down
1 change: 0 additions & 1 deletion src/components/home-governance/voting-card/voting-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface VotingCardProps {
}

export function VotingCard({item, onPress}: VotingCardProps) {
console.log('item', item);
if (item.status === 'voting' || item.status === 'deposited') {
return <VotingCardActive item={item} onPress={onPress} />;
} else {
Expand Down
110 changes: 110 additions & 0 deletions src/event-actions/on-staking-rewards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import {app} from '@app/contexts';
import {
awaitForBluetooth,
awaitForLedger,
awaitForPopupClosed,
getProviderInstanceForWallet,
} from '@app/helpers';
import {
StakingMetadata,
StakingMetadataType,
} from '@app/models/staking-metadata';
import {Wallet} from '@app/models/wallet';
import {Cosmos} from '@app/services/cosmos';
import {WalletType} from '@app/types';
import {MIN_AMOUNT} from '@app/variables/common';

export async function onStakingRewards() {
const cosmos = new Cosmos(app.provider!);
const visible = Wallet.getAllVisible();
const rewards = StakingMetadata.getAllByType(StakingMetadataType.reward);
const delegators: any = {};

for (const row of rewards) {
if (row.amount > MIN_AMOUNT) {
delegators[row.delegator] = (delegators[row.delegator] ?? []).concat(
row.validator,
);
}
}

const exists = visible.filter(
w => w.isValid() && w.cosmosAddress in delegators,
);

const queue = exists
.filter(w => w.type !== WalletType.ledgerBt)
.map(async w => {
const provider = await getProviderInstanceForWallet(w);
await cosmos.multipleWithdrawDelegatorReward(
provider,
w.path!,
delegators[w.cosmosAddress],
);
return [w.cosmosAddress, delegators[w.cosmosAddress]];
});

const ledger = exists.filter(w => w.type === WalletType.ledgerBt);

while (ledger.length) {
const current = ledger.shift();

if (current && current.isValid()) {
const transport = await getProviderInstanceForWallet(current);
console.log('iter transport', transport);

queue.push(
cosmos
.multipleWithdrawDelegatorReward(
transport,
current.path!,
delegators[current.cosmosAddress],
)
.then(() => [
current.cosmosAddress,
delegators[current.cosmosAddress],
]),
);
try {
await awaitForBluetooth();
await awaitForLedger(transport);
} catch (e) {
if (e === '27010') {
await awaitForPopupClosed('ledgerLocked');
}
transport.abort();
}
}
}

const responses = await Promise.all(
queue.map(p =>
p
.then(value => ({
status: 'fulfilled',
value,
}))
.catch(reason => ({
status: 'rejected',
reason,
value: null,
})),
),
);

for (const resp of responses) {
if (resp.status === 'fulfilled' && resp.value) {
for (const reward of rewards) {
if (
reward &&
reward.delegator === resp.value[0] &&
reward.validator === resp.value[1]
) {
StakingMetadata.remove(reward.hash);
}
}
}
}

rewards.forEach(r => StakingMetadata.remove(r.hash));
}
127 changes: 9 additions & 118 deletions src/screens/home-earn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ import {CaptchaType} from '@app/components/captcha';
import {HomeEarn} from '@app/components/home-earn';
import {Loading} from '@app/components/ui';
import {app} from '@app/contexts';
import {onStakingRewards} from '@app/event-actions/on-staking-rewards';
import {onTrackEvent} from '@app/event-actions/on-track-event';
import {
awaitForPopupClosed,
captureException,
getProviderInstanceForWallet,
} from '@app/helpers';
import {awaitForBluetooth} from '@app/helpers/await-for-bluetooth';
import {captureException, getProviderInstanceForWallet} from '@app/helpers';
import {awaitForCaptcha} from '@app/helpers/await-for-captcha';
import {awaitForLedger} from '@app/helpers/await-for-ledger';
import {getLeadingAccount} from '@app/helpers/get-leading-account';
import {getUid} from '@app/helpers/get-uid';
import {sumReduce} from '@app/helpers/staking';
import {useCosmos, useTypedNavigation, useWalletsVisible} from '@app/hooks';
import {useTypedNavigation, useWalletsVisible} from '@app/hooks';
import {I18N} from '@app/i18n';
import {
StakingMetadata,
Expand All @@ -25,8 +20,8 @@ import {
import {Wallet} from '@app/models/wallet';
import {sendNotification} from '@app/services';
import {Backend} from '@app/services/backend';
import {AdjustEvents, Raffle, RaffleStatus, WalletType} from '@app/types';
import {MIN_AMOUNT, NUM_PRECISION, WEI} from '@app/variables/common';
import {AdjustEvents, Raffle, RaffleStatus} from '@app/types';
import {NUM_PRECISION, WEI} from '@app/variables/common';

const initData = {
stakingSum: 0,
Expand All @@ -38,7 +33,6 @@ const initData = {
export const HomeEarnScreen = () => {
const navigation = useTypedNavigation();
const visible = useWalletsVisible();
const cosmos = useCosmos();
const [raffles, setRaffles] = useState<null | Raffle[]>(null);
const [isRafflesLoading, setIsRafflesLoading] = useState<boolean>(false);

Expand All @@ -55,7 +49,7 @@ export const HomeEarnScreen = () => {
[data],
);

const haveAvailablSum = useMemo(
const haveAvailableSum = useMemo(
() => data.availableSum >= 1 / NUM_PRECISION,
[data],
);
Expand Down Expand Up @@ -125,106 +119,6 @@ export const HomeEarnScreen = () => {
navigation.navigate('staking');
}, [navigation]);

const onPressGetRewards = useCallback(async () => {
const rewards = StakingMetadata.getAllByType(StakingMetadataType.reward);
console.log('rewards', rewards);
const delegators: any = {};

for (const row of rewards) {
if (row.amount > MIN_AMOUNT) {
delegators[row.delegator] = (delegators[row.delegator] ?? []).concat(
row.validator,
);
}
}

const exists = visible.filter(
w => w.isValid() && w.cosmosAddress in delegators,
);

console.log('exists', exists);

const queue = exists
.filter(w => w.type !== WalletType.ledgerBt)
.map(async w => {
const provider = await getProviderInstanceForWallet(w);
await cosmos.multipleWithdrawDelegatorReward(
provider,
w.path!,
delegators[w.cosmosAddress],
);
return [w.cosmosAddress, delegators[w.cosmosAddress]];
});

const ledger = exists.filter(w => w.type === WalletType.ledgerBt);

console.log('queue', queue);

while (ledger.length) {
const current = ledger.shift();

if (current && current.isValid()) {
const transport = await getProviderInstanceForWallet(current);
console.log('iter transport', transport);

queue.push(
cosmos
.multipleWithdrawDelegatorReward(
transport,
current.path!,
delegators[current.cosmosAddress],
)
.then(() => [
current.cosmosAddress,
delegators[current.cosmosAddress],
]),
);
try {
await awaitForBluetooth();
await awaitForLedger(transport);
} catch (e) {
if (e === '27010') {
await awaitForPopupClosed('ledgerLocked');
}
transport.abort();
}
}
}

const responses = await Promise.all(
queue.map(p =>
p
.then(value => ({
status: 'fulfilled',
value,
}))
.catch(reason => ({
status: 'rejected',
reason,
value: null,
})),
),
);

console.log('responses', responses);

for (const resp of responses) {
if (resp.status === 'fulfilled' && resp.value) {
for (const reward of rewards) {
if (
reward &&
reward.delegator === resp.value[0] &&
reward.validator === resp.value[1]
) {
StakingMetadata.remove(reward.hash);
}
}
}
}

rewards.forEach(r => StakingMetadata.remove(r.hash));
}, [cosmos, visible]);

const onPressGetTicket = useCallback(
async (raffle: Raffle) => {
const leadingAccount = getLeadingAccount();
Expand All @@ -244,15 +138,14 @@ export const HomeEarnScreen = () => {
);

try {
const res = await Backend.instance.contestParticipate(
await Backend.instance.contestParticipate(
raffle.id,
uid,
session,
signature,
leadingAccount?.address ?? '',
);
sendNotification(I18N.earnTicketRecieved);
console.log('🟢 onPressGetTicket', JSON.stringify(res, null, 2));
await loadRaffles();
} catch (e) {
captureException(e, 'onPressGetTicket');
Expand All @@ -262,7 +155,6 @@ export const HomeEarnScreen = () => {
);
const onPressShowResult = useCallback(
(raffle: Raffle) => {
console.log('🟢 onPressShowResult', JSON.stringify(raffle, null, 2));
navigation.navigate('raffleReward', {item: raffle});
},
[navigation],
Expand All @@ -285,7 +177,6 @@ export const HomeEarnScreen = () => {
?.filter?.(it => it.status === RaffleStatus.closed)
.reduce((prev, curr) => prev + curr.winner_tickets, 0) || 0;

console.log('🟢 onPressRaffle', JSON.stringify(raffle, null, 2));
navigation.navigate('raffleDetails', {
item: raffle,
prevIslmCount,
Expand All @@ -302,12 +193,12 @@ export const HomeEarnScreen = () => {
return (
<HomeEarn
rewardAmount={data.rewardsSum}
showStakingRewards={haveAvailablSum}
showStakingRewards={haveAvailableSum}
showStakingGetRewardsButtons={canGetRewards}
raffleList={raffles}
isRafflesLoading={isRafflesLoading}
loadRaffles={loadRaffles}
onPressGetRewards={onPressGetRewards}
onPressGetRewards={onStakingRewards}
onPressGetTicket={onPressGetTicket}
onPressShowResult={onPressShowResult}
onPressStaking={onPressStaking}
Expand Down
Loading

0 comments on commit 9eac49c

Please sign in to comment.