oracle-core icon indicating copy to clipboard operation
oracle-core copied to clipboard

[200 SigmaUSD] Newtypes for pool, refresh, oracle, etc. tokens

Open greenhat opened this issue 3 years ago • 4 comments

Motivation

To employ type checks in API where Token is expected now.

Implementation details

Make newtype for each token type and use them instead of Token and TokenId throughout the codebase. Each newtype checked ctor should check the token id against the one loaded from the config.

greenhat avatar Jun 09 '22 09:06 greenhat

Do you mean something like this?

for example,

fn display_update_diff(
    old_oracle_config: &OracleConfig,
    new_oracle_config: &OracleConfig,
    old_pool_box: PoolBoxWrapper,
    new_reward_tokens: Option<Token>,
) {
    let new_tokens = new_reward_tokens.unwrap_or_else(|| old_pool_box.reward_token());
    let new_pool_contract = PoolContract::new(PoolContractInputs::from((
        &new_oracle_config.pool_contract_parameters,
        &new_oracle_config.token_ids,
    )))
    .unwrap();
...

after this issue

fn display_update_diff(
    old_oracle_config: &OracleConfig,
    new_oracle_config: &OracleConfig,
    old_pool_box: PoolBoxWrapper,
    new_reward_tokens: Option<RewardToken>,
) {
    let new_tokens = new_reward_tokens.unwrap_or_else(|| old_pool_box.reward_token());
    let new_pool_contract = PoolContract::new(PoolContractInputs::from((
        &new_oracle_config.pool_contract_parameters,
        &new_oracle_config.token_ids,
    )))
    .unwrap();
...

hanbu97 avatar Aug 31 '22 14:08 hanbu97

Yes. Ideally, I'd want the newtype RewardToken to use instead of Token. But given that it's only the token id that differentiates new token types, I'm thinking of making a new polymorphic Token type and using it instead of Token. Something like:

struct SpecToken<T: TokenIdKind> {
  id: T,
  amount: TokenAmount
}

trait TokenIdKind {};

struct RewardTokenId(TokenId);

impl TokenIdKind for RewardTokenId {};

and use it as new_reward_token: Option<SpecToken<RewardTokenId>.

And when we now use TokenId, use RewardTokenId directly.

greenhat avatar Aug 31 '22 15:08 greenhat

@hanbu97 Hi, are you interested in working on this? I'd like to take this issue if it's available

sethdusek avatar Oct 22 '22 09:10 sethdusek

@SethDusek Sure, go ahead. I interested in it but do not figure out clearly how to work it out. Thank you.

hanbu97 avatar Oct 25 '22 05:10 hanbu97

Done in #164. The bounty is sent. Thank you!

greenhat avatar Nov 22 '22 07:11 greenhat