Skip to content

Commit

Permalink
feat: add name service (dcSpark#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
arein authored Oct 4, 2022
1 parent a2f08c2 commit 9e1421e
Show file tree
Hide file tree
Showing 7 changed files with 689 additions and 252 deletions.
12 changes: 10 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getBalance,
signAndSendTransaction,
watchTransaction,
fetchName,
} from "solib";
import { useCallback, useState } from "react";
import "./App.css";
Expand All @@ -24,16 +25,20 @@ import {
function App() {
const toast = useToast();
const [address, setAddress] = useState<string | undefined>("");
const [name, setName] = useState<string | undefined>("");
const [balance, setBalance] = useState<string | undefined>("");
const [signature, setSignature] = useState<string | undefined>("");
const [message, setMessage] = useState<string | undefined>("");
const [toAddress, setToAddress] = useState<string | undefined>("");
const [amount, setAmount] = useState<number>(0);
const onClick = useCallback(() => {
connect()
.then((publicKey) => setAddress(publicKey!))
.then(() => {
.then((publicKey) => {setAddress(publicKey!); return publicKey})
.then((publicKey) => {
getBalance().then((value) => setBalance(value.toString()));
fetchName({address: publicKey}).then((name) => {
setName(name || publicKey);
});
});
}, []);

Expand Down Expand Up @@ -136,6 +141,9 @@ function App() {
</Flex>
</Flex>
)}
{name && (
<Flex>SNS Name: {name}</Flex>
)}
</Flex>
</div>
);
Expand Down
520 changes: 271 additions & 249 deletions example/yarn.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
"preview": "vite preview"
},
"devDependencies": {
"@types/bn.js": "^5.1.1",
"typescript": "^4.6.4",
"vite": "^3.1.0"
},
"dependencies": {
"@ethersproject/sha2": "^5.7.0",
"@solana/web3.js": "^1.63.0",
"bn.js": "^5.2.1",
"borsh": "^0.7.0",
"bs58": "^5.0.0",
"buffer": "^6.0.3",
"valtio": "^1.7.0"
Expand Down
2 changes: 2 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export {
sendTransaction,
watchTransaction,
} from "./transactions";

export { fetchName } from "./nameService";
40 changes: 40 additions & 0 deletions src/actions/nameService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Connection, PublicKey } from "@solana/web3.js";
import { solanaClusters } from "../defaults/clusters";
import { getFavoriteDomain, getAllDomains, performReverseLookup } from "./spl";

export type FetchNameArgs = {
address: string
}

export type FetchNameResult = string | null

/**
* Retrieves a domain name to display for a user if any
* First it attempts to get the favorite domain.
* This feature doesn't appear to commonly be used hence
* if none is set we try to capture other domains associated to the
* account and use the first one that pops up.
* @param connection The Solana RPC connection object
* @param owner The owner you want to retrieve the favorite domain for
* @returns
*/
export async function fetchName(args: FetchNameArgs): Promise<FetchNameResult> {
const connection = new Connection(solanaClusters.mainnetBeta.endpoint, 'confirmed');
const address = new PublicKey(args.address);

try {
return (await getFavoriteDomain(connection, address)).reverse;
} catch (e) {}

const otherDomains = await getSolDomainsFromPublicKey(connection, address);

return otherDomains.length > 0 ? otherDomains[0] : null;
}

async function getSolDomainsFromPublicKey(connection: Connection, wallet: PublicKey):Promise<string[]>{
const allDomainKeys = await getAllDomains(connection, wallet);
const allDomainNames = await Promise.all(allDomainKeys.map((key: PublicKey) => {
return performReverseLookup(connection, key)
}));
return allDomainNames;
}
Loading

0 comments on commit 9e1421e

Please sign in to comment.