Skip to content

Commit

Permalink
feat(tx-builder)!: unpack poi field of channel CloseSolo Slash tx
Browse files Browse the repository at this point in the history
BREAKING CHANGE: TxBuilder accepts and returns `poi` field unpacked as TreesPoi

BREAKING CHANGE: `Channel.poi` returns unpacked TreesPoi
Use just `await channel.poi(...)` instead of `unpackTx(await channel.poi(...))`.
  • Loading branch information
davidyuk committed Dec 29, 2022
1 parent 5898b3a commit 4a49b03
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/channel/Spend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { ChannelConnectionError } from '../utils/errors';
import {
awaitingCompletion, channelOpen, handleUnexpectedMessage, signAndNotify,
} from './handlers';
import { unpackTx } from '../tx/builder';
import { Tag } from '../tx/builder/constants';

export default class ChannelSpend extends Channel {
/**
Expand Down Expand Up @@ -133,8 +135,11 @@ export default class ChannelSpend extends Channel {
accounts: Encoded.AccountAddress[];
contracts?: Encoded.ContractAddress[];
},
): Promise<Encoded.Poi> {
return (await call(this, 'channels.get.poi', { accounts, contracts })).poi;
): Promise<ReturnType<typeof unpackTx<Tag.TreesPoi>>> {
return unpackTx(
(await call(this, 'channels.get.poi', { accounts, contracts })).poi,
Tag.TreesPoi,
);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/tx/builder/field-types/entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { decode, encode, Encoding } from '../../../utils/encoder';
import { Tag } from '../constants';
import type { unpackTx as unpackTxType, buildTx as buildTxType } from '../index';

export default function genEntryField<T extends Tag>(tag: T): {
serialize: (
value: Parameters<typeof buildTxType<T>>[0], options: { buildTx: typeof buildTxType },
) => Buffer;
deserialize: (
value: Buffer, options: { unpackTx: typeof unpackTxType },
) => ReturnType<typeof unpackTxType<T>>;
} {
return {
serialize(txParams, { buildTx }) {
return decode(buildTx({ ...txParams, tag }));
},

deserialize(buf, { unpackTx }) {
return unpackTx(encode(buf, Encoding.Transaction), tag);
},
};
}
3 changes: 3 additions & 0 deletions src/tx/builder/field-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import _coinAmount from './coin-amount';
import _address from './address';
import _addresses from './addresses';
import _deposit from './deposit';
import _entry from './entry';
import _enumeration from './enumeration';
import _fee from './fee';
import _gasLimit from './gas-limit';
Expand All @@ -23,6 +24,7 @@ const coinAmount = _coinAmount;
const address = _address;
const addresses = _addresses;
const deposit = _deposit;
const entry = _entry;
const enumeration = _enumeration;
const fee = _fee;
const gasLimit = _gasLimit;
Expand All @@ -47,6 +49,7 @@ export {
address,
addresses,
deposit,
entry,
enumeration,
fee,
gasLimit,
Expand Down
2 changes: 1 addition & 1 deletion src/tx/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function serializeField(value: any, type: FIELD_TYPES | Field, params: any): any
default:
if (typeof type === 'number') return value;
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return type.serialize(value, { ...params, unpackTx });
return type.serialize(value, { ...params, unpackTx, buildTx });
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/tx/builder/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import BigNumber from 'bignumber.js';
import { Tag } from './constants';
import {
Field, uInt, shortUInt, coinAmount, name, nameId, nameFee, deposit, gasLimit, gasPrice, fee,
address, addresses, pointers, enumeration, mptrees,
address, addresses, pointers, entry, enumeration, mptrees,
} from './field-types';
import { Encoded, Encoding } from '../../utils/encoder';
import { UnionToIntersection } from '../../utils/other';
Expand Down Expand Up @@ -444,7 +444,7 @@ export const TX_SCHEMA = {
['channelId', address(Encoding.Channel)],
['fromId', address(Encoding.AccountAddress)],
['payload', FIELD_TYPES.binary, Encoding.Transaction],
['poi', FIELD_TYPES.binary, Encoding.Poi],
['poi', entry(Tag.TreesPoi)],
['ttl', shortUInt],
['fee', fee],
['nonce', shortUInt],
Expand All @@ -456,7 +456,7 @@ export const TX_SCHEMA = {
['channelId', address(Encoding.Channel)],
['fromId', address(Encoding.AccountAddress)],
['payload', FIELD_TYPES.binary, Encoding.Transaction],
['poi', FIELD_TYPES.binary, Encoding.Poi],
['poi', entry(Tag.TreesPoi)],
['ttl', shortUInt],
['fee', fee],
['nonce', shortUInt],
Expand Down
15 changes: 10 additions & 5 deletions test/integration/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,10 @@ describe('Channel', () => {
const responderAddr = aeSdkResponder.address;
const params = { accounts: [initiatorAddr, responderAddr] };
const initiatorPoi = await initiatorCh.poi(params);
expect(initiatorPoi).to.be.equal(await responderCh.poi(params));
initiatorPoi.should.be.a('string');
const responderPoi = await responderCh.poi(params);
expect(initiatorPoi).to.be.eql(responderPoi);
expect(initiatorPoi.tx.accounts[0].isEqual(responderPoi.tx.accounts[0]))
.to.be.equal(true);
});

it('can send a message', async () => {
Expand Down Expand Up @@ -722,7 +724,8 @@ describe('Channel', () => {
const closeSoloTx = await aeSdkInitiatior.buildTx(Tag.ChannelCloseSoloTx, {
channelId: await initiatorCh.id(),
fromId: initiatorAddr,
poi,
// @ts-expect-error remove after fixing tx-builder types
poi: poi.tx,
payload: signedTx,
});
const closeSoloTxFee = unpackTx(closeSoloTx, Tag.ChannelCloseSoloTx).tx.fee;
Expand Down Expand Up @@ -784,7 +787,8 @@ describe('Channel', () => {
const closeSoloTx = await aeSdkInitiatior.buildTx(Tag.ChannelCloseSoloTx, {
channelId: initiatorCh.id(),
fromId: initiatorAddr,
poi: oldPoi,
// @ts-expect-error remove after fixing tx-builder types
poi: oldPoi.tx,
payload: oldUpdate.signedTx,
});
const closeSoloTxFee = unpackTx(closeSoloTx, Tag.ChannelCloseSoloTx).tx.fee;
Expand All @@ -793,7 +797,8 @@ describe('Channel', () => {
const slashTx = await aeSdkResponder.buildTx(Tag.ChannelSlashTx, {
channelId: responderCh.id(),
fromId: responderAddr,
poi: recentPoi,
// @ts-expect-error remove after fixing tx-builder types
poi: recentPoi.tx,
payload: recentUpdate.signedTx,
});
const slashTxFee = unpackTx(slashTx, Tag.ChannelSlashTx).tx.fee;
Expand Down

0 comments on commit 4a49b03

Please sign in to comment.