Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benji/ext issue 29 calculate subtotal in SessionApprove TableInvoice #2787

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Code Improvements] [#2785](https://github.com/cosmos/lunie/pull/2785) Refactors SessionApprove to pass TableInvoice the subtotal fee and amount of the transaction. TableInvoice was refactored in PR #2785 @thebkr7
47 changes: 21 additions & 26 deletions src/components/common/SessionApprove.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
From
<Bech32 :address="senderAddress" />
</div>

<!-- <TableInvoice :fees="fees" /> -->

<TableInvoice
:amount="amount"
Copy link
Contributor

Choose a reason for hiding this comment

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

This won't (yet) pass the tests since the TableInvoice code is in the other PR #2802

:estimated-fee="fees"
:bond-denom="bondDenom"
/>
<TmFormGroup
:error="$v.password.$error && $v.password.$invalid"
class="action-modal-group"
Expand Down Expand Up @@ -70,34 +72,18 @@ import TmFormGroup from "common/TmFormGroup"
import TmField from "common/TmField"
import TmFormMsg from "common/TmFormMsg"
import LiAnyTransaction from "transactions/LiAnyTransaction"
// import TableInvoice from "src/ActionModal/components/TableInvoice"
import TableInvoice from "src/ActionModal/components/TableInvoice"
import Bech32 from "common/Bech32"
import { required } from "vuelidate/lib/validators"

// TODO move into own helper file
// Parse into Lunie tx format from signMessage to display tx properly
function parseTx(signMessage) {
const { msgs, fee, memo } = JSON.parse(signMessage)

return {
tx: {
type: "auth/StdTx",
value: {
msg: msgs,
fee,
memo
}
}
}
}
import { parseTx, parseFee, parseValueObj } from "../../scripts/parsers.js"

export default {
name: `session-ext-approve-tran`,
name: `session-approve`,
components: {
TmBtn,
TmFormGroup,
LiAnyTransaction,
// TableInvoice,
TableInvoice,
Bech32,
TmField,
TmFormMsg
Expand All @@ -111,11 +97,20 @@ export default {
tx() {
return this.signRequest ? parseTx(this.signRequest.signMessage) : null
},
// fees() {
// return this.tx ? this.tx.tx.value.fee.amount : null
// },
fees() {
return this.tx ? parseFee(this.tx.tx) : null
},
senderAddress() {
return this.signRequest ? this.signRequest.senderAddress : null
},
amountCoin() {
return this.tx ? parseValueObj(this.tx.tx) : null
},
amount() {
return this.amountCoin ? Number(this.amountCoin.amount) : null
},
bondDenom() {
return this.amountCoin ? this.amountCoin.denom : null
}
},
methods: {
Expand Down
34 changes: 34 additions & 0 deletions src/scripts/parsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use strict"

export const parseTx = signMessage => {
const { msgs, fee, memo } = JSON.parse(signMessage)
thebkr7 marked this conversation as resolved.
Show resolved Hide resolved

return {
tx: {
type: "auth/StdTx",
value: {
msg: msgs,
fee,
memo
}
}
}
}

export const parseFee = stdTx => {
const {
value: { fee }
} = stdTx
return Number(fee.amount[0].amount)
}

export const parseValueObj = stdTx => {
const {
value: { msg }
} = stdTx
if (msg[0].type === "cosmos-sdk/MsgSend") {
return msg[0].value.amount[0]
} else {
return msg[0].value.amount
}
}
8 changes: 5 additions & 3 deletions test/unit/specs/components/common/SessionApprove.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ describe(`SessionApprove`, () => {

let wrapper, $store

const signMessage = `{"account_number":"1","chain_id":"testnet","fee":{"amount":[{"amount":"40","denom":"stake"}],"gas":"39953"},"memo":"(Sent via Lunie)","msgs":[{"type":"cosmos-sdk/MsgSend","value":{"amount":[{"amount":"12000000","denom":"stake"}],"from_address":"cosmos1ek9cd8ewgxg9w5xllq9um0uf4aaxaruvcw4v9e","to_address":"cosmos1324vt5j3wzx0xsc32mjhkrvy5gn5ef2hrwcg29"}}],"sequence":"0"}`

beforeEach(() => {
const getters = {
signRequest: {
senderAddress: "cosmos1234",
signMessage: "{}"
signMessage
}
}

Expand Down Expand Up @@ -67,7 +69,7 @@ describe(`SessionApprove`, () => {
await wrapper.vm.$nextTick()
expect(wrapper.vm.$store.dispatch).toHaveBeenCalledWith(
"approveSignRequest",
{ password: "1234", senderAddress: "cosmos1234", signMessage: "{}" }
{ password: "1234", senderAddress: "cosmos1234", signMessage }
)
expect(wrapper.vm.close).toHaveBeenCalled()
})
Expand All @@ -79,7 +81,7 @@ describe(`SessionApprove`, () => {
await wrapper.vm.$nextTick()
expect(wrapper.vm.$store.dispatch).toHaveBeenCalledWith(
"rejectSignRequest",
{ senderAddress: "cosmos1234", signMessage: "{}" }
{ signMessage, senderAddress: "cosmos1234" }
)
expect(wrapper.vm.close).toHaveBeenCalled()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ exports[`SessionApprove shows the approval modal with the transaction and an inv
/>
</div>

<tableinvoice-stub
amount="12000000"
bond-denom="stake"
estimated-fee="40"
gasprice="2.5e-8"
/>

<tmformgroup-stub
class="action-modal-group"
fieldid="password"
Expand Down
103 changes: 103 additions & 0 deletions test/unit/specs/scripts/parsers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { parseTx, parseFee, parseValueObj } from "scripts/parsers"

const signedMessage = {
type: "auth/StdTx",
value: {
msg: [
{
type: "cosmos-sdk/MsgSend",
value: {
amount: [
{
amount: "10000000",
denom: "stake"
}
],
from_address: "cosmos1ek9cd8ewgxg9w5xllq9um0uf4aaxaruvcw4v9e",
to_address: "cosmos1324vt5j3wzx0xsc32mjhkrvy5gn5ef2hrwcg29"
}
}
],
fee: {
amount: [
{
amount: "40",
denom: "stake"
}
],
gas: "39953"
},
memo: "(Sent via Lunie)"
}
}

const signedActionMessage = {
type: "auth/StdTx",
value: {
msg: [
{
type: "cosmos-sdk/MsgDelegate",
value: {
amount: [
{
amount: "10000000",
denom: "stake"
}
],
from_address: "cosmos1ek9cd8ewgxg9w5xllq9um0uf4aaxaruvcw4v9e",
to_address: "cosmos1324vt5j3wzx0xsc32mjhkrvy5gn5ef2hrwcg29"
}
}
],
fee: {
amount: [
{
amount: "40",
denom: "stake"
}
],
gas: "39953"
},
memo: "(Sent via Lunie)"
}
}

describe(`parsers helper`, () => {
it(`should parse a signedmessaged ParseTx`, () => {
const shortMessage = {
tx: {
type: "auth/StdTx",
value: {
msg: "some message",
fee: 0.01,
memo: "Sent from Lunie"
}
}
}
expect(
parseTx(`{"msgs":"some message","fee":0.01,"memo":"Sent from Lunie"}`)
).toMatchObject(shortMessage)
})

it(`should parse a signedmessaged parseFee`, () => {
expect(parseFee(signedMessage)).toBe(40)
})

it(`should parse a signedmessaged parseValueObj`, () => {
const parsedValueObj = {
amount: "10000000",
denom: "stake"
}
expect(parseValueObj(signedMessage)).toMatchObject(parsedValueObj)
})

it(`should parse a signedActionMessage parseValueObj`, () => {
const parsedValueObj = [
{
amount: "10000000",
denom: "stake"
}
]
expect(parseValueObj(signedActionMessage)).toMatchObject(parsedValueObj)
})
})