Skip to content

Commit

Permalink
Update abi.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
KABBOUCHI committed Sep 7, 2024
1 parent 103312f commit 88261aa
Showing 1 changed file with 169 additions and 101 deletions.
270 changes: 169 additions & 101 deletions test/abi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,127 +25,195 @@ const defaultAbiFetcher = new AbiFetcher({
}
})

describe.concurrent('abi', () => {
test('can fetch abi', async () => {
const abi = await defaultAbiFetcher.get('0x0000000000000000000000000000000000001010', 'polygon')

expect(abi).toEqual(polygonMaticABI)
})

test('can define a custom cache', async () => {
const abiCache = new NodeCache()

const cache: ICache = {
get (key) {
return abiCache.get(key)
},
set (key, value) {
return abiCache.set(key, value, 10)
}
}

const cacheGet = vi.spyOn(cache, 'get')
const cacheSet = vi.spyOn(cache, 'set')

const abiFetcher = new AbiFetcher({
cache
describe.concurrent(
'abi',
() => {
test('can fetch abi', async () => {
const abi = await defaultAbiFetcher.get(
'0x0000000000000000000000000000000000001010',
'polygon'
)

expect(abi).toEqual(polygonMaticABI)
})
expect(await abiFetcher.get('0x0000000000000000000000000000000000001010', 'polygon')).toEqual(polygonMaticABI)
expect(await abiFetcher.get('0x0000000000000000000000000000000000001010', 'polygon')).toEqual(polygonMaticABI)

expect(cacheGet).toBeCalledTimes(2)
expect(cacheSet).toBeCalledTimes(1)
})
test('can define a custom cache', async () => {
const abiCache = new NodeCache()

test('can set etherscan keys', async () => {
const etherscanApiKey = {
mainnet: '9D13ZE7XSBTJ94N9BNJ2MA33VMAY2YPIRB'
}

const etherscan = vi.spyOn(etherscanApiKey, 'mainnet', 'get')
const cache: ICache = {
get (key) {
return abiCache.get(key)
},
set (key, value) {
return abiCache.set(key, value, 10)
}
}

const abiFetcher = new AbiFetcher({
etherscanApiKey
const cacheGet = vi.spyOn(cache, 'get')
const cacheSet = vi.spyOn(cache, 'set')

const abiFetcher = new AbiFetcher({
cache,
etherscanApiKey: defaultAbiFetcher.options.etherscanApiKey
})
expect(
await abiFetcher.get(
'0x0000000000000000000000000000000000001010',
'polygon'
)
).toEqual(polygonMaticABI)
expect(
await abiFetcher.get(
'0x0000000000000000000000000000000000001010',
'polygon'
)
).toEqual(polygonMaticABI)

expect(cacheGet).toBeCalledTimes(2)
expect(cacheSet).toBeCalledTimes(1)
})

await abiFetcher.get('0xB8c77482e45F1F44dE1745F52C74426C631bDD52', 'mainnet')

expect(etherscan).toBeCalledTimes(1)
})

test('can fetch proxy implementation - EIP-1967', async () => {
const usdcAddress = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
const network = 'mainnet'

const proxyOnlyAbi = await defaultAbiFetcher.get(usdcAddress, network, 'proxyOnly')
expect(proxyOnlyAbi.length).toBe(9)
test('can set etherscan keys', async () => {
const etherscanApiKey = {
mainnet: '9D13ZE7XSBTJ94N9BNJ2MA33VMAY2YPIRB'
}

const implementationOnlyAbi = await defaultAbiFetcher.get(usdcAddress, network, 'implementationOnly')
expect(implementationOnlyAbi.length).toBe(72)
const etherscan = vi.spyOn(etherscanApiKey, 'mainnet', 'get')

const abi = await defaultAbiFetcher.get(usdcAddress, network, 'proxyAndImplementation')
expect(abi.length).toBe(81)
})
const abiFetcher = new AbiFetcher({
etherscanApiKey
})

test('can fetch proxy implementation - EIP-897', async () => {
const usdcAddress = '0xc2132d05d31c914a87c6611c10748aeb04b58e8f'
const network = 'polygon'
await abiFetcher.get(
'0xB8c77482e45F1F44dE1745F52C74426C631bDD52',
'mainnet'
)

const proxyOnlyAbi = await defaultAbiFetcher.get(usdcAddress, network, 'proxyOnly')
expect(proxyOnlyAbi.length).toBeTruthy()
expect(etherscan).toBeCalledTimes(1)
})

const implementationOnlyAbi = await defaultAbiFetcher.get(usdcAddress, network, 'implementationOnly')
expect(implementationOnlyAbi.length).toBeTruthy()
})
test('can fetch proxy implementation - EIP-1967', async () => {
const usdcAddress = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
const network = 'mainnet'

const proxyOnlyAbi = await defaultAbiFetcher.get(
usdcAddress,
network,
'proxyOnly'
)
expect(proxyOnlyAbi.length).toBe(9)

const implementationOnlyAbi = await defaultAbiFetcher.get(
usdcAddress,
network,
'implementationOnly'
)
expect(implementationOnlyAbi.length).toBe(72)

const abi = await defaultAbiFetcher.get(
usdcAddress,
network,
'proxyAndImplementation'
)
expect(abi.length).toBe(81)
})

test('throw exception on bad address', async () => {
await expect(defaultAbiFetcher.get('asdasd', 'polygon')).rejects.toThrowError()
})
test('can fetch proxy implementation - EIP-897', async () => {
const usdcAddress = '0xc2132d05d31c914a87c6611c10748aeb04b58e8f'
const network = 'polygon'

const proxyOnlyAbi = await defaultAbiFetcher.get(
usdcAddress,
network,
'proxyOnly'
)
expect(proxyOnlyAbi.length).toBeTruthy()

const implementationOnlyAbi = await defaultAbiFetcher.get(
usdcAddress,
network,
'implementationOnly'
)
expect(implementationOnlyAbi.length).toBeTruthy()
})

test('throw exception on EOA', async () => {
await expect(defaultAbiFetcher.get('0xA7366d1aE09e6fD6Cb43CFa39A2D5E43f120222c', 'polygon')).rejects.toThrowError()
})
test('throw exception on bad address', async () => {
await expect(
defaultAbiFetcher.get('asdasd', 'polygon')
).rejects.toThrowError()
})

test('can fetch compound v2 proxy implementation', async () => {
const abi = await defaultAbiFetcher.get('0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B', 'mainnet', 'implementationOnly')
test('throw exception on EOA', async () => {
await expect(
defaultAbiFetcher.get(
'0xA7366d1aE09e6fD6Cb43CFa39A2D5E43f120222c',
'polygon'
)
).rejects.toThrowError()
})

expect(abi).to.deep.include({
anonymous: false,
inputs: [
{
indexed: false,
internalType: 'contract CToken',
name: 'cToken',
type: 'address'
}
],
name: 'MarketListed',
type: 'event'
test('can fetch compound v2 proxy implementation', async () => {
const abi = await defaultAbiFetcher.get(
'0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B',
'mainnet',
'implementationOnly'
)

expect(abi).to.deep.include({
anonymous: false,
inputs: [
{
indexed: false,
internalType: 'contract CToken',
name: 'cToken',
type: 'address'
}
],
name: 'MarketListed',
type: 'event'
})
})
})

test('can fetch abi using blockscout v5 - aurora', async () => {
const abi = await defaultAbiFetcher.get('0x4988a896b1227218e4A686fdE5EabdcAbd91571f', 'aurora', 'implementationOnly')
test('can fetch abi using blockscout v5 - aurora', async () => {
const abi = await defaultAbiFetcher.get(
'0x4988a896b1227218e4A686fdE5EabdcAbd91571f',
'aurora',
'implementationOnly'
)

expect(abi.length).to.be.greaterThan(0)
})
expect(abi.length).to.be.greaterThan(0)
})

test('can fetch abi using blockscout v5 - fuse', async () => {
const abi = await defaultAbiFetcher.get('0xeEeEEb57642040bE42185f49C52F7E9B38f8eeeE', 'fuse', 'implementationOnly')
test('can fetch abi using blockscout v5 - fuse', async () => {
const abi = await defaultAbiFetcher.get(
'0xeEeEEb57642040bE42185f49C52F7E9B38f8eeeE',
'fuse',
'implementationOnly'
)

expect(abi.length).to.be.greaterThan(0)
})
expect(abi.length).to.be.greaterThan(0)
})

test('support raw implementation address', async () => {
const abi = await defaultAbiFetcher.get('0x0204Cd037B2ec03605CFdFe482D8e257C765fA1B', 'mainnet', 'implementationOnly')
test('support raw implementation address', async () => {
const abi = await defaultAbiFetcher.get(
'0x0204Cd037B2ec03605CFdFe482D8e257C765fA1B',
'mainnet',
'implementationOnly'
)

expect(abi.length).to.be.greaterThan(0)
})
expect(abi.length).to.be.greaterThan(0)
})

test('support fluid proxy implementation address', async () => {
const abi = await defaultAbiFetcher.get('0x264786EF916af64a1DB19F513F24a3681734ce92', 'mainnet', 'implementationOnly')
expect(abi.length).to.be.greaterThan(0)
})
}, {
retry: 2
})
test('support fluid proxy implementation address', async () => {
const abi = await defaultAbiFetcher.get(
'0x264786EF916af64a1DB19F513F24a3681734ce92',
'mainnet',
'implementationOnly'
)
expect(abi.length).to.be.greaterThan(0)
})
},
{
retry: 2
}
)

0 comments on commit 88261aa

Please sign in to comment.