-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add optimistic response for shielded transfers (#1615)
- Loading branch information
1 parent
893f85e
commit a27e004
Showing
5 changed files
with
69 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
storageShieldedBalanceAtom, | ||
viewingKeysAtom, | ||
} from "atoms/balance/atoms"; | ||
import { chainAssetsMapAtom } from "atoms/chain/atoms"; | ||
import BigNumber from "bignumber.js"; | ||
import { useAtomValue, useSetAtom } from "jotai"; | ||
import { Address } from "types"; | ||
import { toBaseAmount } from "utils"; | ||
|
||
type Amount = string | number | BigNumber; | ||
|
||
const sum = (a: Amount, b: Amount): string => { | ||
return BigNumber(a).plus(b).toString(); | ||
}; | ||
|
||
export const useOptimisticTransferUpdate = () => { | ||
const setStorageShieldedBalance = useSetAtom(storageShieldedBalanceAtom); | ||
const chainAssetsMap = useAtomValue(chainAssetsMapAtom); | ||
|
||
const [viewingKeyData] = useAtomValue(viewingKeysAtom).data ?? []; | ||
const viewingKey = viewingKeyData?.key; | ||
|
||
return (token: Address, incrementDisplayAmount: BigNumber) => { | ||
const asset = chainAssetsMap[token]; | ||
if (!viewingKey || !asset) { | ||
return; | ||
} | ||
const baseAmount = toBaseAmount(asset, incrementDisplayAmount); | ||
setStorageShieldedBalance((storage) => ({ | ||
...storage, | ||
[viewingKey]: storage[viewingKey].map((item) => | ||
item.address === token ? | ||
{ ...item, minDenomAmount: sum(item.minDenomAmount, baseAmount) } | ||
: item | ||
), | ||
})); | ||
}; | ||
}; |
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