Skip to content

Commit 42d3f69

Browse files
committed
wip
1 parent 0eb481b commit 42d3f69

File tree

6 files changed

+23
-28
lines changed

6 files changed

+23
-28
lines changed

src/app.config.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { zeroAddress } from "viem"
2-
31
export const apiConfig = {
42
tokensUrl: 'https://tokens.coingecko.com/uniswap/all.json',
53
// tokensUrl: 'https://erc20.cmc.eth.link/',
@@ -17,8 +15,7 @@ export const notificarionConfig = {
1715
duration: 5000,
1816
}
1917

20-
export const ethTokenConfig = {
21-
address: zeroAddress,
18+
export const etherToken = {
2219
name: 'Ether',
2320
symbol: 'ETH',
2421
decimals: 18,

src/components/Swap/Swap.svelte

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,19 @@
7979
8080
console.log('Quote data:', quotaData)
8181
82-
// Approve the 0x contract to spend the token
83-
const txHash = await approveAllowance({
84-
account: $account.address,
85-
address: tokenFrom.address,
86-
spender: quotaData.allowanceTarget, // contains the address of the 0x contract
87-
amount: amountFrom,
88-
chainId: $network.id,
89-
})
90-
91-
console.log('Approve tx hash:', txHash)
82+
// If token has address then it's an ERC20 token
83+
if (tokenFrom.address) {
84+
// Approve the 0x contract to spend the token
85+
const txHash = await approveAllowance({
86+
account: $account.address,
87+
address: tokenFrom.address,
88+
spender: quotaData.allowanceTarget, // contains the address of the 0x contract
89+
amount: amountFrom,
90+
chainId: $network.id,
91+
})
92+
93+
console.log('Approve tx hash:', txHash)
94+
}
9295
} catch (err) {
9396
console.error(err)
9497
errorToast('There was an error trading the tokens.')

src/components/Swap/TokenSelector.svelte

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import fetchAllTokens from '../../libs/token/fetchAllTokens'
1010
import type { Token } from '../../libs/token/types'
1111
import Loading from '../Loading'
12-
import { ethTokenConfig, inputConfig, tokenSelectorConfig } from '../../app.config'
12+
import { etherToken, inputConfig, tokenSelectorConfig } from '../../app.config'
1313
import { mainnet } from 'viem/chains'
1414
1515
export let value: Token
@@ -81,12 +81,8 @@
8181
8282
// We want to add ETH in our list of tokens as the first token
8383
tokens.unshift({
84+
...etherToken,
8485
chainId: mainnet.id, // TODO: other chains?
85-
address: ethTokenConfig.address,
86-
name: ethTokenConfig.name,
87-
symbol: ethTokenConfig.symbol,
88-
decimals: ethTokenConfig.decimals,
89-
logoURI: ethTokenConfig.logoURI,
9086
})
9187
9288
// Creates a map to quickly look up tokens based
@@ -95,7 +91,7 @@
9591
indexTokenMap = tokens.reduce((acc, token) => {
9692
const name = token.name.toLowerCase()
9793
const symbol = token.symbol.toLowerCase()
98-
const address = token.address.toLowerCase()
94+
const address = token?.address?.toLowerCase()
9995
const index = `${name}|${symbol}|${address}`
10096
10197
acc[index] = token

src/libs/api/0x.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { zeroAddress } from 'viem'
21
import type { GetPriceArgs, GetQuoteArgs, OxApiArgs, PriceResponseData, QuoteResponseData } from './types'
32

43
async function OxApi({ apiCall, sellToken, buyToken, sellAmount, chainId }: OxApiArgs) {
@@ -7,9 +6,9 @@ async function OxApi({ apiCall, sellToken, buyToken, sellAmount, chainId }: OxAp
76
const queryParams = new URLSearchParams({
87
apiCall,
98

10-
// For Ether, which uses the zero address, we use the symbol instead of the address
11-
sellToken: sellToken === zeroAddress ? 'ETH' : sellToken,
12-
9+
// If no sellAddress is passed, we asume it's ETH
10+
sellToken: sellToken ?? 'ETH',
11+
1312
buyToken,
1413
sellAmount,
1514
chainId,

src/libs/api/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import type { Address } from 'viem'
22
import type { Token } from '../token/types'
33

44
export interface BaseArgs {
5-
sellToken: Address
6-
buyToken: string
75
sellAmount: string
86
chainId: string
7+
sellToken?: Address
8+
buyToken?: string
99
slippagePercentage?: string
1010
}
1111

src/libs/token/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { Address } from 'viem'
22

33
export type Token = {
44
chainId: number
5-
address: Address
65
name: string
76
symbol: string
87
decimals: number
98
logoURI: string
9+
address?: Address
1010
}

0 commit comments

Comments
 (0)