-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* remove unnecessary dependencies * Add subsidization endpoints and change flow * Fix linting errors
- Loading branch information
1 parent
9c48085
commit 80d6851
Showing
36 changed files
with
551 additions
and
5,233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
signer-service/src/api/controllers/subsidize.controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const { Keypair } = require('stellar-sdk'); | ||
const { ApiPromise, WsProvider, Keyring } = require('@polkadot/api'); | ||
const Big = require('big.js'); | ||
|
||
const { PENDULUM_WSS, PENDULUM_FUNDING_SEED } = require('../../constants/constants'); | ||
|
||
const { TOKEN_CONFIG } = require('../../constants/tokenConfig'); | ||
|
||
const TOKEN_TO_SWAP = 'usdc.axl'; | ||
|
||
exports.subsidizePreSwap = async (req, res) => { | ||
try { | ||
const { pendulumCurrencyId, maximumSubsidyAmountRaw } = TOKEN_CONFIG[TOKEN_TO_SWAP]; | ||
|
||
const { address, amountRaw } = req.body; | ||
console.log('Subsidize pre swap', address, amountRaw); | ||
|
||
if (Big(amountRaw).gt(Big(maximumSubsidyAmountRaw))) { | ||
throw new Error('Amount exceeds maximum subsidy amount'); | ||
} | ||
|
||
const keyring = new Keyring({ type: 'sr25519' }); | ||
const fundingAccountKeypair = keyring.addFromUri(PENDULUM_FUNDING_SEED); | ||
|
||
const wsProvider = new WsProvider(PENDULUM_WSS); | ||
const api = await ApiPromise.create({ provider: wsProvider }); | ||
await api.isReady; | ||
|
||
await api.tx.tokens.transfer(address, pendulumCurrencyId, amountRaw).signAndSend(fundingAccountKeypair); | ||
|
||
return res.status(200).json({ message: 'Subsidy transferred successfully' }); | ||
} catch (error) { | ||
console.error('Error in subsidizePreSwap::', error); | ||
return res.status(500).json({ error: 'Server error', details: error.message }); | ||
} | ||
}; | ||
|
||
exports.subsidizePostSwap = async (req, res) => { | ||
try { | ||
const { address, amountRaw, token } = req.body; | ||
console.log('Subsidize post swap', address, amountRaw, token); | ||
|
||
const { assetCode, assetIssuer, maximumSubsidyAmountRaw } = TOKEN_CONFIG[token]; | ||
|
||
if (Big(amountRaw).gt(Big(maximumSubsidyAmountRaw))) { | ||
throw new Error('Amount exceeds maximum subsidy amount'); | ||
} | ||
|
||
const assetIssuerHex = `0x${Keypair.fromPublicKey(assetIssuer).rawPublicKey().toString('hex')}`; | ||
const pendulumCurrencyId = { | ||
Stellar: { | ||
AlphaNum4: { code: assetCode.padEnd(4, '\0'), issuer: assetIssuerHex }, | ||
}, | ||
}; | ||
|
||
const keyring = new Keyring({ type: 'sr25519' }); | ||
const fundingAccountKeypair = keyring.addFromUri(PENDULUM_FUNDING_SEED); | ||
|
||
const wsProvider = new WsProvider(PENDULUM_WSS); | ||
const api = await ApiPromise.create({ provider: wsProvider }); | ||
await api.isReady; | ||
|
||
await api.tx.tokens.transfer(address, pendulumCurrencyId, amountRaw).signAndSend(fundingAccountKeypair); | ||
|
||
return res.status(200).json({ message: 'Subsidy transferred successfully' }); | ||
} catch (error) { | ||
console.error('Error in subsidizePreSwap::', error); | ||
return res.status(500).json({ error: 'Server error', details: error.message }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const express = require('express'); | ||
const controller = require('../../controllers/subsidize.controller'); | ||
const { | ||
validatePreSwapSubsidizationInput, | ||
validatePostSwapSubsidizationInput, | ||
} = require('../../middlewares/validators'); | ||
|
||
const router = express.Router({ mergeParams: true }); | ||
|
||
router.route('/preswap').post(validatePreSwapSubsidizationInput, controller.subsidizePreSwap); | ||
router.route('/postswap').post(validatePostSwapSubsidizationInput, controller.subsidizePostSwap); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.