Skip to content

Commit e014a8f

Browse files
authored
Merge pull request #1855 from oasisprotocol/lw/allowance-prefix
Prefix amount_change with "+" in allowance transactions
2 parents a3ac9cf + 65960b3 commit e014a8f

File tree

5 files changed

+106
-5
lines changed

5 files changed

+106
-5
lines changed

.changelog/1855.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prefix amount_change with "+" in allowance transactions

playwright/tests/allowance.spec.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { Page, expect, test } from '@playwright/test'
2+
import { Transaction } from '../../src/oasis-nexus/api'
3+
4+
async function setup(page: Page) {
5+
await page.route(
6+
'https://api.coingecko.com/api/v3/simple/price?ids=oasis-network&vs_currencies=usd',
7+
route => {
8+
// Don't respond
9+
},
10+
)
11+
await page.route(
12+
url => url.href.includes('.oasis.io/v1/'),
13+
route => {
14+
// Don't respond
15+
},
16+
)
17+
await page.route(
18+
'**/v1/consensus/transactions/10dc8edd24ae89b264a295a046d9ac9b99a59d81acf3b0394bac309c09bdd7c7',
19+
route => {
20+
route.fulfill({
21+
body: JSON.stringify({
22+
is_total_count_clipped: false,
23+
total_count: 1,
24+
transactions: [
25+
{
26+
block: 10759920,
27+
body: {
28+
amount_change: '1000000000',
29+
beneficiary: 'oasis1qqczuf3x6glkgjuf0xgtcpjjw95r3crf7y2323xd',
30+
},
31+
fee: '0',
32+
gas_limit: '1288',
33+
hash: '10dc8edd24ae89b264a295a046d9ac9b99a59d81acf3b0394bac309c09bdd7c7',
34+
index: 0,
35+
method: 'staking.Allow',
36+
nonce: 41,
37+
sender: 'oasis1qrtyn2q78jv6plrmexrsrv4dh89wv4n49udtg2km',
38+
success: true,
39+
timestamp: '2022-07-20T17:20:45Z',
40+
} satisfies Partial<Transaction>,
41+
],
42+
}),
43+
})
44+
},
45+
)
46+
await page.route(
47+
'**/v1/consensus/transactions/4d4903206ee68d5c60ea9b26f4a7b218b263e66e032772f2faa61a2bf7d27b2b',
48+
route => {
49+
route.fulfill({
50+
body: JSON.stringify({
51+
is_total_count_clipped: false,
52+
total_count: 1,
53+
transactions: [
54+
{
55+
block: 20780944,
56+
body: {
57+
amount_change: '5000000000000000',
58+
beneficiary: 'oasis1qrd3mnzhhgst26hsp96uf45yhq6zlax0cuzdgcfc',
59+
negative: true,
60+
},
61+
fee: '0',
62+
gas_limit: '1290',
63+
hash: '4d4903206ee68d5c60ea9b26f4a7b218b263e66e032772f2faa61a2bf7d27b2b',
64+
index: 24,
65+
method: 'staking.Allow',
66+
nonce: 6,
67+
sender: 'oasis1qppz78n4lpnw9knz7d7jz7vvjrt7qd2c6ypsa7me',
68+
success: true,
69+
timestamp: '2024-08-28T02:00:29Z',
70+
} satisfies Partial<Transaction>,
71+
],
72+
}),
73+
})
74+
},
75+
)
76+
}
77+
78+
test('Allowance transaction should display relative amount', async ({ page }) => {
79+
await setup(page)
80+
await page.goto(
81+
'http://localhost:1234/testnet/consensus/tx/10dc8edd24ae89b264a295a046d9ac9b99a59d81acf3b0394bac309c09bdd7c7',
82+
)
83+
await expect(page.getByText('+1 TEST')).toBeVisible()
84+
})
85+
86+
test('Allowance transaction should display negative amount', async ({ page }) => {
87+
await setup(page)
88+
await page.goto(
89+
'http://localhost:1234/mainnet/consensus/tx/4d4903206ee68d5c60ea9b26f4a7b218b263e66e032772f2faa61a2bf7d27b2b',
90+
)
91+
await expect(page.getByText('-5,000,000 ROSE')).toBeVisible()
92+
})

src/app/utils/transaction.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { consensusDecimals } from '../../config'
12
import { ConsensusTxMethod, Transaction } from '../../oasis-nexus/api'
3+
import { fromBaseUnits } from './number-utils'
24

35
export const getConsensusTransactionToAddress = (transaction: Transaction) => {
46
switch (transaction.method) {
@@ -18,12 +20,14 @@ export const getConsensusTransactionToAddress = (transaction: Transaction) => {
1820
export const getConsensusTransactionAmount = (transaction: Transaction) => {
1921
switch (transaction.method) {
2022
case ConsensusTxMethod.stakingAllow:
23+
if (transaction.body?.amount_change === undefined) return undefined
2124
return transaction.body?.negative
22-
? `-${transaction.body?.amount_change}`
23-
: transaction.body?.amount_change
25+
? `-${fromBaseUnits(transaction.body?.amount_change, consensusDecimals)}`
26+
: `+${fromBaseUnits(transaction.body?.amount_change, consensusDecimals)}` // "+" sign is kept in getPreciseNumberFormat
2427
case ConsensusTxMethod.stakingAddEscrow:
2528
case ConsensusTxMethod.stakingTransfer:
26-
return transaction.body?.amount
29+
if (transaction.body?.amount === undefined) return undefined
30+
return fromBaseUnits(transaction.body?.amount, consensusDecimals)
2731
default:
2832
return undefined
2933
}

src/locales/getPreciseNumberFormat.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export function getPreciseNumberFormat(value: string) {
2828
// throws an error, i18n silences it, and displays unformatted original string
2929
maximumFractionDigits:
3030
(isPreciseIntlSupported || isFloatPrecise) && decimalPlaces <= 20 ? 20 : Infinity,
31+
32+
// Display "+" prefix for amount_change to indicate relative change.
33+
// Hopefully negative numbers have "-" prefix in all languages.
34+
signDisplay: value.startsWith('+') ? 'always' : 'auto',
3135
} satisfies Intl.NumberFormatOptions,
3236
},
3337
}

src/oasis-nexus/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export const useGetConsensusTransactions: typeof generated.useGetConsensusTransa
225225
const to = getConsensusTransactionToAddress(tx)
226226
return {
227227
...tx,
228-
amount: amount ? fromBaseUnits(amount, consensusDecimals) : undefined,
228+
amount,
229229
to,
230230
network,
231231
layer: Layer.consensus,
@@ -348,7 +348,7 @@ export const useGetConsensusTransactionsTxHash: typeof generated.useGetConsensus
348348
const to = getConsensusTransactionToAddress(tx)
349349
return {
350350
...tx,
351-
amount: amount ? fromBaseUnits(amount, consensusDecimals) : undefined,
351+
amount,
352352
to,
353353
network,
354354
layer: Layer.consensus,

0 commit comments

Comments
 (0)