-
Notifications
You must be signed in to change notification settings - Fork 781
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add vendor-v2-borrower-collateral-balance-of (#1391)
- Loading branch information
Showing
5 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/strategies/vendor-v2-borrower-collateral-balance-of/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# vendor-v2-borrower-collateral-balance-of | ||
|
||
This returns the voting power of a borrower locked in a vendor lending pool. | ||
|
||
Here is an example of parameters: | ||
|
||
```json | ||
{ | ||
"address": "0xA4B49b1A717E9e002104E2B4517A8B7086DF479b", | ||
"collateralDecimals": 9, | ||
"weight": 5 | ||
} | ||
``` |
21 changes: 21 additions & 0 deletions
21
src/strategies/vendor-v2-borrower-collateral-balance-of/examples.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[ | ||
{ | ||
"name": "Example query", | ||
"strategy": { | ||
"name": "vendor-v2-borrower-collateral-balance-of", | ||
"params": { | ||
"address": "0xA4B49b1A717E9e002104E2B4517A8B7086DF479b", | ||
"collateralDecimals": 9, | ||
"weight": 5 | ||
} | ||
}, | ||
"network": "42161", | ||
"addresses": [ | ||
"0xeFAD4c712CBa7F7a136C6B3C6f861fb787a348a0", | ||
"0xc12DE812ae612B6d514b52d529F97f6Acb524c8E", | ||
"0x18252F28234C010Cf3353A82f3cBe71DB1B74773", | ||
"0x82aF49447D8a07e3bd95BD0d56f35241523fBab1" | ||
], | ||
"snapshot": 170740021 | ||
} | ||
] |
89 changes: 89 additions & 0 deletions
89
src/strategies/vendor-v2-borrower-collateral-balance-of/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { BigNumberish } from '@ethersproject/bignumber'; | ||
import { formatUnits } from '@ethersproject/units'; | ||
import { AbiCoder } from '@ethersproject/abi'; | ||
import { Multicaller } from '../../utils'; | ||
import { BlockTag, StaticJsonRpcProvider } from '@ethersproject/providers'; | ||
|
||
export const author = '0xdapper'; | ||
export const version = '0.1.0'; | ||
|
||
const abi = [ | ||
'function debts(address borrower) external view returns (uint256, uint256)', | ||
// (poolType, owner, expiry, colToken, protocolFee, lendToken, ltv, pauseTime, lendRatio, feeRatesAndType) | ||
'function getPoolSettings() external view returns (uint8, address, uint48, address, uint48, address, uint48, uint48, uint256, address[], bytes32)' | ||
]; | ||
|
||
const decodePoolSettings = (poolSettings: string) => { | ||
const abiCoder = new AbiCoder(); | ||
const [ | ||
[ | ||
poolType, | ||
owner, | ||
expiry, | ||
colToken, | ||
protocolFee, | ||
lendToken, | ||
ltv, | ||
pauseTime, | ||
lendRatio, | ||
allowList, | ||
feeRatesAndType | ||
] | ||
] = abiCoder.decode( | ||
[ | ||
'(uint8, address, uint48, address, uint48, address, uint48, uint48, uint256, address[], bytes32)' | ||
], | ||
poolSettings | ||
); | ||
return { | ||
poolType, | ||
owner, | ||
expiry, | ||
colToken, | ||
protocolFee, | ||
lendToken, | ||
ltv, | ||
pauseTime, | ||
lendRatio, | ||
feeRatesAndType, | ||
allowList | ||
}; | ||
}; | ||
|
||
export async function strategy( | ||
space, | ||
network, | ||
provider: StaticJsonRpcProvider, | ||
addresses, | ||
options, | ||
snapshot | ||
): Promise<Record<string, number>> { | ||
const blockTag: BlockTag = typeof snapshot === 'number' ? snapshot : 'latest'; | ||
const blockTime = (await provider.getBlock(blockTag)).timestamp; | ||
const poolSettings = decodePoolSettings( | ||
await provider.call( | ||
{ | ||
to: options.address, | ||
data: '0xe4a0ce2f' | ||
}, | ||
blockTag | ||
) | ||
); | ||
const hasExpired = poolSettings.expiry < blockTime; | ||
|
||
const multi = new Multicaller(network, provider, abi, { blockTag }); | ||
addresses.forEach((address) => | ||
multi.call(address, options.address, 'debts', [address]) | ||
); | ||
const result: Record<string, [BigNumberish, BigNumberish]> = | ||
await multi.execute(); | ||
const multiplier = hasExpired ? 0 : options.weight || 1; | ||
|
||
return Object.fromEntries( | ||
Object.entries(result).map(([address, [_debt, collAmount]]) => [ | ||
Check warning on line 83 in src/strategies/vendor-v2-borrower-collateral-balance-of/index.ts GitHub Actions / lint / Lint
|
||
address, | ||
parseFloat(formatUnits(collAmount, options.collateralDecimals)) * | ||
multiplier | ||
]) | ||
); | ||
} |
42 changes: 42 additions & 0 deletions
42
src/strategies/vendor-v2-borrower-collateral-balance-of/schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$ref": "#/definitions/Strategy", | ||
"definitions": { | ||
"Strategy": { | ||
"title": "Strategy", | ||
"type": "object", | ||
"properties": { | ||
"address": { | ||
"type": "string", | ||
"title": "Contract address", | ||
"examples": [ | ||
Check failure on line 12 in src/strategies/vendor-v2-borrower-collateral-balance-of/schema.json GitHub Actions / lint / Lint
|
||
"e.g. 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984" | ||
], | ||
"pattern": "^0x[a-fA-F0-9]{40}$", | ||
"minLength": 42, | ||
"maxLength": 42 | ||
}, | ||
"collateralDecimals": { | ||
"type": "number", | ||
"examples": [ | ||
18 | ||
], | ||
"title": "Decimals" | ||
}, | ||
"weight": { | ||
"type": "number", | ||
"title": "Weight", | ||
"examples": [ | ||
0.5, | ||
2 | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"address", | ||
"collateralDecimals" | ||
], | ||
"additionalProperties": false | ||
} | ||
} | ||
} |