Skip to content

Commit

Permalink
Merge pull request #1580 from balancednetwork/refactor/intents-sdk-co…
Browse files Browse the repository at this point in the history
…nfig

refactor(intents-sdk): make IntentService configurable
  • Loading branch information
0xmilktea authored Jan 7, 2025
2 parents 0a3c118 + abb0a70 commit 472592c
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 87 deletions.
5 changes: 3 additions & 2 deletions apps/hswap/src/app/components/WalletModal/CancelIntent.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';

import { EvmProvider, IntentService, SuiProvider } from '@balancednetwork/intents-sdk';
import { EvmProvider, SuiProvider } from '@balancednetwork/intents-sdk';
import { EvmXService, useCurrentAccount, useCurrentWallet, useSuiClient, useXService } from '@balancednetwork/xwagmi';

import { intentService } from '@/lib/intent';
import { MMTransaction, MMTransactionActions } from '@/store/transactions/useMMTransactionStore';

enum TransactionStatus {
Expand Down Expand Up @@ -32,7 +33,7 @@ function CancelIntent({ transaction }: { transaction: MMTransaction }) {

setStatus(TransactionStatus.Signing);
try {
const result = await IntentService.cancelIntentOrder(
const result = await intentService.cancelIntentOrder(
transaction.orderId,
transaction.fromAmount.currency.xChainId === '0xa4b1.arbitrum' ? 'arb' : 'sui',
transaction.fromAmount.currency.xChainId === '0xa4b1.arbitrum'
Expand Down
24 changes: 11 additions & 13 deletions apps/hswap/src/app/pages/trade/xswap/_components/MMSwapModal.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import React, { useCallback, useEffect, useState } from 'react';

import {
CreateIntentOrderPayload,
EvmProvider,
IntentService,
SolverApiService,
SuiProvider,
} from '@balancednetwork/intents-sdk';
import { CreateIntentOrderPayload, EvmProvider, SolverApiService, SuiProvider } from '@balancednetwork/intents-sdk';
import {
EvmXService,
XToken,
Expand All @@ -27,6 +21,7 @@ import { Modal } from '@/app/components/Modal';
import { useEvmSwitchChain } from '@/hooks/useEvmSwitchChain';
import { MODAL_ID, modalActions, useModalOpen } from '@/hooks/useModalStore';
import useXCallGasChecker from '@/hooks/useXCallGasChecker';
import { intentService, intentServiceConfig } from '@/lib/intent';
import { MMTrade } from '@/store/swap/hooks';
import { Field } from '@/store/swap/reducer';
import {
Expand Down Expand Up @@ -141,7 +136,7 @@ const MMSwapModal = ({
: // @ts-ignore
new SuiProvider({ client: suiClient, wallet: suiWallet, account: suiAccount });

const intentHash = await IntentService.createIntentOrder(order, provider);
const intentHash = await intentService.createIntentOrder(order, provider);

setOrderStatus(IntentOrderStatus.Executing);

Expand All @@ -159,7 +154,7 @@ const MMSwapModal = ({
return;
}

const intentResult = await IntentService.getOrder(
const intentResult = await intentService.getOrder(
intentHash.value,
currencies[Field.INPUT].xChainId === '0xa4b1.arbitrum' ? 'arb' : 'sui',
provider,
Expand All @@ -181,10 +176,13 @@ const MMSwapModal = ({
});
setIntentId(intentHash.value);

const executionResult = await SolverApiService.postExecution({
intent_tx_hash: intentHash.value,
quote_uuid: trade.uuid,
});
const executionResult = await SolverApiService.postExecution(
{
intent_tx_hash: intentHash.value,
quote_uuid: trade.uuid,
},
intentServiceConfig,
);

if (executionResult.ok) {
MMTransactionActions.setTaskId(intentHash.value, executionResult.value.task_id);
Expand Down
7 changes: 7 additions & 0 deletions apps/hswap/src/lib/intent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IntentService, IntentServiceConfig } from '@balancednetwork/intents-sdk';

export const intentServiceConfig: IntentServiceConfig = {
solverApiEndpoint: 'https://solver.iconblockchain.xyz',
};

export const intentService = new IntentService(intentServiceConfig);
4 changes: 2 additions & 2 deletions apps/hswap/src/store/swap/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export function useInitialSwapLoad(): void {
}, [currencies, pair, navigate, firstLoad]);
}

import { IntentService } from '@balancednetwork/intents-sdk';
import { intentService } from '@/lib/intent';
import { useQuery } from '@tanstack/react-query';

export interface MMTrade {
Expand Down Expand Up @@ -435,7 +435,7 @@ export function useMMTrade(inputAmount: CurrencyAmount<XToken> | undefined, outp
return;
}

const res = await IntentService.getQuote({
const res = await intentService.getQuote({
token_src: inputAmount.currency.address,
token_src_blockchain_id: inputAmount.currency.xChainId,
token_dst: outputCurrency.address,
Expand Down
5 changes: 3 additions & 2 deletions apps/hswap/src/store/transactions/useMMTransactionStore.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { useMemo } from 'react';

import { IntentService, IntentStatusCode } from '@balancednetwork/intents-sdk';
import { IntentStatusCode } from '@balancednetwork/intents-sdk';
import { CurrencyAmount } from '@balancednetwork/sdk-core';
import { XToken, jsonStorageOptions } from '@balancednetwork/xwagmi';
import { useQuery } from '@tanstack/react-query';

import { intentService } from '@/lib/intent';
import { create } from 'zustand';
import { createJSONStorage, persist } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
Expand Down Expand Up @@ -162,7 +163,7 @@ export const Updater = () => {
queryFn: async () => {
return Promise.all(
pendingIntents.map(async t => {
const intentStatus = await IntentService.getStatus({ task_id: t.taskId });
const intentStatus = await intentService.getStatus({ task_id: t.taskId });

if (
intentStatus.ok &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, { useMemo } from 'react';

import { EvmProvider, IntentService, SuiProvider } from '@balancednetwork/intents-sdk';
import { EvmProvider, SuiProvider } from '@balancednetwork/intents-sdk';
import { EvmXService, useCurrentAccount, useCurrentWallet, useSuiClient, useXService } from '@balancednetwork/xwagmi';
import { Flex } from 'rebass';
import styled from 'styled-components';

import { UnderlineText } from '@/app/components/DropdownText';
import { Typography } from '@/app/theme';
import { useSignedInWallets } from '@/hooks/useWallets';
import { intentService } from '@/lib/intent';
import { useRatesWithOracle } from '@/queries/reward';
import {
MMTransaction,
Expand Down Expand Up @@ -78,7 +79,7 @@ function PendingIntent({ transaction }: { transaction: MMTransaction }) {

setStatus(TransactionStatus.Signing);
try {
const result = await IntentService.cancelIntentOrder(
const result = await intentService.cancelIntentOrder(
transaction.orderId,
transaction.fromAmount.currency.xChainId === '0xa4b1.arbitrum' ? 'arb' : 'sui',
transaction.fromAmount.currency.xChainId === '0xa4b1.arbitrum'
Expand Down
26 changes: 12 additions & 14 deletions apps/web/src/app/pages/trade/xswap/_components/MMSwapModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo, useCallback, useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';

import { Trans } from '@lingui/macro';
import BigNumber from 'bignumber.js';
Expand All @@ -14,6 +14,7 @@ import TickIcon from '@/assets/icons/tick.svg';
import { useEvmSwitchChain } from '@/hooks/useEvmSwitchChain';
import { MODAL_ID, modalActions, useModalOpen } from '@/hooks/useModalStore';
import useXCallGasChecker from '@/hooks/useXCallGasChecker';
import { intentService, intentServiceConfig } from '@/lib/intent';
import { MMTrade } from '@/store/swap/hooks';
import { Field } from '@/store/swap/reducer';
import {
Expand All @@ -23,13 +24,7 @@ import {
} from '@/store/transactions/useMMTransactionStore';
import { formatBigNumber, shortenAddress } from '@/utils';
import { getNetworkDisplayName } from '@/utils/xTokens';
import {
CreateIntentOrderPayload,
EvmProvider,
IntentService,
SolverApiService,
SuiProvider,
} from '@balancednetwork/intents-sdk';
import { CreateIntentOrderPayload, EvmProvider, SolverApiService, SuiProvider } from '@balancednetwork/intents-sdk';
import {
EvmXService,
XToken,
Expand Down Expand Up @@ -146,7 +141,7 @@ const MMSwapModal = ({
: // @ts-ignore
new SuiProvider({ client: suiClient, wallet: suiWallet, account: suiAccount });

const intentHash = await IntentService.createIntentOrder(order, provider);
const intentHash = await intentService.createIntentOrder(order, provider);

setOrderStatus(IntentOrderStatus.Executing);

Expand All @@ -164,7 +159,7 @@ const MMSwapModal = ({
return;
}

const intentResult = await IntentService.getOrder(
const intentResult = await intentService.getOrder(
intentHash.value,
currencies[Field.INPUT].xChainId === '0xa4b1.arbitrum' ? 'arb' : 'sui',
provider,
Expand All @@ -185,10 +180,13 @@ const MMSwapModal = ({
});
setIntentId(intentHash.value);

const executionResult = await SolverApiService.postExecution({
intent_tx_hash: intentHash.value,
quote_uuid: trade.uuid,
});
const executionResult = await SolverApiService.postExecution(
{
intent_tx_hash: intentHash.value,
quote_uuid: trade.uuid,
},
intentServiceConfig,
);

if (executionResult.ok) {
MMTransactionActions.setTaskId(intentHash.value, executionResult.value.task_id);
Expand Down
7 changes: 7 additions & 0 deletions apps/web/src/lib/intent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IntentService, IntentServiceConfig } from '@balancednetwork/intents-sdk';

export const intentServiceConfig: IntentServiceConfig = {
solverApiEndpoint: 'https://solver.iconblockchain.xyz',
};

export const intentService = new IntentService(intentServiceConfig);
4 changes: 2 additions & 2 deletions apps/web/src/store/swap/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ export function useInitialSwapLoad(): void {
}, [currencies, pair, navigate, firstLoad]);
}

import { IntentService } from '@balancednetwork/intents-sdk';
import { intentService } from '@/lib/intent';
import { useQuery } from '@tanstack/react-query';

export interface MMTrade {
Expand Down Expand Up @@ -403,7 +403,7 @@ export function useMMTrade(inputAmount: CurrencyAmount<XToken> | undefined, outp
return;
}

const res = await IntentService.getQuote({
const res = await intentService.getQuote({
token_src: inputAmount.currency.address,
token_src_blockchain_id: inputAmount.currency.xChainId,
token_dst: outputCurrency.address,
Expand Down
7 changes: 4 additions & 3 deletions apps/web/src/store/transactions/useMMTransactionStore.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { useMemo } from 'react';
import { useMemo } from 'react';

import { IntentService, IntentStatusCode } from '@balancednetwork/intents-sdk';
import { IntentStatusCode } from '@balancednetwork/intents-sdk';
import { CurrencyAmount } from '@balancednetwork/sdk-core';
import { XToken, jsonStorageOptions } from '@balancednetwork/xwagmi';
import { useQuery } from '@tanstack/react-query';

import { intentService } from '@/lib/intent';
import { create } from 'zustand';
import { createJSONStorage, persist } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
Expand Down Expand Up @@ -154,7 +155,7 @@ export const Updater = () => {
queryFn: async () => {
return Promise.all(
pendingIntents.map(async t => {
const intentStatus = await IntentService.getStatus({ task_id: t.taskId });
const intentStatus = await intentService.getStatus({ task_id: t.taskId });

if (
intentStatus.ok &&
Expand Down
Loading

0 comments on commit 472592c

Please sign in to comment.