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: upgrade to record v2 #30

Merged
merged 5 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
66 changes: 10 additions & 56 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
"@bonfida/name-offers": "0.0.10",
"@bonfida/name-tokenizer": "0.0.12",
"@bonfida/sns-emitter": "0.1.7",
"@bonfida/sns-react": "2.0.2",
"@bonfida/spl-name-service": "2.0.3",
"@bonfida/sns-react": "2.0.3",
"@bonfida/sns-records": "0.0.1-alpha.8",
"@bonfida/spl-name-service": "2.0.4",
"@coral-xyz/common-public": "0.2.0-latest.3375",
"@expo-google-fonts/dev": "*",
"@expo/html-elements": "0.5.1",
Expand Down
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { NavigatorTabsParamList } from "@src/types";
import { LanguageHeader } from "@src/components/Header";
import { SolanaProvider } from "@src/providers/SolanaProvider";
import ErrorBoundary from "react-native-error-boundary";
import { RecordV2BadgeExplanationModal } from "./components/RecordV2Badge";

const xnftjson = require("../xnft.json");

Expand All @@ -65,6 +66,7 @@ const modalConfig = {
DomainSizeModal: DomainSizeModal,
LanguageModal: LanguageModal,
TokenizeModal: TokenizeModal,
RecordV2BadgeExplanationModal: RecordV2BadgeExplanationModal,
};

const stackModal = createModalStack(modalConfig);
Expand Down
92 changes: 28 additions & 64 deletions src/components/EditPicture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@ import * as ImagePicker from "expo-image-picker";
import {
Record,
getDomainKeySync,
NameRegistryState,
transferInstruction,
NAME_PROGRAM_ID,
createNameRegistry,
updateInstruction,
Numberu32,
NAME_OFFERS_ID,
getRecordV2Key,
createRecordV2Instruction,
updateRecordV2Instruction,
validateRecordV2Content,
} from "@bonfida/spl-name-service";
import { registerFavourite } from "@bonfida/name-offers";
import { isTokenized } from "@bonfida/name-tokenizer";
import { t } from "@lingui/macro";
import { PublicKey, TransactionInstruction } from "@solana/web3.js";
import { useStatusModalContext } from "@src/contexts/StatusModalContext";
import { useSolanaConnection } from "@src/hooks/xnft-hooks";
import { removeZeroRight } from "@src/utils/record/zero";
import { sendTx } from "@src/utils/send-tx";
import { WrapModal } from "./WrapModal";
import { unwrap } from "@src/utils/unwrap";
Expand Down Expand Up @@ -51,10 +48,7 @@ export const EditPicture = ({
try {
setLoading(true);
const ixs: TransactionInstruction[] = [];
const { pubkey, parent } = getDomainKeySync(
Record.Pic + "." + domain,
true,
);
const recordKey = getRecordV2Key(domain, Record.Pic);

try {
new URL(pic);
Expand All @@ -81,67 +75,37 @@ export const EditPicture = ({
}

// Check if exists
const info = await connection.getAccountInfo(pubkey);
const info = await connection.getAccountInfo(recordKey);
if (!info?.data) {
const space = 2_000;
const lamports = await connection.getMinimumBalanceForRentExemption(
space + NameRegistryState.HEADER_LEN,
);
const ix = await createNameRegistry(
connection,
Buffer.from([1]).toString() + Record.Pic,
space, // Hardcode space to 2kB
new PublicKey(publicKey),
new PublicKey(publicKey),
lamports,
undefined,
parent,
const ix = createRecordV2Instruction(
domain,
Record.Pic,
pic,
publicKey,
publicKey,
);
ixs.push(ix);
} else {
// Zero the data stored
const { registry } = await NameRegistryState.retrieve(
connection,
pubkey,
const ix = updateRecordV2Instruction(
domain,
Record.Pic,
pic,
publicKey,
publicKey,
);

if (!registry.owner.equals(new PublicKey(publicKey))) {
// Record was created before domain was transfered
const ix = transferInstruction(
NAME_PROGRAM_ID,
pubkey,
new PublicKey(publicKey),
registry.owner,
undefined,
parent,
new PublicKey(publicKey),
);
ixs.push(ix);
}

if (registry.data) {
const trimmed = removeZeroRight(registry.data);
const zero = Buffer.alloc(trimmed.length);
const ix = updateInstruction(
NAME_PROGRAM_ID,
pubkey,
new Numberu32(0),
zero,
new PublicKey(publicKey),
);
ixs.push(ix);
}
ixs.push(ix);
}

const data = Buffer.from(pic, "utf-8");
const ix = updateInstruction(
NAME_PROGRAM_ID,
pubkey,
new Numberu32(0),
data,
new PublicKey(publicKey),
ixs.push(
validateRecordV2Content(
true,
domain,
Record.Pic,
publicKey,
publicKey,
publicKey,
),
);
ixs.push(ix);

const sig = await sendTx(
connection,
Expand Down
13 changes: 8 additions & 5 deletions src/components/ProfileBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import Clipboard from "@react-native-clipboard/clipboard";
import { t } from "@lingui/macro";
import { LinearGradient } from "expo-linear-gradient";
import { Feather, FontAwesome } from "@expo/vector-icons";
import { useProfilePic } from "@bonfida/sns-react";
import { useModal } from "react-native-modalfy";
import { useStatusModalContext } from "@src/contexts/StatusModalContext";
import tw from "@src/utils/tailwind";
import { abbreviate } from "@src/utils/abbreviate";
import { useWallet } from "@src/hooks/useWallet";
import { useFavoriteDomain } from "@src/hooks/useFavoriteDomain";
import { usePicRecord } from "@src/hooks/useRecords";

interface ProfileBlockProps {
children?: ReactNode;
owner: string;
domain: string;
picRecord: ReturnType<typeof useProfilePic>;
picRecord: ReturnType<typeof usePicRecord>;
}

export const ProfileBlock = ({
Expand Down Expand Up @@ -52,8 +52,10 @@ export const ProfileBlock = ({
>
<Image
source={
picRecord.result
? { uri: picRecord.result }
picRecord.uri
? {
uri: picRecord.uri,
}
: require("@assets/default-pic.png")
}
style={tw`w-full h-full rounded-full`}
Expand All @@ -62,9 +64,10 @@ export const ProfileBlock = ({
<TouchableOpacity
onPress={() =>
openModal("EditPicture", {
currentPic: picRecord.result,
currentPic: picRecord.uri,
domain: domain,
setAsFav: !favorite.result?.reverse,
refresh: picRecord.execute,
})
}
style={tw`h-[24px] w-[24px] rounded-full flex items-center justify-center absolute bottom-0 right-0 bg-brand-accent`}
Expand Down
Loading
Loading