Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vendor-v2-borrower-collateral-balance-of] feat: add vendor-v2-borrower-collateral-balance-of #1391

Merged
merged 1 commit into from
Jan 16, 2024
Merged
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
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]]) => [
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": [
"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
}
}
}
Loading