Skip to content

Commit

Permalink
Fabo/allow to sign messages for Terra (#3421)
Browse files Browse the repository at this point in the history
* allow to sign messages for Terra

* Update src/ActionModal/utils/MessageConstructor.js

Co-Authored-By: Jordan Bibla <[email protected]>

* added more messages to terra

* skip balance query if no denom known

* refactor send modal

* handle denoms when no balances

* fix amount of undefined

* fix selected index not checked correctly

* changelog

* fixed some tests

* fixed sendmodal tests

Co-authored-by: Jordan Bibla <[email protected]>
Co-authored-by: Ana G. <[email protected]>
  • Loading branch information
3 people authored Jan 17, 2020
1 parent 53e5470 commit a1489df
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 207 deletions.
1 change: 1 addition & 0 deletions changes/fabo_cross-signing-2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Added] Allow signing for Terra @faboweb
8 changes: 4 additions & 4 deletions src/ActionModal/components/DelegationModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
{{ denom }}s
</span>
<TmFormMsg
v-if="balance === 0"
v-if="balance.amount === '0'"
:msg="`doesn't have any ${denom}s`"
name="Wallet"
type="custom"
Expand Down Expand Up @@ -164,7 +164,7 @@ export default {
},
data: () => ({
amount: null,
fromSelectedIndex: `0`,
fromSelectedIndex: 0,
balance: {
amount: null,
denom: ``
Expand Down Expand Up @@ -266,7 +266,7 @@ export default {
return this.fromOptions[this.fromSelectedIndex].maximum
},
isRedelegation() {
return this.fromSelectedIndex !== `0`
return this.fromSelectedIndex !== 0 && this.fromSelectedIndex !== "0" // where are these 0 strings comming from?
}
},
methods: {
Expand Down Expand Up @@ -376,7 +376,7 @@ export default {
`,
skip() {
/* istanbul ignore next */
return !this.address
return !this.address || !this.denom
},
variables() {
/* istanbul ignore next */
Expand Down
26 changes: 8 additions & 18 deletions src/ActionModal/components/SendModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ export default {
editMemo: false,
isFirstLoad: true,
selectedToken: ``,
selectedBalance: ``,
balances: [
{
amount: null,
Expand All @@ -197,9 +196,12 @@ export default {
computed: {
...mapGetters([`network`]),
...mapGetters({ userAddress: `address` }),
selectedBalance() {
return this.balances.filter(
balance => balance.denom === this.selectedToken || this.denoms[0]
)[0]
},
transactionData() {
// This is the best place I have found so far to call this function
this.setTokenAndBalance()
return {
type: transaction.SEND,
toAddress: this.address,
Expand All @@ -221,7 +223,9 @@ export default {
}
},
getDenoms() {
return this.denoms.map(denom => (denom = { key: denom, value: denom }))
return this.denoms
? this.denoms.map(denom => (denom = { key: denom, value: denom }))
: []
}
},
watch: {
Expand Down Expand Up @@ -271,20 +275,6 @@ export default {
)
}
},
setTokenAndBalance() {
// if it is single-token network, then we take the first and only value from the
// balances array
if (this.balances.length === 1) {
this.selectedToken = this.getDenoms[0].value
this.selectedBalance = this.balances[0]
// if it is a multiple-tokens network and we already have a selectedToken by the user
// then we search for the corresponding balance from the array
} else if (this.selectedToken) {
this.selectedBalance = this.balances.filter(
balance => balance.denom === this.selectedToken
)[0]
}
},
token() {
if (!this.selectedToken) return ``
Expand Down
20 changes: 14 additions & 6 deletions src/ActionModal/utils/ActionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,22 @@ export default class ActionManager {
context.bondDenom,
context.rewards
)
validators.forEach(validator => {
const txMessage = transformMessage(type, context.userAddress, {
validatorAddress: validator
await Promise.all(
validators.map(async validator => {
const txMessage = await transformMessage(
context.networkId,
type,
context.userAddress,
{
validatorAddress: validator
}
)
txMessages.push(txMessage)
})
txMessages.push(txMessage)
})
)
} else {
const txMessage = transformMessage(
const txMessage = await transformMessage(
context.networkId,
type,
context.userAddress,
transactionProperties
Expand Down
203 changes: 27 additions & 176 deletions src/ActionModal/utils/MessageConstructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ export const getMessage = async (

const getMessageConstructor = async context => {
switch (context.networkId) {
case `local-cosmos-hub-testnet`: {
const { default: Cosmos } = await import("cosmos-apiV0")
const cosmos = new Cosmos(context.url || "", context.chainId || "")
return (messageType, userAddress, transactionProperties) =>
cosmos[messageType](userAddress, transactionProperties)
}
case `local-cosmos-hub-testnet`:
case `terra-mainnet`:
case `terra-testnet`:
case `cosmos-hub-mainnet`:
case `cosmos-hub-testnet`: {
const { default: Cosmos } = await import("cosmos-apiV2")
Expand All @@ -33,10 +30,9 @@ const getMessageConstructor = async context => {

export const getTransactionSigner = async context => {
switch (context.networkId) {
case `local-cosmos-hub-testnet`: {
const { createSignedTransaction } = await import("cosmos-apiV0")
return createSignedTransaction
}
case `local-cosmos-hub-testnet`:
case `terra-mainnet`:
case `terra-testnet`:
case `cosmos-hub-mainnet`:
case `cosmos-hub-testnet`: {
const { createSignedTransaction } = await import("cosmos-apiV2")
Expand All @@ -48,11 +44,9 @@ export const getTransactionSigner = async context => {

export const getMultiMessage = async (context, messages) => {
switch (context.networkId) {
case `local-cosmos-hub-testnet`: {
const { default: Cosmos } = await import("cosmos-apiV0")
const cosmos = new Cosmos(context.url || "", context.chainId || "")
return cosmos.MultiMessage(context.userAddress, messages)
}
case `local-cosmos-hub-testnet`:
case `terra-mainnet`:
case `terra-testnet`:
case `cosmos-hub-mainnet`:
case `cosmos-hub-testnet`: {
const { default: Cosmos } = await import("cosmos-apiV2")
Expand All @@ -62,174 +56,31 @@ export const getMultiMessage = async (context, messages) => {
}
}

// Bank
/* istanbul ignore next */
export function MsgSend(
senderAddress,
{
toAddress,
amounts // [{ denom, amount}]
}
) {
return {
type: `cosmos-sdk/MsgSend`,
value: {
from_address: senderAddress,
to_address: toAddress,
amount: amounts.map(Coin)
}
}
}
async function getMessageFormatter(network, messageType) {
let networkMessages
try {
networkMessages = await import(`./networkMessages/${network}.js`)
} catch (err) {
throw new Error("Signing for this network is not enabled.")
}
const messageFormatter = networkMessages[messageType]

// Staking
export function MsgDelegate(
senderAddress,
{ validatorAddress, amount, denom }
) {
/* istanbul ignore next */
return {
type: `cosmos-sdk/MsgDelegate`,
value: {
delegator_address: senderAddress,
validator_address: validatorAddress,
amount: Coin({ amount, denom })
}
if (!messageFormatter) {
throw new Error(
`Creating the '${messageType}' message for the ${network} network is not supported`
)
}
}

export function MsgUndelegate(
senderAddress,
{ validatorAddress, amount, denom }
) {
/* istanbul ignore next */
return {
type: `cosmos-sdk/MsgUndelegate`,
value: {
validator_address: validatorAddress,
delegator_address: senderAddress,
amount: Coin({ amount, denom })
}
}
return messageFormatter
}

export function MsgRedelegate(
senderAddress,
{ validatorSourceAddress, validatorDestinationAddress, amount, denom }
) {
/* istanbul ignore next */
return {
type: `cosmos-sdk/MsgBeginRedelegate`,
value: {
delegator_address: senderAddress,
validator_src_address: validatorSourceAddress,
validator_dst_address: validatorDestinationAddress,
amount: Coin({ amount, denom })
}
}
}

// Governance
export function MsgSubmitProposal(
senderAddress,
{
title,
description,
initialDeposits // [{ denom, amount }]
}
) {
/* istanbul ignore next */
return {
type: `cosmos-sdk/MsgSubmitProposal`,
value: {
content: {
type: "cosmos-sdk/TextProposal",
value: {
title,
description
}
},
proposer: senderAddress,
initial_deposit: initialDeposits.map(Coin)
}
}
}

export function MsgVote(senderAddress, { proposalId, option }) {
/* istanbul ignore next */
return {
type: `cosmos-sdk/MsgVote`,
value: {
voter: senderAddress,
proposal_id: proposalId,
option
}
}
}

export function MsgDeposit(
senderAddress,
{
proposalId,
amounts // [{ denom, amount }]
}
) {
/* istanbul ignore next */
return {
type: `cosmos-sdk/MsgDeposit`,
value: {
depositor: senderAddress,
proposal_id: proposalId,
amount: amounts.map(Coin)
}
}
}

export function MsgWithdrawDelegationReward(
export async function transformMessage(
network,
messageType,
senderAddress,
{ validatorAddress }
message
) {
/* istanbul ignore next */
return {
type: `cosmos-sdk/MsgWithdrawDelegationReward`,
value: {
delegator_address: senderAddress,
validator_address: validatorAddress
}
}
}

function Coin({ amount, denom }) {
return {
amount: String(amount),
denom
}
}

/* istanbul ignore next */
function getMessageFormatter(messageType) {
switch (messageType) {
case "MsgSend":
return MsgSend
case "MsgDelegate":
return MsgDelegate
case "MsgUndelegate":
return MsgUndelegate
case "MsgRedelegate":
return MsgRedelegate
case "MsgSubmitProposal":
return MsgSubmitProposal
case "MsgVote":
return MsgVote
case "MsgDeposit":
return MsgDeposit
case "MsgWithdrawDelegationReward":
return MsgWithdrawDelegationReward
default:
return null
}
}

export function transformMessage(messageType, senderAddress, message) {
const messageFormatter = getMessageFormatter(messageType)
const messageFormatter = await getMessageFormatter(network, messageType)
return messageFormatter(senderAddress, message)
}
Loading

0 comments on commit a1489df

Please sign in to comment.