Skip to content

Commit

Permalink
fix: handle claim tip with debio (#1773)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulhakim2902 authored Mar 17, 2023
1 parent 5d07049 commit 2405c04
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
13 changes: 8 additions & 5 deletions src/components/Tip/Tip.container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const TipContainer: React.FC = () => {

const handleClaimTip = (networkId: string, ftIdentifier: string) => {
claim(networkId, ftIdentifier, ({ claimSuccess, errorMessage }) => {
if (networkId === NetworkIdEnum.MYRIAD) {
if (networkId !== NetworkIdEnum.NEAR) {
// TODO: Register translation
const variant = claimSuccess ? 'success' : 'warning';
const message = claimSuccess
Expand All @@ -82,7 +82,7 @@ export const TipContainer: React.FC = () => {

const handleClaimTipAll = (networkId: string) => {
claimAll(networkId, ({ claimSuccess, errorMessage }) => {
if (networkId === NetworkIdEnum.MYRIAD) {
if (networkId !== NetworkIdEnum.NEAR) {
// TODO: Register translation
const variant = claimSuccess ? 'success' : 'warning';
const message = claimSuccess
Expand Down Expand Up @@ -114,10 +114,13 @@ export const TipContainer: React.FC = () => {
throw new Error('Insufficient Gas Fee');
}

if (!server?.accountId?.[currentWallet?.networkId])
throw new Error('Server not exists');
const serverId =
currentWallet.networkId === NetworkIdEnum.NEAR
? server?.accountId?.[currentWallet.networkId]
: server?.accountId?.[NetworkIdEnum.MYRIAD];

if (!serverId) throw new Error('Server not exists');

const serverId = server.accountId[currentWallet?.networkId];
const tipsBalanceInfo = {
serverId: serverId,
referenceType: 'user',
Expand Down
32 changes: 21 additions & 11 deletions src/hooks/use-claim-tip.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useEnqueueSnackbar } from 'components/common/Snackbar/useEnqueueSnackba
import { VariantType } from 'notistack';
import { FeeInfo, TipsBalanceInfo } from 'src/interfaces/blockchain-interface';
import { Network, NetworkIdEnum } from 'src/interfaces/network';
import { BlockchainPlatform } from 'src/interfaces/wallet';
import * as TransactionAPI from 'src/lib/api/transaction';
import * as WalletAPI from 'src/lib/api/wallet';
import i18n from 'src/locale';
Expand All @@ -35,7 +36,7 @@ export const useClaimTip = () => {
state => state.userState,
);
const { getClaimTipNear } = useNearApi();
const { getClaimTipMyriad } = usePolkadotApi();
const { getClaimTipSubstrate } = usePolkadotApi();
const { server, provider } = useBlockchain();

const [loading, setLoading] = useState(true);
Expand Down Expand Up @@ -133,12 +134,10 @@ export const useClaimTip = () => {

try {
const networkCallback = async (network: Network) => {
if (!server?.accountId?.[network.id]) return network;

switch (network.id) {
case NetworkIdEnum.DEBIO:
case NetworkIdEnum.MYRIAD: {
const result = await getClaimTipMyriad(
const result = await getClaimTipSubstrate(
server,
user.id,
currentWallet,
Expand All @@ -157,6 +156,7 @@ export const useClaimTip = () => {
}

case NetworkIdEnum.NEAR: {
if (!server?.accountId?.[network.id]) return network;
const referenceIds = socials.map(social => social.peopleId);
const result = await getClaimTipNear(
server.accountId[network.id],
Expand Down Expand Up @@ -218,14 +218,18 @@ export const useClaimTip = () => {
let errorMessage = null;
let claimSuccess = true;

setClaiming(true);

try {
if (!server?.accountId?.[selectedNetwork.id]) {
const serverId =
selectedNetwork.blockchainPlatform === BlockchainPlatform.SUBSTRATE
? server?.accountId?.[NetworkIdEnum.MYRIAD]
: server?.accountId?.[selectedNetwork.id];

if (!serverId) {
throw new Error('ServerNotExists');
}

const serverId = server?.accountId?.[selectedNetwork.id];
setClaiming(true);

const currency = selectedNetwork.currencies?.find(
({ native, referenceId }) => {
if (ftIdentifier === 'native' && native) return true;
Expand Down Expand Up @@ -268,11 +272,17 @@ export const useClaimTip = () => {
let errorMessage = null;
let claimSuccess = true;

setClaimingAll(true);

const walletId = currentWallet.id;
const serverId = server?.accountId?.[networkId];
const selectedNetwork = networks.find(network => network.id == networkId);
const serverId =
selectedNetwork.blockchainPlatform === BlockchainPlatform.SUBSTRATE
? server?.accountId?.[NetworkIdEnum.MYRIAD]
: server?.accountId?.[selectedNetwork.id];

if (!serverId) return;

setClaimingAll(true);

const userId = user.id;
const currencyIds = [];
const ftIdentifiers = ['native'];
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/use-polkadot-api.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import {
TipsBalanceData,
TipsResult,
} from 'src/interfaces/blockchain-interface';
import { Network } from 'src/interfaces/network';
import { Network, NetworkIdEnum } from 'src/interfaces/network';
import { SocialMedia } from 'src/interfaces/social';
import { Wallet } from 'src/interfaces/user';
import { Server } from 'src/lib/api/server';
import { PolkadotJs } from 'src/lib/services/polkadot-js';

export const usePolkadotApi = () => {
const getClaimTipMyriad = async (
const getClaimTipSubstrate = async (
server: Server,
referenceId: string,
wallet: Wallet,
socials: SocialMedia[],
network: Network,
): Promise<TipsResultsProps> => {
const serverId = server.accountId[network.id];
const serverId = server.accountId[NetworkIdEnum.MYRIAD];
const accountId = wallet?.id ?? null;
const peopleIds: string[] = [];
const data: TipsBalanceData = {
Expand Down Expand Up @@ -173,6 +173,6 @@ export const usePolkadotApi = () => {
};

return {
getClaimTipMyriad,
getClaimTipSubstrate,
};
};

0 comments on commit 2405c04

Please sign in to comment.