Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: declarative actions on meta pn #1427

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ export default class MetaPaymentNetwork<
});
});

const genericCreationAction = super.applyCreation(extensionAction, timestamp);

return {
...super.applyCreation(extensionAction, timestamp),
...genericCreationAction,
events: [
{
name: 'create',
Expand All @@ -124,7 +126,10 @@ export default class MetaPaymentNetwork<
timestamp,
},
],
values,
values: {
...genericCreationAction.values,
...values,
},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,32 @@ describe('extensions/payment-network/meta', () => {
});
});
});

describe('declarative tests', () => {
describe('applyActionToExtension/declareSentPayment', () => {
it('can applyActionToExtensions of declareSentPayment', () => {
expect(
metaPn.applyActionToExtension(
MetaCreate.requestFullStateCreated.extensions,
MetaCreate.actionDeclareSentPayment,
MetaCreate.requestFullStateCreated,
TestData.payerRaw.identity,
TestData.arbitraryTimestamp,
),
).toEqual(MetaCreate.extensionStateWithDeclaredSent);
});

it('cannot applyActionToExtensions of declareSentPayment without a previous state', () => {
expect(() => {
metaPn.applyActionToExtension(
MetaCreate.requestStateNoExtensions.extensions,
MetaCreate.actionDeclareSentPayment,
MetaCreate.requestStateNoExtensions,
TestData.payerRaw.identity,
TestData.arbitraryTimestamp,
);
}).toThrowError(`The extension should be created before receiving any other action`);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export const feeAmount = '2000000000000000000';
export const invalidAddress = '0x not and address';
export const tokenAddress = '0x6b175474e89094c44da98b954eedeac495271d0f';
export const network = 'mainnet';
export const amount = '12345';
export const note = '123456789';
export const txHash = '0x123456789';
export const saltMain = 'ea3bc7caf64110ca';
export const salt1 = 'ea3bc7caf64110cb';
export const salt2 = 'ea3bc7caf64110cc';
Expand Down Expand Up @@ -71,6 +74,17 @@ export const actionApplyActionToPn = {
},
};

export const actionDeclareSentPayment = {
action: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_SENT_PAYMENT,
id: ExtensionTypes.PAYMENT_NETWORK_ID.META,
parameters: {
amount,
note,
txHash,
network,
},
};

// ---------------------------------------------------------------------
// extensions states
export const extensionFullStateMultipleAnyToErc20 = {
Expand Down Expand Up @@ -98,6 +112,10 @@ export const extensionFullStateMultipleAnyToErc20 = {
AnyToErc20Create.extensionFullState(salt2)[
ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY
],
receivedPaymentAmount: '0',
receivedRefundAmount: '0',
sentPaymentAmount: '0',
sentRefundAmount: '0',
},
version: '0.1.0',
},
Expand Down Expand Up @@ -142,6 +160,10 @@ export const extensionStateCreatedMissingAddress = {
[salt2]: AnyToErc20Create.extensionFullState(salt2, null)[
ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY
],
receivedPaymentAmount: '0',
receivedRefundAmount: '0',
sentPaymentAmount: '0',
sentRefundAmount: '0',
},
version: '0.1.0',
},
Expand Down Expand Up @@ -206,6 +228,55 @@ export const extensionStateWithApplyAddPaymentAddressAfterCreation = {
paymentAddress,
},
},
receivedPaymentAmount: '0',
receivedRefundAmount: '0',
sentPaymentAmount: '0',
sentRefundAmount: '0',
},
version: '0.1.0',
},
};

export const extensionStateWithDeclaredSent: RequestLogicTypes.IExtensionStates = {
[ExtensionTypes.PAYMENT_NETWORK_ID.META as string]: {
events: [
{
name: 'create',
parameters: {
[ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY]: [
baseParams(salt1),
baseParams(salt2),
],
},
timestamp: arbitraryTimestamp,
},
{
name: ExtensionTypes.PnAnyDeclarative.ACTION.DECLARE_SENT_PAYMENT,
parameters: {
amount,
note,
txHash,
network,
},
timestamp: arbitraryTimestamp,
from: TestData.payerRaw.identity,
},
],
id: ExtensionTypes.PAYMENT_NETWORK_ID.META,
type: ExtensionTypes.TYPE.PAYMENT_NETWORK,
values: {
[salt1]:
AnyToErc20Create.extensionFullState(salt1)[
ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY
],
[salt2]:
AnyToErc20Create.extensionFullState(salt2)[
ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY
],
receivedPaymentAmount: '0',
receivedRefundAmount: '0',
sentRefundAmount: '0',
sentPaymentAmount: amount,
},
version: '0.1.0',
},
Expand Down
6 changes: 5 additions & 1 deletion packages/payment-detection/src/meta-payment-detector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ export class MetaDetector extends DeclarativePaymentDetectorBase<
for (const value of Object.values(
paymentExtension.values as Record<string, ExtensionTypes.IState<any>>,
)) {
if (supportedPns.includes(value.id as keyof ExtensionTypes.PnMeta.ICreationParameters)) {
if (
!!value &&
value.type === ExtensionTypes.TYPE.PAYMENT_NETWORK &&
supportedPns.includes(value.id as keyof ExtensionTypes.PnMeta.ICreationParameters)
) {
const detectorClass = detectorMap[value.id as keyof typeof detectorMap];
const extensionKey = advancedLogicMap[value.id as keyof typeof advancedLogicMap];
const extension =
Expand Down
8 changes: 8 additions & 0 deletions packages/payment-detection/test/meta-payment-network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ const requestMock: RequestLogicTypes.IRequest = {
},
},
salt: 'main-salt',
payeeDelegate: undefined,
payerDelegate: undefined,
paymentInfo: undefined,
refundInfo: undefined,
receivedPaymentAmount: '0',
receivedRefundAmount: '0',
sentPaymentAmount: '0',
sentRefundAmount: '0',
},
version: '0',
},
Expand Down