Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
Cosmos JS (#1731)
Browse files Browse the repository at this point in the history
* increase gas amplifier

* fix payload construction

handle payload atomic construction

* More accurate gas amplifier

* increase gas amplifier

* use same node for calculation and broadcast

* fix amount payload

* fix fees/gas calculation

* fix signature

fix public key when account is derivate

* fix fees regression

* More accurate pubkey selection

* don't use extra.tx_bytes

* fix pubkey selection

* simplify hex serialization

* update transaction: more strict types

* many things

restruct operation builder
simulate now return int
prepareTransaction use patch format

* accuracy

more accuracy int value
small refactor

* remove useless isPreValidation

* fix strange edge effect of ledger live desktop

* Update xpub during sync

* temporary enable log for bot

* LL-9159 cosmos node

* Update js-signOperation.ts

revert back test trace for bot

* fix signature

* fix redelegate payload

* fix payload send transaction when sendmax

* fix optimistic operation type

* fix typo

* bugfix

* fix regression

* update optimistic operation

fix regression
add operation type
more consistent fee

* fix prettier

* more deterministic transaction parsing

* clarify code readable

* adjust sender and recipient

* fix fees when is ibc transaction

* fix redelegations data mapping

* fix mixed styles

* fix array cast type

* fix more determinist operation data

* fix could not find optimisticOperation in redelegate transaction

* restore getStargateRewardsState methode

Co-authored-by: Alexandre Alouit <[email protected]>
  • Loading branch information
wa-aal and alexalouit authored Feb 22, 2022
1 parent 33fe422 commit 9f1159b
Showing 1 changed file with 98 additions and 77 deletions.
175 changes: 98 additions & 77 deletions src/families/cosmos/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ const getRewardsState = makeLRUCache(
);

const getStargateRewardsState = makeLRUCache(
async (_currency: CryptoCurrency) => {
// Fake numbers until Gaia fixes its endpoints
async (currency: CryptoCurrency) => {
/*
return {
targetBondedRatio: 0.01,
communityPoolCommission: 0.0,
Expand All @@ -196,81 +196,102 @@ const getStargateRewardsState = makeLRUCache(
averageDailyFees: 0,
currentValueInflation: 0.01,
};
/*
// All obtained values are strings ; so sometimes we will need to parse them as numbers
const inflationUrl = `${getBaseApiUrl(
currency
)}/cosmos/mint/v1beta1/inflation`;
const { data: inflationData } = await network({
url: inflationUrl,
method: "GET",
});
const currentValueInflation = parseFloat(inflationData.inflation);
const inflationParametersUrl = `${getBaseApiUrl(
currency
)}/cosmos/mint/v1beta1/params`;
const { data: inflationParametersData } = await network({
url: inflationParametersUrl,
method: "GET",
});
const inflationRateChange = parseFloat(
inflationParametersData.params.inflation_rate_change
);
const inflationMaxRate = parseFloat(
inflationParametersData.params.inflation_max
);
const inflationMinRate = parseFloat(
inflationParametersData.params.inflation_min
);
const targetBondedRatio = parseFloat(
inflationParametersData.params.goal_bonded
);
// Source for seconds per year : https://github.com/gavinly/CosmosParametersWiki/blob/master/Mint.md#notes-3
// 365.24 (days) * 24 (hours) * 60 (minutes) * 60 (seconds) = 31556736 seconds
const assumedTimePerBlock =
31556736.0 / parseFloat(inflationParametersData.params.blocks_per_year);
const communityTaxUrl = `${getBaseApiUrl(
currency
)}/cosmos/distribution/v1beta1/params`;
const { data: communityTax } = await network({
url: communityTaxUrl,
method: "GET",
});
const communityPoolCommission = parseFloat(
communityTax.params.community_tax
);
const supplyUrl = `${getBaseApiUrl(currency)}/cosmos/bank/v1beta1/supply/${
currency.id == "cosmos_testnet" ? "umuon" : "uatom"
}`;
const { data: totalSupplyData } = await network({
url: supplyUrl,
method: "GET",
});
const totalSupply = parseUatomStrAsAtomNumber(
totalSupplyData.amount.amount
);
const ratioUrl = `${getBaseApiUrl(currency)}/cosmos/staking/v1beta1/pool`;
const { data: ratioData } = await network({ url: ratioUrl, method: "GET" });
const actualBondedRatio =
parseUatomStrAsAtomNumber(ratioData.pool.bonded_tokens) / totalSupply;
// Arbitrary value in ATOM.
const averageDailyFees = 20;
// Arbitrary value in seconds
const averageTimePerBlock = 7.5;
return {
targetBondedRatio,
communityPoolCommission,
assumedTimePerBlock,
inflationRateChange,
inflationMaxRate,
inflationMinRate,
actualBondedRatio,
averageTimePerBlock,
totalSupply,
averageDailyFees,
currentValueInflation,
};
*/
*/

// All obtained values are strings ; so sometimes we will need to parse them as numbers
const inflationUrl = `${getBaseApiUrl(
currency
)}/cosmos/mint/v1beta1/inflation`;

const { data: inflationData } = await network({
url: inflationUrl,
method: "GET",
});

const currentValueInflation = parseFloat(inflationData.inflation);

const inflationParametersUrl = `${getBaseApiUrl(
currency
)}/cosmos/mint/v1beta1/params`;

const { data: inflationParametersData } = await network({
url: inflationParametersUrl,
method: "GET",
});

const inflationRateChange = parseFloat(
inflationParametersData.params.inflation_rate_change
);

const inflationMaxRate = parseFloat(
inflationParametersData.params.inflation_max
);

const inflationMinRate = parseFloat(
inflationParametersData.params.inflation_min
);

const targetBondedRatio = parseFloat(
inflationParametersData.params.goal_bonded
);

// Source for seconds per year : https://github.com/gavinly/CosmosParametersWiki/blob/master/Mint.md#notes-3
// 365.24 (days) * 24 (hours) * 60 (minutes) * 60 (seconds) = 31556736 seconds
const assumedTimePerBlock =
31556736.0 / parseFloat(inflationParametersData.params.blocks_per_year);

const communityTaxUrl = `${getBaseApiUrl(
currency
)}/cosmos/distribution/v1beta1/params`;

const { data: communityTax } = await network({
url: communityTaxUrl,
method: "GET",
});

const communityPoolCommission = parseFloat(
communityTax.params.community_tax
);

const supplyUrl = `${getBaseApiUrl(currency)}/cosmos/bank/v1beta1/supply/${
currency.id == "cosmos_testnet" ? "umuon" : "uatom"
}`;

const { data: totalSupplyData } = await network({
url: supplyUrl,
method: "GET",
});

const totalSupply = parseUatomStrAsAtomNumber(
totalSupplyData.amount.amount
);

const ratioUrl = `${getBaseApiUrl(currency)}/cosmos/staking/v1beta1/pool`;

const { data: ratioData } = await network({ url: ratioUrl, method: "GET" });

const actualBondedRatio =
parseUatomStrAsAtomNumber(ratioData.pool.bonded_tokens) / totalSupply;

// Arbitrary value in ATOM.
const averageDailyFees = 20;

// Arbitrary value in seconds
const averageTimePerBlock = 7.5;

return {
targetBondedRatio,
communityPoolCommission,
assumedTimePerBlock,
inflationRateChange,
inflationMaxRate,
inflationMinRate,
actualBondedRatio,
averageTimePerBlock,
totalSupply,
averageDailyFees,
currentValueInflation,
};
},
(currency: CryptoCurrency) => currency.id
);
Expand Down

1 comment on commit 9f1159b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 2 txs ($45.87) for Bot 'Cosmos JS'

⚠️ 4 mutations uncovered

Details of the 2 mutations

Spec Cosmos (15)

Spec Cosmos found 15 Cosmos accounts (preload: 1018ms). Will use Cosmos 2.18.0 on nanoS 2.0.0
(658ms) Cosmos 1: 0.06871 ATOM (27ops) (cosmos1zpvthe3pxvyje7vrcqv2m6qe2qgu0r8gkz64er on 44'/118'/0'/0/0) #0 js:2:cosmos:cosmos1zpvthe3pxvyje7vrcqv2m6qe2qgu0r8gkz64er:
(719ms) Cosmos 2: 0.09223 ATOM (34ops) (cosmos1zd0e8j9g9wur8up7j5aqahz233r4jmvqhyffnv on 44'/118'/1'/0/0) #1 js:2:cosmos:cosmos1zd0e8j9g9wur8up7j5aqahz233r4jmvqhyffnv:
(637ms) Cosmos 3: 0.3633 ATOM (30ops) (cosmos1uty5nf28emeahuhdg9runmzuluq9uj5uek4jk0 on 44'/118'/2'/0/0) #2 js:2:cosmos:cosmos1uty5nf28emeahuhdg9runmzuluq9uj5uek4jk0:
(489ms) Cosmos 4: 0.08939 ATOM (24ops) (cosmos1ccemcd8p3r293e5rcv0awaj9hfnn6wmxkz520n on 44'/118'/3'/0/0) #3 js:2:cosmos:cosmos1ccemcd8p3r293e5rcv0awaj9hfnn6wmxkz520n:
(711ms) Cosmos 5: 0.0623 ATOM (23ops) (cosmos1mvk3fwxgdfac4yjmgl9hdz0555q8gljydzvjtu on 44'/118'/4'/0/0) #4 js:2:cosmos:cosmos1mvk3fwxgdfac4yjmgl9hdz0555q8gljydzvjtu:
(818ms) Cosmos 6: 0.09779 ATOM (22ops) (cosmos1draxuzz0aukggx63m852wm8mlxqpus2009cs97 on 44'/118'/5'/0/0) #5 js:2:cosmos:cosmos1draxuzz0aukggx63m852wm8mlxqpus2009cs97:
(976ms) Cosmos 7: 0.08934 ATOM (24ops) (cosmos14k7faf4cvxlflta4hytxavx3p7vj6uw69lgf03 on 44'/118'/6'/0/0) #6 js:2:cosmos:cosmos14k7faf4cvxlflta4hytxavx3p7vj6uw69lgf03:
(547ms) Cosmos 8: 0.10255 ATOM (19ops) (cosmos12uwsw42ec4lh75202zcp0r5ggj9vlwp4c05ydz on 44'/118'/7'/0/0) #7 js:2:cosmos:cosmos12uwsw42ec4lh75202zcp0r5ggj9vlwp4c05ydz:
(513ms) Cosmos 9: 0.0774 ATOM (18ops) (cosmos1pggp9ffssaf8hdev9aekcafarxnuxmz4er44hm on 44'/118'/8'/0/0) #8 js:2:cosmos:cosmos1pggp9ffssaf8hdev9aekcafarxnuxmz4er44hm:
(964ms) Cosmos 10: 0.12663 ATOM (28ops) (cosmos1cnr3madhjge7s3lpz52qdcay69llm6uwx3s8y3 on 44'/118'/9'/0/0) #9 js:2:cosmos:cosmos1cnr3madhjge7s3lpz52qdcay69llm6uwx3s8y3:
(501ms) Cosmos 11: 0.13847 ATOM (10ops) (cosmos1gyqj09h2yc0hs4ggwy74qca8t4hl6ns35w92l5 on 44'/118'/10'/0/0) #10 js:2:cosmos:cosmos1gyqj09h2yc0hs4ggwy74qca8t4hl6ns35w92l5:
(871ms) Cosmos 12: 0.16743 ATOM (20ops) (cosmos1zkx8vmvzfz6ah8les5y62rxmvdrk4egvcfzfcu on 44'/118'/11'/0/0) #11 js:2:cosmos:cosmos1zkx8vmvzfz6ah8les5y62rxmvdrk4egvcfzfcu:
(469ms) Cosmos 13: 0.07866 ATOM (8ops) (cosmos19y63dxgjvuj46hcgrt0w0rpct85d3sfxgmlgpa on 44'/118'/12'/0/0) #12 js:2:cosmos:cosmos19y63dxgjvuj46hcgrt0w0rpct85d3sfxgmlgpa:
(793ms) Cosmos 14: 0.1 ATOM (1ops) (cosmos19fm3nalhxtswpn78ng2rl4vs7p04vljgfeq2qs on 44'/118'/13'/0/0) #13 js:2:cosmos:cosmos19fm3nalhxtswpn78ng2rl4vs7p04vljgfeq2qs:
(1302ms) Cosmos 15: 0 ATOM (0ops) (cosmos14ps228paxvqngcz6adj0d98f4qftkl68atrvjw on 44'/118'/14'/0/0) #14 js:2:cosmos:cosmos14ps228paxvqngcz6adj0d98f4qftkl68atrvjw:
all accounts sync in 3.1s
▬ Cosmos 2.18.0 on nanoS 2.0.0
→ FROM Cosmos 11: 0.13847 ATOM (10ops) (cosmos1gyqj09h2yc0hs4ggwy74qca8t4hl6ns35w92l5 on 44'/118'/10'/0/0) #10 js:2:cosmos:cosmos1gyqj09h2yc0hs4ggwy74qca8t4hl6ns35w92l5: (! sum of ops 0.150106 ATOM) 0.138452 ATOM spendable. 0.000027 ATOM delegated. 
DELEGATIONS
  to cosmosvaloper14jlpmqquh0gste6nzf4dn43kc8h50l6fmx6dfs 0.000027 ATOM 
UNDELEGATIONS
  from cosmosvaloper14jlpmqquh0gste6nzf4dn43kc8h50l6fmx6dfs 0.001663 ATOM
  from cosmosvaloper1gdg6qqe5a3u483unqlqsnullja23g0xvqkxtk0 0.006992 ATOM
  from cosmosvaloper1g48268mu5vfp4wk7dk89r0wdrakm9p5xk0q50k 0.002972 ATOM

max spendable ~0.13364
★ using mutation 'delegate new validators'
✔️ transaction 
DELEGATE 
TO 
  0.000058 -> cosmosvaloper1e859xaue4k2jzqw20cv6l7p3tmc378pc3k8g2u
  0.000045 -> cosmosvaloper13x77yexvf6qexfjg9czp6jhpv7vpjdwwkyhe4p
with fees=0.01291
  memo=LedgerLiveBot
STATUS (1271ms)
  amount: 0.000103 ATOM
  estimated fees: 0.01291 ATOM
  total spent: 0.013013 ATOM
✔️ has been signed! (2925ms) 
✔️ broadcasted! (127ms) optimistic operation: 
  -0 ATOM            DELEGATE   ABC651DF9CB03010E9248E4E07087609BCB087F4C9C0B3D37AC2E29848E75B37 2022-02-22T15:30
✔️ operation confirmed (17.1s): 
  -0.01291 ATOM      DELEGATE   ABC651DF9CB03010E9248E4E07087609BCB087F4C9C0B3D37AC2E29848E75B37 2022-02-22T15:30
    to cosmosvaloper1e859xaue4k2jzqw20cv6l7p3tmc378pc3k8g2u 0.000058 ATOM   
✔️ Cosmos 11: 0.12556 ATOM (11ops) (cosmos1gyqj09h2yc0hs4ggwy74qca8t4hl6ns35w92l5 on 44'/118'/10'/0/0) #10 js:2:cosmos:cosmos1gyqj09h2yc0hs4ggwy74qca8t4hl6ns35w92l5: (! sum of ops 0.137196 ATOM) 0.125439 ATOM spendable. 0.000129 ATOM delegated. 
DELEGATIONS
  to cosmosvaloper1e859xaue4k2jzqw20cv6l7p3tmc378pc3k8g2u 0.000057 ATOM 
  to cosmosvaloper14jlpmqquh0gste6nzf4dn43kc8h50l6fmx6dfs 0.000027 ATOM 
  to cosmosvaloper13x77yexvf6qexfjg9czp6jhpv7vpjdwwkyhe4p 0.000045 ATOM 
UNDELEGATIONS
  from cosmosvaloper14jlpmqquh0gste6nzf4dn43kc8h50l6fmx6dfs 0.001663 ATOM
  from cosmosvaloper1gdg6qqe5a3u483unqlqsnullja23g0xvqkxtk0 0.006992 ATOM
  from cosmosvaloper1g48268mu5vfp4wk7dk89r0wdrakm9p5xk0q50k 0.002972 ATOM

(final state reached in 17.1s)

all accounts sync in 2839ms
▬ Cosmos 2.18.0 on nanoS 2.0.0
→ FROM Cosmos 12: 0.15041 ATOM (20ops) (cosmos1zkx8vmvzfz6ah8les5y62rxmvdrk4egvcfzfcu on 44'/118'/11'/0/0) #11 js:2:cosmos:cosmos1zkx8vmvzfz6ah8les5y62rxmvdrk4egvcfzfcu: (! sum of ops 0.236616 ATOM) 0.143078 ATOM spendable. 0.007339 ATOM delegated. 
DELEGATIONS
  to cosmosvaloper1kg99k8wd67r0ffxwavgnxup7yk46rvttxc53j7 0.004232 ATOM 
  to cosmosvaloper13sduv92y3xdhy3rpmhakrc3v7t37e7ps9l0kpv 0.00282 ATOM 
  to cosmosvaloper1keltez56g3zm9w8wr8gcmmulze48g2q3usuw8c 0.000287 ATOM 
  to cosmosvaloper157v7tczs40axfgejp2m43kwuzqe0wsy0rv8puv 0.003302 ATOM 
  to cosmosvaloper196ax4vc0lwpxndu9dyhvca7jhxp70rmcvrj90c 0.013999 ATOM 
UNDELEGATIONS
  from cosmosvaloper1s05va5d09xlq3et8mapsesqh6r5lqy7mkhwshm 0.000501 ATOM
  from cosmosvaloper1jlr62guqwrwkdt4m3y00zh2rrsamhjf9num5xr 0.004001 ATOM
  from cosmosvaloper15r4tc0m6hc7z8drq3dzlrtcs6rq2q9l2nvwher 0.017837 ATOM
  from cosmosvaloper1zc0z44e42qhzltqc8qpj5qrzn836d3lftnqmgw 0.000048 ATOM
  from cosmosvaloper1wrx0x9m9ykdhw9sg04v7uljme53wuj03aa5d4f 0.040059 ATOM
  from cosmosvaloper1fun809ksxh87nzf88yashp9ynjz6xkscrtvzvw 0.006452 ATOM

max spendable ~0.13827
★ using mutation 'undelegate'
✔️ transaction 
UNDELEGATE 
TO 
  0.003302 -> cosmosvaloper157v7tczs40axfgejp2m43kwuzqe0wsy0rv8puv
with fees=0.009953
  memo=LedgerLiveBot
STATUS (962ms)
  amount: 0 ATOM
  estimated fees: 0.009953 ATOM
  total spent: 0.009953 ATOM
✔️ has been signed! (2200ms) 
✔️ broadcasted! (165ms) optimistic operation: 
  -0 ATOM            UNDELEGATE 60B6061B7055BAE1320D3437114226877C13A1402ED498730A48F6FBBC1CEAA5 2022-02-22T15:30
✔️ operation confirmed (39s): 
  -0.009953 ATOM     UNDELEGATE 60B6061B7055BAE1320D3437114226877C13A1402ED498730A48F6FBBC1CEAA5 2022-02-22T15:31
    to cosmosvaloper157v7tczs40axfgejp2m43kwuzqe0wsy0rv8puv 0.003302 ATOM   
✔️ Cosmos 12: 0.15446 ATOM (21ops) (cosmos1zkx8vmvzfz6ah8les5y62rxmvdrk4egvcfzfcu on 44'/118'/11'/0/0) #11 js:2:cosmos:cosmos1zkx8vmvzfz6ah8les5y62rxmvdrk4egvcfzfcu: (! sum of ops 0.226663 ATOM) 0.133125 ATOM spendable. 0.021338 ATOM delegated. 
DELEGATIONS
  to cosmosvaloper1keltez56g3zm9w8wr8gcmmulze48g2q3usuw8c 0.000287 ATOM 
  to cosmosvaloper1kg99k8wd67r0ffxwavgnxup7yk46rvttxc53j7 0.004232 ATOM 
  to cosmosvaloper196ax4vc0lwpxndu9dyhvca7jhxp70rmcvrj90c 0.013999 ATOM 
  to cosmosvaloper13sduv92y3xdhy3rpmhakrc3v7t37e7ps9l0kpv 0.00282 ATOM 
UNDELEGATIONS
  from cosmosvaloper1s05va5d09xlq3et8mapsesqh6r5lqy7mkhwshm 0.000501 ATOM
  from cosmosvaloper1jlr62guqwrwkdt4m3y00zh2rrsamhjf9num5xr 0.004001 ATOM
  from cosmosvaloper15r4tc0m6hc7z8drq3dzlrtcs6rq2q9l2nvwher 0.017837 ATOM
  from cosmosvaloper1zc0z44e42qhzltqc8qpj5qrzn836d3lftnqmgw 0.000048 ATOM
  from cosmosvaloper1wrx0x9m9ykdhw9sg04v7uljme53wuj03aa5d4f 0.040059 ATOM
  from cosmosvaloper1fun809ksxh87nzf88yashp9ynjz6xkscrtvzvw 0.006452 ATOM
  from cosmosvaloper157v7tczs40axfgejp2m43kwuzqe0wsy0rv8puv 0.003302 ATOM

(final state reached in 39s)


Details of the 4 uncovered mutations

Spec Cosmos (4)

  • send some: balance is too low (13)
  • send max: balance is too low (13)
  • redelegate: balance is too low (13)
  • claim rewards: balance is too low (13)

Portfolio ($45.87)

Details of the 1 currencies
Spec (accounts) Operations Balance funds?
Cosmos (14) 290 (+2) 1.2526 ATOM (- 0.022966) ($45.87) 👍 cosmos1zpvthe3pxvyje7vrcqv2m6qe2qgu0r8gkz64er

Please sign in to comment.