Skip to content

Commit

Permalink
Fix no storage -> deposit issue (#635)
Browse files Browse the repository at this point in the history
* fix deposit issue

* remove comment

* update deposit cost
  • Loading branch information
Megha-Dev-19 authored Jan 16, 2024
1 parent 802e892 commit 4bb2bfd
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 17 deletions.
3 changes: 2 additions & 1 deletion replacements.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"REPL_DEVHUB_CONTRACT": "thomaspreview.testnet",
"REPL_NEAR": "discom.testnet",
"REPL_MOB": "eugenethedream",
"REPL_EFIZ": "efiz.testnet"
"REPL_EFIZ": "efiz.testnet",
"REPL_SOCIAL_CONTRACT": "v1.social08.testnet"
}
3 changes: 2 additions & 1 deletion replacements.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"REPL_DEVHUB_CONTRACT": "devgovgigs.near",
"REPL_NEAR": "near",
"REPL_MOB": "mob.near",
"REPL_EFIZ": "efiz.near"
"REPL_EFIZ": "efiz.near",
"REPL_SOCIAL_CONTRACT": "social.near"
}
3 changes: 2 additions & 1 deletion replacements.testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"REPL_DEVHUB_CONTRACT": "thomaselliot.testnet",
"REPL_NEAR": "discom.testnet",
"REPL_MOB": "eugenethedream",
"REPL_EFIZ": "efiz.testnet"
"REPL_EFIZ": "efiz.testnet",
"REPL_SOCIAL_CONTRACT": "v1.social08.testnet"
}
16 changes: 16 additions & 0 deletions src/core/lib/common.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// https://docs.rs/near-sdk/latest/near_sdk/env/constant.STORAGE_PRICE_PER_BYTE.html
const STORAGE_PRICE_PER_BYTE = "10000000000000000000";
// https://github.com/NearSocial/social-db/blob/d28c647252ce25a06c70c3b7f4930ccdcd217fd9/contract/src/account.rs#L8C5-L8C50
const MIN_STORAGE_BYTES = "2000";
const MIN_STORAGE_COST = Big(STORAGE_PRICE_PER_BYTE).times(MIN_STORAGE_BYTES);

// in case the user is new and doesn't have min storage cost, increasing the deposit
function getDepositAmountForWriteAccess(userStorageDeposit) {
const depositAmt = Big(userStorageDeposit?.available).gt(MIN_STORAGE_COST)
? Big(10).pow(22)
: Big(MIN_STORAGE_COST).plus(Big(10).pow(22));

return depositAmt;
}

return { getDepositAmountForWriteAccess };
30 changes: 23 additions & 7 deletions src/devhub/entity/post/Post.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Ideally, this would be a page

const { href } = VM.require("${REPL_DEVHUB}/widget/core.lib.url");
const { getDepositAmountForWriteAccess } = VM.require(
"${REPL_DEVHUB}/widget/core.lib.common"
);

getDepositAmountForWriteAccess || (getDepositAmountForWriteAccess = () => {});
const { draftState, onDraftStateChange } = VM.require(
"${REPL_DEVHUB}/widget/devhub.entity.post.draft"
);
Expand Down Expand Up @@ -262,12 +266,24 @@ const containsLike = props.isPreview
const likeBtnClass = containsLike ? fillIcons.Like : emptyIcons.Like;
// This must be outside onLike, because Near.view returns null at first, and when the view call finished, it returns true/false.
// If checking this inside onLike, it will give `null` and we cannot tell the result is true or false.
let grantNotify = Near.view("social.near", "is_write_permission_granted", {
predecessor_id: "${REPL_DEVHUB_CONTRACT}",
key: context.accountId + "/index/notify",
});
let grantNotify = Near.view(
"${REPL_SOCIAL_CONTRACT}",
"is_write_permission_granted",
{
predecessor_id: "${REPL_DEVHUB_CONTRACT}",
key: context.accountId + "/index/notify",
}
);

const userStorageDeposit = Near.view(
"${REPL_SOCIAL_CONTRACT}",
"storage_balance_of",
{
account_id: context.accountId,
}
);

if (grantNotify === null) {
if (grantNotify === null || userStorageDeposit === null) {
return;
}

Expand All @@ -289,14 +305,14 @@ const onLike = () => {

if (grantNotify === false) {
likeTxn.unshift({
contractName: "social.near",
contractName: "${REPL_SOCIAL_CONTRACT}",
methodName: "grant_write_permission",
args: {
predecessor_id: "${REPL_DEVHUB_CONTRACT}",
keys: [context.accountId + "/index/notify"],
},
gas: Big(10).pow(14),
deposit: Big(10).pow(22),
deposit: getDepositAmountForWriteAccess(userStorageDeposit),
});
}

Expand Down
30 changes: 23 additions & 7 deletions src/devhub/entity/post/PostEditor.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const { normalize } = VM.require("${REPL_DEVHUB}/widget/core.lib.stringUtils");
const { getDepositAmountForWriteAccess } = VM.require(
"${REPL_DEVHUB}/widget/core.lib.common"
);

getDepositAmountForWriteAccess || (getDepositAmountForWriteAccess = () => {});
normalize || (normalize = () => {});

const CenteredMessage = styled.div`
Expand Down Expand Up @@ -33,6 +37,14 @@ if (!context.accountId) {
);
}

const userStorageDeposit = Near.view(
"${REPL_SOCIAL_CONTRACT}",
"storage_balance_of",
{
account_id: context.accountId,
}
);

const cleanDescription = (description) => {
return description
? description.replace(
Expand Down Expand Up @@ -256,11 +268,15 @@ const typeSwitch = (optionName) => {

// This must be outside onClick, because Near.view returns null at first, and when the view call finished, it returns true/false.
// If checking this inside onClick, it will give `null` and we cannot tell the result is true or false.
let grantNotify = Near.view("social.near", "is_write_permission_granted", {
predecessor_id: "${REPL_DEVHUB_CONTRACT}",
key: context.accountId + "/index/notify",
});
if (grantNotify === null) {
let grantNotify = Near.view(
"${REPL_SOCIAL_CONTRACT}",
"is_write_permission_granted",
{
predecessor_id: "${REPL_DEVHUB_CONTRACT}",
key: context.accountId + "/index/notify",
}
);
if (grantNotify === null || userStorageDeposit === null) {
return;
}

Expand Down Expand Up @@ -356,14 +372,14 @@ const onSubmit = () => {
if (mode == "Create" || mode == "Edit") {
if (grantNotify === false) {
txn.unshift({
contractName: "social.near",
contractName: "${REPL_SOCIAL_CONTRACT}",
methodName: "grant_write_permission",
args: {
predecessor_id: "${REPL_DEVHUB_CONTRACT}",
keys: [context.accountId + "/index/notify"],
},
gas: Big(10).pow(14),
deposit: Big(10).pow(22),
deposit: getDepositAmountForWriteAccess(userStorageDeposit),
});
}
Near.call(txn);
Expand Down

0 comments on commit 4bb2bfd

Please sign in to comment.