Skip to content

Commit

Permalink
fix: added support for 18 decimals tip
Browse files Browse the repository at this point in the history
  • Loading branch information
bee344 committed Dec 7, 2023
1 parent 1bd1b38 commit 5bb7925
Show file tree
Hide file tree
Showing 22 changed files with 390 additions and 13 deletions.
18 changes: 18 additions & 0 deletions packages/txwrapper-core/src/core/construct/createSignedTx.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_METHOD_ARGS,
ASTAR_TEST_OPTIONS,
KUSAMA_TEST_OPTIONS,
signWithAlice,
TEST_BASE_TX_INFO,
Expand All @@ -24,4 +27,19 @@ describe('createSignedTx', () => {
'0x2d028400d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d005b4cf1d210c93eb6e6428a88c1b298e280d761fe50fe02d6c3253075ed23f239bcbc9a4f9bf0494a869f797355daf55ed6de373572328a1f6f4519f48f8f280feb58080004070096074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d30'
);
});

it('should work for Astar', async () => {
const unsigned = balancesTransfer(
ASTAR_TEST_METHOD_ARGS.balances.transfer,
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_OPTIONS
);
const signingPayload = createSigningPayload(unsigned, ASTAR_TEST_OPTIONS);
const signature = await signWithAlice(signingPayload);

const tx = createSignedTx(unsigned, signature, ASTAR_TEST_OPTIONS);
expect(tx).toBe(
'0x4902840046ef637afac0ffa9ec7a7cf76c5e0148200a401e2a1efcf2c16ee1554b74a22000fff4ac96df07f429c5be356f536a794b4892ec2d5fb516e04f559b0f2548be2ce48fc01501353d244aff9b660fd086c9d1681ba47fade8194c2a4c62d6c5b50adb2a080f0000c52ebca2b11f070046ef637afac0ffa9ec7a7cf76c5e0148200a401e2a1efcf2c16ee1554b74a22030'
);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_METHOD_ARGS,
ASTAR_TEST_OPTIONS,
KUSAMA_TEST_OPTIONS,
TEST_BASE_TX_INFO,
TEST_METHOD_ARGS,
Expand All @@ -22,4 +25,19 @@ describe('createSigningPayload', () => {
'0x9004070096074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d30eb580800d624000006000000e3777fa922cafbff200cadeaea1a76bd7898ad5b89f7848999058b50e715f6361fc7493f3c1e9ac758a183839906475f8363aafb1b1d3e910fe16fab4ae1b582'
);
});

it('should work for Astar', () => {
const signingPayload = createSigningPayload(
balancesTransfer(
ASTAR_TEST_METHOD_ARGS.balances.transfer,
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_OPTIONS
),
ASTAR_TEST_OPTIONS
);

expect(signingPayload).toEqual(
'0x901f070046ef637afac0ffa9ec7a7cf76c5e0148200a401e2a1efcf2c16ee1554b74a22030db2a080f0000c52ebca2b148000000020000009eb76c5184c4ab8679d2d5d819fdf90b9c001403e9e17da2e14b6d8aec4029c66c8453305b978e867b2ccacea94e01a4ea49dfd6eb0965b6c0ccf36b295e16b5'
);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { u8aToHex } from '@polkadot/util';
import {
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_METHOD_ARGS,
ASTAR_TEST_OPTIONS,
KUSAMA_TEST_OPTIONS,
TEST_BASE_TX_INFO,
TEST_METHOD_ARGS,
Expand All @@ -23,4 +26,19 @@ describe('createSigningPayloadToU8a', () => {
'0x04070096074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d30eb580800d624000006000000e3777fa922cafbff200cadeaea1a76bd7898ad5b89f7848999058b50e715f6361fc7493f3c1e9ac758a183839906475f8363aafb1b1d3e910fe16fab4ae1b582'
);
});

it('should workfor Astar', () => {
const signingPayload = createSigningPayloadToU8a(
balancesTransfer(
ASTAR_TEST_METHOD_ARGS.balances.transfer,
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_OPTIONS
),
ASTAR_TEST_OPTIONS
);

expect(u8aToHex(signingPayload)).toEqual(
'0x1f070046ef637afac0ffa9ec7a7cf76c5e0148200a401e2a1efcf2c16ee1554b74a22030db2a080f0000c52ebca2b148000000020000009eb76c5184c4ab8679d2d5d819fdf90b9c001403e9e17da2e14b6d8aec4029c66c8453305b978e867b2ccacea94e01a4ea49dfd6eb0965b6c0ccf36b295e16b5'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {
KUSAMA_TEST_OPTIONS,
TEST_BASE_TX_INFO,
TEST_METHOD_ARGS,
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_METHOD_ARGS,
ASTAR_TEST_OPTIONS

Check failure on line 7 in packages/txwrapper-core/src/core/construct/encodeUnsignedTx.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
Expand All @@ -21,4 +24,18 @@ describe('encodeUnsignedTransaction', () => {
'0x940404070096074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d30'
);
});

it('should return the correct output for Astar', () => {
const unsigned = balancesTransfer(
ASTAR_TEST_METHOD_ARGS.balances.transfer,
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_OPTIONS
);

const encoded = encodeUnsignedTransaction(unsigned, ASTAR_TEST_OPTIONS);

expect(encoded).toBe(
'0x94041f070046ef637afac0ffa9ec7a7cf76c5e0148200a401e2a1efcf2c16ee1554b74a22030'
);
});
});
26 changes: 26 additions & 0 deletions packages/txwrapper-core/src/core/construct/getTxHash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
signWithAlice,
TEST_BASE_TX_INFO,
TEST_METHOD_ARGS,
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_METHOD_ARGS,
ASTAR_TEST_OPTIONS

Check failure on line 8 in packages/txwrapper-core/src/core/construct/getTxHash.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
Expand Down Expand Up @@ -32,4 +35,27 @@ describe('getTxHash', () => {
'0x50cbe175485736a5284a5758e29608d4e3f95c329ca42b108f7445f9f2ed06bb'
);
});

it('should work for Astar', async () => {
const unsigned = balancesTransfer(
ASTAR_TEST_METHOD_ARGS.balances.transfer,
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_OPTIONS
);
const signingPayload = construct.signingPayload(
unsigned,
ASTAR_TEST_OPTIONS
);
const signature = await signWithAlice(signingPayload);
const signedTx = construct.signedTx(
unsigned,
signature,
ASTAR_TEST_OPTIONS
);

const txHash = getTxHash(signedTx);
expect(txHash).toBe(
'0x1fc108525d6569889d53cbc092cba9c7ad0f59d668c60eff3447c774815d0485'
);
});
});
37 changes: 36 additions & 1 deletion packages/txwrapper-core/src/core/decode/decodeSignedTx.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_METHOD_ARGS,
ASTAR_TEST_OPTIONS,
KUSAMA_TEST_OPTIONS,
signWithAlice,
TEST_BASE_TX_INFO,
TEST_METHOD_ARGS,
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
import { balancesTransfer, itDecodesBalancesTransferAstar } from '../../test-helpers';

Check failure on line 11 in packages/txwrapper-core/src/core/decode/decodeSignedTx.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·balancesTransfer,·itDecodesBalancesTransferAstar·` with `⏎↹balancesTransfer,⏎↹itDecodesBalancesTransferAstar,⏎`
import { itDecodesBalancesTransferCommon } from '../../test-helpers';
import { DecodedSignedTx } from '../../types';
import { construct } from '..';
Expand All @@ -19,6 +22,14 @@ export function itDecodesSignedBalancesTransferTx(
);
}

export function itDecodesSignedBalancesTransferTxAstar(
decoded: DecodedSignedTx
): void {
(['address', 'metadataRpc', 'nonce', 'tip'] as const).forEach((key) =>
expect(decoded[key]).toBe(ASTAR_TEST_BASE_TX_INFO[key])
);
}

describe('decodeSignedTx', () => {
it('should decode balances::transfer', async () => {
const unsigned = balancesTransfer(
Expand All @@ -43,4 +54,28 @@ describe('decodeSignedTx', () => {

itDecodesBalancesTransferCommon(decoded);
});

it('should decode balances::transfer for Astar', async () => {
const unsigned = balancesTransfer(
ASTAR_TEST_METHOD_ARGS.balances.transfer,
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_OPTIONS
);
const signingPayload = construct.signingPayload(
unsigned,
ASTAR_TEST_OPTIONS
);
const signature = await signWithAlice(signingPayload);
const signedTx = construct.signedTx(
unsigned,
signature,
ASTAR_TEST_OPTIONS
);

const decoded = decodeSignedTx(signedTx, ASTAR_TEST_OPTIONS);

itDecodesSignedBalancesTransferTxAstar(decoded);

itDecodesBalancesTransferAstar(decoded);
});
});
2 changes: 1 addition & 1 deletion packages/txwrapper-core/src/core/decode/decodeSignedTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ export function decodeSignedTx(
metadataRpc,
method,
nonce: tx.nonce.toNumber(),
tip: tx.tip.toNumber(),
tip: tx.tip.toString(),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import {
KUSAMA_TEST_OPTIONS,
TEST_BASE_TX_INFO,
TEST_METHOD_ARGS,
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_METHOD_ARGS,
ASTAR_TEST_OPTIONS,
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
import { itDecodesBalancesTransferCommon } from '../../test-helpers';
import { itDecodesBalancesTransferCommon, itDecodesBalancesTransferAstar } from '../../test-helpers';

Check failure on line 11 in packages/txwrapper-core/src/core/decode/decodeSigningPayload.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·itDecodesBalancesTransferCommon,·itDecodesBalancesTransferAstar·` with `⏎↹itDecodesBalancesTransferCommon,⏎↹itDecodesBalancesTransferAstar,⏎`
import { DecodedSigningPayload } from '../../types';
import { construct } from '..';
import { decodeSigningPayload } from './decodeSigningPayload';
Expand All @@ -25,6 +28,21 @@ export function itDecodesSigningPayloadBalancesTransfer(
).forEach((key) => expect(decoded[key]).toBe(TEST_BASE_TX_INFO[key]));
}

export function itDecodesSigningPayloadBalancesTransferAstar(
decoded: DecodedSigningPayload
): void {
(
[
'blockHash',
'genesisHash',
'metadataRpc',
'nonce',
'specVersion',
'tip',
] as const
).forEach((key) => expect(decoded[key]).toBe(ASTAR_TEST_BASE_TX_INFO[key]));
}

describe('decodeSigningPayload', () => {
it('should decode balances::transfer', () => {
const signingPayload = construct.signingPayload(
Expand Down Expand Up @@ -57,4 +75,36 @@ describe('decodeSigningPayload', () => {

expect(decoded.eraPeriod).toBe(0);
});

it('should decode balances::transfer for Astar', () => {
const signingPayload = construct.signingPayload(
balancesTransfer(
ASTAR_TEST_METHOD_ARGS.balances.transfer,
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_OPTIONS
),
ASTAR_TEST_OPTIONS
);

const decoded = decodeSigningPayload(signingPayload, ASTAR_TEST_OPTIONS);

itDecodesSigningPayloadBalancesTransferAstar(decoded);

itDecodesBalancesTransferAstar(decoded);
});

it('should decode balances::transfer with an immortal era for Astar', () => {
const signingPayload = construct.signingPayload(
balancesTransfer(
ASTAR_TEST_METHOD_ARGS.balances.transfer,
ASTAR_TEST_BASE_TX_INFO,
Object.assign({}, ASTAR_TEST_OPTIONS, { isImmortalEra: true })
),
ASTAR_TEST_OPTIONS
);

const decoded = decodeSigningPayload(signingPayload, ASTAR_TEST_OPTIONS);

expect(decoded.eraPeriod).toBe(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function decodeSigningPayload(
method,
nonce: payload.nonce.toNumber(),
specVersion: payload.specVersion.toNumber(),
tip: payload.tip.toNumber(),
tip: payload.tip.toString(),
transactionVersion: payload.transactionVersion.toNumber(),
};
}
55 changes: 53 additions & 2 deletions packages/txwrapper-core/src/core/decode/decodeUnsignedTx.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {

Check failure on line 1 in packages/txwrapper-core/src/core/decode/decodeUnsignedTx.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Run autofix to sort these imports!
ASTAR_TEST_OPTIONS,
KUSAMA_TEST_OPTIONS,
TEST_BASE_TX_INFO,
TEST_METHOD_ARGS,
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_METHOD_ARGS

Check failure on line 7 in packages/txwrapper-core/src/core/decode/decodeUnsignedTx.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
import { itDecodesBalancesTransferCommon } from '../../test-helpers';
import { itDecodesBalancesTransferCommon, itDecodesBalancesTransferAstar } from '../../test-helpers';

Check failure on line 11 in packages/txwrapper-core/src/core/decode/decodeUnsignedTx.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·itDecodesBalancesTransferCommon,·itDecodesBalancesTransferAstar·` with `⏎↹itDecodesBalancesTransferCommon,⏎↹itDecodesBalancesTransferAstar,⏎`
import { DecodedUnsignedTx } from '../../types';
import { decodeUnsignedTx } from './decodeUnsignedTx';

Expand All @@ -27,8 +30,26 @@ export function itDecodesUnsignedBalanceTransferTx(
).forEach((key) => expect(decoded[key]).toBe(TEST_BASE_TX_INFO[key]));
}

export function itDecodesAstarUnsignedBalanceTransferTx(
decoded: DecodedUnsignedTx
): void {
(
[
'address',
'blockHash',
'blockNumber',
'genesisHash',
'metadataRpc',
'nonce',
'specVersion',
'transactionVersion',
'tip',
] as const
).forEach((key) => expect(decoded[key]).toBe(ASTAR_TEST_BASE_TX_INFO[key]));
}

describe('decodeUnsignedTx', () => {
it('should decode balances::transfer', () => {
it('should decode balances::transfer for Kusama', () => {
const unsigned = balancesTransfer(
TEST_METHOD_ARGS.balances.transfer,
TEST_BASE_TX_INFO,
Expand All @@ -42,6 +63,20 @@ describe('decodeUnsignedTx', () => {
itDecodesBalancesTransferCommon(decoded);
});

it('should decode balances::transfer for Astar', () => {
const unsigned = balancesTransfer(
ASTAR_TEST_METHOD_ARGS.balances.transfer,
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_OPTIONS
);

const decoded = decodeUnsignedTx(unsigned, ASTAR_TEST_OPTIONS);

itDecodesAstarUnsignedBalanceTransferTx(decoded);

itDecodesBalancesTransferAstar(decoded);
});

it('Should decode balances::transfer for an immortal era', () => {
const adjustedOptions = {
...KUSAMA_TEST_OPTIONS,
Expand All @@ -57,4 +92,20 @@ describe('decodeUnsignedTx', () => {

expect(decoded.eraPeriod).toBe(0);
});

it('Should decode balances::transfer for an immortal era fro Astar', () => {
const adjustedOptions = {
...ASTAR_TEST_OPTIONS,
isImmortalEra: true,
};
const unsigned = balancesTransfer(
ASTAR_TEST_METHOD_ARGS.balances.transfer,
ASTAR_TEST_BASE_TX_INFO,
adjustedOptions
);

const decoded = decodeUnsignedTx(unsigned, adjustedOptions);

expect(decoded.eraPeriod).toBe(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function decodeUnsignedTx(
method,
nonce: registry.createType('Compact<Index>', unsigned.nonce).toNumber(),
specVersion: registry.createType('u32', unsigned.specVersion).toNumber(),
tip: registry.createType('Compact<Balance>', unsigned.tip).toNumber(),
tip: registry.createType('Compact<Balance>', unsigned.tip).toString(),
transactionVersion: registry
.createType('u32', unsigned.transactionVersion)
.toNumber(),
Expand Down
Loading

0 comments on commit 5bb7925

Please sign in to comment.