Skip to content

Commit

Permalink
feat: Display Linea ENS domain
Browse files Browse the repository at this point in the history
  • Loading branch information
alainncls committed Jun 28, 2024
1 parent 92a89f1 commit 1881cf0
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 22 deletions.
63 changes: 51 additions & 12 deletions packages/functions/global-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,25 @@ export async function handler(event: {
};
}

const [activations, pohStatus, openBlockScore, lxpBalance, lxpLBalance] =
await Promise.all([
getActivations(),
fetchPohStatus(address),
getOpenBlockScore(address.toLowerCase()),
isLineascan
? fetchBalanceFromLineascan(LXP_CONTRACT_ADDRESS, address)
: Promise.resolve('0'),
isLineascan
? fetchBalanceFromLineascan(LXP_L_CONTRACT_ADDRESS, address)
: Promise.resolve('0'),
]);
const [
activations,
pohStatus,
openBlockScore,
lxpBalance,
lxpLBalance,
name,
] = await Promise.all([
getActivations(),
fetchPohStatus(address),
getOpenBlockScore(address.toLowerCase()),
isLineascan
? fetchBalanceFromLineascan(LXP_CONTRACT_ADDRESS, address)
: Promise.resolve('0'),
isLineascan
? fetchBalanceFromLineascan(LXP_L_CONTRACT_ADDRESS, address)
: Promise.resolve('0'),
isLineascan ? fetchLineaEns(address.toLowerCase()) : undefined,
]);

return {
statusCode: 200,
Expand All @@ -77,6 +84,7 @@ export async function handler(event: {
openBlockScore,
lxpBalance,
lxpLBalance,
name,
}),
};
}
Expand Down Expand Up @@ -110,6 +118,23 @@ async function getData(
return response.json();
}

async function postData(url: string, data: Record<string, string>) {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});

if (!response.ok) {
console.error(`Call to ${url} failed with status ${response.status}`);
throw new Error(`HTTP error! Status: ${response.status}`);
}

return response.json();
}

/**
* Get current active activations from Contentful.
* @returns The activations object.
Expand Down Expand Up @@ -194,3 +219,17 @@ async function fetchPohStatus(address: string) {
return false;
}
}

async function fetchLineaEns(address: string) {
try {
const res = await postData(
`https://api.studio.thegraph.com/query/69290/ens-linea-sepolia/v0.0.4`,
{
query: `query getNamesForAddress {domains(first: 1, where: {and: [{or: [{owner: \"${address}\"}, {registrant: \"${address}\"}, {wrappedOwner: \"${address}\"}]}, {parent_not: \"0x91d1777781884d03a6757a803996e38de2a42967fb37eeaca72729271025a9e2\"}, {or: [{expiryDate_gt: \"1718459214\"}, {expiryDate: null}]}, {or: [{owner_not: \"0x0000000000000000000000000000000000000000\"}, {resolver_not: null}, {and: [{registrant_not: \"0x0000000000000000000000000000000000000000\"}, {registrant_not: null}]}]}]}) {...DomainDetailsWithoutParent}} fragment DomainDetailsWithoutParent on Domain {name}`,
},
);
return res.data.domains[0].name as string;
} catch (e) {
return undefined;
}
}
4 changes: 2 additions & 2 deletions packages/site/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ <h1>
</h1>
<h2>Follow your Linea Voyage</h2>
<p>
View your LXP and LXP-L balances, POH status, and current activations
inside of MetaMask!
View your LXP and LXP-L balances, POH status, Linea ENS domain and
current activations inside of MetaMask!
</p>
<p id="loading">Loading...</p>
<div id="context"></div>
Expand Down
2 changes: 1 addition & 1 deletion packages/site/src/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const snapId = 'npm:@consensys/linea-voyager';
const snapVersion = '^0.8.0';
const snapVersion = '^0.9.0';
let isAccountConnected = false;

const isLatestVersion = (installedVersion) => {
Expand Down
1 change: 1 addition & 0 deletions packages/snap/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"none": "There are no current activations",
"one": "There is {count} active LXP activation"
},
"lineaEns": "Linea ENS domain",
"lxpAddress": {
"heading": "LXP wallet address",
"prompt": "Please enter the wallet address linked to your LXP"
Expand Down
1 change: 1 addition & 0 deletions packages/snap/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"none": "No hay activas activaciones de LXP",
"one": "Hay {count} activacion de LXP activa"
},
"lineaEns": "Dominion Linea ENS",
"lxpAddress": {
"heading": "Cuenta que usas para LXP",
"prompt": "Ingresa tu cuenta para ver tu balance"
Expand Down
1 change: 1 addition & 0 deletions packages/snap/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"none": "Il n'y a pas d'activation active",
"one": "Il y a {count} activation LXP active"
},
"lineaEns": "Domaine Linea ENS",
"lxpAddress": {
"heading": "Adresse du portefeuille LXP",
"prompt": "Veuillez saisir l'adresse de votre wallet lié à vos LXP"
Expand Down
5 changes: 3 additions & 2 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@consensys/linea-voyager",
"version": "0.8.0",
"description": "View your minted LXP and LXP-L balances, POH status, and current activations.",
"version": "0.9.0",
"description": "View your minted LXP and LXP-L balances, POH status, Linea ENS domain and current activations.",
"repository": {
"type": "git",
"url": "https://github.com/Consensys/linea-voyager-snap"
Expand All @@ -28,6 +28,7 @@
"test": "jest"
},
"dependencies": {
"@metamask/abi-utils": "^2.0.2",
"@metamask/snaps-sdk": "3.2.0",
"@metamask/utils": "^8.4.0",
"buffer": "^6.0.3"
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "0.8.0",
"description": "View your minted LXP and LXP-L balances, POH status, and current activations.",
"version": "0.9.0",
"description": "View your minted LXP and LXP-L balances, POH status, Linea ENS domain and current activations.",
"proposedName": "Linea Voyager",
"repository": {
"type": "git",
Expand Down
11 changes: 9 additions & 2 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,22 @@ export const onHomePage: OnHomePageHandler = async () => {

const myAccount = snapState.lxpAddress as string;

const { lxpBalance, lxpLBalance, openBlockScore, pohStatus, activations } =
await getDataForUser(myAccount, chainId);
const {
lxpBalance,
lxpLBalance,
openBlockScore,
pohStatus,
activations,
name,
} = await getDataForUser(myAccount, chainId);

await setState({
myLxpBalance: lxpBalance,
myLxpLBalance: lxpLBalance,
myOpenBlockScore: openBlockScore,
myPohStatus: pohStatus,
activations,
myLineaEns: name,
});

return renderMainUi(myAccount);
Expand Down
56 changes: 55 additions & 1 deletion packages/snap/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { decode } from '@metamask/abi-utils';
import type { Hex } from '@metamask/utils';

import { callGlobalApi } from './api';
import type { UserData } from './types';
import {
Expand All @@ -18,10 +21,11 @@ export async function getDataForUser(
): Promise<UserData> {
try {
const isLineascan = chainId !== '0xe708';
const [userData, lxpBalanceRaw, lxpLBalanceRaw] = await Promise.all([
const [userData, lxpBalanceRaw, lxpLBalanceRaw, name] = await Promise.all([
callGlobalApi(address, isLineascan),
isLineascan ? 0 : getBalanceFromChain(LXP_CONTRACT_ADDRESS, address),
isLineascan ? 0 : getBalanceFromChain(LXP_L_CONTRACT_ADDRESS, address),
isLineascan ? '' : getNameFromChain(address),
]);

userData.lxpBalance = isLineascan
Expand All @@ -30,6 +34,7 @@ export async function getDataForUser(
userData.lxpLBalance = isLineascan
? convertBalanceToDisplay(userData.lxpLBalance.toString())
: lxpLBalanceRaw;
userData.name = isLineascan ? userData.name : name;

return userData;
} catch (error) {
Expand All @@ -39,6 +44,7 @@ export async function getDataForUser(
lxpLBalance: 0,
pohStatus: false,
activations: [],
name: '',
};
}
}
Expand All @@ -63,3 +69,51 @@ async function getBalanceFromChain(tokenAddress: string, address: string) {

return convertBalanceToDisplay(rawBalance);
}

/**
* Get the Linea ENS node hash from the chain.
* @param address - The address to get the Linea ENS node hash for.
* @returns The Linea ENS node hash for the address.
*/
async function getNodeHashFromChain(address: string) {
const method = 'eth_call';
const params = [
{
to: '0x2372154B01F1071b2f2BB02e93Ab97404f1F7a76',
data: `0xbffbe61c000000000000000000000000${address.slice(2)}`,
},
'latest',
];

return ethereum.request<string>({ method, params });
}

/**
* Get the Linea ENS domain name for an address from the chain.
* @param address - The address to get the Linea ENS domain name for.
* @returns The Linea ENS domain name for the address.
*/
async function getNameFromChain(address: string): Promise<string> {
const nodeHash = await getNodeHashFromChain(address);

if (!nodeHash || nodeHash === '0x') {
return '';
}

const method = 'eth_call';
const params = [
{
to: '0x5bDA6a6B90452e8a399B412E70915B61Dd50c82B',
data: `0x691f3431${nodeHash.slice(2)}`,
},
'latest',
];

const rawName = await ethereum.request<string>({ method, params });

if (!rawName || rawName === '0x') {
return '';
}

return decode(['string'], rawName.toString() as Hex)[0] as string;
}
3 changes: 3 additions & 0 deletions packages/snap/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type Captions = {
none: string;
one: string;
};
lineaEns: string;
lxpAddress: {
heading: string;
prompt: string;
Expand Down Expand Up @@ -68,6 +69,7 @@ export type UserData = {
openBlockScore: number;
lxpBalance: number;
lxpLBalance: number;
name: string;
};

export type SnapState = {
Expand All @@ -78,4 +80,5 @@ export type SnapState = {
myOpenBlockScore?: number;
myPohStatus?: boolean;
activations?: Activation[];
myLineaEns?: string;
};
7 changes: 7 additions & 0 deletions packages/snap/src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ export async function renderMainUi(myAccount: string) {
const lxpLBalance = snapState?.myLxpLBalance ?? 0;
const openBlockScore = snapState?.myOpenBlockScore ?? 0;
const activations = snapState?.activations ?? [];
const name = snapState?.myLineaEns ?? '';

const captions = snapState?.captions;

const labelBalance = captions?.balance;
const labelLxpLBalance = captions?.balanceLxpL;
const labelPendingLxpLBalance = captions?.pendingBalanceLxpL;
const labelAddress = captions?.address;
const labelPohStatus = captions?.pohStatus;
const labelLineaEns = captions?.lineaEns;

const pohStatus = `${
snapState?.myPohStatus
Expand Down Expand Up @@ -85,6 +88,10 @@ export async function renderMainUi(myAccount: string) {
);
}

if (name && name !== '') {
myData.push(row(labelLineaEns, text(name)));
}

const help = captions?.help as string;
const viewBalance = captions?.viewBalance as string;
const viewLxpLBalance = captions?.viewLxpLBalance as string;
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,7 @@ __metadata:
resolution: "@consensys/linea-voyager@workspace:packages/snap"
dependencies:
"@jest/globals": ^29.5.0
"@metamask/abi-utils": ^2.0.2
"@metamask/auto-changelog": 3.4.4
"@metamask/eslint-config": 12.2.0
"@metamask/eslint-config-jest": 12.1.0
Expand Down

0 comments on commit 1881cf0

Please sign in to comment.