|
| 1 | +import { assert } from 'chai' |
| 2 | +import { createHash } from 'crypto' |
| 3 | +import { |
| 4 | + EdgeCorePluginOptions, |
| 5 | + EdgeCurrencyWallet, |
| 6 | + EdgeSwapRequest |
| 7 | +} from 'edge-core-js/types' |
| 8 | +import fs from 'fs' |
| 9 | +import { describe, it } from 'mocha' |
| 10 | +import path from 'path' |
| 11 | + |
| 12 | +import { makeHoudiniPlugin } from '../src/swap/central/houdini' |
| 13 | + |
| 14 | +/** |
| 15 | + * Houdini partner-API test. |
| 16 | + * |
| 17 | + * The HARD free-tier rate limits (quote 5/min, exchange 1/min) make repeated |
| 18 | + * live calls impractical, so every API response is cached to |
| 19 | + * `test/houdini/fixtures/` on first fetch and replayed afterwards. CI runs |
| 20 | + * entirely from fixtures with no credentials or network. |
| 21 | + * |
| 22 | + * To refresh fixtures against the live API: |
| 23 | + * HOUDINI_LIVE=1 HOUDINI_API_KEY=... HOUDINI_API_SECRET=... \ |
| 24 | + * yarn test --grep houdini |
| 25 | + * (delete `test/houdini/fixtures/` first for a clean capture). |
| 26 | + */ |
| 27 | + |
| 28 | +const FIXTURE_DIR = path.join(__dirname, 'houdini', 'fixtures') |
| 29 | +const LIVE = process.env.HOUDINI_LIVE === '1' |
| 30 | + |
| 31 | +const cacheKey = (method: string, url: string, body: string): string => { |
| 32 | + const slug = `${method}_${url |
| 33 | + .replace('https://api-partner.houdiniswap.com/v2/', '') |
| 34 | + .replace(/[^a-z0-9]+/gi, '_')}`.slice(0, 80) |
| 35 | + const hash = createHash('sha1') |
| 36 | + .update(`${method} ${url} ${body}`) |
| 37 | + .digest('hex') |
| 38 | + .slice(0, 10) |
| 39 | + return `${slug}__${hash}.json` |
| 40 | +} |
| 41 | + |
| 42 | +interface CachedResponse { |
| 43 | + status: number |
| 44 | + body: string |
| 45 | +} |
| 46 | + |
| 47 | +const makeResponse = (cached: CachedResponse): any => ({ |
| 48 | + ok: cached.status >= 200 && cached.status < 300, |
| 49 | + status: cached.status, |
| 50 | + text: async (): Promise<string> => cached.body, |
| 51 | + json: async (): Promise<unknown> => JSON.parse(cached.body) |
| 52 | +}) |
| 53 | + |
| 54 | +interface FetchOpts { |
| 55 | + method?: string |
| 56 | + body?: string |
| 57 | + headers?: { [key: string]: string } |
| 58 | +} |
| 59 | + |
| 60 | +/** A `fetch` that replays cached fixtures and only hits the network on refresh. */ |
| 61 | +const cachingFetch = async ( |
| 62 | + url: string, |
| 63 | + opts: FetchOpts = {} |
| 64 | +): Promise<unknown> => { |
| 65 | + const method = opts.method ?? 'GET' |
| 66 | + const body = opts.body ?? '' |
| 67 | + const file = path.join(FIXTURE_DIR, cacheKey(method, url, body)) |
| 68 | + |
| 69 | + if (fs.existsSync(file)) { |
| 70 | + const cached: CachedResponse = JSON.parse(fs.readFileSync(file, 'utf8')) |
| 71 | + return makeResponse(cached) |
| 72 | + } |
| 73 | + |
| 74 | + if (!LIVE) { |
| 75 | + throw new Error( |
| 76 | + `Houdini fixture missing for ${method} ${url}. Re-run with HOUDINI_LIVE=1 and credentials to capture it.` |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + const response = await fetch(url, opts) |
| 81 | + const text = await response.text() |
| 82 | + const cached: CachedResponse = { status: response.status, body: text } |
| 83 | + // Only persist successful responses so a transient rate-limit (429) or server |
| 84 | + // error never poisons the replay cache. |
| 85 | + if (response.ok) { |
| 86 | + fs.mkdirSync(FIXTURE_DIR, { recursive: true }) |
| 87 | + fs.writeFileSync(file, JSON.stringify(cached, null, 2)) |
| 88 | + } |
| 89 | + return makeResponse(cached) |
| 90 | +} |
| 91 | + |
| 92 | +interface MockToken { |
| 93 | + currencyCode: string |
| 94 | + multiplier: string |
| 95 | + contractAddress: string |
| 96 | +} |
| 97 | + |
| 98 | +interface MockWalletParams { |
| 99 | + pluginId: string |
| 100 | + currencyCode: string |
| 101 | + multiplier: string |
| 102 | + address: string |
| 103 | + tokens?: { [tokenId: string]: MockToken } |
| 104 | +} |
| 105 | + |
| 106 | +const makeMockWallet = (params: MockWalletParams): EdgeCurrencyWallet => { |
| 107 | + const { pluginId, currencyCode, multiplier, address, tokens = {} } = params |
| 108 | + |
| 109 | + const allTokens: { [tokenId: string]: unknown } = {} |
| 110 | + for (const tokenId of Object.keys(tokens)) { |
| 111 | + const token = tokens[tokenId] |
| 112 | + allTokens[tokenId] = { |
| 113 | + currencyCode: token.currencyCode, |
| 114 | + denominations: [ |
| 115 | + { name: token.currencyCode, multiplier: token.multiplier } |
| 116 | + ], |
| 117 | + networkLocation: { contractAddress: token.contractAddress } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + const wallet = { |
| 122 | + id: `${pluginId}-wallet`, |
| 123 | + currencyInfo: { |
| 124 | + pluginId, |
| 125 | + currencyCode, |
| 126 | + denominations: [{ name: currencyCode, multiplier }] |
| 127 | + }, |
| 128 | + currencyConfig: { allTokens }, |
| 129 | + getAddresses: async (): Promise< |
| 130 | + Array<{ addressType: string; publicAddress: string }> |
| 131 | + > => [{ addressType: 'publicAddress', publicAddress: address }], |
| 132 | + // Echo the spendInfo back so makeSwapPluginQuote can read the swap action. |
| 133 | + makeSpend: async (spendInfo: any): Promise<any> => ({ |
| 134 | + networkFee: '0', |
| 135 | + tokenId: spendInfo.tokenId, |
| 136 | + spendTargets: spendInfo.spendTargets, |
| 137 | + savedAction: spendInfo.savedAction, |
| 138 | + assetAction: spendInfo.assetAction |
| 139 | + }) |
| 140 | + } |
| 141 | + return (wallet as unknown) as EdgeCurrencyWallet |
| 142 | +} |
| 143 | + |
| 144 | +const BTC_ADDRESS = 'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq' |
| 145 | +const ETH_ADDRESS = '0x9f1f9a5c0f1d9a5c0f1d9a5c0f1d9a5c0f1d9a5c' |
| 146 | +const USDC_TOKEN_ID = 'a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' |
| 147 | + |
| 148 | +const btcWallet = makeMockWallet({ |
| 149 | + pluginId: 'bitcoin', |
| 150 | + currencyCode: 'BTC', |
| 151 | + multiplier: '100000000', |
| 152 | + address: BTC_ADDRESS |
| 153 | +}) |
| 154 | + |
| 155 | +const ethWallet = makeMockWallet({ |
| 156 | + pluginId: 'ethereum', |
| 157 | + currencyCode: 'ETH', |
| 158 | + multiplier: '1000000000000000000', |
| 159 | + address: ETH_ADDRESS, |
| 160 | + tokens: { |
| 161 | + [USDC_TOKEN_ID]: { |
| 162 | + currencyCode: 'USDC', |
| 163 | + multiplier: '1000000', |
| 164 | + contractAddress: '0xA0b86991c6218b36C1d19D4a2e9Eb0cE3606eB48' |
| 165 | + } |
| 166 | + } |
| 167 | +}) |
| 168 | + |
| 169 | +const makePlugin = (): ReturnType<typeof makeHoudiniPlugin> => { |
| 170 | + const opts = { |
| 171 | + initOptions: { |
| 172 | + apiKey: process.env.HOUDINI_API_KEY ?? '', |
| 173 | + apiSecret: process.env.HOUDINI_API_SECRET ?? '' |
| 174 | + }, |
| 175 | + io: { fetch: cachingFetch }, |
| 176 | + log: Object.assign(() => undefined, { |
| 177 | + warn: () => undefined, |
| 178 | + error: () => undefined |
| 179 | + }) |
| 180 | + } |
| 181 | + return makeHoudiniPlugin((opts as unknown) as EdgeCorePluginOptions) |
| 182 | +} |
| 183 | + |
| 184 | +describe('houdini swap quote + exchange', function () { |
| 185 | + it('BTC -> ETH (private)', async function () { |
| 186 | + this.timeout(60000) // Live capture is slow; replay from fixtures is instant. |
| 187 | + const plugin = makePlugin() |
| 188 | + const request: EdgeSwapRequest = { |
| 189 | + fromWallet: btcWallet, |
| 190 | + toWallet: ethWallet, |
| 191 | + fromTokenId: null, |
| 192 | + toTokenId: null, |
| 193 | + nativeAmount: '1000000', // 0.01 BTC |
| 194 | + quoteFor: 'from' |
| 195 | + } |
| 196 | + |
| 197 | + const quote = await plugin.fetchSwapQuote(request, undefined, { |
| 198 | + infoPayload: {} |
| 199 | + }) |
| 200 | + |
| 201 | + assert.equal(quote.pluginId, 'houdini') |
| 202 | + assert.isTrue( |
| 203 | + quote.toNativeAmount !== '0' && quote.toNativeAmount.length > 0 |
| 204 | + ) |
| 205 | + assert.equal(quote.fromNativeAmount, '1000000') |
| 206 | + }) |
| 207 | + |
| 208 | + it('ETH -> USDC (private)', async function () { |
| 209 | + this.timeout(60000) // Live capture is slow; replay from fixtures is instant. |
| 210 | + const plugin = makePlugin() |
| 211 | + const request: EdgeSwapRequest = { |
| 212 | + fromWallet: ethWallet, |
| 213 | + toWallet: ethWallet, |
| 214 | + fromTokenId: null, |
| 215 | + toTokenId: USDC_TOKEN_ID, |
| 216 | + nativeAmount: '50000000000000000', // 0.05 ETH |
| 217 | + quoteFor: 'from' |
| 218 | + } |
| 219 | + |
| 220 | + const quote = await plugin.fetchSwapQuote(request, undefined, { |
| 221 | + infoPayload: {} |
| 222 | + }) |
| 223 | + |
| 224 | + assert.equal(quote.pluginId, 'houdini') |
| 225 | + assert.isTrue( |
| 226 | + quote.toNativeAmount !== '0' && quote.toNativeAmount.length > 0 |
| 227 | + ) |
| 228 | + assert.equal(quote.fromNativeAmount, '50000000000000000') |
| 229 | + }) |
| 230 | +}) |
0 commit comments