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

feat: enable memo field on transfers of token #613

Merged
merged 9 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
"@cosmjs/proto-signing": "^0.32.1",
"@cosmjs/stargate": "^0.32.1",
"@cosmjs/tendermint-rpc": "^0.32.1",
"@dydxprotocol/v4-abacus": "1.7.33",
"@dydxprotocol/v4-abacus": "1.7.43",
"@dydxprotocol/v4-client-js": "^1.1.10",
"@dydxprotocol/v4-localization": "^1.1.90",
"@dydxprotocol/v4-localization": "^1.1.100",
"@ethersproject/providers": "^5.7.2",
"@js-joda/core": "^5.5.3",
"@privy-io/react-auth": "^1.59.7",
Expand Down
21 changes: 13 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 32 additions & 16 deletions public/configs/v1/env.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-dev-2": {
Expand Down Expand Up @@ -321,7 +322,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-dev-3": {
Expand Down Expand Up @@ -356,7 +358,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-dev-4": {
Expand Down Expand Up @@ -391,7 +394,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-dev-5": {
Expand Down Expand Up @@ -425,7 +429,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-staging": {
Expand Down Expand Up @@ -460,7 +465,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-staging-forced-update": {
Expand Down Expand Up @@ -507,7 +513,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-staging-west": {
Expand Down Expand Up @@ -542,7 +549,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-testnet": {
Expand Down Expand Up @@ -581,7 +589,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-testnet-dydx": {
Expand Down Expand Up @@ -615,7 +624,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-testnet-nodefleet": {
Expand Down Expand Up @@ -650,7 +660,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-testnet-kingnodes": {
Expand Down Expand Up @@ -684,7 +695,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-testnet-liquify": {
Expand Down Expand Up @@ -719,7 +731,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-testnet-polkachu": {
Expand Down Expand Up @@ -754,7 +767,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-testnet-bware": {
Expand Down Expand Up @@ -789,7 +803,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": true
}
},
"dydxprotocol-mainnet": {
Expand Down Expand Up @@ -824,7 +839,8 @@
"CCTPWithdrawalOnly": true,
"CCTPDepositOnly": true,
"isSlTpEnabled": true,
"isSlTpLimitOrdersEnabled": false
"isSlTpLimitOrdersEnabled": false,
"showMemoTransferField": false
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useEnvFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface EnvironmentFeatures {
CCTPDepositOnly: boolean;
isSlTpEnabled: boolean;
isSlTpLimitOrdersEnabled: boolean;
showMemoTransferField: boolean;
}

export const useEnvFeatures = (): EnvironmentFeatures => {
Expand Down
10 changes: 6 additions & 4 deletions src/hooks/useSubaccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ const useSubaccountContext = ({ localDydxWallet }: { localDydxWallet?: LocalWall
subaccountClient,
amount,
recipient,
memo,
}: {
subaccountClient: SubaccountClient;
amount: number;
recipient: string;
memo?: string;
}) => {
try {
return await compositeClient?.validatorClient.post.send(
Expand All @@ -187,7 +189,7 @@ const useSubaccountContext = ({ localDydxWallet }: { localDydxWallet?: LocalWall
}),
false,
compositeClient?.validatorClient?.post.defaultDydxGasPrice,
undefined,
memo,
Method.BroadcastTxCommit
);
} catch (error) {
Expand Down Expand Up @@ -305,13 +307,13 @@ const useSubaccountContext = ({ localDydxWallet }: { localDydxWallet?: LocalWall
// ------ Transfer Methods ------ //

const transfer = useCallback(
async (amount: number, recipient: string, coinDenom: string) => {
async (amount: number, recipient: string, coinDenom: string, memo?: string) => {
if (!subaccountClient) {
return undefined;
}
return (await (coinDenom === usdcDenom
? transferFromSubaccountToAddress
: transferNativeToken)({ subaccountClient, amount, recipient })) as IndexedTx;
? transferFromSubaccountToAddress({ subaccountClient, amount, recipient })
: transferNativeToken({ subaccountClient, amount, recipient, memo }))) as IndexedTx;
},
[subaccountClient, transferFromSubaccountToAddress, transferNativeToken]
);
Expand Down
1 change: 1 addition & 0 deletions src/lib/abacus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class AbacusStateManager {
this.setTransferValue({ value: null, field: TransferInputField.address });
this.setTransferValue({ value: null, field: TransferInputField.size });
this.setTransferValue({ value: null, field: TransferInputField.usdcSize });
this.setTransferValue({ value: null, field: TransferInputField.MEMO });
};

clearTriggerOrdersInputValues = () => {
Expand Down
52 changes: 51 additions & 1 deletion src/views/forms/TransferForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { DydxChainAsset } from '@/constants/wallets';
import { useAccountBalance } from '@/hooks/useAccountBalance';
import { useAccounts } from '@/hooks/useAccounts';
import { useDydxClient } from '@/hooks/useDydxClient';
import { useEnvFeatures } from '@/hooks/useEnvFeatures';
import { useRestrictions } from '@/hooks/useRestrictions';
import { useStringGetter } from '@/hooks/useStringGetter';
import { useSubaccount } from '@/hooks/useSubaccount';
Expand Down Expand Up @@ -59,6 +60,8 @@ export const TransferForm = ({
className,
}: TransferFormProps) => {
const stringGetter = useStringGetter();
const { showMemoTransferField } = useEnvFeatures();

const { freeCollateral } = useSelector(getSubaccount, shallowEqual) ?? {};
const { dydxAddress } = useAccounts();
const { transfer } = useSubaccount();
Expand All @@ -72,6 +75,7 @@ export const TransferForm = ({
size,
fee,
token,
memo,
} = useSelector(getTransferInputs, shallowEqual) ?? {};

// Form states
Expand All @@ -85,9 +89,13 @@ export const TransferForm = ({
}, [fee]);

const asset = (token ?? selectedAsset) as DydxChainAsset;
const isChainTokenSelected = asset === DydxChainAsset.CHAINTOKEN;
const isUSDCSelected = asset === DydxChainAsset.USDC;
const amount = isUSDCSelected ? size?.usdcSize : size?.size;
const showNotEnoughGasWarning = fee && isUSDCSelected && usdcBalance < fee;
const showMemoField = showMemoTransferField && isChainTokenSelected;
const showMemoEmptyWarning = showMemoField && !memo;
Copy link
Contributor

@aforaleka aforaleka May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is there a way to only show this after address/amount is filled? i think there were previous complaints when we just say address is invalid when no address has been filled

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh hm yeah i can def make edits to do that!


const balance = isUSDCSelected ? freeCollateral?.current : nativeTokenBalance;

// BN
Expand Down Expand Up @@ -164,7 +172,8 @@ export const TransferForm = ({
const txResponse = await transfer(
amountBN.toNumber(),
recipientAddress as string,
tokensConfigs[asset]?.denom
tokensConfigs[asset]?.denom,
memo ?? undefined
);

if (txResponse?.code === 0) {
Expand Down Expand Up @@ -210,6 +219,13 @@ export const TransferForm = ({
});
};

const onChangeMemo = (value: string) => {
abacusStateManager.setTransferValue({
value,
field: TransferInputField.MEMO,
});
};

const onPasteAddress = async () => {
try {
const value = await navigator.clipboard.readText();
Expand All @@ -219,6 +235,15 @@ export const TransferForm = ({
}
};

const onPasteMemo = async () => {
try {
const value = await navigator.clipboard.readText();
onChangeMemo(value);
} catch (err) {
// expected error if user rejects clipboard access
}
};

const assetOptions = [
{
value: DydxChainAsset.USDC,
Expand Down Expand Up @@ -374,6 +399,31 @@ export const TransferForm = ({
/>
</$WithDetailsReceipt>

{showMemoField && (
<FormInput
label={stringGetter({ key: STRING_KEYS.MEMO })}
placeholder={stringGetter({ key: STRING_KEYS.REQUIRED_FOR_TRANSFERS_TO_CEX })}
type={InputType.Text}
onInput={(e: SyntheticInputEvent) => onChangeMemo(e.target?.value || '')}
value={memo ?? undefined}
slotRight={renderFormInputButton({
label: stringGetter({ key: STRING_KEYS.PASTE }),
isInputEmpty: !memo,
onClear: () => onChangeMemo(''),
onClick: onPasteMemo,
})}
disabled={isLoading}
/>
)}

{showMemoEmptyWarning && (
<AlertMessage type={AlertType.Warning}>
{stringGetter({
key: STRING_KEYS.TRANSFER_WITHOUT_MEMO,
})}
</AlertMessage>
)}

{showNotEnoughGasWarning && (
<AlertMessage type={AlertType.Warning}>
{stringGetter({
Expand Down
Loading