Skip to content

Commit

Permalink
Merge pull request #61 from CudoVentures/cudos-dev
Browse files Browse the repository at this point in the history
Sync with cudos-dev
  • Loading branch information
avalkov authored Aug 12, 2022
2 parents 3902f09 + 8ed51a8 commit 1211ddb
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/components/msg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
convertMsgsToModels,
} from './utils';

import {
mutateMessages,
} from './mutate';

// =========================
// msg components
// =========================
Expand Down Expand Up @@ -80,6 +84,7 @@ export {
getMessageModelByType,
getMessageByType,
convertMsgsToModels,
mutateMessages,
};

export {
Expand Down
141 changes: 141 additions & 0 deletions src/components/msg/mutate/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import * as R from 'ramda';
import * as MODELS from '@models';
import { getMessageModelByType } from '@msg';

export const mutateMessages = (transaction: any) => {
return insertMissingWithdrawRewardsMsgWhenDelegating(transaction);
};

const insertMissingWithdrawRewardsMsgWhenDelegating = (transaction: any) => {
const messages = R.pathOr([], ['messages'], transaction);
const logs = R.pathOr([], ['logs'], transaction);

let isWithdrawMsgFound = false;

for (let i = 0; i < messages.length; i += 1) {
const model = getMessageModelByType(messages[i]?.['@type']);

if (model === MODELS.MsgWithdrawDelegatorReward) {
isWithdrawMsgFound = true;
break;
}
}

if (isWithdrawMsgFound) {
return [messages, logs];
}

for (let i = 0; i < messages.length; i += 1) {
const model = getMessageModelByType(messages[i]?.['@type']);

if (model === MODELS.MsgDelegate) {
const coinReceivedEvent = getEventAtIndex(transaction, i, 'coin_received');
const coinSpentEvent = getEventAtIndex(transaction, i, 'coin_spent');

if (coinReceivedEvent && coinSpentEvent) {
const [withdrawMessage, events] = buildWithdrawRewardsMessage(messages[i],
coinReceivedEvent, coinSpentEvent);

if (i > 0) {
i -= 1;
}

messages.splice(i, 0, withdrawMessage);
logs.splice(i, 0, { events });

break;
}
}
}

return [messages, logs];
};

const getEventAtIndex = (transaction: any, index: number, eventType: string) => {
const logs = R.pathOr([], ['logs'], transaction);
if (logs.length <= index) {
return;
}

const { events } = logs[index];

for (let i = 0; i < events.length; i += 1) {
if (events[i].type === eventType) {
return events[i];
}
}
};

const buildWithdrawRewardsMessage = (delegateMsg: any, coinReceivedEvent: any,
coinSpentEvent: any) => {
const withdrawMsg = {
'@type': '/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward',
delegator_address: delegateMsg.delegator_address,
validator_address: delegateMsg.validator_address,
};

const events = [];

events.push(buildEvent('coin_received', [
{
key: 'receiver', value: coinReceivedEvent.attributes[0].value,
},
{
key: 'amount', value: coinReceivedEvent.attributes[1].value,
},
]));

events.push(buildEvent('coin_spent', [
{
key: 'spender', value: coinSpentEvent.attributes[0].value,
},
{
key: 'amount', value: coinSpentEvent.attributes[1].value,
},
]));

events.push(buildEvent('message', [
{
key: 'action', value: '/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward',
},
{
key: 'sender', value: coinSpentEvent.attributes[0].value,
},
{
key: 'module', value: 'distribution',
},
{
key: 'sender', value: coinReceivedEvent.attributes[0].value,
},
]));

events.push(buildEvent('transfer', [
{
key: 'recipient', value: coinReceivedEvent.attributes[0].value,
},
{
key: 'sender', value: coinSpentEvent.attributes[0].value,
},
{
key: 'amount', value: coinReceivedEvent.attributes[1].value,
},
]));

events.push(buildEvent('withdraw_rewards', [
{
key: 'amount', value: coinReceivedEvent.attributes[1].value,
},
{
key: 'validator', value: delegateMsg.validator_address,
},
]));

return [withdrawMsg, events];
};

const buildEvent = (eventType: any, attributes: any) => {
return {
type: eventType,
attributes,
};
};
9 changes: 8 additions & 1 deletion src/screens/transaction_details/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
TransactionDetailsQuery,
} from '@graphql/types';
import { formatToken } from '@utils/format_token';
import { convertMsgsToModels } from '@msg';
import {
convertMsgsToModels, mutateMessages,
} from '@msg';

import {
TransactionState,
} from './types';
Expand Down Expand Up @@ -113,6 +116,10 @@ export const useTransactionDetails = () => {
};
stateChange.logs = formatLogs();

const [mutatedMessages, logs] = mutateMessages(data.transaction[0]);
data.transaction[0].messages = mutatedMessages;
data.transaction[0].logs = logs;

// =============================
// messages
// =============================
Expand Down

0 comments on commit 1211ddb

Please sign in to comment.