Skip to content

Commit

Permalink
change parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnxie999 committed Jun 30, 2023
1 parent e72b8b4 commit aa76c32
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 53 deletions.
23 changes: 22 additions & 1 deletion src/containers/Transactions/Meta/RippleState.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CURRENCY_OPTIONS } from '../../shared/transactionUtils'
import { localizeNumber } from '../../shared/utils'
import { Account } from '../../shared/components/Account'

const render = (t, language, action, node, index) => {
export const computeBalanceChange = (node) => {
const fields = node.FinalFields || node.NewFields
const prev = node.PreviousFields
const { currency } = fields.Balance
Expand All @@ -24,6 +24,27 @@ const render = (t, language, action, node, index) => {
}

const change = finalBalance - previousBalance
return {
change,
numberOption,
previousBalance,
finalBalance,
currency,
account,
counterAccount,
}
}

const render = (t, language, action, node, index) => {
const {
change,
numberOption,
previousBalance,
finalBalance,
currency,
account,
counterAccount,
} = computeBalanceChange(node)

const line1 = (
<Trans i18nKey="transaction_balance_line_one">
Expand Down
15 changes: 6 additions & 9 deletions src/containers/shared/components/Transaction/Clawback/Simple.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@ import { Amount } from '../../Amount'
export const Simple: TransactionSimpleComponent = ({
data,
}: TransactionSimpleProps<ClawbackInstructions>) => {
const { account: issuer, amount, holder } = data.instructions
const { amount, holder } = data.instructions
const { t } = useTranslation()

return (
<>
<SimpleRow label={t('issuer')} data-test="issuer">
<Account account={issuer} />
<SimpleRow label={t('holder')} data-test="holder">
<Account account={holder} />
</SimpleRow>
{holder && (
<SimpleRow label={t('holder')} data-test="holder">
<Account account={holder} />
{amount && (
<SimpleRow label={t('amount')} data-test="amount">
<Amount value={amount} displayIssuer />
</SimpleRow>
)}
<SimpleRow label={t('amount')} data-test="amount">
<Amount value={amount} displayIssuer />
</SimpleRow>
</>
)
}
23 changes: 22 additions & 1 deletion src/containers/shared/components/Transaction/Clawback/parser.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
import { Clawback, ClawbackInstructions } from './types'
import { TransactionParser } from '../types'
import { formatAmount } from '../../../../../rippled/lib/txSummary/formatAmount'
import { computeBalanceChange } from '../../../../Transactions/Meta/RippleState'

export const parser: TransactionParser<Clawback, ClawbackInstructions> = (

Check failure on line 6 in src/containers/shared/components/Transaction/Clawback/parser.ts

View workflow job for this annotation

GitHub Actions / typescript-check (18.12)

Type '(tx: Clawback, meta: any) => { holder: string | undefined; amount?: undefined; } | { amount: ExplorerAmount; holder: string | undefined; }' is not assignable to type 'TransactionParser<Clawback, ClawbackInstructions>'.
tx,
meta,
) => {
const account = tx.Account
const amount = formatAmount(tx.Amount)
const holder = amount.issuer
amount.issuer = account

// At this point, we need to get the ACTUAL balance change as a
// result of Clawback. If the issuer tries to claw back more than
// what holder has, only the max available balance is clawed.
const trustlineNode = meta.AffectedNodes.filter(
(node: any) =>
node.DeletedNode?.LedgerEntryType === 'RippleState' ||
node.ModifiedNode?.LedgerEntryType === 'RippleState',
)

if (!trustlineNode || trustlineNode.length != 1)
return {
holder,
}

const { change } = computeBalanceChange(
trustlineNode[0].ModifiedNode ?? trustlineNode[0].DeletedNode,
)

amount.amount = Math.abs(change)

return {
account,
amount,
holder,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import { createSimpleWrapperFactory, expectSimpleRowText } from '../../test'
import {
createSimpleWrapperFactory,
expectSimpleRowText,
expectSimpleRowNotToExist,
} from '../../test'
import { Simple } from '../Simple'
import transaction from './mock_data/Clawback.json'
import transactionFailure from './mock_data/Clawback_Failure.json'

const createWrapper = createSimpleWrapperFactory(Simple)

describe('Clawback', () => {
it('handles Clawback simple view ', () => {
const wrapper = createWrapper(transaction)
expectSimpleRowText(wrapper, 'issuer', 'rBUPvxccLNQJ3mKKK4JZgrJL6q5SBR674X')
expectSimpleRowText(wrapper, 'holder', 'rn9N1bqXzKV6AwDXiS1mCjy27BGuwwweY6')
expectSimpleRowText(wrapper, 'holder', 'rscBWQpyZEmQvupeB1quu7Ky8YX4f5CHDP')
expectSimpleRowText(
wrapper,
'amount',
'3,840.00 FOO.rBUPvxccLNQJ3mKKK4JZgrJL6q5SBR674X',
'3,840.00 FOO.rDZ713igKfedN4hhY6SjQse4Mv3ZrBxnn9',
)
wrapper.unmount()
})

it('handles failed Clawback simple view ', () => {
const wrapper = createWrapper(transactionFailure)
expectSimpleRowText(wrapper, 'holder', 'rDZ713igKfedN4hhY6SjQse4Mv3ZrBxnn9')
expectSimpleRowNotToExist(wrapper, 'amount')
wrapper.unmount()
})
})
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
{
"tx": {
"Account": "rBUPvxccLNQJ3mKKK4JZgrJL6q5SBR674X",
"Account": "rDZ713igKfedN4hhY6SjQse4Mv3ZrBxnn9",
"Amount": {
"currency": "FOO",
"issuer": "rn9N1bqXzKV6AwDXiS1mCjy27BGuwwweY6",
"value": "3840"
"issuer": "rscBWQpyZEmQvupeB1quu7Ky8YX4f5CHDP",
"value": "4840"
},
"Fee": "10",
"Flags": 0,
"LastLedgerSequence": 315,
"Sequence": 293,
"SigningPubKey": "EDC76D14371304057E386EDA6F159CD7D8463413CF2B48A7759E830912F53C70C7",
"LastLedgerSequence": 515,
"Sequence": 492,
"SigningPubKey": "ED4B74169976E689D549ED4A50BF06C1174115D363CF9E14031030D2BB5CAA274D",
"TransactionType": "Clawback",
"TxnSignature": "F4ABC4F8A1AE5AD0F3854B106524F6BD50D05F725D78AF5E2DB7D27D00221ED2756476ABB6AE12B363940C9F0AF963ED831097B8AFFF2CD6D777184616475B09",
"date": 1688059056000
"TxnSignature": "E3C2138CA0C09DD4243DB47B562BDE334CFADFDFF39B50DE7E2A3D6FBCB1ADD89CCAF19A7DE08C3C295974492C3FC7E7CD71FB040A582154EC26DB09D1AEF800",
"date": 1688136937000
},
"meta": {
"AffectedNodes": [
{
"ModifiedNode": {
"FinalFields": {
"Account": "rDZ713igKfedN4hhY6SjQse4Mv3ZrBxnn9",
"Balance": "99999999960",
"Flags": 2155872256,
"OwnerCount": 0,
"Sequence": 493
},
"LedgerEntryType": "AccountRoot",
"LedgerIndex": "057A552C60FE5AC6C77FC70C28209BB4D33C60C6A350DACEF609C48FE12A4387",
"PreviousFields": {
"Balance": "99999999970",
"Sequence": 492
},
"PreviousTxnID": "E6EF19434F7FF87AB6EE7127A19D9FBC822B3E8E7C26264A902703424AB1F188",
"PreviousTxnLgrSeq": 493
}
},
{
"ModifiedNode": {
"FinalFields": {
Expand All @@ -28,54 +47,35 @@
"Flags": 65536,
"HighLimit": {
"currency": "FOO",
"issuer": "rBUPvxccLNQJ3mKKK4JZgrJL6q5SBR674X",
"issuer": "rDZ713igKfedN4hhY6SjQse4Mv3ZrBxnn9",
"value": "0"
},
"HighNode": "0",
"LowLimit": {
"currency": "FOO",
"issuer": "rn9N1bqXzKV6AwDXiS1mCjy27BGuwwweY6",
"issuer": "rscBWQpyZEmQvupeB1quu7Ky8YX4f5CHDP",
"value": "0"
},
"LowNode": "0"
},
"LedgerEntryType": "RippleState",
"LedgerIndex": "4AFC29AB739180A9AD6348A9A821AAD63BF74ED26657D22FB472B1EBB04A29F5",
"LedgerIndex": "907573E62311BAC99A985BBBB38DAB09D89B0C47167AB6E280DD2B309CFAF31B",
"PreviousFields": {
"Balance": {
"currency": "FOO",
"issuer": "rrrrrrrrrrrrrrrrrrrrBZbvji",
"value": "3840"
}
},
"PreviousTxnID": "0815E4EAE51B91D63788FDE648033FC38CB05FAB2BFF73300F16F01EDE67AFC2",
"PreviousTxnLgrSeq": 295
}
},
{
"ModifiedNode": {
"FinalFields": {
"Account": "rBUPvxccLNQJ3mKKK4JZgrJL6q5SBR674X",
"Balance": "99999999960",
"Flags": 2155872256,
"OwnerCount": 0,
"Sequence": 294
},
"LedgerEntryType": "AccountRoot",
"LedgerIndex": "E5ECCC4DACFA965AC1EA37DFFE605A9F10E471EB97A35F1EADC4ABAA1B26A995",
"PreviousFields": {
"Balance": "99999999970",
"Sequence": 293
},
"PreviousTxnID": "597540CF49E0A63CBDA67691791A3F3894EFEA1FD117373FD5077311DE5D110F",
"PreviousTxnLgrSeq": 294
"PreviousTxnID": "FEF8B23EB453ECC8BE0D4104CEB0B481E029213848EC1BD11470F8B8C3E6B424",
"PreviousTxnLgrSeq": 494
}
}
],
"TransactionIndex": 0,
"TransactionResult": "tesSUCCESS"
},
"hash": "D6D6A6B9B1F6CEF05F70B842BE0DA04DBB36ED523770CF3FFED6C4B7E931303C",
"ledger_index": 296,
"date": 1688059056000
"hash": "0E09D8C61C799AF206D66F81561EC5B52641B439EEA47CB4F5637A918FB51536",
"ledger_index": 496,
"date": 1688136937000
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"tx": {
"Account": "rscBWQpyZEmQvupeB1quu7Ky8YX4f5CHDP",
"Amount": {
"currency": "FOO",
"issuer": "rDZ713igKfedN4hhY6SjQse4Mv3ZrBxnn9",
"value": "4840"
},
"Fee": "10",
"Flags": 0,
"LastLedgerSequence": 514,
"Sequence": 491,
"SigningPubKey": "EDF33A9B13FC43193E136D5E76DD171DF8E7E7184979F214480252A812E148436F",
"TransactionType": "Clawback",
"TxnSignature": "4B36BF456ED06B326CF7C130FDD96F4BA0F6475CA23D5362A1FEB5D10905B377B5B359E077478E4E7412E3ED5A17FE7E7D5E7DAF554976B0B582526E082E0A0D",
"date": 1688136936000
},
"meta": {
"AffectedNodes": [
{
"ModifiedNode": {
"FinalFields": {
"Account": "rscBWQpyZEmQvupeB1quu7Ky8YX4f5CHDP",
"Balance": "99999999970",
"Flags": 0,
"OwnerCount": 1,
"Sequence": 492
},
"LedgerEntryType": "AccountRoot",
"LedgerIndex": "9815D11E762BFF706A1FA0362217D0DB3F3756204523D7DE46469D0DE2A2A83E",
"PreviousFields": {
"Balance": "99999999980",
"Sequence": 491
},
"PreviousTxnID": "FEF8B23EB453ECC8BE0D4104CEB0B481E029213848EC1BD11470F8B8C3E6B424",
"PreviousTxnLgrSeq": 494
}
}
],
"TransactionIndex": 0,
"TransactionResult": "tecNO_PERMISSION"
},
"hash": "6B8CD7D19CD1E3E428F26A897C4CAEDFE4474390C7CBF6D885A76CC66E80DEFA",
"ledger_index": 495,
"date": 1688136936000
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export interface Clawback extends TransactionCommonFields {
}

export interface ClawbackInstructions {
account: string
amount: { currency: string; amount: number; issuer?: string }
holder?: string
amount?: { currency: string; amount: number; issuer?: string }
holder: string
}

0 comments on commit aa76c32

Please sign in to comment.