Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/poolz/ThePoolz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,24 @@ class ThePoolz implements EnforceInterface<IThePoolzInterface, ThePoolz> {
const contract = new this.web3.eth.Contract(abi as AbiItem[], address)
this.#contracts.set(collectionName, contract)
return contract
} catch (e) {}
} catch (e) {
throw new Error(`Failed to create contract ${name} at ${address}: ${(e as Error).message}`)
}
}
/**
* Fetches ABI by contract name version from Poolz database.
* Throws an error when the request fails or the ABI is missing.
*/
private async fetchContractAbi(nameVersion: string) {
const response = await fetch(`https://poolzfinancedata.com/contracts?_where[0][NameVersion]=${nameVersion}`)
return (await response.json())[0].ABI
if (!response.ok) {
throw new Error(`Unable to fetch ABI for ${nameVersion}: ${response.status} ${response.statusText}`)
}
const data = await response.json()
if (!Array.isArray(data) || !data[0]?.ABI) {
throw new Error(`ABI for ${nameVersion} not found`)
}
return data[0].ABI
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/ThePoolz.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ jest.mock("web3", () => {

global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
status: 200,
statusText: "OK",
json: () => Promise.resolve([{ ABI: { rates: { CAD: 1.42 } } }])
})
) as jest.Mock
Expand Down Expand Up @@ -116,4 +119,22 @@ describe("ThePoolz", () => {
await thePoolz.init()
expect(thePoolz.CPoolx?.address).toEqual("0x000")
})

test("Contract ABI fetch failure", async () => {
expect.assertions(1)
;(fetch as jest.Mock).mockImplementationOnce(() => Promise.reject(new Error("network")))
const thePoolz = new ThePoolz({ isTrustWallet: true })
await thePoolz.init()
await expect(thePoolz.Contract("ERC20", "0x000")).rejects.toThrow("network")
})

test("Contract ABI invalid data", async () => {
expect.assertions(1)
;(fetch as jest.Mock).mockImplementationOnce(() =>
Promise.resolve({ ok: true, status: 200, statusText: "OK", json: () => Promise.resolve([]) })
)
const thePoolz = new ThePoolz({ isTrustWallet: true })
await thePoolz.init()
await expect(thePoolz.Contract("ERC20", "0x000")).rejects.toThrow("ABI for ERC20 not found")
})
})
Loading