-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added create ICA account advanced action.
- Loading branch information
Showing
12 changed files
with
485 additions
and
97 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
96 changes: 96 additions & 0 deletions
96
packages/stateful/actions/core/advanced/CreateIcaAccount/Component.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,96 @@ | ||
import { useFormContext } from 'react-hook-form' | ||
import { useTranslation } from 'react-i18next' | ||
|
||
import { | ||
CopyToClipboard, | ||
IbcDestinationChainPicker, | ||
InputErrorMessage, | ||
Loader, | ||
WarningCard, | ||
useChain, | ||
} from '@dao-dao/stateless' | ||
import { LoadingDataWithError } from '@dao-dao/types' | ||
import { ActionComponent } from '@dao-dao/types/actions' | ||
import { getDisplayNameForChainId, getImageUrlForChainId } from '@dao-dao/utils' | ||
|
||
export type CreateIcaAccountData = { | ||
chainId: string | ||
} | ||
|
||
export type CreateIcaAccountOptions = { | ||
createdAddressLoading: LoadingDataWithError<string | undefined> | ||
} | ||
|
||
export const CreateIcaAccountComponent: ActionComponent< | ||
CreateIcaAccountOptions | ||
> = ({ | ||
fieldNamePrefix, | ||
isCreating, | ||
errors, | ||
options: { createdAddressLoading }, | ||
}) => { | ||
const { t } = useTranslation() | ||
const { watch, setValue } = useFormContext<CreateIcaAccountData>() | ||
const { chain_id: sourceChainId } = useChain() | ||
|
||
const destinationChainId = watch((fieldNamePrefix + 'chainId') as 'chainId') | ||
const imageUrl = getImageUrlForChainId(destinationChainId) | ||
|
||
return ( | ||
<> | ||
{isCreating ? ( | ||
<> | ||
<IbcDestinationChainPicker | ||
buttonClassName="self-start" | ||
includeSourceChain={false} | ||
onChainSelected={(chainId) => | ||
setValue((fieldNamePrefix + 'chainId') as 'chainId', chainId) | ||
} | ||
selectedChainId={destinationChainId} | ||
sourceChainId={sourceChainId} | ||
/> | ||
|
||
<InputErrorMessage className="-mt-2" error={errors?.chainId} /> | ||
</> | ||
) : ( | ||
<div className="flex flex-row flex-wrap items-center justify-between gap-x-4 gap-y-2 rounded-md bg-background-secondary px-4 py-3"> | ||
<div className="flex flex-row items-center gap-2"> | ||
{imageUrl && ( | ||
<div | ||
className="h-6 w-6 bg-contain bg-center bg-no-repeat" | ||
style={{ | ||
backgroundImage: `url(${imageUrl})`, | ||
}} | ||
></div> | ||
)} | ||
|
||
<p className="primary-text shrink-0"> | ||
{getDisplayNameForChainId(destinationChainId)} | ||
</p> | ||
</div> | ||
|
||
{createdAddressLoading.loading ? ( | ||
<Loader /> | ||
) : createdAddressLoading.errored ? ( | ||
<WarningCard | ||
content={ | ||
createdAddressLoading.error instanceof Error | ||
? createdAddressLoading.error.message | ||
: `${createdAddressLoading.error}` | ||
} | ||
/> | ||
) : createdAddressLoading.data ? ( | ||
<CopyToClipboard | ||
className="min-w-0" | ||
takeN={18} | ||
tooltip={t('button.clickToCopyAddress')} | ||
value={createdAddressLoading.data} | ||
/> | ||
) : ( | ||
<p className="secondary-text">{t('info.pending')}</p> | ||
)} | ||
</div> | ||
)} | ||
</> | ||
) | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/stateful/actions/core/advanced/CreateIcaAccount/README.md
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,20 @@ | ||
# CreateIcaAccount | ||
|
||
Create an account via Interchain Accounts IBC module. | ||
|
||
## Bulk import format | ||
|
||
This is relevant when bulk importing actions, as described in [this | ||
guide](https://github.com/DA0-DA0/dao-dao-ui/wiki/Bulk-importing-actions). | ||
|
||
### Key | ||
|
||
`createIcaAccount` | ||
|
||
### Data format | ||
|
||
```json | ||
{ | ||
"chainId": "<CHAIN ID>" | ||
} | ||
``` |
169 changes: 169 additions & 0 deletions
169
packages/stateful/actions/core/advanced/CreateIcaAccount/index.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,169 @@ | ||
import { useCallback, useEffect } from 'react' | ||
import { useFormContext } from 'react-hook-form' | ||
import { useTranslation } from 'react-i18next' | ||
|
||
import { MsgRegisterInterchainAccount } from '@dao-dao/protobuf/codegen/ibc/applications/interchain_accounts/controller/v1/tx' | ||
import { Metadata } from '@dao-dao/protobuf/codegen/ibc/applications/interchain_accounts/v1/metadata' | ||
import { icaRemoteAddressSelector } from '@dao-dao/state/recoil' | ||
import { ChainEmoji, useCachedLoadingWithError } from '@dao-dao/stateless' | ||
import { | ||
ActionComponent, | ||
ActionKey, | ||
ActionMaker, | ||
UseDecodedCosmosMsg, | ||
UseDefaults, | ||
UseTransformToCosmos, | ||
} from '@dao-dao/types' | ||
import { | ||
getChainForChainName, | ||
getDisplayNameForChainId, | ||
getIbcTransferInfoBetweenChains, | ||
getIbcTransferInfoFromConnection, | ||
isDecodedStargateMsg, | ||
makeStargateMessage, | ||
} from '@dao-dao/utils' | ||
|
||
import { useActionOptions } from '../../../react' | ||
import { CreateIcaAccountComponent, CreateIcaAccountData } from './Component' | ||
|
||
const Component: ActionComponent = (props) => { | ||
const { t } = useTranslation() | ||
const { | ||
address, | ||
chain: { chain_id: srcChainId }, | ||
} = useActionOptions() | ||
|
||
const { watch, setError, clearErrors } = | ||
useFormContext<CreateIcaAccountData>() | ||
const destChainId = watch((props.fieldNamePrefix + 'chainId') as 'chainId') | ||
|
||
const createdAddressLoading = useCachedLoadingWithError( | ||
icaRemoteAddressSelector({ | ||
address, | ||
srcChainId, | ||
destChainId, | ||
}) | ||
) | ||
|
||
// If ICA account already exists for this chain during creation, add error | ||
// preventing submission. | ||
useEffect(() => { | ||
if ( | ||
!createdAddressLoading.loading && | ||
!createdAddressLoading.errored && | ||
createdAddressLoading.data && | ||
props.isCreating | ||
) { | ||
setError((props.fieldNamePrefix + 'chainId') as 'chainId', { | ||
type: 'manual', | ||
message: t('error.icaAccountAlreadyExists', { | ||
chain: getDisplayNameForChainId(destChainId), | ||
}), | ||
}) | ||
} else { | ||
clearErrors((props.fieldNamePrefix + 'chainId') as 'chainId') | ||
} | ||
}, [ | ||
clearErrors, | ||
createdAddressLoading, | ||
destChainId, | ||
props.fieldNamePrefix, | ||
props.isCreating, | ||
setError, | ||
t, | ||
]) | ||
|
||
return ( | ||
<CreateIcaAccountComponent | ||
{...props} | ||
options={{ | ||
createdAddressLoading, | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
export const makeCreateIcaAccountAction: ActionMaker<CreateIcaAccountData> = ({ | ||
t, | ||
chain: { chain_id: sourceChainId }, | ||
address, | ||
}) => { | ||
const useDefaults: UseDefaults<CreateIcaAccountData> = () => ({ | ||
chainId: '', | ||
}) | ||
|
||
const useTransformToCosmos: UseTransformToCosmos<CreateIcaAccountData> = () => | ||
useCallback(({ chainId }) => { | ||
if (!chainId) { | ||
return | ||
} | ||
|
||
const info = getIbcTransferInfoBetweenChains(sourceChainId, chainId) | ||
|
||
return chainId | ||
? makeStargateMessage({ | ||
stargate: { | ||
typeUrl: MsgRegisterInterchainAccount.typeUrl, | ||
value: MsgRegisterInterchainAccount.fromPartial({ | ||
owner: address, | ||
connectionId: info.sourceChain.connection_id, | ||
version: JSON.stringify( | ||
Metadata.fromPartial({ | ||
version: 'ics27-1', | ||
controllerConnectionId: info.sourceChain.connection_id, | ||
hostConnectionId: info.destinationChain.connection_id, | ||
// Empty when registering a new address. | ||
address: '', | ||
encoding: 'proto3', | ||
txType: 'sdk_multi_msg', | ||
}) | ||
), | ||
}), | ||
}, | ||
}) | ||
: undefined | ||
}, []) | ||
|
||
const useDecodedCosmosMsg: UseDecodedCosmosMsg<CreateIcaAccountData> = ( | ||
msg: Record<string, any> | ||
) => { | ||
if ( | ||
!isDecodedStargateMsg(msg) || | ||
msg.stargate.typeUrl !== MsgRegisterInterchainAccount.typeUrl | ||
) { | ||
return { | ||
match: false, | ||
} | ||
} | ||
|
||
try { | ||
const { connectionId } = msg.stargate.value | ||
const { destinationChain } = getIbcTransferInfoFromConnection( | ||
sourceChainId, | ||
connectionId | ||
) | ||
|
||
return { | ||
match: true, | ||
data: { | ||
chainId: getChainForChainName(destinationChain.chain_name).chain_id, | ||
}, | ||
} | ||
} catch (err) { | ||
return { | ||
match: false, | ||
} | ||
} | ||
} | ||
|
||
return { | ||
key: ActionKey.CreateIcaAccount, | ||
Icon: ChainEmoji, | ||
label: t('title.createICAAccount'), | ||
description: t('info.createICAAccountDescription'), | ||
Component, | ||
useDefaults, | ||
useTransformToCosmos, | ||
useDecodedCosmosMsg, | ||
} | ||
} |
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
Oops, something went wrong.