Skip to content

Commit

Permalink
Merge branch 'stage' into jose/test-one-click-trading
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddyMc committed Jun 6, 2024
2 parents 96ca580 + 994085f commit d1e2cff
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 31 deletions.
51 changes: 51 additions & 0 deletions packages/tx/src/__tests__/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ describe("getSumTotalSpenderCoinsSpent", () => {

expect(coins).toEqual([]);
});

it("should only total the amounts for the specified sender", () => {
const coins = getSumTotalSpenderCoinsSpent(
"osmo1gtgx92pxk6hvhc3c3g0xlkrwqq6knymu0e0caw",
mockMultipleEvents
);

// Expected result is the sum of the amounts for the specified sender, not for osmo13vhcd3xllpvz8tql4dzp8yszxeas8zxpzptyvjttdy7m64kuyz5sv6caqq
const expectedResult = [
{
denom:
"factory/osmo1z0qrq605sjgcqpylfl4aa6s90x738j7m58wyatt0tdzflg2ha26q67k743/wbtc",
amount: "58573",
},
{
denom:
"ibc/EA1D43981D5C9A1C4AAEA9C23BB1D4FA126BA9BC7020A25E0AE4AA841EA25DC5",
amount: "21083680327000100",
},
{
denom:
"ibc/F4A070A6D78496D53127EA85C094A9EC87DFC1F36071B8CCDDBD020F933D213D",
amount: "131211643845355500",
},
{ denom: "uosmo", amount: "95799380" },
];

expect(coins).toEqual(expectedResult);
});
});

describe("matchRawCoinValue", () => {
Expand Down Expand Up @@ -225,4 +254,26 @@ const mockMultipleEvents = [
},
],
},
{
type: "coin_spent",
attributes: [
{
key: "spender",
value:
"osmo13vhcd3xllpvz8tql4dzp8yszxeas8zxpzptyvjttdy7m64kuyz5sv6caqq",
index: true,
},
{
key: "amount",
value:
"2605ibc/498A0751C798A0D9A389AA3691123DADA57DAA4FE165D5C75894505B876BA6E4",
index: true,
},
{
key: "msg_index",
value: "0",
index: true,
},
],
},
];
3 changes: 2 additions & 1 deletion packages/tx/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export function getSumTotalSpenderCoinsSpent(
if (type !== "coin_spent") return;
if (attributes.length === 0) return;
const spendAttribute = attributes.find((attr) => attr.key === "spender");
if (spendAttribute && spendAttribute.value !== spenderBech32Address) return;
if (!spendAttribute || spendAttribute.value !== spenderBech32Address)
return;

// a comma separated list of coins spent
const coinsSpentRawAttribute = attributes.find(
Expand Down
12 changes: 9 additions & 3 deletions packages/tx/src/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,15 @@ export async function getGasFeeAmount({
bech32Address,
}),
]);
const feeBalances = balances.filter((balance) =>
chainFeeDenoms.includes(balance.denom)
);
const feeBalances: { denom: string; amount: string }[] = [];

// iterate in order of fee denoms
for (const denom of chainFeeDenoms) {
const balance = balances.find((balance) => balance.denom === denom);
if (balance) {
feeBalances.push(balance);
}
}

if (!feeBalances.length) {
throw new InsufficientFeeError(
Expand Down
9 changes: 3 additions & 6 deletions packages/web/components/swap-tool/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,10 @@ export const SwapTool: FunctionComponent<SwapToolProps> = observer(
});
}
})
.catch((error) => {
console.error("swap failed", error);
if (error instanceof Error && error.message === "Request rejected") {
// don't log when the user rejects in wallet
return;
.catch((e) => {
if (e instanceof Error && e.message.includes("Failed to send")) {
logEvent([EventName.Swap.swapFailed, baseEvent]);
}
logEvent([EventName.Swap.swapFailed, baseEvent]);
})
.finally(() => {
setIsSendingTx(false);
Expand Down
4 changes: 4 additions & 0 deletions packages/web/config/ibc-overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,10 @@ const MainnetIBCAdditionalData: Partial<
depositUrlOverride: "https://app.picasso.network/?from=SOLANA&to=OSMOSIS",
withdrawUrlOverride: "https://app.picasso.network/?from=OSMOSIS&to=SOLANA",
},
WHINE: {
depositUrlOverride: "https://app.picasso.network/?from=SOLANA&to=OSMOSIS",
withdrawUrlOverride: "https://app.picasso.network/?from=OSMOSIS&to=SOLANA",
},
};

export const IBCAdditionalData: AdditionalData = IS_TESTNET
Expand Down
57 changes: 36 additions & 21 deletions packages/web/hooks/use-swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ export function useSwap(
// includes check for quoteQueryEnabled
!isQuoteLoading &&
Boolean(quote) &&
Boolean(account?.address);
Boolean(account?.address) &&
inAmountInput.debouncedInAmount !== null &&
inAmountInput.balance &&
inAmountInput.debouncedInAmount.toDec().lte(inAmountInput.balance.toDec());
const {
data: networkFee,
error: networkFeeError,
Expand Down Expand Up @@ -269,14 +272,18 @@ export function useSwap(
() =>
new Promise<"multiroute" | "multihop" | "exact-in">(
async (resolve, reject) => {
if (!maxSlippage) return reject("No max slippage");
if (!inAmountInput.amount) return reject("No input amount");
if (!account) return reject("No account");
if (!swapAssets.fromAsset) return reject("No from asset");
if (!swapAssets.toAsset) return reject("No to asset");
if (!maxSlippage)
return reject(new Error("Max slippage is not defined."));
if (!inAmountInput.amount)
return reject(new Error("Input amount is not specified."));
if (!account)
return reject(new Error("Account information is missing."));
if (!swapAssets.fromAsset)
return reject(new Error("From asset is not specified."));
if (!swapAssets.toAsset)
return reject(new Error("To asset is not specified."));

let txParams: ReturnType<typeof getSwapTxParameters>;

try {
txParams = getSwapTxParameters({
coinAmount: inAmountInput.amount.toCoin().amount,
Expand All @@ -287,7 +294,9 @@ export function useSwap(
});
} catch (e) {
const error = e as Error;
return reject(error.message);
return reject(
new Error(`Transaction preparation failed: ${error.message}`)
);
}

const { routes, tokenIn, tokenOutMinAmount } = txParams;
Expand Down Expand Up @@ -344,7 +353,7 @@ export function useSwap(
);
});
} catch (e) {
return reject("Rejected manual approval");
return reject(new Error("Rejected manual approval"));
}
}

Expand Down Expand Up @@ -373,14 +382,15 @@ export function useSwap(
tokenOutMinAmount,
undefined,
signOptions,
() => {
resolve(pools.length === 1 ? "exact-in" : "multihop");
({ code }) => {
if (code)
reject(
new Error("Failed to send swap exact amount in message")
);
else resolve(pools.length === 1 ? "exact-in" : "multihop");
}
)
.catch((reason) => {
reject(reason);
});
return pools.length === 1 ? "exact-in" : "multihop";
.catch(reject);
} else if (routes.length > 1) {
account.osmosis
.sendSplitRouteSwapExactAmountInMsg(
Expand All @@ -389,15 +399,20 @@ export function useSwap(
tokenOutMinAmount,
undefined,
signOptions,
() => {
resolve("multiroute");
({ code }) => {
if (code)
reject(
new Error(
"Failed to send split route swap exact amount in message"
)
);
else resolve("multiroute");
}
)
.catch((reason) => {
reject(reason);
});
.catch(reject);
} else {
reject("No routes given");
// should not be possible because button should be disabled
reject(new Error("No routes given"));
}
}
).finally(() => {
Expand Down

0 comments on commit d1e2cff

Please sign in to comment.