forked from hyperweb-io/telescope
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
claiming test with new amino encoder
- Loading branch information
1 parent
9690b21
commit 9814c65
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
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,118 @@ | ||
import { | ||
baseUnitsToDisplayUnitsByDenom, | ||
gasEstimation, | ||
} from '@cosmology/core'; | ||
import { | ||
decodeCosmosSdkDecFromProto, | ||
assertIsDeliverTxSuccess | ||
} from '@cosmjs/stargate'; | ||
import { Dec } from '@keplr-wallet/unit'; | ||
|
||
import { cosmos, getSigningCosmosClient } from './codegen'; | ||
import { getOfflineSignerAmino } from 'cosmjs-utils'; | ||
|
||
const RPC_ENDPOINT = 'https://rpc.cosmos.directory/osmosis' | ||
|
||
const main = async () => { | ||
|
||
const { | ||
withdrawDelegatorReward | ||
} = cosmos.distribution.v1beta1.MessageComposer.fromPartial; | ||
|
||
const signer = await getOfflineSignerAmino({ | ||
mnemonic: process.env.MNEMONIC, | ||
chain: { | ||
bech32_prefix: 'osmo', | ||
slip44: 118 | ||
} | ||
}); | ||
|
||
const client = await cosmos.ClientFactory.createRPCQueryClient({ rpcEndpoint: RPC_ENDPOINT }); | ||
|
||
const [mainAccount] = await signer.getAccounts(); | ||
|
||
const { address } = mainAccount; | ||
|
||
const delegations = await client.cosmos.staking.v1beta1.delegatorDelegations({ | ||
delegatorAddr: address | ||
}); | ||
|
||
if (!delegations.delegationResponses || !delegations.delegationResponses.length) { | ||
console.log('no delegations. Exiting.'); | ||
return; | ||
} | ||
|
||
const messagesToClaim = []; | ||
let totalClaimable = new Dec(0); | ||
|
||
const rewards = await client.cosmos.distribution.v1beta1.delegationTotalRewards({ | ||
delegatorAddress: address | ||
}); | ||
|
||
if (rewards && rewards.rewards && rewards.rewards.length) { | ||
rewards.rewards.forEach((data) => { | ||
const { validatorAddress, reward } = data; | ||
if (reward && reward.length) { | ||
const rewardWeWant = reward.find((r) => r.denom === 'uosmo'); | ||
if (!rewardWeWant) return; | ||
// https://github.com/osmosis-labs/telescope/issues/247 | ||
totalClaimable = totalClaimable.add(new Dec(decodeCosmosSdkDecFromProto(rewardWeWant.amount).toString())); | ||
messagesToClaim.push( | ||
withdrawDelegatorReward({ | ||
delegatorAddress: address, | ||
validatorAddress | ||
}) | ||
); | ||
} | ||
}); | ||
} | ||
|
||
if (!messagesToClaim.length) { | ||
console.log('no rewards. Exiting.'); | ||
return; | ||
} | ||
|
||
const stargateClient = await getSigningCosmosClient({ | ||
rpcEndpoint: RPC_ENDPOINT, | ||
signer | ||
}); | ||
|
||
const fee = await gasEstimation( | ||
'uosmo', | ||
// @ts-ignore | ||
stargateClient, | ||
address, | ||
messagesToClaim, | ||
'', | ||
1.3 | ||
); | ||
|
||
const totalClaimableInDisplayUnits = new Dec(baseUnitsToDisplayUnitsByDenom( | ||
'uosmo', | ||
totalClaimable.toString() | ||
)); | ||
|
||
|
||
console.log( | ||
`${totalClaimableInDisplayUnits} available, starting claim process...` | ||
); | ||
|
||
const result = await stargateClient.signAndBroadcast( | ||
address, | ||
messagesToClaim, | ||
fee | ||
) | ||
|
||
assertIsDeliverTxSuccess(result); | ||
stargateClient.disconnect(); | ||
console.log( | ||
`⚛️ success in claiming ${totalClaimableInDisplayUnits.toString()} rewards` | ||
); | ||
console.log(result); | ||
|
||
|
||
}; | ||
|
||
main().then(() => { | ||
console.log('all done') | ||
}) |
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 |
---|---|---|
|
@@ -2437,6 +2437,15 @@ | |
long "^4.0.0" | ||
secretjs "0.17.7" | ||
|
||
"@keplr-wallet/[email protected]": | ||
version "0.11.25" | ||
resolved "https://registry.npmjs.org/@keplr-wallet/types/-/types-0.11.25.tgz#81a281712ce58f131d8579898dcde66934bcb52e" | ||
integrity sha512-A2n2PXdRpn+v4icW34sbovcr4sZY14J+zZeEavBkuMtVyk49JlOOBj6orI+ofFl+SXzro+4vWei/XAJFrd/swg== | ||
dependencies: | ||
axios "^0.27.2" | ||
long "^4.0.0" | ||
secretjs "0.17.7" | ||
|
||
"@keplr-wallet/[email protected]": | ||
version "0.11.22" | ||
resolved "https://registry.npmjs.org/@keplr-wallet/unit/-/unit-0.11.22.tgz#c2a2f51f880cf8a0685560cc02e09a12006c529d" | ||
|
@@ -2446,6 +2455,15 @@ | |
big-integer "^1.6.48" | ||
utility-types "^3.10.0" | ||
|
||
"@keplr-wallet/[email protected]": | ||
version "0.11.25" | ||
resolved "https://registry.npmjs.org/@keplr-wallet/unit/-/unit-0.11.25.tgz#2e36aaf86f328b07053c2aeb459d035af76abee4" | ||
integrity sha512-aALEZTWQR3z1Z9/OADGA/iaim82buOBjHNc+NG6FtXBhekRt0gD2jZ0RI6s6bh4Vjj2O+GNRzteJBoC7pBCiXg== | ||
dependencies: | ||
"@keplr-wallet/types" "0.11.25" | ||
big-integer "^1.6.48" | ||
utility-types "^3.10.0" | ||
|
||
"@lerna/[email protected]": | ||
version "4.0.0" | ||
resolved "https://registry.npmjs.org/@lerna/add/-/add-4.0.0.tgz#c36f57d132502a57b9e7058d1548b7a565ef183f" | ||
|