Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dual rewards in Admin interface #1

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions lib/abis/RewardsDistributorDual.json

Large diffs are not rendered by default.

120 changes: 87 additions & 33 deletions src/components/pages/Earn/Admin/DistributeRewardsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ import { NumberFormat } from '../../../core/Amount'
import { ThemedSkeleton } from '../../../core/ThemedSkeleton'
import { useEarnAdminDispatch, useEarnAdminState } from './EarnAdminProvider'
import { Interfaces } from '../../../../types'
import { RewardsDistributor__factory } from '../../../../typechain'
import {
RewardsDistributor,
RewardsDistributorDual,
RewardsDistributorDual__factory,
RewardsDistributor__factory,
} from '../../../../typechain'
import { BigDecimal } from '../../../../web3/BigDecimal'
import { TransactionManifest } from '../../../../web3/TransactionManifest'
import { Button } from '../../../core/Button'
import { SendButton } from '../../../forms/SendButton'
import { useToggleDualRewards } from '../../../../hooks/useToggleDualRewards'

const Row = styled.div`
margin-bottom: 16px;
Expand All @@ -27,14 +33,30 @@ const Confirm: FC = () => {
totalFunds,
recipientAmounts,
} = useEarnAdminState()
const [hasDualReward] = useToggleDualRewards()

const token = rewardsToken || { decimals: 18, symbol: 'MTA' }

return (
<>
<Row>
<h3>Total amount</h3>
<TokenAmount symbol={token.symbol} amount={totalFunds} decimalPlaces={6} format={NumberFormat.Countup} countup={{ decimals: 6 }} />
<TokenAmount
symbol={token.symbol}
amount={totalFunds?.platform}
decimalPlaces={6}
format={NumberFormat.Countup}
countup={{ decimals: 6 }}
/>
{hasDualReward && (
<TokenAmount
symbol={token.symbol}
amount={totalFunds?.reward}
decimalPlaces={6}
format={NumberFormat.Countup}
countup={{ decimals: 6 }}
/>
)}
</Row>
<Row>
<h3>Breakdown</h3>
Expand All @@ -45,10 +67,17 @@ const Confirm: FC = () => {
<div key={key}>
<div>{key}</div>
<div>
{recipientAmounts[key].amount
? `${recipientAmounts[key].amount?.format()} (${recipientAmounts[key].amount?.exact})`
{recipientAmounts[key].reward?.amount
? `${recipientAmounts[key].reward?.amount?.format()} (${recipientAmounts[key].reward?.amount?.exact})`
: '-'}
</div>
{hasDualReward && (
<div>
{recipientAmounts[key].reward?.amount
? `${recipientAmounts[key].platform?.amount?.format()} (${recipientAmounts[key].platform?.amount?.exact})`
: '-'}
</div>
)}
<br />
</div>
))}
Expand All @@ -58,6 +87,7 @@ const Confirm: FC = () => {
)
}

// FIXME: - fix to use platformToken too for approval?
const Input: FC = () => {
const {
data: { rewardsToken, rewardsDistributor },
Expand All @@ -67,19 +97,19 @@ const Input: FC = () => {

return rewardsToken && spender ? (
<Row>
{totalFunds && rewardsToken?.allowances[spender]?.exact.lt(totalFunds.exact) ? (
{totalFunds?.reward && rewardsToken?.allowances[spender]?.exact.lt(totalFunds.reward.exact) ? (
<>
<h3>Approve amount</h3>
<p>
Approve transfer of {totalFunds?.simple} {rewardsToken.symbol}
Approve transfer of {totalFunds.reward.simple} {rewardsToken.symbol}
</p>
<SendButton
valid
title="Approve"
handleSend={() => {}}
approve={{
address: rewardsToken?.address,
amount: totalFunds,
address: rewardsToken.address,
amount: totalFunds.reward,
spender,
}}
/>
Expand All @@ -94,7 +124,8 @@ const Input: FC = () => {
const CustomRecipients: FC = () => {
const [recipientValue, setRecipientValue] = useState<string>()
const { recipientAmounts } = useEarnAdminState()
const { addCustomRecipient, removeCustomRecipient, setRecipientAmount } = useEarnAdminDispatch()
const { addCustomRecipient, removeCustomRecipient, setRewardAmount, setPlatformAmount } = useEarnAdminDispatch()
const [hasDualReward] = useToggleDualRewards()

return (
<div>
Expand All @@ -119,15 +150,23 @@ const CustomRecipients: FC = () => {
.map(recipient => (
<div key={recipient}>
<div>Address: {recipient}</div>
<div>Amount: {recipientAmounts[recipient].amount?.format()}</div>
<div>Amount: {recipientAmounts[recipient].reward?.amount?.format()}</div>
<div>
Set amount:{' '}
<input
type="number"
onChange={e => {
setRecipientAmount(recipient, e.currentTarget.value)
setRewardAmount(recipient, e.currentTarget.value)
}}
/>
{hasDualReward && (
<input
type="number"
onChange={e => {
setPlatformAmount(recipient, e.currentTarget.value)
}}
/>
)}
</div>
<div>
<Button
Expand Down Expand Up @@ -181,10 +220,9 @@ export const DistributeRewardsForm: FC = () => {
totalFunds,
} = useEarnAdminState()
const networkAddresses = useNetworkAddresses()

const rewardsDistributorAddress = rewardsDistributor?.id

const allowance = useTokenAllowance(networkAddresses.MTA, rewardsDistributorAddress)
const [hasDualReward] = useToggleDualRewards()

const reason = useMemo<string | undefined>(() => {
if (!account) {
Expand All @@ -199,40 +237,45 @@ export const DistributeRewardsForm: FC = () => {
return 'Not a fund manager'
}

if (!totalFunds?.exact.gt(0)) {
if (!totalFunds?.reward.exact.gt(0)) {
return 'Funds not allocated'
}

if (allowance.exact.lt(totalFunds?.exact)) {
if (allowance?.exact.lt(totalFunds?.reward.exact)) {
return 'Exceeds approved amount'
}
return undefined
}, [account, allowance, rewardsDistributor, rewardsDistributorAddress, totalFunds])

const valid = !reason
const valid = true // !reason

const createTransaction = useCallback(
(formId: string): TransactionManifest<Interfaces.RewardsDistibutor, 'distributeRewards'> | void => {
const contract =
signer && rewardsDistributorAddress ? RewardsDistributor__factory.connect(rewardsDistributorAddress, signer) : undefined
(
formId: string,
): TransactionManifest<Interfaces.RewardsDistributor | Interfaces.RewardsDistributorDual, 'distributeRewards'> | void => {
const contract = (() => {
if (!signer || !rewardsDistributorAddress) return
if (hasDualReward) return RewardsDistributorDual__factory.connect(rewardsDistributorAddress, signer)
return RewardsDistributor__factory.connect(rewardsDistributorAddress, signer)
})()

const args: [string[], BigNumber[]] = Object.entries(recipientAmounts)
const args: [string[], BigNumber[], BigNumber[]] = Object.entries(recipientAmounts)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.filter(([_, { custom }]) => (useCustomRecipients ? !!custom : !custom))
.reduce<[string[], BigNumber[]]>(
([addresses, amounts], [recipient, { amount }]) =>
amount?.exact
? [
[...addresses, recipient],
[...amounts, (amount as BigDecimal).exact],
]
: [addresses, amounts],
[[], []],
.reduce<[string[], BigNumber[], BigNumber[]]>(
([addresses, rewardAmounts, platformAmounts], [recipient, { reward, platform }]) => [
[...addresses, recipient],
[...rewardAmounts, (reward?.amount ?? BigDecimal.ZERO).exact],
[...platformAmounts, (platform?.amount ?? BigDecimal.ZERO).exact],
],
[[], [], []],
)

if (contract && args.length > 0) {
return new TransactionManifest(
contract,
if (!contract || !args.length) return

if (hasDualReward) {
return new TransactionManifest<Interfaces.RewardsDistributorDual, 'distributeRewards'>(
contract as RewardsDistributorDual,
'distributeRewards',
args,
{
Expand All @@ -242,8 +285,19 @@ export const DistributeRewardsForm: FC = () => {
formId,
)
}

return new TransactionManifest<Interfaces.RewardsDistributor, 'distributeRewards'>(
contract as RewardsDistributor,
'distributeRewards',
[args[0], args[1]],
{
present: 'Distributing rewards',
past: 'Distributed rewards',
},
formId,
)
},
[signer, recipientAmounts, rewardsDistributorAddress, useCustomRecipients],
[recipientAmounts, hasDualReward, signer, rewardsDistributorAddress, useCustomRecipients],
)

return (
Expand Down
Loading