From c80ff0e332e4098d4b133615b6f60b1f5c4e17c7 Mon Sep 17 00:00:00 2001 From: Gianfranco Date: Wed, 13 Nov 2024 12:47:14 -0300 Subject: [PATCH] refactor asset code representation --- .../src/api/controllers/subsidize.controller.js | 6 +++--- signer-service/src/api/middlewares/validators.js | 2 +- signer-service/src/api/services/stellar.service.js | 6 +++--- signer-service/src/constants/tokenConfig.js | 14 ++++++++------ src/constants/tokenConfig.ts | 9 +++------ src/contexts/events.tsx | 2 +- src/hooks/useMainProcess.ts | 4 ++-- src/pages/progress/index.tsx | 4 ++-- src/services/anchor/index.ts | 4 ++-- src/services/nabla.ts | 4 ++-- src/services/polkadot/index.tsx | 2 +- src/services/stellar/index.tsx | 11 ++++------- 12 files changed, 32 insertions(+), 36 deletions(-) diff --git a/signer-service/src/api/controllers/subsidize.controller.js b/signer-service/src/api/controllers/subsidize.controller.js index b2001d16..7d03e334 100644 --- a/signer-service/src/api/controllers/subsidize.controller.js +++ b/signer-service/src/api/controllers/subsidize.controller.js @@ -4,7 +4,7 @@ const Big = require('big.js'); const { PENDULUM_WSS, PENDULUM_FUNDING_SEED } = require('../../constants/constants'); -const { TOKEN_CONFIG } = require('../../constants/tokenConfig'); +const { TOKEN_CONFIG, getPaddedAssetCode } = require('../../constants/tokenConfig'); const TOKEN_TO_SWAP = 'usdc.axl'; @@ -40,7 +40,7 @@ exports.subsidizePostSwap = async (req, res) => { const { address, amountRaw, token } = req.body; console.log('Subsidize post swap', address, amountRaw, token); - const { assetCodeRaw, assetIssuer, maximumSubsidyAmountRaw } = TOKEN_CONFIG[token]; + const { assetCode, assetIssuer, maximumSubsidyAmountRaw } = TOKEN_CONFIG[token]; if (Big(amountRaw).gt(Big(maximumSubsidyAmountRaw))) { throw new Error('Amount exceeds maximum subsidy amount'); @@ -49,7 +49,7 @@ exports.subsidizePostSwap = async (req, res) => { const assetIssuerHex = `0x${Keypair.fromPublicKey(assetIssuer).rawPublicKey().toString('hex')}`; const pendulumCurrencyId = { Stellar: { - AlphaNum4: { code: assetCodeRaw, issuer: assetIssuerHex }, + AlphaNum4: { code: getPaddedAssetCode(assetCode), issuer: assetIssuerHex }, }, }; diff --git a/signer-service/src/api/middlewares/validators.js b/signer-service/src/api/middlewares/validators.js index ac31c1fe..70a2c43b 100644 --- a/signer-service/src/api/middlewares/validators.js +++ b/signer-service/src/api/middlewares/validators.js @@ -132,7 +132,7 @@ const validatePostSwapSubsidizationInput = (req, res, next) => { } const tokenConfig = TOKEN_CONFIG[token]; - if (tokenConfig === undefined || tokenConfig.assetCodeRaw === undefined || tokenConfig.assetIssuer === undefined) { + if (tokenConfig === undefined || tokenConfig.assetCode === undefined || tokenConfig.assetIssuer === undefined) { return res.status(400).json({ error: 'Invalid "token" parameter' }); } diff --git a/signer-service/src/api/services/stellar.service.js b/signer-service/src/api/services/stellar.service.js index 278a0327..758acb02 100644 --- a/signer-service/src/api/services/stellar.service.js +++ b/signer-service/src/api/services/stellar.service.js @@ -47,7 +47,7 @@ async function buildCreationStellarTx(fundingSecret, ephemeralAccountId, maxTime .addOperation( Operation.changeTrust({ source: ephemeralAccountId, - asset: new Asset(tokenConfig.assetCodeStellar, tokenConfig.assetIssuer), + asset: new Asset(tokenConfig.assetCode, tokenConfig.assetIssuer), }), ) .setTimebounds(0, maxTime) @@ -97,7 +97,7 @@ async function buildPaymentAndMergeTx( .addOperation( Operation.payment({ amount, - asset: new Asset(tokenConfig.assetCodeStellar, tokenConfig.assetIssuer), + asset: new Asset(tokenConfig.assetCode, tokenConfig.assetIssuer), destination: offrampingAccount, }), ) @@ -111,7 +111,7 @@ async function buildPaymentAndMergeTx( }) .addOperation( Operation.changeTrust({ - asset: new Asset(tokenConfig.assetCodeStellar, tokenConfig.assetIssuer), + asset: new Asset(tokenConfig.assetCode, tokenConfig.assetIssuer), limit: '0', }), ) diff --git a/signer-service/src/constants/tokenConfig.js b/signer-service/src/constants/tokenConfig.js index d8bef835..9fe1efd3 100644 --- a/signer-service/src/constants/tokenConfig.js +++ b/signer-service/src/constants/tokenConfig.js @@ -1,8 +1,7 @@ const TOKEN_CONFIG = { eurc: { tomlFileUrl: 'https://circle.anchor.mykobo.co/.well-known/stellar.toml', - assetCodeRaw: 'EURC', - assetCodeStellar: 'EURC', + assetCode: 'EURC', assetIssuer: 'GDHU6WRG4IEQXM5NZ4BMPKOXHW76MZM4Y2IEMFDVXBSDP6SJY4ITNPP2', vaultAccountId: '6bsD97dS8ZyomMmp1DLCnCtx25oABtf19dypQKdZe6FBQXSm', minWithdrawalAmount: '10000000000000', @@ -25,8 +24,7 @@ const TOKEN_CONFIG = { }, ars: { tomlFileUrl: 'https://api.anclap.com/.well-known/stellar.toml', - assetCodeRaw: 'ARS\0', - assetCodeStellar: 'ARS', + assetCode: 'ARS', assetIssuer: 'GCYE7C77EB5AWAA25R5XMWNI2EDOKTTFTTPZKM2SR5DI4B4WFD52DARS', vaultAccountId: '6bE2vjpLRkRNoVDqDtzokxE34QdSJC2fz7c87R9yCVFFDNWs', minWithdrawalAmount: '11000000000000', // 11 ARS. Anchor minimum limit. @@ -46,11 +44,15 @@ const TOKEN_CONFIG = { function getTokenConfigByAssetCode(config, assetCode) { for (const key in config) { - if (config[key].assetCodeRaw === assetCode) { + if (config[key].assetCode === assetCode) { return config[key]; } } return undefined; } -module.exports = { TOKEN_CONFIG, getTokenConfigByAssetCode }; +function getPaddedAssetCode(assetCode) { + return assetCode.padEnd(4, '\0'); +} + +module.exports = { TOKEN_CONFIG, getTokenConfigByAssetCode, getPaddedAssetCode }; diff --git a/src/constants/tokenConfig.ts b/src/constants/tokenConfig.ts index 86a62474..a682e2bc 100644 --- a/src/constants/tokenConfig.ts +++ b/src/constants/tokenConfig.ts @@ -29,8 +29,7 @@ export interface OutputTokenDetails { stellarAsset: { code: { hex: string; - stringRaw: string; // stringRaw. With /0 if the Asset has less than 4 letters. - stringStellar: string; + string: string; // Stellar representation (3 or 4 letter code) }; issuer: { hex: string; @@ -84,8 +83,7 @@ export const OUTPUT_TOKEN_CONFIG: Record = stellarAsset: { code: { hex: '0x45555243', - stringStellar: 'EURC', - stringRaw: 'EURC', + string: 'EURC', }, issuer: { hex: '0xcf4f5a26e2090bb3adcf02c7a9d73dbfe6659cc690461475b86437fa49c71136', @@ -109,8 +107,7 @@ export const OUTPUT_TOKEN_CONFIG: Record = stellarAsset: { code: { hex: '0x41525300', - stringStellar: 'ARS', - stringRaw: 'ARS\0', + string: 'ARS', }, issuer: { hex: '0xb04f8bff207a0b001aec7b7659a8d106e54e659cdf9533528f468e079628fba1', diff --git a/src/contexts/events.tsx b/src/contexts/events.tsx index 511d7eb5..f718a783 100644 --- a/src/contexts/events.tsx +++ b/src/contexts/events.tsx @@ -235,7 +235,7 @@ export function createTransactionEvent(type: TransactionEvent['event'], state: O return { event: type, from_asset: INPUT_TOKEN_CONFIG[state.inputTokenType].assetSymbol, - to_asset: OUTPUT_TOKEN_CONFIG[state.outputTokenType].stellarAsset.code.stringRaw, + to_asset: OUTPUT_TOKEN_CONFIG[state.outputTokenType].stellarAsset.code.string, from_amount: state.inputAmount.units, to_amount: state.outputAmount.units, }; diff --git a/src/hooks/useMainProcess.ts b/src/hooks/useMainProcess.ts index f421eab4..14009c1a 100644 --- a/src/hooks/useMainProcess.ts +++ b/src/hooks/useMainProcess.ts @@ -128,7 +128,7 @@ export const useMainProcess = () => { trackEvent({ event: 'transaction_confirmation', from_asset: INPUT_TOKEN_CONFIG[inputTokenType].assetSymbol, - to_asset: OUTPUT_TOKEN_CONFIG[outputTokenType].stellarAsset.code.stringRaw, + to_asset: OUTPUT_TOKEN_CONFIG[outputTokenType].stellarAsset.code.string, from_amount: amountInUnits, to_amount: offrampAmount.toFixed(2, 0), }); @@ -197,7 +197,7 @@ export const useMainProcess = () => { trackEvent({ event: 'kyc_started', from_asset: INPUT_TOKEN_CONFIG[executionInputState.inputTokenType].assetSymbol, - to_asset: OUTPUT_TOKEN_CONFIG[executionInputState.outputTokenType].stellarAsset.code.stringRaw, + to_asset: OUTPUT_TOKEN_CONFIG[executionInputState.outputTokenType].stellarAsset.code.string, from_amount: executionInputState.amountInUnits, to_amount: executionInputState.offrampAmount.toFixed(2, 0), }); diff --git a/src/pages/progress/index.tsx b/src/pages/progress/index.tsx index c7eb70fe..97db632e 100644 --- a/src/pages/progress/index.tsx +++ b/src/pages/progress/index.tsx @@ -21,9 +21,9 @@ function createOfframpingPhaseMessage(offrampingState: OfframpingState): string case 'nablaApprove': case 'nablaSwap': case 'subsidizePostSwap': - return `Swapping to ${outputToken.stellarAsset.code.stringRaw} on Vortex DEX`; + return `Swapping to ${outputToken.stellarAsset.code.string} on Vortex DEX`; case 'executeSpacewalkRedeem': - return `Bridging ${outputToken.stellarAsset.code.stringRaw} to Stellar via Spacewalk`; + return `Bridging ${outputToken.stellarAsset.code.string} to Stellar via Spacewalk`; case 'pendulumCleanup': case 'stellarOfframp': case 'stellarCleanup': diff --git a/src/services/anchor/index.ts b/src/services/anchor/index.ts index 0b4ddfa5..dbb6b2af 100644 --- a/src/services/anchor/index.ts +++ b/src/services/anchor/index.ts @@ -216,14 +216,14 @@ export async function sep24First( throw new Error('Master must be defined at this point.'); } sep24Params = new URLSearchParams({ - asset_code: sessionParams.tokenConfig.stellarAsset.code.stringStellar, + asset_code: sessionParams.tokenConfig.stellarAsset.code.string, amount: sessionParams.offrampAmount, account: sep10Account, // THIS is a particularity of Anclap. Should be able to work without it, or with a different one // to that of the sep-10 }); } else { sep24Params = new URLSearchParams({ - asset_code: sessionParams.tokenConfig.stellarAsset.code.stringStellar, + asset_code: sessionParams.tokenConfig.stellarAsset.code.string, amount: sessionParams.offrampAmount, }); } diff --git a/src/services/nabla.ts b/src/services/nabla.ts index 60bfe4ed..f37b1759 100644 --- a/src/services/nabla.ts +++ b/src/services/nabla.ts @@ -271,7 +271,7 @@ export async function prepareNablaSwapTransaction( // Try swap try { renderEvent( - `Swapping ${inputAmount.units} ${inputToken.axelarEquivalent.pendulumAssetSymbol} to ${outputAmount.units} ${outputToken.stellarAsset.code.stringRaw} `, + `Swapping ${inputAmount.units} ${inputToken.axelarEquivalent.pendulumAssetSymbol} to ${outputAmount.units} ${outputToken.stellarAsset.code.string} `, EventStatus.Waiting, ); @@ -336,7 +336,7 @@ export async function nablaSwap(state: OfframpingState, { renderEvent }: Executi try { renderEvent( - `Swapping ${inputAmount.units} ${inputToken.axelarEquivalent.pendulumAssetSymbol} to ${outputAmount.units} ${outputToken.stellarAsset.code.stringRaw} `, + `Swapping ${inputAmount.units} ${inputToken.axelarEquivalent.pendulumAssetSymbol} to ${outputAmount.units} ${outputToken.stellarAsset.code.string} `, EventStatus.Waiting, ); diff --git a/src/services/polkadot/index.tsx b/src/services/polkadot/index.tsx index 4174a615..6a11bd48 100644 --- a/src/services/polkadot/index.tsx +++ b/src/services/polkadot/index.tsx @@ -202,7 +202,7 @@ function checkBalancePeriodically( try { const someBalanceUnits = await getStellarBalanceUnits( stellarTargetAccountId, - outputToken.stellarAsset.code.stringStellar, + outputToken.stellarAsset.code.string, ); console.log(`Balance check: ${someBalanceUnits.toString()} / ${amountDesiredUnitsBig.toString()}`); diff --git a/src/services/stellar/index.tsx b/src/services/stellar/index.tsx index 34d21013..3ea5e736 100644 --- a/src/services/stellar/index.tsx +++ b/src/services/stellar/index.tsx @@ -102,7 +102,7 @@ async function setupStellarAccount( body: JSON.stringify({ accountId: ephemeralAccountId, maxTime, - assetCode: outputToken.stellarAsset.code.stringRaw, + assetCode: outputToken.stellarAsset.code.string, }), }); @@ -142,10 +142,7 @@ async function setupStellarAccount( .addOperation( Operation.changeTrust({ source: ephemeralAccountId, - asset: new Asset( - outputToken.stellarAsset.code.stringStellar, - outputToken.stellarAsset.issuer.stellarEncoding, - ), + asset: new Asset(outputToken.stellarAsset.code.string, outputToken.stellarAsset.issuer.stellarEncoding), }), ) .setTimebounds(0, maxTime) @@ -195,7 +192,7 @@ async function createOfframpAndMergeTransaction( throw new Error(`Unexpected offramp memo type: ${memoType}`); } - const stellarAsset = new Asset(code.stringStellar, issuer.stellarEncoding); + const stellarAsset = new Asset(code.string, issuer.stellarEncoding); // this operation would run completely in the browser // that is where the signature of the ephemeral account is added @@ -250,7 +247,7 @@ async function createOfframpAndMergeTransaction( paymentData: sepResult, sequence, maxTime, - assetCode: code.stringRaw, + assetCode: code.string, }), });