forked from smartcontractkit/external-adapters-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
57 lines (51 loc) · 1.91 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { BigNumberish, ethers } from 'ethers'
import { types } from '@chainlink/token-allocation-adapter'
import { getSymbol } from '../symbols'
import { AdapterDataProviderError, util } from '@chainlink/ea-bootstrap'
/*
NOTICE!
The current implementation is fetching data directly from SetToken contracts (https://etherscan.io/address/0x78733fa5e70e3ab61dc49d93921b289e4b667093#code)
Note that this implementation won't work in other networks unless we deploy a copy of the contract.
The correct implementation should use SetProtocol.js typed library instead to fetch data directly from the SetToken contract directly.
The ChainlinkAdapter.getAllocations(ISetToken _setToken) should be reimplemented in JS in order to use it.
*/
const ABI = [
{
inputs: [{ internalType: 'contract ISetToken', name: '_setToken', type: 'address' }],
name: 'getAllocations',
outputs: [
{ internalType: 'address[]', name: '', type: 'address[]' },
{ internalType: 'int256[]', name: '', type: 'int256[]' },
],
stateMutability: 'view',
type: 'function',
},
]
export const getAllocations = async (
contractAddress: string,
setAddress: string,
rpcUrl: string,
network: string,
): Promise<types.TokenAllocations> => {
const provider = new ethers.providers.JsonRpcProvider(rpcUrl)
const index = new ethers.Contract(contractAddress, ABI, provider)
let addresses
let balances: BigNumberish[]
try {
;[addresses, balances] = await index.getAllocations(setAddress)
} catch (e: any) {
throw new AdapterDataProviderError({
network,
message: util.mapRPCErrorMessage(e?.code, e?.message),
cause: e,
})
}
// Token balances are coming already normalized as 18 decimals token
return await Promise.all(
addresses.map(async (address: string, i: number) => ({
balance: balances[i],
symbol: await getSymbol(address, rpcUrl, network),
decimals: 18,
})),
)
}