Skip to content

Commit

Permalink
feat: add vendor-v2-borrower-collateral-balance-of (#1391)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdapper authored Jan 16, 2024
1 parent 7e19db8 commit dd89cb9
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/strategies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ import * as synthetixQuadratic_1 from './synthetix-quadratic_1';
import * as synthetix_1 from './synthetix_1';
import * as totalAxionShares from './total-axion-shares';
import * as unipoolSameToken from './unipool-same-token';
import * as vendorV2BorrowerCollateralBalanceOf from './vendor-v2-borrower-collateral-balance-of';
import * as voltVotingPower from './volt-voting-power';
import * as xdaiStakersAndHolders from './xdai-stakers-and-holders';

Expand Down Expand Up @@ -821,6 +822,8 @@ const strategies = {
synthetix_1,
'total-axion-shares': totalAxionShares,
'unipool-same-token': unipoolSameToken,
'vendor-v2-borrower-collateral-balance-of':
vendorV2BorrowerCollateralBalanceOf,
'volt-voting-power': voltVotingPower,
'xdai-stakers-and-holders': xdaiStakersAndHolders
};
Expand Down
13 changes: 13 additions & 0 deletions src/strategies/vendor-v2-borrower-collateral-balance-of/README.md
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
}
```
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 src/strategies/vendor-v2-borrower-collateral-balance-of/index.ts
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

View workflow job for this annotation

GitHub Actions / lint / Lint

'_debt' is defined but never used

Check warning on line 83 in src/strategies/vendor-v2-borrower-collateral-balance-of/index.ts

View workflow job for this annotation

GitHub Actions / fix-lint

'_debt' is defined but never used
address,
parseFloat(formatUnits(collAmount, options.collateralDecimals)) *
multiplier
])
);
}
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

View workflow job for this annotation

GitHub Actions / lint / Lint

Replace `⏎············"e.g.·0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984"⏎··········` with `"e.g.·0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984"`
"e.g. 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984"
],
"pattern": "^0x[a-fA-F0-9]{40}$",
"minLength": 42,
"maxLength": 42
},
"collateralDecimals": {
"type": "number",
"examples": [

Check failure on line 21 in src/strategies/vendor-v2-borrower-collateral-balance-of/schema.json

View workflow job for this annotation

GitHub Actions / lint / Lint

Replace `⏎············18⏎··········` with `18`
18
],
"title": "Decimals"
},
"weight": {
"type": "number",
"title": "Weight",
"examples": [

Check failure on line 29 in src/strategies/vendor-v2-borrower-collateral-balance-of/schema.json

View workflow job for this annotation

GitHub Actions / lint / Lint

Replace `⏎············0.5,⏎············2⏎··········` with `0.5,·2`
0.5,
2
]
}
},
"required": [

Check failure on line 35 in src/strategies/vendor-v2-borrower-collateral-balance-of/schema.json

View workflow job for this annotation

GitHub Actions / lint / Lint

Replace `⏎········"address",⏎········"collateralDecimals"⏎······` with `"address",·"collateralDecimals"`
"address",
"collateralDecimals"
],
"additionalProperties": false
}
}
}

0 comments on commit dd89cb9

Please sign in to comment.