import { getAddress } from '@ethersproject/address' ; import { BigNumber } from '@ethersproject/bignumber' ; import { Contract } from '@ethersproject/contracts' ; import { Provider } from '@ethersproject/providers' ; import { subgraphRequest } from '../../utils' ; import { DebtCacheABI , debtL1 , debtL2 , returnGraphParams , SNXHoldersResult , SynthetixStateABI } from '../synthetix/helper' ; export const author = 'andytcf' ; export const version = '1.0.0' ; const MED_PRECISE_UNIT = 1e18 ; const SynthetixStateContractAddress = '0x4b9Ca5607f1fF8019c1C6A3c2f0CC8de622D5B82' ; const DebtCacheContractAddress = '0x9D5551Cd3425Dd4585c3E7Eb7E4B98902222521E' ; const defaultGraphs = { '1' : 'https://api.thegraph.com/subgraphs/name/synthetixio-team/synthetix' , '10' : 'https://api.thegraph.com/subgraphs/name/synthetixio-team/optimism-main' } ; const loadLastDebtLedgerEntry = async ( provider , snapshot ) => { const contract = new Contract ( SynthetixStateContractAddress , SynthetixStateABI , provider ) ; const lastDebtLedgerEntry = await contract . lastDebtLedgerEntry ( { blockTag : snapshot } ) ; return BigNumber . from ( lastDebtLedgerEntry ) ; } ; const loadL1TotalDebt = async ( provider , snapshot ) => { const contract = new Contract ( DebtCacheContractAddress , DebtCacheABI , provider ) ; const currentDebtObject = await contract . currentDebt ( { blockTag : snapshot } ) ; return Number ( currentDebtObject . debt ) / MED_PRECISE_UNIT ; } ; export async function strategy ( _space , _network , _provider , _addresses , _ , snapshot ) { const score = { } ; const blockTag = typeof snapshot === 'number' ? snapshot : 'latest' ; const totalL1Debt = await loadL1TotalDebt ( _provider , snapshot ) ; const lastDebtLedgerEntry = await loadLastDebtLedgerEntry ( _provider , snapshot ) ; const totalL2Debt = 48646913 ; const lastDebtLedgerEntryL2 = 9773647546760863848975891 ; const normalisedL2CRatio = 500 / 400 ; const L2BlockNumber = 919219 ; const scaledTotalL2Debt = totalL2Debt * normalisedL2CRatio ; const l1Results = ( await subgraphRequest ( defaultGraphs [ 1 ] , returnGraphParams ( blockTag , _addresses ) ) ) as SNXHoldersResult ; if ( l1Results && l1Results . snxholders ) { for ( let i = 0 ; i < l1Results . snxholders . length ; i ++ ) { const holder = l1Results . snxholders [ i ] ; const vote = await debtL1 ( holder . initialDebtOwnership , holder . debtEntryAtIndex , totalL1Debt , scaledTotalL2Debt , lastDebtLedgerEntry , true ) ; score [ getAddress ( holder . id ) ] = vote ; } } const l2Results = ( await subgraphRequest ( defaultGraphs [ 10 ] , returnGraphParams ( L2BlockNumber , _addresses ) ) ) as SNXHoldersResult ; if ( l2Results && l2Results . snxholders ) { for ( let i = 0 ; i < l2Results . snxholders . length ; i ++ ) { const holder = l2Results . snxholders [ i ] ; const vote = await debtL2 ( holder . initialDebtOwnership , holder . debtEntryAtIndex , totalL1Debt , scaledTotalL2Debt , lastDebtLedgerEntryL2 , true ) ; if ( score [ getAddress ( holder . id ) ] ) { score [ getAddress ( holder . id ) ] += vote ; } else { score [ getAddress ( holder . id ) ] = vote ; } } } return score || { } ; }