Skip to content

Commit

Permalink
Merge pull request #15 from spknetwork/feat/rc-delegation
Browse files Browse the repository at this point in the history
initiate rc feature
  • Loading branch information
igormuba authored Dec 26, 2023
2 parents a6d78fd + 3c18770 commit 69571e1
Show file tree
Hide file tree
Showing 22 changed files with 1,759 additions and 23 deletions.
21 changes: 21 additions & 0 deletions src/common/api/hive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,24 @@ export const getBlogEntries = (username: string, limit: number = dataLimit): Pro
0,
limit
]);

export const getRcOperationStats = (): Promise<any> => client.call("rc_api", "get_rc_stats", {});

export const getOutgoingRc = async (
from: string,
to: string = "",
limit: number = 50
): Promise<any[]> => {
const data = await client.call("rc_api", "list_rc_direct_delegations", {
start: [from, to],
limit: limit
});
return data;
};

export const getIncomingRc = async (user: string): Promise<any[]> => {
const data = await fetch(`https://ecency.com/private-api/received-rc/${user}`)
.then((res: any) => res.json())
.then((r: any) => r);
return data;
};
39 changes: 39 additions & 0 deletions src/common/api/mutations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useMutation } from "@tanstack/react-query";
import { usrActivity } from "./private-api";
import { claimAccount, claimAccountByKeychain } from "./operations";
import { FullAccount } from "../store/accounts/types";
import { PrivateKey } from "@hiveio/dhive";

interface Params {
bl?: string | number;
tx?: string | number;
}

export function useUserActivity(username: string | undefined, ty: number) {
return useMutation(["user-activity", username, ty], async (params: Params | undefined) => {
if (username) {
await usrActivity(username, ty, params?.bl, params?.tx);
}
});
}

export function useAccountClaiming(account: FullAccount) {
return useMutation(
["account-claiming", account.name],
async ({ isKeychain, key }: { key?: PrivateKey; isKeychain?: boolean }) => {
try {
if (isKeychain) {
return await claimAccountByKeychain(account);
}

if (key) {
return await claimAccount(account, key);
}

throw new Error();
} catch (error) {
throw new Error("Failed RC claiming. Please, try again or contact with support.");
}
}
);
}
83 changes: 80 additions & 3 deletions src/common/api/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import hs from "hivesigner";

import {PrivateKey, Operation, OperationName, TransactionConfirmation, AccountUpdateOperation, CustomJsonOperation} from '@hiveio/dhive';

import {Parameters} from 'hive-uri';
import {encodeOp, Parameters} from 'hive-uri';

import {client as hiveClient} from "./hive";

import {Account} from "../store/accounts/types";
import {Account, FullAccount} from "../store/accounts/types";

import {usrActivity} from "./private-api";

Expand Down Expand Up @@ -1211,4 +1211,81 @@ export const createAccountWithCredit = async (data: any, creator_account: string
return err;
}
};


export const delegateRC = (
delegator: string,
delegatees: string,
max_rc: string | number
): Promise<TransactionConfirmation> => {
const json = [
"delegate_rc",
{
from: delegator,
delegatees: delegatees.includes(",") ? delegatees.split(",") : [delegatees],
max_rc: max_rc
}
];

return broadcastPostingJSON(delegator, "rc", json);
};

export const claimAccountByHiveSigner = (account: FullAccount) =>
hotSign(
encodeOp(
[
"claim_account",
{
fee: "0.000 HIVE",
creator: account.name,
extensions: []
}
],
{}
).replace("hive://sign/", ""),
{
authority: "active",
required_auths: `["${account.name}"]`,
required_posting_auths: "[]"
},
`@${account.name}/wallet`
);

export const claimAccount = async (account: FullAccount, key: PrivateKey) => {
if (!key) {
throw new Error("[Account claiming] Active/owner key is not provided");
}

return hiveClient.broadcast.sendOperations(
[
[
"claim_account",
{
fee: {
amount: "0",
precision: 3,
nai: "@@000000021"
},
creator: account.name,
extensions: []
}
]
],
key
);
};

export const claimAccountByKeychain = (account: FullAccount) =>
keychain.broadcast(
account.name,
[
[
"claim_account",
{
creator: account.name,
extensions: [],
fee: "0.000 HIVE"
}
]
],
"Active"
);
14 changes: 9 additions & 5 deletions src/common/components/profile-card/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@
}

.vpower-line {
background: $duck-egg-blue;
height: 2px;
// background: $duck-egg-blue;
// height: 15px;
width: 300px;
// border-radius: 20px;
margin: 0 auto 8px auto;

cursor: pointer;

@include themify(dusk) {
backgorund: $dusk-background;
}
Expand All @@ -77,8 +79,10 @@
}

.vpower-line-inner {
background: $dark-sky-blue;
height: 2px;
// background: $dark-sky-blue;
// height: 100%;
width: 100%;
border-radius: 20px;

@include themify(dusk) {
backgorund: $dusk-primary;
Expand Down
32 changes: 22 additions & 10 deletions src/common/components/profile-card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {History} from "history";
import {Link} from "react-router-dom";

import moment from "moment";
import { RCAccount } from "@hiveio/dhive/lib/chain/rc";

import {Global} from "../../store/global/types";
import {Account, FullAccount} from "../../store/accounts/types";
Expand All @@ -20,7 +21,7 @@ import formattedNumber from "../../util/formatted-number";

import defaults from "../../constants/defaults.json";

import {votingPower} from "../../api/hive";
import {votingPower, findRcAccounts, rcPower} from "../../api/hive";

import {_t} from "../../i18n";

Expand All @@ -37,6 +38,7 @@ import {
import { EditPic } from '../community-card';
import { getRelationshipBetweenAccounts } from "../../api/bridge";
import { Skeleton } from "../skeleton";
import { ResourceCreditsInfo } from "../resource-credits";

interface Props {
global: Global;
Expand All @@ -61,6 +63,8 @@ export const ProfileCard = (props: Props) => {
const [followsActiveUser, setFollowsActiveUser] = useState(false);
const [isMounted, setIsmounted] = useState(false);
const [followsActiveUserLoading, setFollowsActiveUserLoading] = useState(false);
const [rcPercent, setRcPercent] = useState(100);

const [, updateState] = useState();
const forceUpdate = useCallback(() => updateState({} as any), []);

Expand All @@ -71,6 +75,16 @@ export const ProfileCard = (props: Props) => {
setFollowsActiveUserLoading(activeUser && activeUser.username ? true : false);
getFollowsInfo(account.name);
}

findRcAccounts(account?.name)
.then((r: RCAccount[]) => {
if (r && r[0]) {
setRcPercent(rcPower(r[0]));
}
})
.catch((e) => {
setRcPercent(100);
});
},
[account]);

Expand Down Expand Up @@ -145,16 +159,15 @@ export const ProfileCard = (props: Props) => {
<div className="username">{account.name}</div>
</h1>

<div className="vpower-line">
<div className="vpower-line-inner" style={{width: `${vPower}%`}}/>
<div>
<ResourceCreditsInfo {...props} rcPercent={rcPercent} account={account} />
</div>

<div className="vpower-percentage">
<Tooltip content={_t("profile.voting-power")}>
<span>{vPower.toFixed(2)}</span>
</Tooltip>
</div>
{loggedIn && !isMyProfile && <div className="d-flex justify-content-center mb-3 d-md-block">{followsActiveUserLoading ? <Skeleton className="loading-follows-you" /> : followsActiveUser ? <div className="follow-pill d-inline text-lowercase">{_t("profile.follows-you")}</div> : null}</div>}
{loggedIn && !isMyProfile &&
<div className="d-flex justify-content-center mb-3 d-md-block">
{followsActiveUserLoading ? <Skeleton className="loading-follows-you" /> : followsActiveUser ?
<div className="follow-pill d-inline text-lowercase">{_t("profile.follows-you")}</div> : null}
</div>}

{(account.profile?.name || account.profile?.about) && (
<div className="basic-info">
Expand All @@ -165,7 +178,6 @@ export const ProfileCard = (props: Props) => {

{account.__loaded && (
<div className="stats">

{account.follow_stats?.follower_count !== undefined && (
<div className="stat followers">
<Tooltip content={_t("profile.followers")}>
Expand Down
Loading

0 comments on commit 69571e1

Please sign in to comment.