Skip to content

Commit

Permalink
feat(react-account/use-tip): support tipsWithFee contract
Browse files Browse the repository at this point in the history
  • Loading branch information
runjuu committed Aug 12, 2023
1 parent 21a7328 commit 68da84a
Showing 1 changed file with 64 additions and 46 deletions.
110 changes: 64 additions & 46 deletions packages/react-account/src/hooks/mira/use-tip.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,84 @@
import type { Numberish } from "crossbell";
import type { Address } from "viem";

import { getMiraTokenDecimals, emailTip } from "../../apis";
import { createAccountTypeBasedMutationHooks } from "../account-type-based-hooks";

import { SCOPE_KEY_TIPS_LIST } from "./use-tip-list";
import { SCOPE_KEY_ACCOUNT_MIRA_BALANCE } from "./use-account-mira-balance";

export const useTip = createAccountTypeBasedMutationHooks<
void,
{ characterId: Numberish; noteId?: Numberish; amount: number }
>({ actionDesc: "send tip", withParams: false }, () => ({
wallet: {
supportOPSign: false,
export type UseTipOptions = {
amount: number;
characterId: Numberish;
noteId?: Numberish;
feeReceiver?: Address;
};

async action({ characterId, noteId, amount }, { contract, account }) {
if (account.characterId) {
const decimal = await getMiraTokenDecimals(contract);
export const useTip = createAccountTypeBasedMutationHooks<void, UseTipOptions>(
{ actionDesc: "send tip", withParams: false },
() => ({
wallet: {
supportOPSign: false,

if (noteId) {
return contract?.tips.tipCharacterForNote({
async action(
{ characterId, noteId: toNoteId, amount, feeReceiver },
{ contract, account },
) {
if (account.characterId) {
const decimal = await getMiraTokenDecimals(contract);
const options = {
fromCharacterId: account.characterId,
toCharacterId: characterId,
toNoteId: noteId,
amount: BigInt(amount) * BigInt(10) ** BigInt(decimal),
});
} else {
return contract?.tips.tipCharacter({
fromCharacterId: account.characterId,
toCharacterId: characterId,
amount: BigInt(amount) * BigInt(10) ** BigInt(decimal),
});
};

if (toNoteId) {
return feeReceiver
? contract.tipsWithFee.tipCharacterForNote({
...options,
toNoteId,
feeReceiver,
})
: contract.tips.tipCharacterForNote({
...options,
toNoteId,
});
} else {
return feeReceiver
? contract.tipsWithFee.tipCharacter({ ...options, feeReceiver })
: contract.tips.tipCharacter(options);
}
}
}
},
},
},

async email({ characterId, noteId, amount }, { contract, account }) {
const decimal = await getMiraTokenDecimals(contract);
async email({ characterId, noteId, amount }, { contract, account }) {
const decimal = await getMiraTokenDecimals(contract);

return emailTip({
token: account.token,
characterId,
noteId,
amount: BigInt(amount) * BigInt(10) ** BigInt(decimal),
});
},
return emailTip({
token: account.token,
characterId,
noteId,
amount: BigInt(amount) * BigInt(10) ** BigInt(decimal),
});
},

onSuccess({ queryClient, variables, account }) {
const { characterId, noteId } = variables;
onSuccess({ queryClient, variables, account }) {
const { characterId, noteId } = variables;

return Promise.all([
queryClient.invalidateQueries(
SCOPE_KEY_ACCOUNT_MIRA_BALANCE({ address: account!.address! }),
),
return Promise.all([
queryClient.invalidateQueries(
SCOPE_KEY_ACCOUNT_MIRA_BALANCE({ address: account!.address! }),
),

queryClient.invalidateQueries(
SCOPE_KEY_TIPS_LIST({
toCharacterId: characterId,
toNoteId: noteId,
characterId: account?.characterId,
}),
),
]);
},
}));
queryClient.invalidateQueries(
SCOPE_KEY_TIPS_LIST({
toCharacterId: characterId,
toNoteId: noteId,
characterId: account?.characterId,
}),
),
]);
},
}),
);

0 comments on commit 68da84a

Please sign in to comment.