Skip to content

Commit

Permalink
feat: added support for large tip (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
bee344 authored Dec 7, 2023
1 parent 1bd1b38 commit bb9e677
Show file tree
Hide file tree
Showing 22 changed files with 415 additions and 9 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
@@ -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 @@ -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
@@ -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 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'
);
});
});
40 changes: 39 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,17 @@
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';
import { itDecodesBalancesTransferCommon } from '../../test-helpers';
import { DecodedSignedTx } from '../../types';
import { construct } from '..';
Expand All @@ -19,6 +25,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 +57,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);
});
});
10 changes: 9 additions & 1 deletion packages/txwrapper-core/src/core/decode/decodeSignedTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ export function decodeSignedTx(
const methodCall = registry.createType('Call', tx.method);
const method = toTxMethod(registry, methodCall);

let tip: number | string;

try {
tip = tx.tip.toNumber();
} catch (_error) {
tip = tx.tip.toString();
}

return {
address: tx.signer.toString(),
eraPeriod: tx.era.asMortalEra.period.toNumber(),
metadataRpc,
method,
nonce: tx.nonce.toNumber(),
tip: tx.tip.toNumber(),
tip,
};
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import {
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_METHOD_ARGS,
ASTAR_TEST_OPTIONS,
KUSAMA_TEST_OPTIONS,
TEST_BASE_TX_INFO,
TEST_METHOD_ARGS,
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
import { itDecodesBalancesTransferCommon } from '../../test-helpers';
import {
itDecodesBalancesTransferAstar,
itDecodesBalancesTransferCommon,
} from '../../test-helpers';
import { DecodedSigningPayload } from '../../types';
import { construct } from '..';
import { decodeSigningPayload } from './decodeSigningPayload';
Expand All @@ -25,6 +31,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 +78,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 @@ -61,6 +61,12 @@ export function decodeSigningPayload(
? parseInt(payload.inner.assetId.toString())
: undefined;
}
let tip: number | string;
try {
tip = payload.tip.toNumber();
} catch (_error) {
tip = payload.tip.toString();
}

return {
assetId,
Expand All @@ -71,7 +77,7 @@ export function decodeSigningPayload(
method,
nonce: payload.nonce.toNumber(),
specVersion: payload.specVersion.toNumber(),
tip: payload.tip.toNumber(),
tip,
transactionVersion: payload.transactionVersion.toNumber(),
};
}
Loading

0 comments on commit bb9e677

Please sign in to comment.