Skip to content

Commit d4e55c1

Browse files
committed
swaps wip
1 parent 9fb6f3e commit d4e55c1

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

apps/namadillo/src/App/Ibc/OsmosisSwap.tsx

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Asset } from "@chain-registry/types";
1+
import { Asset, IBCTrace } from "@chain-registry/types";
22
import { Stack } from "@namada/components";
33
import { AccountType, BparamsMsgValue } from "@namada/types";
44
import { allDefaultAccountsAtom } from "atoms/accounts";
55
import { namadaShieldedAssetsAtom } from "atoms/balance";
66
import { chainParametersAtom } from "atoms/chain";
7-
import { findAssetsByChainById, ibcChannelsFamily } from "atoms/integrations";
7+
import { findAssetsByChainId, ibcChannelsFamily } from "atoms/integrations";
88
import { SwapResponse, SwapResponseError, SwapResponseOk } from "atoms/swaps";
99
import { createOsmosisSwapTxAtom } from "atoms/transfer/atoms";
1010
import BigNumber from "bignumber.js";
@@ -31,14 +31,16 @@ export const OsmosisSwap: React.FC = () => {
3131
const chainParameters = useAtomValue(chainParametersAtom);
3232
const namadaAssets =
3333
chainParameters.data ?
34-
findAssetsByChainById(chainParameters.data.chainId)
34+
findAssetsByChainId(chainParameters.data.chainId)
3535
: [];
36+
const osmosisAssets =
37+
chainParameters.data ? findAssetsByChainId("osmosis-1") : [];
3638

3739
const [from, setFrom] = useState<AddressWithAssetAndAmount | undefined>();
3840
const [to, setTo] = useState<Asset | undefined>();
3941
const [amount, setAmount] = useState<string>("");
4042
const [recipient, setRecipient] = useState<string>(
41-
"znam17k7jw0wmvzdzmfm46m8600t9cah5mjl6se75cu9jvwxywk75k3kmxehmxk7wha62l35puzl6srd"
43+
"znam17drxewzvge966gzcl0u6tr4j90traepujm2vd8ptwwkgrftnhs2hdtnyzgl5freyjsdnchn4ddy"
4244
);
4345
const [localRecoveryAddr, setLocalRecoveryAddress] = useState<string>(
4446
"osmo18st0wqx84av8y6xdlss9d6m2nepyqwj6n3q7js"
@@ -53,11 +55,24 @@ export const OsmosisSwap: React.FC = () => {
5355

5456
useEffect(() => {
5557
const call = async (): Promise<void> => {
58+
invariant(from, "No from asset selected");
59+
invariant(to, "No to asset selected");
60+
// We have to map namada assets to osmosis assets to get correct base
61+
const fromOsmosis = osmosisAssets.find(
62+
(assets) => assets.symbol === from.asset.symbol
63+
);
64+
const toOsmosis = osmosisAssets.find(
65+
(assets) => assets.symbol === to.symbol
66+
);
67+
68+
invariant(fromOsmosis, "From asset is not found in Osmosis assets");
69+
invariant(toOsmosis, "To asset is not found in Osmosis assets");
70+
5671
const quote = await fetch(
5772
"https://sqs.osmosis.zone/router/quote?" +
5873
new URLSearchParams({
59-
tokenIn: `${amount}${from!.asset.base}`,
60-
tokenOutDenom: to!.base,
74+
tokenIn: `${amount}${fromOsmosis.base}`,
75+
tokenOutDenom: toOsmosis.base,
6176
humanDenoms: "false",
6277
}).toString()
6378
);
@@ -90,11 +105,21 @@ export const OsmosisSwap: React.FC = () => {
90105
invariant(transparentAccount, "No transparent account is found");
91106
invariant(shieldedAccount, "No shielded account is found");
92107
invariant(from, "No from asset");
108+
invariant(to, "No to asset");
93109
invariant(ibcChannels, "No ibc channels");
94110
invariant(quote, "No quote");
95111
invariant(localRecoveryAddr, "No local recovery address");
96112
invariant(recipient, "No recipient");
97113

114+
const toTrace = to.traces?.find((t): t is IBCTrace => t.type === "ibc")
115+
?.chain.path;
116+
invariant(toTrace, "No IBC trace found for the to asset");
117+
invariant(quote.route[0], "No route found in the quote");
118+
const route = quote.route[0].pools.map((p) => ({
119+
poolId: String(p.id),
120+
tokenOutDenom: p.token_out_denom,
121+
}));
122+
98123
let bparams: BparamsMsgValue[] | undefined;
99124
if (transparentAccount.type === AccountType.Ledger) {
100125
const sdk = await getSdkInstance();
@@ -106,7 +131,7 @@ export const OsmosisSwap: React.FC = () => {
106131
const transfer = {
107132
amountInBaseDenom: BigNumber(amount),
108133
// osmosis channel
109-
channelId: "channel-13",
134+
channelId: "channel-7",
110135
portId: "transfer",
111136
token: from.originalAddress,
112137
source: shieldedAccount.pseudoExtendedKey!,
@@ -118,7 +143,7 @@ export const OsmosisSwap: React.FC = () => {
118143
};
119144
const params = {
120145
transfer,
121-
outputDenom: "TODO",
146+
outputDenom: toTrace,
122147
recipient,
123148
// TODO: this should also be disposable address most likely
124149
overflow: transparentAccount.address,
@@ -128,6 +153,7 @@ export const OsmosisSwap: React.FC = () => {
128153
.toString(),
129154
},
130155
localRecoveryAddr,
156+
route,
131157
// TODO: not sure if hardcoding is ok, maybe we should connect keplr wallet
132158
osmosisRestRpc: "https://osmosis-rest.publicnode.com",
133159
};
@@ -149,7 +175,8 @@ export const OsmosisSwap: React.FC = () => {
149175
encodedTxData,
150176
transparentAccount.address!
151177
);
152-
await broadcastTransaction(encodedTxData, signedTxs);
178+
const wwww = await broadcastTransaction(encodedTxData, signedTxs);
179+
console.log("Transaction broadcasted:", wwww);
153180
alert("Transaction sent 🚀");
154181
} catch (error) {
155182
console.error("Error performing Osmosis swap:", error);
@@ -166,8 +193,8 @@ export const OsmosisSwap: React.FC = () => {
166193
onChange={(e) => setFrom(availableAssets?.[e.target.value])}
167194
>
168195
<option value=""></option>
169-
{Object.values(availableAssets || {}).map((al) => (
170-
<option key={al.asset.base} value={al.originalAddress}>
196+
{Object.values(availableAssets || {}).map((al, idx) => (
197+
<option key={`${al.asset.base}_${idx}`} value={al.originalAddress}>
171198
{al.asset.symbol}
172199
</option>
173200
))}

apps/namadillo/src/App/Sidebars/MaspAssetRewards.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ export const MaspAssetRewards = (): JSX.Element => {
5050
)}
5151
<ul className="flex flex-col gap-1">
5252
{rewards.data &&
53-
rewards.data.map((reward) => (
54-
<li key={reward.asset.base}>
53+
rewards.data.map((reward, idx) => (
54+
<li key={`${reward.asset.base}_${idx}`}>
5555
<MaspAssetRewardsItem
5656
asset={reward.asset}
5757
percentage={reward.maxRewardRate}

apps/namadillo/src/atoms/integrations/functions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ const findOriginalAsset = async (
250250
};
251251
};
252252

253-
export const findAssetsByChainById = (chainId: string): Asset[] => {
253+
export const findAssetsByChainId = (chainId: string): Asset[] => {
254254
const chain = findChainById(chainId);
255255
if (!chain) {
256256
throw new Error(`Chain with ID ${chainId} not found`);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ impl SigningData {
100100
None => None,
101101
};
102102

103-
let mut public_keys = IndexSet::new();
103+
let mut public_keys = vec![];
104104
for pk in self.public_keys.clone() {
105105
let pk = PublicKey::from_str(&pk)?;
106-
public_keys.insert(pk);
106+
public_keys.push(pk);
107107
}
108108

109109
let fee_payer = PublicKey::from_str(&self.fee_payer)?;

0 commit comments

Comments
 (0)