Skip to content

Commit

Permalink
Verify allowance and balance of mint fee token too
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarenaldi committed May 16, 2024
1 parent e4e9a08 commit d7593d9
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 42 deletions.
2 changes: 1 addition & 1 deletion examples/web/src/publications/UseOpenAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function UseOpenAction() {
metadata: uri,
actions: [
{
type: OpenActionType.SIMPLE_COLLECT,
type: OpenActionType.SHARED_REVENUE_COLLECT,
followerOnly: false,
collectLimit: 5,
},
Expand Down
22 changes: 14 additions & 8 deletions packages/domain/src/use-cases/publications/OpenAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ export enum AllOpenActionType {
UNKNOWN_OPEN_ACTION = 'UNKNOWN_OPEN_ACTION',
}

export enum FeeType {
COLLECT = 'COLLECT',
MINT = 'MINT',
}

export type CollectFee = {
type: FeeType.COLLECT;
amount: Amount<Erc20>;
contractAddress: EvmAddress;
};

export type SharedMintFee = {
export type MintFee = {
type: FeeType.MINT;
amount: Amount<Erc20>;
contractAddress: EvmAddress;
executorClient?: EvmAddress;
};

Expand All @@ -54,7 +62,6 @@ export type MultirecipientCollectRequest = {
publicationId: PublicationId;
referrers?: Referrers;
fee: CollectFee;
collectModule: EvmAddress;
public: boolean;
signless: boolean;
sponsored: boolean;
Expand All @@ -66,7 +73,6 @@ export type SimpleCollectRequest = {
publicationId: PublicationId;
referrers?: Referrers;
fee?: CollectFee;
collectModule: EvmAddress;
public: boolean;
signless: boolean;
sponsored: boolean;
Expand All @@ -77,10 +83,7 @@ export type SharedRevenueCollectRequest = {
type: AllOpenActionType.SHARED_REVENUE_COLLECT;
publicationId: PublicationId;
referrers?: Referrers;
fee?: CollectFee;
mintFee?: SharedMintFee;
executorClient?: EvmAddress;
collectModule: EvmAddress;
fee: CollectFee | MintFee;
public: boolean;
signless: boolean;
sponsored: boolean;
Expand Down Expand Up @@ -127,8 +130,11 @@ export function isUnknownActionRequest(
return request.type === AllOpenActionType.UNKNOWN_OPEN_ACTION;
}

export type PaidCollectRequest = CollectRequest & { fee: CollectFee };
type PaidCollectRequest = CollectRequest & { fee: CollectFee | MintFee };

/**
* @internal
*/
export function isPaidCollectRequest(request: OpenActionRequest): request is PaidCollectRequest {
return isCollectRequest(request) && 'fee' in request && request.fee !== undefined;
}
Expand Down
16 changes: 13 additions & 3 deletions packages/domain/src/use-cases/publications/__helpers__/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import { HidePublicationRequest } from '../HidePublication';
import {
AllOpenActionType,
CollectFee,
FeeType,
LegacyCollectRequest,
MintFee,
MultirecipientCollectRequest,
SharedRevenueCollectRequest,
SimpleCollectRequest,
Expand Down Expand Up @@ -133,6 +135,16 @@ export function mockCollectFee(overrides?: Partial<CollectFee>): CollectFee {
amount: mockDaiAmount(1, ChainType.POLYGON),
contractAddress: mockEvmAddress(),
...overrides,
type: FeeType.COLLECT,
};
}

export function mockMintFee(overrides?: Partial<MintFee>): MintFee {
return {
amount: mockDaiAmount(1, ChainType.POLYGON),
contractAddress: mockEvmAddress(),
...overrides,
type: FeeType.MINT,
};
}

Expand All @@ -155,10 +167,10 @@ export function mockSharedRevenueCollectRequest(
): SharedRevenueCollectRequest {
return {
publicationId: mockPublicationId(),
fee: mockMintFee(),
public: false,
signless: true,
sponsored: true,
collectModule: mockEvmAddress(),
...overrides,
type: AllOpenActionType.SHARED_REVENUE_COLLECT,
kind: TransactionKind.ACT_ON_PUBLICATION,
Expand All @@ -173,7 +185,6 @@ export function mockSimpleCollectRequest(
public: false,
signless: true,
sponsored: true,
collectModule: mockEvmAddress(),
...overrides,
type: AllOpenActionType.SIMPLE_COLLECT,
kind: TransactionKind.ACT_ON_PUBLICATION,
Expand All @@ -189,7 +200,6 @@ export function mockMultirecipientCollectRequest(
public: false,
signless: true,
sponsored: true,
collectModule: mockEvmAddress(),
...overrides,
type: AllOpenActionType.MULTIRECIPIENT_COLLECT,
kind: TransactionKind.ACT_ON_PUBLICATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import {
import {
mockCollectFee,
mockLegacyCollectRequest,
mockMintFee,
mockMultirecipientCollectRequest,
mockSharedRevenueCollectRequest,
mockSimpleCollectRequest,
mockUnknownActionRequest,
} from '../__helpers__/mocks';
Expand Down Expand Up @@ -54,7 +56,7 @@ function setupOpenAction({
}

describe(`Given the ${OpenAction.name} use-case interactor`, () => {
describe.only.each([
describe.each([
{
description: 'LegacyCollectRequest',
request: mockLegacyCollectRequest({ fee: mockCollectFee() }),
Expand All @@ -63,6 +65,14 @@ describe(`Given the ${OpenAction.name} use-case interactor`, () => {
description: 'SimpleCollectRequest',
request: mockSimpleCollectRequest({ fee: mockCollectFee() }),
},
{
description: 'SharedRevenueCollectRequest with mint fee',
request: mockSharedRevenueCollectRequest({ fee: mockMintFee() }),
},
{
description: 'SharedRevenueCollectRequest with collect fee',
request: mockSharedRevenueCollectRequest({ fee: mockCollectFee() }),
},
{
description: 'MultirecipientCollectRequest',
request: mockMultirecipientCollectRequest(),
Expand All @@ -75,7 +85,7 @@ describe(`Given the ${OpenAction.name} use-case interactor`, () => {
description: 'public MultirecipientCollectRequest',
request: mockMultirecipientCollectRequest({ public: true }),
},
])(`when executed with a request that requires a fee`, ({ request, description }) => {
])(`when executed with a request that involves a fee`, ({ request, description }) => {
invariant(isPaidCollectRequest(request), 'Test misconfiguration.');

it(`should check the token availability for ${description}`, async () => {
Expand Down Expand Up @@ -104,7 +114,7 @@ describe(`Given the ${OpenAction.name} use-case interactor`, () => {
});
});

describe.only.each([
describe.each([
{
type: 'LegacyCollectRequest',
request: mockLegacyCollectRequest({ fee: mockCollectFee() }),
Expand All @@ -113,11 +123,15 @@ describe(`Given the ${OpenAction.name} use-case interactor`, () => {
type: 'SimpleCollectRequest',
request: mockSimpleCollectRequest({ fee: mockCollectFee() }),
},
{
type: 'SharedRevenueCollectRequest with mint fee',
request: mockSharedRevenueCollectRequest(),
},
{
type: 'MultirecipientCollectRequest',
request: mockMultirecipientCollectRequest(),
},
])(`when executed with a request that requires a fee`, ({ request, type }) => {
])(`when executed with a request that involves a fee`, ({ request, type }) => {
invariant(isPaidCollectRequest(request), 'Test misconfiguration.');

it(`should support the ${SignedOnChain.name}<${type}> strategy`, async () => {
Expand All @@ -140,12 +154,17 @@ describe(`Given the ${OpenAction.name} use-case interactor`, () => {
});
});

describe.only.each([
describe.each([
{
type: 'SimpleCollectRequest',
request: mockSimpleCollectRequest({ fee: undefined, public: true }),
tokenAvailability: mock<TokenAvailability>(),
},
{
type: 'SharedRevenueCollectRequest',
request: mockSharedRevenueCollectRequest({ public: true }),
tokenAvailability: mockTokeAvailability({ result: success() }),
},
{
type: 'UnknownActionRequest',
request: mockUnknownActionRequest({ public: true }),
Expand All @@ -170,7 +189,7 @@ describe(`Given the ${OpenAction.name} use-case interactor`, () => {
});
});

describe.only.each([
describe.each([
{
type: 'LegacyCollectRequest',
request: mockLegacyCollectRequest({ fee: undefined }),
Expand All @@ -184,7 +203,7 @@ describe(`Given the ${OpenAction.name} use-case interactor`, () => {
request: mockUnknownActionRequest(),
},
])(
`when executed with a request without fee or for which is not possible to determine (e.g. unknown open action)`,
`when executed with a request without fee or for which is not possible to determine if requires a fee (e.g. unknown open action)`,
({ request, type }) => {
it(`should support the ${DelegableSigning.name}<${type}> strategy`, async () => {
const { openAction, signedExecution, delegableExecution, paidExecution } =
Expand All @@ -199,7 +218,7 @@ describe(`Given the ${OpenAction.name} use-case interactor`, () => {
},
);

describe.only.each([
describe.each([
{
type: 'LegacyCollectRequest',
request: mockLegacyCollectRequest({ sponsored: false }),
Expand All @@ -208,6 +227,10 @@ describe(`Given the ${OpenAction.name} use-case interactor`, () => {
type: 'SimpleCollectRequest',
request: mockSimpleCollectRequest({ sponsored: false }),
},
{
type: 'SharedRevenueCollectRequest',
request: mockSharedRevenueCollectRequest({ sponsored: false }),
},
{
type: 'UnknownActionRequest',
request: mockUnknownActionRequest({ sponsored: false }),
Expand Down
13 changes: 4 additions & 9 deletions packages/react/src/transactions/adapters/OpenActionGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { NativeTransaction, Nonce } from '@lens-protocol/domain/entities';
import {
AllOpenActionType,
DelegableOpenActionRequest,
FeeType,
LegacyCollectRequest,
OpenActionRequest,
isUnknownActionRequest,
Expand Down Expand Up @@ -206,7 +207,7 @@ export class OpenActionGateway
for: request.publicationId,
actOn: {
protocolSharedRevenueCollectOpenAction: {
executorClient: request.executorClient ?? null,
executorClient: request.fee.type === FeeType.MINT ? request.fee.executorClient : null,
},
},
referrers: resolveOnchainReferrers(request.referrers),
Expand Down Expand Up @@ -379,6 +380,7 @@ export class OpenActionGateway
private resolvePublicPaidActAmount(request: NewOpenActionRequest): Erc20Amount {
switch (request.type) {
case AllOpenActionType.MULTIRECIPIENT_COLLECT:
case AllOpenActionType.SHARED_REVENUE_COLLECT:
return request.fee.amount;

case AllOpenActionType.UNKNOWN_OPEN_ACTION:
Expand All @@ -387,13 +389,6 @@ export class OpenActionGateway
case AllOpenActionType.SIMPLE_COLLECT:
return request.fee?.amount ?? never();

case AllOpenActionType.SHARED_REVENUE_COLLECT:
return (
request.fee?.amount ??
request.mintFee?.amount ??
never('Invalid UnknownActionRequest, missing fee and mintFee')
);

default:
never();
}
Expand All @@ -417,7 +412,7 @@ export class OpenActionGateway
},
amount.asset.address,
amount.toBigDecimal().toHexadecimal(),
isUnknownActionRequest(request) ? contract.address : request.collectModule,
isUnknownActionRequest(request) ? contract.address : request.fee?.contractAddress ?? never(),
]);
return {
contractAddress: result.typedData.domain.verifyingContract,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
} from '@lens-protocol/api-bindings/mocks';
import { NativeTransaction, UnsignedTransaction } from '@lens-protocol/domain/entities';
import {
mockCollectFee,
mockLegacyCollectRequest,
mockMintFee,
mockMultirecipientCollectRequest,
mockNonce,
mockProfileId,
Expand Down Expand Up @@ -500,8 +500,9 @@ describe(`Given an instance of ${OpenActionGateway.name}`, () => {
publicationId,
referrers,
public: true,
fee: mockCollectFee(),
executorClient: '0xAbAe21DD8737DbdCa26A16D6210D9293986800f9',
fee: mockMintFee({
executorClient: '0xAbAe21DD8737DbdCa26A16D6210D9293986800f9',
}),
}),
expectedRequest: {
for: publicationId,
Expand Down
15 changes: 10 additions & 5 deletions packages/react/src/transactions/adapters/schemas/publications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
RecipientWithSplit,
ReferencePolicyType,
OpenActionRequest,
FeeType,
} from '@lens-protocol/domain/use-cases/publications';
import { UnknownObject } from '@lens-protocol/shared-kernel';
import { z } from 'zod';
Expand Down Expand Up @@ -177,10 +178,18 @@ export const CreateMirrorRequestSchema: z.ZodType<
});

const CollectFeeSchema = z.object({
type: z.literal(FeeType.COLLECT),
amount: Erc20AmountSchema,
contractAddress: EvmAddressSchema,
});

const MintFeeSchema = z.object({
type: z.literal(FeeType.MINT),
amount: Erc20AmountSchema,
contractAddress: EvmAddressSchema,
executorClient: EvmAddressSchema.optional(),
});

const BaseCollectRequestSchema = z.object({
kind: z.literal(TransactionKind.ACT_ON_PUBLICATION),
publicationId: PublicationIdSchema,
Expand All @@ -201,7 +210,6 @@ export const SimpleCollectRequestSchema = BaseCollectRequestSchema.extend({
publicationId: PublicationIdSchema,
referrers: ReferrersSchema.optional(),
fee: CollectFeeSchema.optional(),
collectModule: EvmAddressSchema,
public: z.boolean(),
signless: z.boolean(),
sponsored: z.boolean(),
Expand All @@ -211,20 +219,17 @@ export const SharedRevenueCollectRequestSchema = BaseCollectRequestSchema.extend
type: z.literal(AllOpenActionType.SHARED_REVENUE_COLLECT),
publicationId: PublicationIdSchema,
referrers: ReferrersSchema.optional(),
fee: CollectFeeSchema.optional(),
collectModule: EvmAddressSchema,
fee: z.discriminatedUnion('type', [CollectFeeSchema, MintFeeSchema]),
public: z.boolean(),
signless: z.boolean(),
sponsored: z.boolean(),
executorClient: EvmAddressSchema.optional(),
});

export const MultirecipientCollectRequestSchema = BaseCollectRequestSchema.extend({
type: z.literal(AllOpenActionType.MULTIRECIPIENT_COLLECT),
publicationId: PublicationIdSchema,
referrers: ReferrersSchema.optional(),
fee: CollectFeeSchema,
collectModule: EvmAddressSchema,
public: z.boolean(),
signless: z.boolean(),
sponsored: z.boolean(),
Expand Down
Loading

0 comments on commit d7593d9

Please sign in to comment.