-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrap sac match logic in try catch to gracefully fall through (#1773)
* wrap sac match logic in try catch to gracefully fall through * add helper for getting token balance by key, add test for history item rendering SAC payments when user has an LP share in balances * adds test for getBalanceByKey helper --------- Co-authored-by: Aristides Staffieri <[email protected]>
- Loading branch information
1 parent
cc23d21
commit 619bb04
Showing
6 changed files
with
177 additions
and
19 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
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,67 @@ | ||
import BigNumber from "bignumber.js"; | ||
|
||
import { TESTNET_NETWORK_DETAILS } from "@shared/constants/stellar"; | ||
import { defaultBlockaidScanAssetResult } from "@shared/helpers/stellar"; | ||
|
||
import { getBalanceByKey } from "../balance"; | ||
|
||
const CLASSIC_ISSUER = | ||
"CBANAM7DMVGXE7C3XPE2RZ7RU5FSVGY7RELHN2B6F5XLGFWX7BQ5D7S5"; | ||
const CONTRACT_ID = "CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC"; | ||
const TOKEN_BALANCE_KEY = `DT:${CONTRACT_ID}`; | ||
const CLASSIC_BALANCE_KEY = | ||
"USDC:GCK3D3V2XNLLKRFGFFFDEJXA4O2J4X36HET2FE446AV3M4U7DPHO3PEM"; | ||
const LP_ID = | ||
"a468d41d8e9b8f3c7209651608b74b7db7ac9952dcae0cdf24871d1d9c7b0088"; | ||
const BALANCES = { | ||
[TOKEN_BALANCE_KEY]: { | ||
token: { | ||
code: "DT", | ||
issuer: { | ||
key: "CCXVDIGMR6WTXZQX2OEVD6YM6AYCYPXPQ7YYH6OZMRS7U6VD3AVHNGBJ", | ||
}, | ||
}, | ||
decimals: 7, | ||
total: new BigNumber("1000000000"), | ||
available: new BigNumber("1000000000"), | ||
blockaidData: defaultBlockaidScanAssetResult, | ||
}, | ||
[CLASSIC_BALANCE_KEY]: { | ||
token: { | ||
code: "USDC", | ||
issuer: { | ||
key: "GCK3D3V2XNLLKRFGFFFDEJXA4O2J4X36HET2FE446AV3M4U7DPHO3PEM", | ||
}, | ||
}, | ||
total: new BigNumber("100"), | ||
available: new BigNumber("100"), | ||
blockaidData: defaultBlockaidScanAssetResult, | ||
}, | ||
[`${LP_ID}:lp`]: { | ||
liquidityPoolId: | ||
"a468d41d8e9b8f3c7209651608b74b7db7ac9952dcae0cdf24871d1d9c7b0088", | ||
total: new BigNumber(10), | ||
limit: new BigNumber(100), | ||
}, | ||
native: { | ||
token: { | ||
code: "XLM", | ||
}, | ||
decimals: 7, | ||
}, | ||
}; | ||
|
||
describe("getBalanceByKey", () => { | ||
it("should return a valid key for a classic asset", () => { | ||
const key = getBalanceByKey( | ||
CLASSIC_ISSUER, | ||
BALANCES, | ||
TESTNET_NETWORK_DETAILS, | ||
); | ||
expect(key).toEqual(CLASSIC_BALANCE_KEY); | ||
}); | ||
it("should return a valid key for a token", () => { | ||
const key = getBalanceByKey(CONTRACT_ID, BALANCES, TESTNET_NETWORK_DETAILS); | ||
expect(key).toEqual(TOKEN_BALANCE_KEY); | ||
}); | ||
}); |
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,48 @@ | ||
import { Asset } from "stellar-sdk"; | ||
import { captureException } from "@sentry/browser"; | ||
|
||
import { BalanceMap } from "@shared/api/types"; | ||
import { LP_ISSUER_KEY } from "@shared/helpers/stellar"; | ||
import { NetworkDetails } from "@shared/constants/stellar"; | ||
import { isContractId } from "./soroban"; | ||
|
||
/* | ||
Attempts to match a balance to a related contract ID, expects a token or SAC contract ID. | ||
BalanceMap keys can be one of two variants - | ||
An asset balance - {code}:{issuer} | ||
A token - {symbol}:{contract id} | ||
An LP share - {LP ID}:lp | ||
*/ | ||
export const getBalanceByKey = ( | ||
contractId: string, | ||
balances: BalanceMap, | ||
networkDetails: NetworkDetails, | ||
) => { | ||
const key = Object.keys(balances).find((balanceKey) => { | ||
const [code, issuer] = | ||
balanceKey === "native" ? ["XLM"] : balanceKey.split(":"); | ||
const matchesIssuer = contractId === issuer; | ||
|
||
// if issuer is a G address or xlm, check for a SAC match | ||
if ( | ||
(issuer && !isContractId(issuer) && issuer !== LP_ISSUER_KEY) || | ||
code === "XLM" | ||
) { | ||
try { | ||
const sacAddress = new Asset(code, issuer).contractId( | ||
networkDetails.networkPassphrase, | ||
); | ||
const matchesSac = contractId === sacAddress; | ||
return matchesSac; | ||
} catch (e) { | ||
console.error(e); | ||
captureException( | ||
`Error checking for SAC match with code ${code} and issuer ${issuer}. Error: ${e}`, | ||
); | ||
} | ||
} | ||
return matchesIssuer; | ||
}); | ||
return key; | ||
}; |