-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show contract balance to admin if less than 2 NEAR (#973)
* feat: show contract balance to admin if less than 2 NEAR * test: fix * test: fix
- Loading branch information
1 parent
1611dd6
commit fe9ecec
Showing
6 changed files
with
204 additions
and
74 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
instances/devhub.near/widget/devhub/components/island/contract-balance.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const { accountId, dark } = props; | ||
|
||
const [remainingBalance, setRemainingBalance] = useState("2"); | ||
|
||
function stringToNear(yoctoString) { | ||
const paddedYoctoString = yoctoString.padStart(25, "0"); | ||
const integerPart = paddedYoctoString.slice(0, -24) || "0"; | ||
const fractionalPart = paddedYoctoString.slice(-24, -18); | ||
return parseFloat(`${integerPart}.${fractionalPart}`); | ||
} | ||
|
||
asyncFetch("${REPL_RPC_URL}", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
jsonrpc: "2.0", | ||
id: "dontcare", | ||
method: "query", | ||
params: { | ||
request_type: "view_account", | ||
finality: "final", | ||
account_id: accountId, | ||
}, | ||
}), | ||
}) | ||
.then(({ body: data }) => { | ||
const storageUsage = data.result?.storage_usage || 0; | ||
const yoctoString = data.result?.amount || "0"; | ||
const usageCostNear = storageUsage * 0.00001; | ||
const nearAmount = stringToNear(yoctoString); | ||
const remainingStorageCostNear = nearAmount - usageCostNear; | ||
setRemainingBalance(remainingStorageCostNear.toFixed(2)); | ||
}) | ||
.catch((error) => console.error(error)); | ||
|
||
return ( | ||
<div | ||
data-testid="contract-balance-wrapper" | ||
className={dark ? "text-white" : "text-black"} | ||
> | ||
{parseFloat(remainingBalance) < 2 ? ( | ||
<span data-testid="contract-balance"> | ||
Remaining Balance: {remainingBalance} | ||
</span> | ||
) : null} | ||
</div> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters