Skip to content

Commit

Permalink
Merge pull request #279 from invariant-labs/dev
Browse files Browse the repository at this point in the history
Update staging
  • Loading branch information
wojciech-cichocki authored Aug 15, 2024
2 parents 37315e6 + b469de2 commit 0ebd656
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 26 deletions.
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions src/components/Swap/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,16 @@ export const Swap: React.FC<ISwap> = ({

useEffect(() => {
if (tokenFrom !== null && tokenTo !== null) {
if (inputRef === inputTarget.FROM) {
if (inputRef === inputTarget.FROM && tokens[tokenTo]) {
const amount = getAmountOut(tokens[tokenTo])
setAmountTo(+amount === 0 ? '' : trimLeadingZeros(amount))
} else {
} else if (tokens[tokenFrom]) {
const amount = getAmountOut(tokens[tokenFrom])
setAmountFrom(+amount === 0 ? '' : trimLeadingZeros(amount))
} else if (!tokens[tokenTo]) {
setAmountTo('')
} else if (!tokens[tokenFrom]) {
setAmountFrom('')
}
}
}, [simulateResult])
Expand Down Expand Up @@ -474,11 +478,15 @@ export const Swap: React.FC<ISwap> = ({
<ExchangeAmountInput
value={amountFrom}
balance={
tokenFrom !== null
tokenFrom !== null && tokens[tokenFrom]
? printBigint(tokens[tokenFrom].balance || 0n, tokens[tokenFrom].decimals)
: '- -'
}
decimal={tokenFrom !== null ? tokens[tokenFrom].decimals : DEFAULT_TOKEN_DECIMAL}
decimal={
tokenFrom !== null && tokens[tokenFrom]
? tokens[tokenFrom].decimals
: DEFAULT_TOKEN_DECIMAL
}
className={classes.amountInput}
setValue={value => {
if (value.match(/^\d*\.?\d*$/)) {
Expand Down Expand Up @@ -557,12 +565,14 @@ export const Swap: React.FC<ISwap> = ({
<ExchangeAmountInput
value={amountTo}
balance={
tokenTo !== null
? printBigint(tokens[tokenTo].balance || 0n, tokens[tokenTo].decimals)
tokenTo !== null && tokens[tokenTo]
? printBigint(tokens[tokenTo]?.balance || 0n, tokens[tokenTo]?.decimals)
: '- -'
}
className={classes.amountInput}
decimal={tokenTo !== null ? tokens[tokenTo].decimals : DEFAULT_TOKEN_DECIMAL}
decimal={
tokenTo !== null && tokens[tokenTo] ? tokens[tokenTo].decimals : DEFAULT_TOKEN_DECIMAL
}
setValue={value => {
if (value.match(/^\d*\.?\d*$/)) {
setAmountTo(value)
Expand Down
22 changes: 16 additions & 6 deletions src/containers/WrappedSwap/WrappedSwap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,14 @@ export const WrappedSwap = ({ initialTokenFrom, initialTokenTo }: Props) => {
}, [isFetchingNewPool])

const lastTokenFrom =
tickerToAddress(initialTokenFrom) ||
localStorage.getItem(`INVARIANT_LAST_TOKEN_FROM_${network}`)
tickerToAddress(initialTokenFrom) && initialTokenFrom !== '-'
? tickerToAddress(initialTokenFrom)
: localStorage.getItem(`INVARIANT_LAST_TOKEN_FROM_${network}`)

const lastTokenTo =
tickerToAddress(initialTokenTo) || localStorage.getItem(`INVARIANT_LAST_TOKEN_TO_${network}`)
tickerToAddress(initialTokenTo) && initialTokenTo !== '-'
? tickerToAddress(initialTokenTo)
: localStorage.getItem(`INVARIANT_LAST_TOKEN_TO_${network}`)

const addTokenHandler = async (address: string) => {
const psp22 = SingletonPSP22.getInstance()
Expand Down Expand Up @@ -145,7 +149,7 @@ export const WrappedSwap = ({ initialTokenFrom, initialTokenTo }: Props) => {
return
}

const id = tokensDict[tokenFrom.toString()].coingeckoId ?? ''
const id = tokensDict[tokenFrom.toString()]?.coingeckoId || ''

if (id.length) {
setPriceFromLoading(true)
Expand All @@ -169,7 +173,7 @@ export const WrappedSwap = ({ initialTokenFrom, initialTokenTo }: Props) => {
return
}

const id = tokensDict[tokenTo.toString()].coingeckoId ?? ''
const id = tokensDict[tokenTo.toString()]?.coingeckoId || ''
if (id.length) {
setPriceToLoading(true)
getCoinGeckoTokenPrice(id)
Expand Down Expand Up @@ -291,7 +295,13 @@ export const WrappedSwap = ({ initialTokenFrom, initialTokenTo }: Props) => {
if (tokenTo !== null) {
localStorage.setItem(`INVARIANT_LAST_TOKEN_TO_${network}`, tokenTo.toString())
}
if (tokenFrom !== null && tokenTo !== null && tokenFrom !== tokenTo) {
if (
tokenFrom !== null &&
tokenTo !== null &&
tokenFrom !== tokenTo &&
tokenFrom !== '-' &&
tokenTo !== '-'
) {
dispatch(
poolsActions.getAllPoolsForPairData({
first: tokenFrom,
Expand Down
20 changes: 12 additions & 8 deletions src/store/sagas/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,20 @@ export function* fetchAllPoolKeys(): Generator {
}

export function* fetchAllPoolsForPairData(action: PayloadAction<PairTokens>) {
const invariant = yield* getInvariant()
try {
const invariant = yield* getInvariant()

const token0 = action.payload.first.toString()
const token1 = action.payload.second.toString()
const poolPairs = yield* call([invariant, invariant.getAllPoolsForPair], token0, token1)
const poolsWithPoolKey: PoolWithPoolKey[] = poolPairs.map(([feeTier, pool]) => {
return { poolKey: newPoolKey(token0, token1, feeTier), ...pool }
})
const token0 = action.payload.first.toString()
const token1 = action.payload.second.toString()
const poolPairs = yield* call([invariant, invariant.getAllPoolsForPair], token0, token1)
const poolsWithPoolKey: PoolWithPoolKey[] = poolPairs.map(([feeTier, pool]) => {
return { poolKey: newPoolKey(token0, token1, feeTier), ...pool }
})

yield* put(actions.addPools(poolsWithPoolKey))
yield* put(actions.addPools(poolsWithPoolKey))
} catch (error) {
console.log(error)
}
}

export function* fetchTicksAndTickMaps(action: PayloadAction<FetchTicksAndTickMaps>) {
Expand Down
4 changes: 2 additions & 2 deletions src/store/sagas/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ export function* handleGetBalances(action: PayloadAction<string[]>): Generator {
}

export function* connectHandler(): Generator {
yield takeLatest(actions.connect, handleConnect)
yield takeLeading(actions.connect, handleConnect)
}

export function* disconnectHandler(): Generator {
yield takeLatest(actions.disconnect, handleDisconnect)
yield takeLeading(actions.disconnect, handleDisconnect)
}

export function* airdropSaga(): Generator {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/web3/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export interface ConnectionOptions {
let _adapter: NightlyConnectAdapter | undefined
export const nightlyConnectAdapter = async (
persisted = true,
connectionOptions: ConnectionOptions = {}
connectionOptions: ConnectionOptions = {
initOnConnect: true
}
) => {
if (_adapter) return _adapter
_adapter = await NightlyConnectAdapter.build(
Expand Down

0 comments on commit 0ebd656

Please sign in to comment.