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

client: add collect from mirror with referrers example #811

Merged
merged 4 commits into from
Jan 31, 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
5 changes: 5 additions & 0 deletions .changeset/perfect-cups-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lens-protocol/client": patch
---

feat: added `isOpenActionModuleWithReferralFee` helper function
20 changes: 10 additions & 10 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
- uses: actions/checkout@v4

- name: Setup pnpm
uses: pnpm/action-setup@v2.4.0
uses: pnpm/action-setup@v2

- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"
cache: "pnpm"
Expand All @@ -34,10 +34,10 @@ jobs:
- uses: actions/checkout@v4

- name: Setup pnpm
uses: pnpm/action-setup@v2.4.0
uses: pnpm/action-setup@v2

- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"
cache: "pnpm"
Expand All @@ -60,10 +60,10 @@ jobs:
- uses: actions/checkout@v4

- name: Setup pnpm
uses: pnpm/action-setup@v2.4.0
uses: pnpm/action-setup@v2

- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"
cache: "pnpm"
Expand All @@ -89,10 +89,10 @@ jobs:
- uses: actions/checkout@v4

- name: Setup pnpm
uses: pnpm/action-setup@v2.4.0
uses: pnpm/action-setup@v2

- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"
cache: "pnpm"
Expand All @@ -110,10 +110,10 @@ jobs:
- uses: actions/checkout@v4

- name: Setup pnpm
uses: pnpm/action-setup@v2.4.0
uses: pnpm/action-setup@v2

- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"
cache: "pnpm"
Expand Down
117 changes: 117 additions & 0 deletions examples/node/scripts/publication/recipes/mirrorReferrer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import {
OpenActionModuleFragment,
PublicationType,
isMirrorPublication,
isOpenActionModuleWithReferralFee,
isRelaySuccess,
} from '@lens-protocol/client';

import { getAuthenticatedClient } from '../../shared/getAuthenticatedClient';
import { setupWallet } from '../../shared/setupWallet';

function mapOpenActionModuleToInputType(openActionModule: OpenActionModuleFragment) {
switch (openActionModule.__typename) {
case 'SimpleCollectOpenActionSettings':
return {
simpleCollectOpenAction: true,
};
case 'MultirecipientFeeCollectOpenActionSettings':
return {
multirecipientCollectOpenAction: true,
};
default:
throw new Error(`Unsupported openActionModule type: ${openActionModule.__typename}`);
}
}

async function main() {
const wallet = setupWallet();
const client = await getAuthenticatedClient(wallet);

// fetch mirrors
const mirrorsResult = await client.publication.fetchAll({
where: {
publicationTypes: [PublicationType.Mirror],
},
});

let mirror = undefined;

// iterate over pages of mirrors to find a mirror of a post with referralFee
while (mirrorsResult.pageInfo.next && !mirror) {
console.log(`Fetching page for cursor: ${mirrorsResult.pageInfo.next}`);

const nextPage = await mirrorsResult.next();

if (nextPage) {
// type safe as Mirror
const mirrors = mirrorsResult.items.filter(isMirrorPublication);

// look for referralFee
mirror = mirrors.find((m) =>
m.mirrorOn.openActionModules.find((o) => 'referralFee' in o && o.referralFee > 0),
);
}
}

if (!mirror) {
throw new Error('No mirror found with referralFee');
}

const openActionWithReferralFee = mirror.mirrorOn.openActionModules.find(
(o) => 'referralFee' in o && o.referralFee > 0,
);

// type safe as OpenActionModuleWithReferralFeeFragment
if (
!openActionWithReferralFee ||
(openActionWithReferralFee && !isOpenActionModuleWithReferralFee(openActionWithReferralFee))
) {
throw new Error('No openAction found with referralFee');
}

console.log(
`We found a mirror with ID ${mirror.id} by profile ${mirror.by.id}
of a publication ID ${mirror.mirrorOn.id} with openActionModule ${openActionWithReferralFee.__typename} and referralFee: ${openActionWithReferralFee.referralFee}`,
);

// map the openActionModule to the correct input type
const actOnInput = mapOpenActionModuleToInputType(openActionWithReferralFee);

// now collect the publication
// notice that paid actions need to be signed and sent through broadcast
const resultTypedData = await client.publication.actions.createActOnTypedData({
actOn: actOnInput,
for: mirror.mirrorOn.id,
referrers: [{ profileId: mirror.by.id }, { publicationId: mirror.id }],
});

const { id, typedData } = resultTypedData.unwrap();

console.log(`Typed data: `, typedData);

// sign with the wallet
const signedTypedData = await wallet._signTypedData(
typedData.domain,
typedData.types,
typedData.value,
);

console.log(`Broadcasting signed typed data...`);

const broadcastResult = await client.transaction.broadcastOnchain({
id,
signature: signedTypedData,
});

const broadcastValue = broadcastResult.unwrap();

if (!isRelaySuccess(broadcastValue)) {
console.log(`Something went wrong`, broadcastValue);
return;
}

console.log(`Transaction was successfully broadcasted with txId`, broadcastValue.txId);
}

main();
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export type NotificationFragment =
| MentionNotificationFragment
| MirrorNotificationFragment
| QuoteNotificationFragment
| ReactionNotificationFragment
| Record<string, never>;
| ReactionNotificationFragment;

/**
* Notifications on many activities for a profile.
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/submodules/publication/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './helpers';
export * from './publicationTypes';
export * from './openActions';
48 changes: 48 additions & 0 deletions packages/client/src/submodules/publication/helpers/openActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
LegacyAaveFeeCollectModuleSettingsFragment,
LegacyErc4626FeeCollectModuleSettingsFragment,
LegacyFeeCollectModuleSettingsFragment,
LegacyFreeCollectModuleSettingsFragment,
LegacyLimitedFeeCollectModuleSettingsFragment,
LegacyLimitedTimedFeeCollectModuleSettingsFragment,
LegacyMultirecipientFeeCollectModuleSettingsFragment,
LegacyRevertCollectModuleSettingsFragment,
LegacySimpleCollectModuleSettingsFragment,
LegacyTimedFeeCollectModuleSettingsFragment,
MultirecipientFeeCollectOpenActionSettingsFragment,
SimpleCollectOpenActionSettingsFragment,
UnknownOpenActionModuleSettingsFragment,
} from '../../../graphql/fragments.generated';

export type OpenActionModuleFragment =
| LegacyAaveFeeCollectModuleSettingsFragment
| LegacyErc4626FeeCollectModuleSettingsFragment
| LegacyFeeCollectModuleSettingsFragment
| LegacyFreeCollectModuleSettingsFragment
| LegacyLimitedFeeCollectModuleSettingsFragment
| LegacyLimitedTimedFeeCollectModuleSettingsFragment
| LegacyMultirecipientFeeCollectModuleSettingsFragment
| LegacyRevertCollectModuleSettingsFragment
| LegacySimpleCollectModuleSettingsFragment
| LegacyTimedFeeCollectModuleSettingsFragment
| MultirecipientFeeCollectOpenActionSettingsFragment
| SimpleCollectOpenActionSettingsFragment
| UnknownOpenActionModuleSettingsFragment;

export type OpenActionModuleWithReferralFeeFragment =
| LegacyAaveFeeCollectModuleSettingsFragment
| LegacyErc4626FeeCollectModuleSettingsFragment
| LegacyFeeCollectModuleSettingsFragment
| LegacyLimitedFeeCollectModuleSettingsFragment
| LegacyLimitedTimedFeeCollectModuleSettingsFragment
| LegacyMultirecipientFeeCollectModuleSettingsFragment
| LegacySimpleCollectModuleSettingsFragment
| LegacyTimedFeeCollectModuleSettingsFragment
| MultirecipientFeeCollectOpenActionSettingsFragment
| SimpleCollectOpenActionSettingsFragment;

export function isOpenActionModuleWithReferralFee(
module: OpenActionModuleFragment,
): module is OpenActionModuleWithReferralFeeFragment {
return 'referralFee' in module;
}
1 change: 0 additions & 1 deletion packages/react/src/wallet/infrastructure/SignerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export class SignerFactory implements ISignerFactory {

const signerAddress = await signer.getAddress();

// TODO fix: this condition is not necessarily true during authentication with a EIP-1271 wallet
if (!isTheSameAddress(address, signerAddress)) {
return failure(new WalletConnectionError(WalletConnectionErrorReason.WRONG_ACCOUNT));
}
Expand Down
Loading