-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-account/use-tip): support
tipsWithFee
contract
- Loading branch information
Showing
1 changed file
with
64 additions
and
46 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -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, | ||
}), | ||
), | ||
]); | ||
}, | ||
}), | ||
); |