diff --git a/package.json b/package.json index 43859d0..e5cf598 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@polymarket/sdk", - "version": "5.0.1", + "version": "5.0.2", "description": "SDK to simplify common interactions with the Polymarket proxy wallet", "author": "Tom French ", "repository": "https://github.com/TokenUnion/polymarket-sdk.git", diff --git a/src/index.ts b/src/index.ts index e00597c..833525d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,6 @@ export * from "./matic"; export * from "./markets"; export * from "./debt"; -export { ethTransferTransaction, erc20TransferTransaction, getIndexSet } from "./utils"; +export { ethTransferTransaction, erc20TransferTransaction, getIndexSet, getMarketIndex } from "./utils"; export { getProxyWalletAddress } from "./proxyWallet"; export { negRiskOperations } from "./negRisk"; diff --git a/src/utils/getIndexSet.ts b/src/utils/getIndexSet.ts index cdd712f..ed144a8 100644 --- a/src/utils/getIndexSet.ts +++ b/src/utils/getIndexSet.ts @@ -1,3 +1,8 @@ +/** + * Gets the index set corresponding to an array of market indices. + * @param indices An array of market indices. + * @returns The corresponding index set. + */ const getIndexSet = (indices: number[]) => // eslint-disable-next-line no-bitwise [...new Set(indices)].reduce((acc, index) => acc + (1 << index), 0); diff --git a/src/utils/getMarketIndex.ts b/src/utils/getMarketIndex.ts new file mode 100644 index 0000000..d4c9a8e --- /dev/null +++ b/src/utils/getMarketIndex.ts @@ -0,0 +1,10 @@ +/** + * Gets the index of a neg-risk market index from the questionId. + * @remarks + * The last byte of the questionId contains the index of the neg-risk market. + * @param questionId The questionId of the neg-risk market. + * @returns The index of the neg-risk market. + */ +const getMarketIndex = (questionId: string): number => parseInt(questionId.slice(-2), 16); + +export { getMarketIndex }; diff --git a/src/utils/index.ts b/src/utils/index.ts index 8af3d20..bbb5333 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -3,3 +3,4 @@ export { erc1155ApprovalTransaction } from "./approveErc1155"; export { erc20TransferTransaction } from "./transferErc20"; export { ethTransferTransaction } from "./transferEth"; export { getIndexSet } from "./getIndexSet"; +export { getMarketIndex } from "./getMarketIndex"; diff --git a/test/getIndexSet.test.ts b/test/getIndexSet.test.ts index 18e37e9..bbc56c0 100644 --- a/test/getIndexSet.test.ts +++ b/test/getIndexSet.test.ts @@ -22,7 +22,7 @@ const testCases = [ ] as [number[], number][]; describe("getIndexSet", () => { - it.each(testCases)(`should compute the index set`, (indices, expectedIndexSet) => { + it.each(testCases)(`should get the index set`, (indices, expectedIndexSet) => { expect(getIndexSet(indices)).toEqual(expectedIndexSet); }); }); diff --git a/test/getMarketIndex.test.ts b/test/getMarketIndex.test.ts new file mode 100644 index 0000000..7875fe2 --- /dev/null +++ b/test/getMarketIndex.test.ts @@ -0,0 +1,18 @@ +/* eslint-env jest */ + +import { getMarketIndex } from "../src"; + +const testCases: [string, number][] = [ + ["0xb20c874543db3de05e85f24dc912e915a641c579db553ff12ed135a68b3f7500", 0], + ["0xb20c874543db3de05e85f24dc912e915a641c579db553ff12ed135a68b3f7501", 1], + ["0xb20c874543db3de05e85f24dc912e915a641c579db553ff12ed135a68b3f7510", 16], + ["0x1d2daa077aa4441be685c80a2ffac5c962eebf791ee72b8d178bfaf305847c11", 17], + ["0x1d2daa077aa4441be685c80a2ffac5c962eebf791ee72b8d178bfaf305847cFF", 255], + ["0x1d2daa077aa4441be685c80a2ffac5c962eebf791ee72b8d178bfaf305847c0A", 10], +]; + +describe("getMarketIndex", () => { + it.each(testCases)(`should get the market index`, (questionId, expectedMarketIndex) => { + expect(getMarketIndex(questionId)).toEqual(expectedMarketIndex); + }); +});