Skip to content

Commit

Permalink
Merge pull request #306 from PotLock/refactor/registry-sdk
Browse files Browse the repository at this point in the history
Adds registry SDK and replaces usage
  • Loading branch information
lachlanglen authored Feb 23, 2024
2 parents cb4532e + 3a102b5 commit 20dae56
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 124 deletions.
7 changes: 6 additions & 1 deletion apps/potlock/widget/Cart/Checkout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const [projectId, setProjectId] = useState("");
// }
// `;

const PotlockRegistrySDK = VM.require("potlock.near/widget/SDK.registry");
const registry = PotlockRegistrySDK({ env: props.env });

const registeredProjects = registry.getProjects();

const Container = styled.div`
background: #fafafa;
display: flex;
Expand Down Expand Up @@ -172,7 +177,7 @@ const allSelected =
// }
// }, [props.transactionHashes]);

if (props.transactionHashes && props.registeredProjects && !state.successfulDonationRecipientId) {
if (props.transactionHashes && registeredProjects && !state.successfulDonationRecipientId) {
const body = JSON.stringify({
jsonrpc: "2.0",
id: "dontcare",
Expand Down
13 changes: 8 additions & 5 deletions apps/potlock/widget/Components/Feed.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const { ownerId } = props;
const projects = props.registeredProjects || [];

const projectIds = useMemo(
() => projects.filter((project) => project.status === "Approved").map((project) => project.id),
[projects]
);
const PotlockRegistrySDK = VM.require(`${ownerId}/widget/SDK.registry`);
const registry = PotlockRegistrySDK({ env: props.env });

const projects = registry.getProjects() || [];

const projectIds = projects
.filter((project) => project.status === "Approved")
.map((project) => project.id);

const Container = styled.div`
padding: 24px 64px;
Expand Down
66 changes: 2 additions & 64 deletions apps/potlock/widget/Index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const ownerId = "potlock.near";
const registryContractId =
props.env === "staging" ? "registry.staging.potlock.near" : "registry.potlock.near";
const donationContractId = "donate.potlock.near";
const potFactoryContractId =
props.env === "staging" ? "potfactory.staging.potlock.near" : "v1.potfactory.potlock.near";
Expand Down Expand Up @@ -66,10 +64,7 @@ State.init({
nearToUsd: null,
isCartModalOpen: false,
isNavMenuOpen: false,
registryConfig: null,
userIsRegistryAdmin: null,
allPots: null,
registeredProjects: null,
donnorProjectId: null,
amount: null,
note: null,
Expand Down Expand Up @@ -137,11 +132,7 @@ if (!state.allPots) {
});
}

if (!state.registeredProjects) {
State.update({ registeredProjects: Near.view(registryContractId, "get_projects", {}) });
}

if (!state.registeredProjects || !state.allPots) return "";
if (!state.allPots) return "";

if (!state.donations) {
State.update({
Expand All @@ -159,58 +150,6 @@ const getImageUrlFromSocialImage = (image) => {
}
};

if (!state.registeredProjects) {
Near.asyncView(registryContractId, "get_projects", {})
.then((projects) => {
// get social data for each project
Near.asyncView("social.near", "get", {
keys: projects.map((project) => `${project.id}/profile/**`),
}).then((socialData) => {
const formattedProjects = projects.map((project) => {
const profileData = socialData[project.id]?.profile;
let profileImageUrl = DEFAULT_PROFILE_IMAGE_URL;
if (profileData.image) {
const imageUrl = getImageUrlFromSocialImage(profileData.image);
if (imageUrl) profileImageUrl = imageUrl;
}
// get banner image URL
let bannerImageUrl = DEFAULT_BANNER_IMAGE_URL;
if (profileData.backgroundImage) {
const imageUrl = getImageUrlFromSocialImage(profileData.backgroundImage);
if (imageUrl) bannerImageUrl = imageUrl;
}
const formatted = {
id: project.id,
name: profileData.name ?? "",
description: profileData.description ?? "",
bannerImageUrl,
profileImageUrl,
status: project.status,
tags: [profileData.category.text ?? CATEGORY_MAPPINGS[profileData.category] ?? ""],
};
return formatted;
});
State.update({
registeredProjects: formattedProjects,
});
});
})
.catch((e) => {
console.log("error getting projects: ", e);
State.update({ getRegisteredProjectsError: e });
});
}

if (state.registryConfig === null) {
const registryConfig = Near.view(registryContractId, "get_config", {});
if (registryConfig) {
State.update({
registryConfig,
userIsRegistryAdmin: registryConfig.admins.includes(context.accountId),
});
}
}

const tabContentWidget = {
[CREATE_PROJECT_TAB]: "Project.Create",
[EDIT_PROJECT_TAB]: "Project.Create",
Expand Down Expand Up @@ -366,7 +305,6 @@ const props = {
},
},
DONATION_CONTRACT_ID: donationContractId,
REGISTRY_CONTRACT_ID: registryContractId,
POT_FACTORY_CONTRACT_ID: potFactoryContractId,
NADABOT_CONTRACT_ID: nadabotContractId,
NADABOT_HUMAN_METHOD: "is_human",
Expand Down Expand Up @@ -610,7 +548,7 @@ const Content = styled.div`

const isForm = [CREATE_PROJECT_TAB].includes(props.tab);

if (!state.cart || !state.registeredProjects) {
if (!state.cart) {
return "";
}

Expand Down
6 changes: 4 additions & 2 deletions apps/potlock/widget/Pots/ConfigForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const {
ownerId,
potDetail,
potId,
REGISTRY_CONTRACT_ID,
POT_FACTORY_CONTRACT_ID,
NADABOT_CONTRACT_ID,
NADABOT_HUMAN_METHOD,
Expand All @@ -12,7 +11,10 @@ const {

// console.log("props in config form: ", props);

const DEFAULT_REGISTRY_PROVIDER = `${REGISTRY_CONTRACT_ID}:is_registered`;
const PotlockRegistrySDK = VM.require("potlock.near/widget/SDK.registry");
const registry = PotlockRegistrySDK({ env: props.env });

const DEFAULT_REGISTRY_PROVIDER = `${registry.getContractId()}:is_registered`;
const DEFAULT_SYBIL_WRAPPER_PROVIDER = `${NADABOT_CONTRACT_ID}:${NADABOT_HUMAN_METHOD}`;
const DEFAULT_PROTOCOL_CONFIG_PROVIDER = `${POT_FACTORY_CONTRACT_ID}:get_protocol_config`;
const CURRENT_SOURCE_CODE_VERSION = "0.1.0";
Expand Down
17 changes: 4 additions & 13 deletions apps/potlock/widget/Profile/Body.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const {
ownerId,
projectId,
REGISTRY_CONTRACT_ID,
userIsRegistryAdmin,
SUPPORTED_FTS: { NEAR },
getTagsFromSocialProfileData,
Expand All @@ -11,19 +10,11 @@ const accountId = props.accountId ?? context.accountId;

const [statusReview, setStatusReview] = useState({ modalOpen: false, notes: "", newStatus: "" });

const PotlockRegistrySDK = VM.require("potlock.near/widget/SDK.registry");
const registry = PotlockRegistrySDK({ env: props.env });

const handleUpdateStatus = () => {
Near.call([
{
contractName: REGISTRY_CONTRACT_ID,
methodName: "admin_set_project_status",
args: {
project_id: projectId,
status: statusReview.newStatus,
review_notes: statusReview.notes,
},
deposit: NEAR.toIndivisible(0.01).toString(),
},
]);
registry.setProjectStatus(projectId, statusReview.newStatus, statusReview.notes);
};

const Wrapper = styled.div`
Expand Down
9 changes: 5 additions & 4 deletions apps/potlock/widget/Project/Actions.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const [isModalDonationOpen, setIsModalDonationOpen] = useState(false);
const [isModalDonationSucessOpen, setIsModalDonationSucessOpen] = useState(false);

const { ownerId, registeredProjects, projectId } = props;
const { ownerId, projectId } = props;

const projectIsApproved = useMemo(() => {
return registeredProjects.some((p) => p.id === projectId && p.status === "Approved");
}, [registeredProjects, projectId]);
const PotlockRegistrySDK = VM.require("potlock.near/widget/SDK.registry");
const registry = PotlockRegistrySDK({ env: props.env });

const projectIsApproved = registry.isProjectApproved(projectId);

const Container = styled.div`
display: flex;
Expand Down
34 changes: 10 additions & 24 deletions apps/potlock/widget/Project/CreateForm.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const {
ownerId,
REGISTRY_CONTRACT_ID,
validateNearAddress,
validateEVMAddress,
validateGithubRepoUrl,
Expand Down Expand Up @@ -41,7 +40,10 @@ const existingHorizonProject = Near.view(HORIZON_CONTRACT_ID, "get_project", {
account_id: context.accountId,
});

const projects = Near.view(REGISTRY_CONTRACT_ID, "get_projects", {});
const PotlockRegistrySDK = VM.require("potlock.near/widget/SDK.registry");
const registry = PotlockRegistrySDK({ env: props.env });

const projects = registry.getProjects() || [];

const imageHeightPx = 120;
const profileImageTranslateYPx = 220;
Expand Down Expand Up @@ -322,8 +324,6 @@ State.init({
githubError: "",
socialDataFetched: false,
socialDataIsFetching: false,
registeredProjects: null,
getRegisteredProjectsError: "",
isMultiAccountModalOpen: false,
teamMember: "",
teamMembers: [],
Expand Down Expand Up @@ -531,17 +531,6 @@ useEffect(() => {
}
}, [state.socialDataFetched, state.isDao, state.daoAddress, context.accountId]);

if (context.accountId && !state.registeredProjects) {
Near.asyncView(REGISTRY_CONTRACT_ID, "get_projects", {})
.then((projects) => {
State.update({ registeredProjects: projects });
})
.catch((e) => {
console.log("error getting projects: ", e);
State.update({ getRegisteredProjectsError: e });
});
}

const isCreateProjectDisabled =
state.daoAddressError ||
!state.name ||
Expand Down Expand Up @@ -709,7 +698,7 @@ const handleCreateOrUpdateProject = (e) => {
transactions.push(
// register project on potlock
{
contractName: REGISTRY_CONTRACT_ID,
contractName: registry.getContractId(),
methodName: "register",
deposit: Big(0.05).mul(Big(10).pow(24)),
args: potlockRegistryArgs,
Expand Down Expand Up @@ -766,7 +755,8 @@ const handleCreateOrUpdateProject = (e) => {
const pollIntervalMs = 1000;
// const totalPollTimeMs = 60000; // consider adding in to make sure interval doesn't run indefinitely
const pollId = setInterval(() => {
Near.asyncView(REGISTRY_CONTRACT_ID, "get_project_by_id", {
// This is an async request, not converting to SDK yet
Near.asyncView(registry.getContractId(), "get_project_by_id", {
project_id: context.accountId,
// TODO: implement pagination (should be OK without until there are 500+ donations from this user)
}).then((_project) => {
Expand All @@ -791,12 +781,8 @@ if (props.projectId) {
}

const registeredProject = useMemo(() => {
return state.registeredProjects
? state.registeredProjects?.find(
(project) => project.id == (state.isDao ? state.daoAddress : context.accountId)
)
: null;
}, [state.registeredProjects, state.isDao, state.daoAddress]);
return registry.getProjectById(state.isDao ? state.daoAddress : context.accountId);
}, [state.isDao, state.daoAddress]);

// console.log("registeredProject: ", registeredProject);

Expand All @@ -810,7 +796,7 @@ const proposalInProgress = useMemo(() => {
return proposals?.find((proposal) => {
return (
proposal.status == "InProgress" &&
proposal.kind.FunctionCall?.receiver_id == REGISTRY_CONTRACT_ID &&
proposal.kind.FunctionCall?.receiver_id == registry.getContractId() &&
proposal.kind.FunctionCall?.actions[0]?.method_name == "register"
);
});
Expand Down
6 changes: 4 additions & 2 deletions apps/potlock/widget/Project/Detail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ const {
ownerId,
projectId,
userIsRegistryAdmin,
REGISTRY_CONTRACT_ID,
tab,
POT_FACTORY_CONTRACT_ID,
DONATION_CONTRACT_ID,
} = props;

const { ProjectOptions } = VM.require(`${ownerId}/widget/Project.Options`);

const project = Near.view(REGISTRY_CONTRACT_ID, "get_project_by_id", { project_id: projectId });
const PotlockRegistrySDK = VM.require("potlock.near/widget/SDK.registry");
const registry = PotlockRegistrySDK({ env: props.env });

const project = registry.getProjectById(projectId);

if (!project || project == null) {
return "Loading";
Expand Down
15 changes: 8 additions & 7 deletions apps/potlock/widget/Project/ListPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,14 @@ const donateRandomly = () => {
});
};

const projects = useMemo(
() =>
userIsRegistryAdmin
? props.registeredProjects
: props.registeredProjects.filter((project) => project.status === "Approved"),
[props.registeredProjects, userIsRegistryAdmin]
);
const PotlockRegistrySDK = VM.require("potlock.near/widget/SDK.registry");
const registry = PotlockRegistrySDK({ env: props.env });

const projects = registry.getProjects() || [];

if (!registry.isRegistryAdmin(context.accountId)) {
projects = projects.filter((project) => project.status === "Approved");
}

const [totalDonations, totalDonors] = useMemo(() => {
if (!props.donations) {
Expand Down
6 changes: 4 additions & 2 deletions apps/potlock/widget/Project/ModalDonation.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const {
ownerId,
registeredProjects,
allPots,
recipientId, // TODO: change this to projectId
referrerId,
Expand All @@ -16,7 +15,10 @@ const {

console.log("props in donation modal: ", props);

const projects = registeredProjects || [];
const PotlockRegistrySDK = VM.require("potlock.near/widget/SDK.registry");
const registry = PotlockRegistrySDK({ env: props.env });

const projects = registry.getProjects() || [];

const approvedProjectIds = useMemo(
// TODO: get projects for pot if potId
Expand Down
Loading

0 comments on commit 20dae56

Please sign in to comment.