Compare commits

...

2 Commits

Author SHA1 Message Date
Bill He
514a2d3ab3
use decimals: 2023-11-06 17:35:57 -08:00
Bill He
c58d94e477
Fix decimal overflow bug 2023-11-06 17:26:27 -08:00
2 changed files with 95 additions and 54 deletions

View File

@ -48,7 +48,7 @@ export const useSubaccount = () => useContext(SubaccountContext);
export const useSubaccountContext = ({ localDydxWallet }: { localDydxWallet?: LocalWallet }) => { export const useSubaccountContext = ({ localDydxWallet }: { localDydxWallet?: LocalWallet }) => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const { usdcDenom } = useTokenConfigs(); const { usdcDenom, usdcDecimals } = useTokenConfigs();
const { compositeClient, faucetClient } = useDydxClient(); const { compositeClient, faucetClient } = useDydxClient();
const { getFaucetFunds } = useMemo( const { getFaucetFunds } = useMemo(
@ -79,16 +79,34 @@ export const useSubaccountContext = ({ localDydxWallet }: { localDydxWallet?: Lo
subaccountClient: SubaccountClient; subaccountClient: SubaccountClient;
assetId?: number; assetId?: number;
amount: number; amount: number;
}) => await compositeClient?.depositToSubaccount(subaccountClient, amount.toString()), }) => {
try {
return await compositeClient?.depositToSubaccount(
subaccountClient,
amount.toFixed(usdcDecimals)
);
} catch (error) {
log('useSubaccount/depositToSubaccount', error);
throw error;
}
},
withdrawFromSubaccount: async ({ withdrawFromSubaccount: async ({
subaccountClient, subaccountClient,
amount, amount,
}: { }: {
subaccountClient: SubaccountClient; subaccountClient: SubaccountClient;
amount: number; amount: number;
}) => await compositeClient?.withdrawFromSubaccount(subaccountClient, amount.toString()), }) => {
try {
return await compositeClient?.withdrawFromSubaccount(
subaccountClient,
amount.toFixed(usdcDecimals)
);
} catch (error) {
log('useSubaccount/withdrawFromSubaccount', error);
throw error;
}
},
transferFromSubaccountToAddress: async ({ transferFromSubaccountToAddress: async ({
subaccountClient, subaccountClient,
assetId = 0, assetId = 0,
@ -99,27 +117,33 @@ export const useSubaccountContext = ({ localDydxWallet }: { localDydxWallet?: Lo
assetId?: number; assetId?: number;
amount: number; amount: number;
recipient: string; recipient: string;
}) => }) => {
await compositeClient?.validatorClient.post.send( try {
subaccountClient?.wallet, return await compositeClient?.validatorClient.post.send(
() => subaccountClient?.wallet,
new Promise((resolve) => { () =>
const msg = new Promise((resolve) => {
compositeClient?.validatorClient.post.composer.composeMsgWithdrawFromSubaccount( const msg =
subaccountClient.address, compositeClient?.validatorClient.post.composer.composeMsgWithdrawFromSubaccount(
subaccountClient.subaccountNumber, subaccountClient.address,
assetId, subaccountClient.subaccountNumber,
Long.fromNumber(amount * QUANTUM_MULTIPLIER), assetId,
recipient Long.fromNumber(amount * QUANTUM_MULTIPLIER),
); recipient
);
resolve([msg]); resolve([msg]);
}), }),
false, false,
undefined, undefined,
undefined, undefined,
Method.BroadcastTxCommit Method.BroadcastTxCommit
), );
} catch (error) {
log('useSubaccount/transferFromSubaccountToAddress', error);
throw error;
}
},
transferNativeToken: async ({ transferNativeToken: async ({
subaccountClient, subaccountClient,
@ -129,24 +153,30 @@ export const useSubaccountContext = ({ localDydxWallet }: { localDydxWallet?: Lo
subaccountClient: SubaccountClient; subaccountClient: SubaccountClient;
amount: number; amount: number;
recipient: string; recipient: string;
}) => }) => {
await compositeClient?.validatorClient.post.send( try {
subaccountClient.wallet, return await compositeClient?.validatorClient.post.send(
() => subaccountClient.wallet,
new Promise((resolve) => { () =>
const msg = compositeClient?.sendTokenMessage( new Promise((resolve) => {
subaccountClient.wallet, const msg = compositeClient?.sendTokenMessage(
amount.toString(), subaccountClient.wallet,
recipient, amount.toString(),
); recipient
);
resolve([msg]); resolve([msg]);
}), }),
false, false,
compositeClient?.validatorClient?.post.defaultDydxGasPrice, compositeClient?.validatorClient?.post.defaultDydxGasPrice,
undefined, undefined,
Method.BroadcastTxCommit Method.BroadcastTxCommit
), );
} catch (error) {
log('useSubaccount/transferNativeToken', error);
throw error;
}
},
sendSquidWithdrawFromSubaccount: async ({ sendSquidWithdrawFromSubaccount: async ({
subaccountClient, subaccountClient,
@ -158,20 +188,27 @@ export const useSubaccountContext = ({ localDydxWallet }: { localDydxWallet?: Lo
payload: string; payload: string;
}) => { }) => {
if (!compositeClient) throw new Error('client not initialized'); if (!compositeClient) throw new Error('client not initialized');
try {
const transaction = JSON.parse(payload);
const transaction = JSON.parse(payload); const msg = compositeClient.withdrawFromSubaccountMessage(
subaccountClient,
amount.toFixed(usdcDecimals)
);
const ibcMsg: EncodeObject = {
typeUrl: transaction.msgTypeUrl,
value: transaction.msg,
};
const msg = compositeClient.withdrawFromSubaccountMessage(subaccountClient, amount.toString()); return await compositeClient.send(
const ibcMsg: EncodeObject = { subaccountClient.wallet,
typeUrl: transaction.msgTypeUrl, () => Promise.resolve([msg, ibcMsg]),
value: transaction.msg, false
}; );
} catch (error) {
return await compositeClient.send( log('useSubaccount/sendSquidWithdrawFromSubaccount', error);
subaccountClient.wallet, throw error;
() => Promise.resolve([msg, ibcMsg]), }
false
);
}, },
}), }),
[compositeClient] [compositeClient]

View File

@ -19,8 +19,10 @@ export const useTokenConfigs = (): {
}, },
}; };
usdcDenom: string; usdcDenom: string;
usdcDecimals: number;
usdcLabel: string; usdcLabel: string;
chainTokenDenom: string; chainTokenDenom: string;
chainTokenDecimals: number;
chainTokenLabel: string; chainTokenLabel: string;
} => { } => {
const { selectedNetwork } = useSelectedNetwork(); const { selectedNetwork } = useSelectedNetwork();
@ -29,8 +31,10 @@ export const useTokenConfigs = (): {
return { return {
tokensConfigs, tokensConfigs,
usdcDenom: tokensConfigs[DydxChainAsset.USDC].denom, usdcDenom: tokensConfigs[DydxChainAsset.USDC].denom,
usdcDecimals: tokensConfigs[DydxChainAsset.USDC].decimals,
usdcLabel: tokensConfigs[DydxChainAsset.USDC].name, usdcLabel: tokensConfigs[DydxChainAsset.USDC].name,
chainTokenDenom: tokensConfigs[DydxChainAsset.CHAINTOKEN].denom, chainTokenDenom: tokensConfigs[DydxChainAsset.CHAINTOKEN].denom,
chainTokenDecimals: tokensConfigs[DydxChainAsset.CHAINTOKEN].decimals,
chainTokenLabel: tokensConfigs[DydxChainAsset.CHAINTOKEN].name, chainTokenLabel: tokensConfigs[DydxChainAsset.CHAINTOKEN].name,
}; };
}; };