-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
118a56f
commit 9eac49c
Showing
9 changed files
with
637 additions
and
40,837 deletions.
There are no files selected for viewing
41,055 changes: 481 additions & 40,574 deletions
41,055
.yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.