Skip to content

Commit 97232e7

Browse files
fix: denominate nam before ibc withdraw (#2183)
1 parent 095c340 commit 97232e7

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

apps/namadillo/src/atoms/transfer/services.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ export type WorkerTransferParams = {
4242

4343
const workerBuildTxPair = async <T>({
4444
rpcUrl,
45-
token,
45+
nativeToken,
4646
buildTxFn,
4747
}: {
4848
rpcUrl: string;
49-
token: Address;
49+
nativeToken: Address;
5050
buildTxFn: (
5151
workerLink: Comlink.Remote<MaspTxWorkerApi>
5252
) => Promise<EncodedTxData<T>>;
@@ -56,7 +56,7 @@ const workerBuildTxPair = async <T>({
5656
const workerLink = Comlink.wrap<MaspTxWorkerApi>(worker);
5757
await workerLink.init({
5858
type: "init",
59-
payload: { rpcUrl, token, maspIndexerUrl: "" },
59+
payload: { rpcUrl, token: nativeToken, maspIndexerUrl: "" },
6060
});
6161
const encodedTxData = await buildTxFn(workerLink);
6262
worker.terminate();
@@ -133,7 +133,7 @@ export const createShieldedTransferTx = async (
133133

134134
return await workerBuildTxPair({
135135
rpcUrl,
136-
token,
136+
nativeToken: chain.nativeTokenAddress,
137137
buildTxFn: async (workerLink) => {
138138
const msgValue = new ShieldedTransferMsgValue({
139139
gasSpendingKey: source,
@@ -185,7 +185,7 @@ export const createShieldingTransferTx = async (
185185

186186
return await workerBuildTxPair({
187187
rpcUrl,
188-
token,
188+
nativeToken: chain.nativeTokenAddress,
189189
buildTxFn: async (workerLink) => {
190190
const publicKeyRevealed = await isPublicKeyRevealed(account.address);
191191
const msgValue = new ShieldingTransferMsgValue({
@@ -239,7 +239,7 @@ export const createUnshieldingTransferTx = async (
239239

240240
return await workerBuildTxPair({
241241
rpcUrl,
242-
token,
242+
nativeToken: chain.nativeTokenAddress,
243243
buildTxFn: async (workerLink) => {
244244
const msgValue = new UnshieldingTransferMsgValue({
245245
source,
@@ -284,7 +284,7 @@ export const createIbcTx = async (
284284

285285
return await workerBuildTxPair({
286286
rpcUrl,
287-
token: props[0].token,
287+
nativeToken: chain.nativeTokenAddress,
288288
buildTxFn: async (workerLink) => {
289289
const msgValue = new IbcTransferMsgValue({
290290
...props[0],

packages/shared/lib/src/sdk/args.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,7 @@ impl IbcTransferMsg {
860860
pub fn ibc_transfer_tx_args(
861861
ibc_transfer_msg: &[u8],
862862
tx_msg: &[u8],
863+
native_token: Address,
863864
) -> Result<(args::TxIbcTransfer, Option<StoredBuildParams>), JsError> {
864865
let ibc_transfer_msg = IbcTransferMsg::try_from_slice(ibc_transfer_msg)?;
865866
let IbcTransferMsg {
@@ -887,11 +888,21 @@ pub fn ibc_transfer_tx_args(
887888
}?;
888889

889890
let token = Address::from_str(&token)?;
890-
let amount = Amount::from_str(&amount_in_base_denom, 0u8).expect("Amount to be valid.");
891+
892+
// As the value we get is always in the base denom, we can use from_string_precise to get the
893+
// amount and drop denom info
894+
let amount = Amount::from_string_precise(&amount_in_base_denom).expect("Amount to be valid.");
895+
896+
let denominated_amount = if token == native_token {
897+
DenominatedAmount::native(amount)
898+
} else {
899+
DenominatedAmount::new(amount, 0u8.into())
900+
};
901+
891902
// Using InputAmount::Validated because the amount is already in the base
892903
// denom. If Unvalidated is used, the SDK will change the denom based on the
893904
// token address, which complicates knowing which amount to pass to this function.
894-
let amount = InputAmount::Validated(amount.into());
905+
let amount = InputAmount::Validated(denominated_amount);
895906
let port_id = PortId::from_str(&port_id).expect("Port id to be valid");
896907
let channel_id = ChannelId::from_str(&channel_id).expect("Channel id to be valid");
897908
let ibc_shielding_data = match shielding_data {

packages/shared/lib/src/sdk/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ impl Sdk {
752752
ibc_transfer_msg: &[u8],
753753
wrapper_tx_msg: &[u8],
754754
) -> Result<JsValue, JsError> {
755-
let (args, bparams) = args::ibc_transfer_tx_args(ibc_transfer_msg, wrapper_tx_msg)?;
755+
let (args, bparams) = args::ibc_transfer_tx_args(ibc_transfer_msg, wrapper_tx_msg, self.namada.native_token())?;
756756

757757
let bparams = if let Some(bparams) = bparams {
758758
BuildParams::StoredBuildParams(bparams)

0 commit comments

Comments
 (0)