Skip to content

Commit f535c1a

Browse files
authored
Merge pull request #348 from emrekayat/fix/assigned-259
fix(sdk): enforce native asset decimal precision (#259)
2 parents 79b8ba4 + 9037fe4 commit f535c1a

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

packages/sdk/src/asset-config.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,27 @@ describe("AssetConfigError", () => {
219219
assert.equal(err.cause, cause);
220220
});
221221
});
222+
223+
describe("asset-specific decimal limits", () => {
224+
for (const [type, maximum] of [["native", 7], ["sac", 18]] as const) {
225+
const base = type === "native" ? ASSET_FIXTURES.valid.native : ASSET_FIXTURES.valid.sac;
226+
for (const decimals of [0, maximum]) {
227+
it(`accepts ${type} decimals=${decimals}`, () => {
228+
assert.equal(validateAssetConfig({ ...base, decimals }).decimals, decimals);
229+
});
230+
}
231+
for (const decimals of [-1, maximum + 1]) {
232+
it(`rejects ${type} decimals=${decimals}`, () => {
233+
assert.throws(() => validateAssetConfig({ ...base, decimals }), (error: unknown) => {
234+
assert.ok(error instanceof AssetConfigError);
235+
assert.equal(error.field, "decimals");
236+
assert.ok(error.message.includes(`0-${maximum}`));
237+
return true;
238+
});
239+
});
240+
}
241+
}
242+
it("retains SAC support above the native precision limit", () => {
243+
assert.equal(validateAssetConfig({ ...ASSET_FIXTURES.valid.sac, decimals: 8 }).decimals, 8);
244+
});
245+
});

packages/sdk/src/asset-config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,11 @@ export function validateAssetConfig(
129129

130130
const decimals = validateNumber(raw.decimals, "decimals");
131131
if (decimals !== undefined) {
132-
if (decimals < 0 || decimals > MAX_TOKEN_DECIMALS) {
132+
const maxDecimals = type === "native" ? MAX_STROOPS_DECIMALS : MAX_TOKEN_DECIMALS;
133+
if (decimals < 0 || decimals > maxDecimals) {
133134
throw new AssetConfigError(
134135
"decimals",
135-
`decimals must be 0-${MAX_TOKEN_DECIMALS}, got ${decimals}`,
136+
`decimals must be 0-${maxDecimals}, got ${decimals}`,
136137
);
137138
}
138139
config.decimals = decimals;

0 commit comments

Comments
 (0)