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

feat: token-gated support for Publication Metadata v3 and Lens API v2 #514

Merged
merged 10 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 6 additions & 0 deletions .changeset/funny-radios-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@lens-protocol/client": minor
"@lens-protocol/gated-content": patch
---

**feat:** token-gated support for LIP-2 metadata spec
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"packages/prettier-config",
"packages/react-web",
"packages/react",
"packages/sdk-gated",
"packages/shared-kernel",
"packages/storage",
"packages/tsconfig",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
},
"devDependencies": {
"@changesets/cli": "^2.26.0",
"@mxssfd/typedoc-theme": "^1.0.6",
"@mxssfd/typedoc-theme": "^1.1.3",
"@preconstruct/cli": "^2.3.0",
"prettier": "^2.8.4",
"rimraf": "^4.1.2",
"turbo": "^1.8.1",
"typedoc": "^0.23.27",
"typedoc": "^0.25.1",
"typescript": "^4.9.5"
},
"resolutions": {
Expand Down
3 changes: 2 additions & 1 deletion packages/client/codegen-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ config:
field: true

schema:
- https://api-v2-mumbai.lens.dev/graphql
# - https://api-v2-mumbai.lens.dev/graphql
- http://localhost:4000/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we revert this line?

documents:
- src/**/*.graphql
generates:
Expand Down
4 changes: 4 additions & 0 deletions packages/client/gated/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "dist/lens-protocol-client-gated.cjs.js",
"module": "dist/lens-protocol-client-gated.esm.js"
}
27 changes: 21 additions & 6 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
"module": "./dist/lens-protocol-client.esm.js",
"default": "./dist/lens-protocol-client.cjs.js"
},
"./gated": {
"module": "./gated/dist/lens-protocol-client-gated.esm.js",
"default": "./gated/dist/lens-protocol-client-gated.cjs.js"
},
"./package.json": "./package.json"
},
"sideEffects": false,
"files": [
"dist/**/*"
"dist",
"gated"
],
"scripts": {
"build": "preconstruct build",
Expand All @@ -35,9 +40,15 @@
},
"license": "MIT",
"dependencies": {
"@lens-protocol/gated-content": "workspace:*",
"@lens-protocol/shared-kernel": "workspace:*",
"@lens-protocol/storage": "workspace:*",
"dotenv": "^16.3.1",
"@ethersproject/address": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/hash": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@ethersproject/wallet": "^5.7.0",
"graphql": "^16.7.1",
"graphql-request": "^6.1.0",
"graphql-tag": "^2.12.6",
Expand All @@ -49,17 +60,21 @@
"@babel/core": "^7.22.10",
"@babel/preset-env": "^7.22.10",
"@babel/preset-typescript": "^7.22.5",
"@bundlr-network/client": "^0.11.9",
"@faker-js/faker": "7.6.0",
"@graphql-codegen/add": "^5.0.0",
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/near-operation-file-preset": "^2.5.0",
"@graphql-codegen/typescript": "^4.0.1",
"@graphql-codegen/typescript-graphql-request": "^5.0.0",
"@graphql-codegen/typescript-operations": "^4.0.1",
"@lens-protocol/eslint-config": "workspace:*",
"@lens-protocol/metadata": "0.1.0-alpha.29",
"@lens-protocol/prettier-config": "workspace:*",
"@lens-protocol/tsconfig": "workspace:*",
"@types/jest": "^29.5.3",
"@types/node": "^18.15.11",
"dotenv": "^16.3.1",
"eslint": "^8.46.0",
"ethers": "^5.7.2",
"jest": "^29.6.2",
Expand All @@ -79,12 +94,12 @@
},
"preconstruct": {
"entrypoints": [
"index.ts"
"index.ts",
"gated/index.ts"
],
"exports": true
},
"typedoc": {
"entryPoint": "./src/index.ts",
"tsconfig": "./tsconfig.json"
"peerDependencies": {
"@lens-protocol/metadata": "0.1.0-alpha.29"
}
}
83 changes: 63 additions & 20 deletions packages/client/src/LensClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { InMemoryStorageProvider, IStorageProvider } from '@lens-protocol/storage';

import { Authentication, IAuthentication } from './authentication';
import { LensConfig } from './consts/config';
import { LensContext, MediaTransformsConfig } from './context';
import { Environment } from './environments';
import {
Explore,
Feed,
Expand All @@ -17,8 +20,40 @@ import {
Wallet,
} from './submodules';

/**
* LensClient configuration
*/
export type LensClientConfig = {
/**
* The environment to use. See {@link production}, {@link development}, and {@link sandbox}.
*/
environment: Environment;
/**
* The storage provider to use.
*
* @defaultValue {@link InMemoryStorageProvider}
*/
storage?: IStorageProvider;

/**
* Media returned from the publication and profile queries can be transformed
* to sizes needed by the SDK consuming application.
* To overwrite default transformation values, provide a `mediaTransforms` object.
*
* @see {@link MediaTransformsConfig} for more information
*/
mediaTransforms?: MediaTransformsConfig;

/**
* The app ids to use to read data from the Lens Protocol.
* If not provided, all apps will be used.
*/
forApps?: string[];
};

/**
* The LensClient is the main entry point for the LensClient SDK.
*
* It provides access to all the different modules.
*
* @group LensClient
Expand All @@ -33,17 +68,25 @@ import {
* ```
*/
export class LensClient {
private readonly _authentication: Authentication;
private readonly config: LensConfig;
protected readonly context: LensContext;
protected readonly _authentication: Authentication;

/**
* @param config - The configuration for the LensClient
*/
constructor(config: LensConfig) {
this._authentication = new Authentication(config);
this.config = config;
constructor(public readonly config: LensClientConfig) {
this.context = {
environment: config.environment,
storage: config.storage || new InMemoryStorageProvider(),
mediaTransforms: config.mediaTransforms || {},
forApps: config.forApps || [],
};
this._authentication = new Authentication(this.context);
}

/**
* The authentication module
*/
get authentication(): IAuthentication {
return this._authentication;
}
Expand All @@ -52,97 +95,97 @@ export class LensClient {
* The Explore module
*/
get explore(): Explore {
return new Explore(this.config, this._authentication);
return new Explore(this.context, this._authentication);
}

/**
* The Feed module
*/
get feed(): Feed {
return new Feed(this.config, this._authentication);
return new Feed(this.context, this._authentication);
}

/**
* The Invites module
*/
get invites(): Invites {
return new Invites(this.config, this._authentication);
return new Invites(this.context, this._authentication);
}

/**
* The Modules module
*/
get modules(): Modules {
return new Modules(this.config, this._authentication);
return new Modules(this.context, this._authentication);
}

/**
* The Momoka module
*/
get momoka(): Momoka {
return new Momoka(this.config, this._authentication);
return new Momoka(this.context, this._authentication);
}

/**
* The Nfts module
*/
get nfts(): Nfts {
return new Nfts(this.config, this._authentication);
return new Nfts(this.context, this._authentication);
}

/**
* The Notifications module
*/
get notifications(): Notifications {
return new Notifications(this.config, this._authentication);
return new Notifications(this.context, this._authentication);
}

/**
* The Poaps module
*/
get poaps(): Poaps {
return new Poaps(this.config, this._authentication);
return new Poaps(this.context, this._authentication);
}

/**
* The Profile module
*/
get profile(): Profile {
return new Profile(this.config, this._authentication);
return new Profile(this.context, this._authentication);
}

/**
* The Publication module
*/
get publication(): Publication {
return new Publication(this.config, this._authentication);
return new Publication(this.context, this._authentication);
}

/**
* The Revenue module
*/
get revenue(): Revenue {
return new Revenue(this.config, this._authentication);
return new Revenue(this.context, this._authentication);
}

/**
* The Search module
*/
get search(): Search {
return new Search(this.config, this._authentication);
return new Search(this.context, this._authentication);
}

/**
* The Transaction module
*/
get transaction(): Transaction {
return new Transaction(this.config, this._authentication);
return new Transaction(this.context, this._authentication);
}

/**
* The Wallet module
*/
get wallet(): Wallet {
return new Wallet(this.config, this._authentication);
return new Wallet(this.context, this._authentication);
}
}
41 changes: 41 additions & 0 deletions packages/client/src/__helpers__/BundlrUploader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { NodeBundlr } from '@bundlr-network/client';
import { UnknownObject } from '@lens-protocol/shared-kernel';
import { Wallet } from 'ethers';

export class BundlrUploader {
constructor(private readonly signer: Wallet) {}

async upload(data: UnknownObject): Promise<string> {
const bundlr = new NodeBundlr(
'https://devnet.bundlr.network',
'matic',
this.signer.privateKey,
{
providerUrl: 'https://rpc-mumbai.maticvigil.com/',
},
);
const atomicBalance = await bundlr.getLoadedBalance();
const balance = bundlr.utils.fromAtomic(atomicBalance);

// fund bundlr balance if empty
if (balance.eq(0)) {
try {
await bundlr.fund(0.1e18); // 0.1 MUMBAI MATIC
} catch (error) {
// eslint-disable-next-line no-console
console.warn(
`Cannot fund ${String(
bundlr.address,
)} Bundlr balance. Mumbai ${balance.toString()} MATIC available.`,
);
}
}

const serialized = JSON.stringify(data);
const tx = await bundlr.upload(serialized, {
tags: [{ name: 'Content-Type', value: 'application/json' }],
});

return `https://arweave.net/${tx.id}`;
}
}
5 changes: 3 additions & 2 deletions packages/client/src/__helpers__/buildTestEnvironment.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as GatedEnvironments from '@lens-protocol/gated-content/environments';
import { invariant } from '@lens-protocol/shared-kernel';
import * as dotenv from 'dotenv';

import { Environment } from '../consts/environments';
import { Environment } from '../environments';

dotenv.config();

export const buildTestEnvironment = (): Environment => {
invariant(process.env.TESTING_ENV_URL, 'TESTING_ENV_URL is not defined in .env file');

return new Environment('testing', process.env.TESTING_ENV_URL);
return new Environment('testing', process.env.TESTING_ENV_URL, GatedEnvironments.sandbox);
};
Loading
Loading