Skip to content

Commit

Permalink
feat: adds e2e onboarding from browser example
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarenaldi committed Dec 16, 2024
1 parent 5790bd7 commit 2a6c3f3
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ node_modules
# lockfiles
package-lock.json
yarn.lock
examples/*/pnpm-lock.yaml

# debug
.pnpm-debug.log*
Expand Down
10 changes: 10 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@
"organizeImports": {
"enabled": false
}
},
{
"include": ["./examples/**/*.ts"],
"linter": {
"rules": {
"style": {
"noNonNullAssertion": "off"
}
}
}
}
]
}
3 changes: 3 additions & 0 deletions examples/viem_eip-1193/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Viem EIP-1193 Example

[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/lens-protocol/lens-sdk/tree/fix/peer-dep/examples/viem_eip-1193)
17 changes: 17 additions & 0 deletions examples/viem_eip-1193/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<h1>Viem EIP-1193 Example</h1>
<div id="app">Loading...</div>
<script type="module">
import out from './index.ts';
document.querySelector('#app').innerHTML = Array.isArray(out)
? out.map((x) => `<div style="margin-bottom: 16px;">${x}</div>`).join('')
: out;
</script>
</body>
</html>
80 changes: 80 additions & 0 deletions examples/viem_eip-1193/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'viem/window';

import { chains } from '@lens-network/sdk/viem';
import { PublicClient, testnet as protocolTestnet } from '@lens-protocol/client';
import { createAccountWithUsername, fetchAccount } from '@lens-protocol/client/actions';
import { handleWith } from '@lens-protocol/client/viem';
import { account } from '@lens-protocol/metadata';
import { StorageClient, testnet as storageTestnet } from '@lens-protocol/storage-node-client';
import { type Address, createWalletClient, custom } from 'viem';

const chain = chains.testnet;

// hoist account
const [address] = (await window.ethereum!.request({ method: 'eth_requestAccounts' })) as [Address];

const walletClient = createWalletClient({
account: address,
chain,
transport: custom(window.ethereum!),
});

// const chainId = await walletClient.getChainId();

// if (chainId !== chain.id) {
// try {
// await walletClient.switchChain({ id: chain.id });
// } catch {
// await walletClient.addChain({ chain });
// }
// }

const client = PublicClient.create({
environment: protocolTestnet,
});

const sessionClient = await client
.login({
onboardingUser: {
wallet: walletClient.account.address,
app: '0xe5439696f4057aF073c0FB2dc6e5e755392922e1',
},
signMessage: async (message) => walletClient.signMessage({ message }),
})
.match(
(result) => result,
(error) => {
throw error;
},
);

const storageClient = StorageClient.create(storageTestnet);

const metadata = account({
name: 'John Doe',
});

const { uri } = await storageClient.uploadFile(
new File([JSON.stringify(metadata)], 'metadata.json', { type: 'application/json' }),
);

const created = await createAccountWithUsername(sessionClient, {
metadataUri: uri,
username: {
localName: `john.doe.${Date.now()}`,
},
})
.andThen(handleWith(walletClient) as any)
.andThen(sessionClient.waitForTransaction as any)
.andThen((txHash) => fetchAccount(sessionClient, { txHash }))
.match(
(result) => result,
(error) => {
throw error;
},
);

export default [
`<h2>${created?.username?.value}</h2>`,
`<p>Address: ${await created?.address}</p>`,
];
19 changes: 19 additions & 0 deletions examples/viem_eip-1193/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "example-viem-eip-1193",
"private": true,
"type": "module",
"scripts": {
"dev": "vite"
},
"dependencies": {
"@lens-network/sdk": "canary",
"@lens-protocol/client": "canary",
"@lens-protocol/metadata": "next",
"@lens-protocol/storage-node-client": "next",
"viem": "^2.21.55"
},
"devDependencies": {
"typescript": "^5.6.3",
"vite": "^5.4.11"
}
}
19 changes: 19 additions & 0 deletions examples/viem_eip-1193/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["ESNext", "DOM"],
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"noEmit": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"skipLibCheck": true
},
"include": ["./"]
}
72 changes: 65 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a6c3f3

Please sign in to comment.