-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Add PactSwap #16651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add PactSwap #16651
Changes from 4 commits
fd0b108
c700018
63a70e7
13324fa
ea53793
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,7 @@ | |
"clover", | ||
"clv", | ||
"cmp", | ||
"coinweb", | ||
"comdex", | ||
"concordium", | ||
"conflux", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const getPactswapConfig = async () => { | ||
const data = await fetch(`https://app.pactswap.io/build-info.json`).then(res => res.json()) | ||
return data | ||
} | ||
|
||
const PACTSWAP_API_URL = 'https://api.pactswap.io/pactswap_cm' | ||
|
||
const chainsConfig = { | ||
ethereum: 'ETH', | ||
bsc: 'BNB', | ||
polygon: 'POL', | ||
bitcoin: 'BTC', | ||
litecoin: 'LTC', | ||
doge: 'DOGE', | ||
tron: 'TRX' | ||
} | ||
const btcLikeChains = ['BTC', 'LTC', 'DOGE'] | ||
|
||
const tvl = async (api) => { | ||
const chain = chainsConfig[api.chain] | ||
if (!chain) return 0 | ||
const psConfig = await getPactswapConfig() | ||
const tokens = [] | ||
|
||
for (const token of Object.keys(psConfig)) { | ||
if (token === chain || token.split('_')[1] === chain) { | ||
tokens.push(token) | ||
} | ||
} | ||
|
||
for (const token of tokens) { | ||
const c2ContractAddress = psConfig[token].L2_CONTRACT_ADDRESS_MAKER.id; | ||
|
||
const c2Orders = await fetch(`${PACTSWAP_API_URL}/getAllC2Orders?contractAddress=${c2ContractAddress}`).then(res => res.json()) | ||
|
||
let value = 0; | ||
|
||
for (const order of c2Orders) { | ||
value += Number(order.l1Amount) | ||
} | ||
|
||
const tokenAddress = psConfig[token].L1_TOKEN_ADDRESS; | ||
if (tokenAddress) { | ||
api.add(tokenAddress, value) | ||
} else if (btcLikeChains.includes(chain)) { | ||
const cgToken = api.chain === 'doge' ? 'dogecoin' : api.chain; | ||
api.addCGToken(cgToken, value / 10 ** 8) | ||
} else { | ||
api.addGasToken(value) | ||
} | ||
} | ||
|
||
return api.getBalances(); | ||
} | ||
|
||
Object.keys(chainsConfig).forEach(chain => { | ||
module.exports[chain] = { tvl } | ||
}) | ||
|
||
const tvlCoinweb = async (api) => { | ||
const psConfig = await getPactswapConfig() | ||
const c1OContractsIds = []; | ||
const c2OContractsIds = []; | ||
let cwebValue = 0; | ||
for (const chain of Object.values(chainsConfig)) { | ||
for (const token of Object.keys(psConfig)) { | ||
if (token === chain || token.split('_')[1] === chain) { | ||
c1OContractsIds.push(psConfig[token].L2_CONTRACT_ADDRESS_BASE.id) | ||
c2OContractsIds.push(psConfig[token].L2_CONTRACT_ADDRESS_MAKER.id) | ||
} | ||
} | ||
} | ||
const allC1Orders = await Promise.all(c1OContractsIds.map(async (contractId) => { | ||
return fetch(`${PACTSWAP_API_URL}/getAllC1Orders?contractAddress=${contractId}`).then(res => res.json()) | ||
})) | ||
const allC2Orders = await Promise.all(c2OContractsIds.map(async (contractId) => { | ||
return fetch(`${PACTSWAP_API_URL}/getAllC2Orders?contractAddress=${contractId}`).then(res => res.json()) | ||
})) | ||
for (const order of allC1Orders.flat()) { | ||
cwebValue += Number(order.funds) | ||
} | ||
for (const order of allC2Orders.flat()) { | ||
cwebValue += Number(order.collateral) | ||
} | ||
api.addCGToken('coinweb', cwebValue / 10 ** 18) | ||
return api.getBalances(); | ||
} | ||
|
||
module.exports.coinweb = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand correctly, the liquidity in pact swap sits on each of the supported chains, and they arent bridged to coinweb chain? In which case, balances should be recorded on the chains they are deposited to, eg bitcoin, ethereum etc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, they are not bridged to coinweb chain. But market maker should lock native cweb on coinweb chain to create any type of orders. Thats the reason why cweb included in tvl. Also market maker lock his tokens in destination chains, its also included in tvl. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should count user deposits to the DEX liquidity, on the chain it is deposited on. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, on Pact Swap user can create orders. To create any type of orders ask/bid users should lock native cweb on coinweb chain. All this locked cweb is inside orders. For ask orders users also should lock base assets and cweb as collateral for every order. |
||
tvl: tvlCoinweb | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible to read contract balances directly from the chain opposed to using your API for token amounts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is possible. I will update the PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated