-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathspookySwap.ts
More file actions
158 lines (142 loc) · 4.58 KB
/
spookySwap.ts
File metadata and controls
158 lines (142 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { asObject, asOptional, asString } from 'cleaners'
import {
EdgeCorePluginOptions,
EdgeSpendInfo,
EdgeSwapInfo,
EdgeSwapPlugin,
EdgeSwapQuote,
EdgeSwapRequest,
EdgeTransaction
} from 'edge-core-js/types'
import { ethers } from 'ethers'
import { fixRequest } from '../../../../util/utils'
import { getInOutTokenAddresses } from '../../defiUtils'
import { getFtmProvider, makeSpookySwapRouterContract } from '../uniV2Contracts'
import {
getSwapAmounts,
getSwapTransactions,
makeUniV2EdgeSwapQuote
} from '../uniV2Utils'
const swapInfo: EdgeSwapInfo = {
pluginId: 'spookySwap',
displayName: 'SpookySwap',
supportEmail: 'support@edge.app'
}
const asInitOptions = asObject({
quiknodeApiKey: asOptional(asString)
})
const EXPIRATION_MS = 1000 * 60
const SLIPPAGE = '0.05'
export function makeSpookySwapPlugin(
opts: EdgeCorePluginOptions
): EdgeSwapPlugin {
const { quiknodeApiKey } = asInitOptions(opts.initOptions)
const provider = getFtmProvider(quiknodeApiKey)
const out: EdgeSwapPlugin = {
swapInfo,
async fetchSwapQuote(rawRequest: EdgeSwapRequest): Promise<EdgeSwapQuote> {
const request = fixRequest(rawRequest)
const {
fromWallet,
toWallet,
fromCurrencyCode,
toCurrencyCode,
quoteFor
} = request
// Sanity check: Both wallets should be of the same chain.
if (
fromWallet.currencyInfo.currencyCode !==
toWallet.currencyInfo.currencyCode
)
throw new Error(`${swapInfo.displayName}: Mismatched wallet chain`)
// Parse input/output token addresses. If either from or to swap sources
// are for the native currency, convert the address to the wrapped equivalent.
const {
fromTokenAddress,
toTokenAddress,
isWrappingSwap
} = getInOutTokenAddresses(
fromWallet.currencyInfo,
fromCurrencyCode,
toCurrencyCode
)
// Calculate swap amounts
const spookySwapRouter = makeSpookySwapRouterContract(provider)
const { amountToSwap, expectedAmountOut } = await getSwapAmounts(
spookySwapRouter,
quoteFor,
request.nativeAmount,
fromTokenAddress,
toTokenAddress,
isWrappingSwap
)
// Generate swap transactions
const fromAddress = (await fromWallet.getReceiveAddress()).publicAddress
const toAddress = (await toWallet.getReceiveAddress()).publicAddress
const expirationDate = new Date(Date.now() + EXPIRATION_MS)
const deadline = Math.round(expirationDate.getTime() / 1000) // unix timestamp
const swapTxs = await getSwapTransactions(
provider,
request,
spookySwapRouter,
amountToSwap,
expectedAmountOut,
toAddress,
SLIPPAGE,
deadline
)
const pluginId = swapInfo.pluginId
const edgeUnsignedTxs = await Promise.all(
swapTxs.map(async swapTx => {
// Convert to our spendInfo
const edgeSpendInfo: EdgeSpendInfo = {
currencyCode: request.fromCurrencyCode, // what is being sent out, only if token. Blank if not token
spendTargets: [
{
nativeAmount:
swapTx.value != null ? swapTx.value.toString() : '0', // biggy/number string integer
publicAddress: swapTx.to,
otherParams: {
data: swapTx.data
}
}
],
customNetworkFee: {
gasPrice:
swapTx.gasPrice != null
? ethers.utils.formatUnits(swapTx.gasPrice, 'gwei').toString()
: '0',
gasLimit: swapTx.gasLimit?.toString() ?? '0'
},
networkFeeOption: 'custom',
swapData: {
isEstimate: false,
payoutAddress: toAddress,
payoutCurrencyCode: request.toCurrencyCode,
payoutNativeAmount: expectedAmountOut.toString(),
payoutWalletId: request.toWallet.id,
plugin: { ...swapInfo },
refundAddress: fromAddress
}
}
const edgeUnsignedTx: EdgeTransaction = await request.fromWallet.makeSpend(
edgeSpendInfo
)
return edgeUnsignedTx
})
)
// Convert that to the EdgeSwapQuote format:
return makeUniV2EdgeSwapQuote(
request,
amountToSwap.toString(),
expectedAmountOut.toString(),
edgeUnsignedTxs,
pluginId,
swapInfo.displayName,
true,
expirationDate
)
}
}
return out
}