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

Commit

Permalink
Cosmos JS (#1743)
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

* amount of the operation more close

* return transaction in prepareTransaction

* keep immutable paradigm for prepareTransaction

Co-authored-by: Alexandre Alouit <[email protected]>
  • Loading branch information
wa-aal and alexalouit authored Feb 25, 2022
1 parent 5e0ac64 commit 07480af
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
27 changes: 19 additions & 8 deletions src/families/cosmos/js-prepareTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ const prepareTransaction = async (
account: Account,
transaction: Transaction
): Promise<Transaction> => {
const patch: Partial<Transaction> = {};
let memo = transaction.memo;
let fees = transaction.fees;
let gas = transaction.gas;
let amount = transaction.amount;

let gasQty = new BigNumber(250000);
const gasPrice = new BigNumber(getEnv("COSMOS_GAS_PRICE"));

if (transaction.useAllAmount) {
patch.amount = getMaxEstimatedBalance(
amount = getMaxEstimatedBalance(
account,
account.balance
.dividedBy(new BigNumber(getEnv("COSMOS_GAS_AMPLIFIER")))
Expand All @@ -39,12 +42,12 @@ const prepareTransaction = async (
}

if (transaction.mode !== "send" && !transaction.memo) {
patch.memo = "Ledger Live";
memo = "Ledger Live";
}

const unsignedPayload = await buildTransaction(account, {
...transaction,
...patch,
amount,
});

// be sure payload is complete
Expand All @@ -53,7 +56,7 @@ const prepareTransaction = async (
typeUrl: "/cosmos.tx.v1beta1.TxBody",
value: {
messages: unsignedPayload,
memo: transaction.memo || patch.memo || "",
memo: transaction.memo || memo || "",
},
};

Expand Down Expand Up @@ -111,11 +114,19 @@ const prepareTransaction = async (
}
}

patch.gas = gasQty;
gas = gasQty;

patch.fees = gasPrice.multipliedBy(gasQty).integerValue();
fees = gasPrice.multipliedBy(gasQty).integerValue();

return { ...transaction, ...patch };
if (
transaction.memo !== memo ||
transaction.fees !== fees ||
transaction.gas !== gas
) {
return { ...transaction, memo, fees, gas };
}

return transaction;
};

export default prepareTransaction;
10 changes: 7 additions & 3 deletions src/families/cosmos/js-signOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const signOperation = ({
let cancelled;

async function main() {
const { accountNumber, sequence } = await getAccount(
const { accountNumber, sequence, spendableBalance } = await getAccount(
account.freshAddress
);

Expand Down Expand Up @@ -152,6 +152,8 @@ const signOperation = ({

const hash = ""; // resolved at broadcast time
const accountId = account.id;
const fee = transaction.fees || new BigNumber(0);

const type: OperationType =
transaction.mode === "undelegate"
? "UNDELEGATE"
Expand All @@ -176,8 +178,10 @@ const signOperation = ({
id: encodeOperationId(accountId, hash, type),
hash,
type,
value: transaction.amount,
fee: transaction.fees || new BigNumber(0),
value: transaction.useAllAmount
? spendableBalance
: transaction.amount.plus(fee),
fee,
extra: {},
blockHash: null,
blockHeight: null,
Expand Down

1 comment on commit 07480af

@github-actions
Copy link

Choose a reason for hiding this comment

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

✅ 1 txs ⚠️ 1 specs ($47.83) for Bot 'Cosmos JS'

⚠️ 5 mutations uncovered

1 critical spec errors

Spec Cosmos failed!

TypeError: Cannot read property 'toFixed' of undefined
Details of the 1 mutations

Spec Cosmos (4)

Spec Cosmos found 20 Cosmos accounts (preload: 1000ms). Will use Cosmos 2.18.0 on nanoS 2.0.0
(785ms) Cosmos 1: 0.06811 ATOM (43ops) (cosmos1zpvthe3pxvyje7vrcqv2m6qe2qgu0r8gkz64er on 44'/118'/0'/0/0) #0 js:2:cosmos:cosmos1zpvthe3pxvyje7vrcqv2m6qe2qgu0r8gkz64er:
(974ms) Cosmos 2: 0.25982 ATOM (51ops) (cosmos1zd0e8j9g9wur8up7j5aqahz233r4jmvqhyffnv on 44'/118'/1'/0/0) #1 js:2:cosmos:cosmos1zd0e8j9g9wur8up7j5aqahz233r4jmvqhyffnv:
(804ms) Cosmos 3: 0.30291 ATOM (41ops) (cosmos1uty5nf28emeahuhdg9runmzuluq9uj5uek4jk0 on 44'/118'/2'/0/0) #2 js:2:cosmos:cosmos1uty5nf28emeahuhdg9runmzuluq9uj5uek4jk0:
(1061ms) Cosmos 4: 0.06887 ATOM (39ops) (cosmos1ccemcd8p3r293e5rcv0awaj9hfnn6wmxkz520n on 44'/118'/3'/0/0) #3 js:2:cosmos:cosmos1ccemcd8p3r293e5rcv0awaj9hfnn6wmxkz520n:
(549ms) Cosmos 5: 0.0913 ATOM (39ops) (cosmos1mvk3fwxgdfac4yjmgl9hdz0555q8gljydzvjtu on 44'/118'/4'/0/0) #4 js:2:cosmos:cosmos1mvk3fwxgdfac4yjmgl9hdz0555q8gljydzvjtu:
(691ms) Cosmos 6: 0.05578 ATOM (32ops) (cosmos1draxuzz0aukggx63m852wm8mlxqpus2009cs97 on 44'/118'/5'/0/0) #5 js:2:cosmos:cosmos1draxuzz0aukggx63m852wm8mlxqpus2009cs97:
(1462ms) Cosmos 7: 0.07142 ATOM (30ops) (cosmos14k7faf4cvxlflta4hytxavx3p7vj6uw69lgf03 on 44'/118'/6'/0/0) #6 js:2:cosmos:cosmos14k7faf4cvxlflta4hytxavx3p7vj6uw69lgf03:
(807ms) Cosmos 8: 0.27035 ATOM (43ops) (cosmos12uwsw42ec4lh75202zcp0r5ggj9vlwp4c05ydz on 44'/118'/7'/0/0) #7 js:2:cosmos:cosmos12uwsw42ec4lh75202zcp0r5ggj9vlwp4c05ydz:
(1085ms) Cosmos 9: 0.06007 ATOM (25ops) (cosmos1pggp9ffssaf8hdev9aekcafarxnuxmz4er44hm on 44'/118'/8'/0/0) #8 js:2:cosmos:cosmos1pggp9ffssaf8hdev9aekcafarxnuxmz4er44hm:
(689ms) Cosmos 10: 0.0955 ATOM (35ops) (cosmos1cnr3madhjge7s3lpz52qdcay69llm6uwx3s8y3 on 44'/118'/9'/0/0) #9 js:2:cosmos:cosmos1cnr3madhjge7s3lpz52qdcay69llm6uwx3s8y3:
(850ms) Cosmos 11: 0.04336 ATOM (23ops) (cosmos1gyqj09h2yc0hs4ggwy74qca8t4hl6ns35w92l5 on 44'/118'/10'/0/0) #10 js:2:cosmos:cosmos1gyqj09h2yc0hs4ggwy74qca8t4hl6ns35w92l5:
(626ms) Cosmos 12: 0.07054 ATOM (31ops) (cosmos1zkx8vmvzfz6ah8les5y62rxmvdrk4egvcfzfcu on 44'/118'/11'/0/0) #11 js:2:cosmos:cosmos1zkx8vmvzfz6ah8les5y62rxmvdrk4egvcfzfcu:
(665ms) Cosmos 13: 0.023944 ATOM (13ops) (cosmos19y63dxgjvuj46hcgrt0w0rpct85d3sfxgmlgpa on 44'/118'/12'/0/0) #12 js:2:cosmos:cosmos19y63dxgjvuj46hcgrt0w0rpct85d3sfxgmlgpa:
(618ms) Cosmos 14: 0.04729 ATOM (18ops) (cosmos19fm3nalhxtswpn78ng2rl4vs7p04vljgfeq2qs on 44'/118'/13'/0/0) #13 js:2:cosmos:cosmos19fm3nalhxtswpn78ng2rl4vs7p04vljgfeq2qs:
(1469ms) Cosmos 15: 0.04307 ATOM (12ops) (cosmos14ps228paxvqngcz6adj0d98f4qftkl68atrvjw on 44'/118'/14'/0/0) #14 js:2:cosmos:cosmos14ps228paxvqngcz6adj0d98f4qftkl68atrvjw:
(458ms) Cosmos 16: 0.10173 ATOM (13ops) (cosmos17wd40x799cjhh84zsz38tlqw4nefpcfwahp87t on 44'/118'/15'/0/0) #15 js:2:cosmos:cosmos17wd40x799cjhh84zsz38tlqw4nefpcfwahp87t:
(442ms) Cosmos 17: 0.04855 ATOM (6ops) (cosmos19u92aw048hh6ggw0jxrhl8npy9dk8mchas5lqk on 44'/118'/16'/0/0) #16 js:2:cosmos:cosmos19u92aw048hh6ggw0jxrhl8npy9dk8mchas5lqk:
(642ms) Cosmos 18: 0.04797 ATOM (12ops) (cosmos19xyjhr494jvemvh2jhq3ws4kpc3c9h6r2zlnmr on 44'/118'/17'/0/0) #17 js:2:cosmos:cosmos19xyjhr494jvemvh2jhq3ws4kpc3c9h6r2zlnmr:
(433ms) Cosmos 19: 0.13736 ATOM (4ops) (cosmos1r0ra6y8vveagmrrs5d8zzkgydhjvyfe6728vq2 on 44'/118'/18'/0/0) #18 js:2:cosmos:cosmos1r0ra6y8vveagmrrs5d8zzkgydhjvyfe6728vq2:
(1675ms) Cosmos 20: 0 ATOM (0ops) (cosmos1zqv0kyf5muqsk2z50j3msxaul00ldunvvp9aym on 44'/118'/19'/0/0) #19 js:2:cosmos:cosmos1zqv0kyf5muqsk2z50j3msxaul00ldunvvp9aym:
all accounts sync in 4.1s
▬ Cosmos 2.18.0 on nanoS 2.0.0
→ FROM Cosmos 1: 0.06811 ATOM (43ops) (cosmos1zpvthe3pxvyje7vrcqv2m6qe2qgu0r8gkz64er on 44'/118'/0'/0/0) #0 js:2:cosmos:cosmos1zpvthe3pxvyje7vrcqv2m6qe2qgu0r8gkz64er: (! sum of ops 0.075783 ATOM) 0.068113 ATOM spendable. 

max spendable ~0.06331
★ using mutation 'send some'
→ TO Cosmos 8: 0.27035 ATOM (43ops) (cosmos12uwsw42ec4lh75202zcp0r5ggj9vlwp4c05ydz on 44'/118'/7'/0/0) #7 js:2:cosmos:cosmos12uwsw42ec4lh75202zcp0r5ggj9vlwp4c05ydz:
✔️ transaction 
SEND  0.029284 ATOM
TO cosmos12uwsw42ec4lh75202zcp0r5ggj9vlwp4c05ydz

with fees=0.00431
  memo=LedgerLiveBot
STATUS (1672ms)
  amount: 0.029284 ATOM
  estimated fees: 0.00431 ATOM
  total spent: 0.033594 ATOM
✔️ has been signed! (2617ms) 
✔️ broadcasted! (146ms) optimistic operation: 
  -0.033594 ATOM     OUT        412937293C200696D05FBDDD4EAAC974872888FF135D8153B08F442EFD9AD96C 2022-02-25T13:46
✔️ operation confirmed (11.3s): 
  -0.033594 ATOM     OUT        412937293C200696D05FBDDD4EAAC974872888FF135D8153B08F442EFD9AD96C 2022-02-25T13:46
✔️ Cosmos 1: 0.03451 ATOM (44ops) (cosmos1zpvthe3pxvyje7vrcqv2m6qe2qgu0r8gkz64er on 44'/118'/0'/0/0) #0 js:2:cosmos:cosmos1zpvthe3pxvyje7vrcqv2m6qe2qgu0r8gkz64er: (! sum of ops 0.042189 ATOM) 0.034519 ATOM spendable. 

(final state reached in 11.3s)


Details of the 5 uncovered mutations

Spec Cosmos (5)

  • send max: balance is too low (3)
  • delegate new validators: balance is too low (3)
  • undelegate: balance is too low (3)
  • redelegate: balance is too low (3)
  • claim rewards: balance is too low (3)

Portfolio ($47.83)

Details of the 1 currencies
Spec (accounts) Operations Balance funds?
Cosmos (19) 512 (+2) 1.2151 ATOM (- 0.00431) ($47.83) 👍 cosmos1zpvthe3pxvyje7vrcqv2m6qe2qgu0r8gkz64er

Please sign in to comment.