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

Fix getCollaterals and getCollateralValue #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

rramsden
Copy link
Contributor

@rramsden rramsden commented Aug 14, 2021

Context

I noticed since bETH was introduced Borrow class getCollaterals and getCollateralValue no longer function.

A case statement was introduced which broke functionality for AddressFromJsonProvider. Before this method just returned the custody contract address but now it can return a null value if the switch case on collateral doesn't match:

custody(_denom: MARKET_DENOMS, collateral: COLLATERAL_DENOMS): string {
switch (collateral) {
case COLLATERAL_DENOMS.UBLUNA: {
return this.data.mmCustody
}
case COLLATERAL_DENOMS.UBETH: {
return this.data.mmCustodyBEth
}
}
}

For example, when Borrow calls queryCustodyBorrower the parameter custody is passed with an instance of MARKET_DENOMS and not with the a collateral denomination:

const userBalance = await queryCustodyBorrower({
lcd: this._lcd,
...getCollateralsOption,
custody: getCollateralsOption.market,
})(this._addressProvider);

Then custody is type-casted to type COLLATERAL_DENOMS and we just get a null value back from our switch case

export const queryCustodyBorrower = ({
lcd,
market,
custody,
address,
}: Option) => async (
addressProvider: AddressProvider,
): Promise<BorrowerResponse> => {
const custodyAddress = custody.startsWith('terra1')
? custody
: addressProvider.custody(market, custody as COLLATERAL_DENOMS);
return lcd.wasm.contractQuery<BorrowerResponse>(custodyAddress, {
borrower: {
address: address,
},
});
};

How to reproduce

You can see master branch no longer works running the following from node console:

let walletAddress = 'YOUR_TERRA_WALLET_ADDRESS'

const Terra = require('@terra-money/terra.js')
const AnchorJS = require('./dist/index')

const LCDClient = Terra.LCDClient
const Anchor = AnchorJS.Anchor
const columbus4 = AnchorJS.columbus4
const AddressProviderFromJson = AnchorJS.AddressProviderFromJson

let client = new LCDClient({
  URL: 'https://lcd.terra.dev',
  chainID: 'columbus-4'
})
let provider = new AddressProviderFromJson(columbus4)
let anchor = new Anchor(client, provider)

anchor.borrow.getCollaterals({ market: 'uusd', address: walletAddress }).then(console.log)
anchor.borrow.getCollateralValue({ market: 'uusd', address: walletAddress }).then(console.log)

@rramsden rramsden changed the title Fix facade getcollaterals Fix getCollaterals and getCollateralValue Aug 14, 2021
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

})(this._addressProvider);

if (userBalance.balance === '0') {
return null;
}
Copy link
Contributor Author

@rramsden rramsden Aug 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a balance check here which returns null. It's a bit weird since if this condition is met you get something like the following back when this function is called:

[ null ]

Better to just return an actual entry e.g.

[
  {
    collateral: 'terra1....',
    balance: '0.000000000000000000'
  },
  {
    collateral: 'terra1....',
    balance: '0.000000000000000000'
  }
]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just my opinion but this was added to make these functions work properly again

const oraclePrice = await queryOraclePrices({ lcd: this._lcd, limit: 30 })(
this._addressProvider,
);
const collaterals = await this.getCollaterals(getCollateralValueOption);

if (collaterals.length === 1 && collaterals[0] === null) {
return new Dec(0).toString();
}
Copy link
Contributor Author

@rramsden rramsden Aug 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be safely removed if the null return is removed, see other comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant