-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #531 from HausDAO/develop
deploy
- Loading branch information
Showing
17 changed files
with
306 additions
and
32 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
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
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,159 @@ | ||
import { useCallback, useEffect, useMemo, useState } from 'react'; | ||
import { RegisterOptions, useFormContext } from 'react-hook-form'; | ||
|
||
import { LOCAL_ABI } from '@daohaus/abis'; | ||
import { ValidNetwork } from '@daohaus/keychain-utils'; | ||
import { MolochV3Dao } from '@daohaus/moloch-v3-data'; | ||
import { | ||
useConnectedMember, | ||
useCurrentDao, | ||
useDaoData, | ||
} from '@daohaus/moloch-v3-hooks'; | ||
import { | ||
Buildable, | ||
Button, | ||
Field, | ||
WrappedInput, | ||
WrappedInputSelect, | ||
} from '@daohaus/ui'; | ||
import { | ||
createViemClient, | ||
handleBaseUnits, | ||
ignoreEmptyVal, | ||
toWholeUnits, | ||
ValidateField, | ||
ZERO_ADDRESS, | ||
} from '@daohaus/utils'; | ||
|
||
type Token = { | ||
name: string; | ||
value: string; | ||
paused: boolean; | ||
decimals: number; | ||
}; | ||
|
||
export const TransferTokens = (props: Buildable<Field>) => { | ||
const { connectedMember } = useConnectedMember(); | ||
const { daoChain } = useCurrentDao(); | ||
const { dao } = useDaoData(); | ||
const { register, setValue, watch } = useFormContext(); | ||
const [selectOptions, setSelectOptions] = useState<Array<Token>>([]); | ||
|
||
const selectedTokenId = 'paymentTokenAddress'; | ||
const selectedTokenAddr = watch(selectedTokenId); | ||
const tokenErrorMsg = 'Token is non-transferrable'; | ||
|
||
const recipientId = 'recipientAddress'; | ||
|
||
register(recipientId); | ||
register(selectedTokenId); | ||
|
||
useEffect(() => { | ||
const getDAOTokens = async (_dao: MolochV3Dao, chainId: ValidNetwork) => { | ||
const client = createViemClient({ | ||
chainId, | ||
}); | ||
const sharesDecimals = await client.readContract({ | ||
abi: LOCAL_ABI.ERC20, | ||
address: _dao.sharesAddress as `0x${string}`, | ||
functionName: 'decimals', | ||
}); | ||
const lootDecimals = await client.readContract({ | ||
abi: LOCAL_ABI.ERC20, | ||
address: _dao.lootAddress as `0x${string}`, | ||
functionName: 'decimals', | ||
}); | ||
setSelectOptions([ | ||
{ | ||
name: `${_dao.shareTokenName} (Voting Tokens)`, | ||
value: _dao.sharesAddress, | ||
paused: _dao.sharesPaused, | ||
decimals: Number(sharesDecimals), | ||
}, | ||
{ | ||
name: `${_dao.lootTokenName} (Non-Voting Tokens)`, | ||
value: _dao.lootAddress, | ||
paused: _dao.lootPaused, | ||
decimals: Number(lootDecimals), | ||
}, | ||
]); | ||
}; | ||
if (!dao || !daoChain) return; | ||
getDAOTokens(dao, daoChain); | ||
}, [dao, daoChain]); | ||
|
||
const selectedToken = useMemo(() => { | ||
if (!selectOptions || !selectedTokenAddr) return; | ||
return selectOptions.find((opt) => opt.value === selectedTokenAddr); | ||
}, [selectOptions, selectedTokenAddr]); | ||
|
||
const tokenBalance = useMemo(() => { | ||
if (!connectedMember || !dao || !selectedToken) return '0'; | ||
if (selectedToken.value === dao.sharesAddress) | ||
return toWholeUnits(connectedMember.shares, selectedToken.decimals); | ||
return toWholeUnits(connectedMember.loot, selectedToken.decimals); | ||
}, [connectedMember, dao, selectedToken]); | ||
|
||
const setMax = useCallback(() => { | ||
console.log('selectedToken', selectedToken); | ||
if (!selectedToken) return; | ||
setValue(props.id, tokenBalance.trim()); | ||
}, [selectedToken]); | ||
|
||
const newRules: RegisterOptions = useMemo(() => { | ||
return { | ||
setValueAs: (value) => handleBaseUnits(value, selectedToken?.decimals), | ||
validate: { | ||
number: (value) => ignoreEmptyVal(value, ValidateField.number), | ||
isTransferrable: () => (selectedToken?.paused ? tokenErrorMsg : true), | ||
tokenSelected: () => !!selectedToken, | ||
maxValue: (value) => | ||
Number(value) <= Number(handleBaseUnits(tokenBalance, 18)) || | ||
'Cannot exceed current token balance', | ||
nonZero: (value) => | ||
Number(value) > 0 || 'Value must be greater than zero', | ||
}, | ||
...props.rules, | ||
}; | ||
}, [selectedToken, tokenBalance]); | ||
|
||
return ( | ||
<> | ||
<WrappedInputSelect | ||
{...props} | ||
placeholder="0" | ||
error={ | ||
selectedToken?.paused | ||
? { type: 'error', message: tokenErrorMsg } | ||
: undefined | ||
} | ||
selectId={selectedTokenId} | ||
selectPlaceholder="--" | ||
options={selectOptions || []} | ||
rightAddon={ | ||
<Button color="secondary" size="sm" onClick={setMax}> | ||
Max: {tokenBalance} | ||
</Button> | ||
} | ||
rules={newRules} | ||
/> | ||
<WrappedInput | ||
address | ||
disabled={selectedToken?.paused} | ||
full | ||
id={recipientId} | ||
label="Recipient" | ||
placeholder="0x..." | ||
rules={{ | ||
required: true, | ||
validate: { | ||
ethAddress: (value) => | ||
ignoreEmptyVal(value, ValidateField.ethAddress), | ||
nonZeroAddress: (value) => | ||
value !== ZERO_ADDRESS || 'Cannot send to the Zero Address', | ||
}, | ||
}} | ||
/> | ||
</> | ||
); | ||
}; |
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
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
45 changes: 45 additions & 0 deletions
45
libs/moloch-v3-macro-ui/src/components/MemberProfileCard/ManageTokens.tsx
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,45 @@ | ||
import { useDHConnect } from '@daohaus/connect'; | ||
import { FormBuilder } from '@daohaus/form-builder'; | ||
import { ValidNetwork } from '@daohaus/keychain-utils'; | ||
import { MolochFields } from '@daohaus/moloch-v3-fields'; | ||
import { | ||
useConnectedMember, | ||
useDaoData, | ||
useDaoMembers, | ||
} from '@daohaus/moloch-v3-hooks'; | ||
import { COMMON_FORMS } from '@daohaus/moloch-v3-legos'; | ||
|
||
type ManageTokensProps = { | ||
daoChain: ValidNetwork; | ||
daoId: string; | ||
}; | ||
|
||
export const ManageTokens = ({ daoChain, daoId }: ManageTokensProps) => { | ||
const { refetch } = useDaoData(); | ||
const { refetch: refetchMembers } = useDaoMembers(); | ||
const { address } = useDHConnect(); | ||
const { refetch: refetchMember } = useConnectedMember({ | ||
daoChain, | ||
daoId, | ||
memberAddress: address as string, | ||
}); | ||
|
||
const onFormComplete = () => { | ||
refetch?.(); | ||
refetchMembers?.(); | ||
refetchMember?.(); | ||
}; | ||
|
||
return ( | ||
<FormBuilder | ||
form={COMMON_FORMS.MANAGE_TOKENS} | ||
customFields={MolochFields} | ||
lifeCycleFns={{ | ||
onPollSuccess: () => { | ||
onFormComplete(); | ||
}, | ||
}} | ||
targetNetwork={daoChain} | ||
/> | ||
); | ||
}; |
Oops, something went wrong.