message: Refactor token-to-usd helper to simplify and improve errors
diff: @@ -28,43 +28,45 @@ const INTERVAL = config.environment === 'test' ? 1000 : 60 * 1000;
  export default class TokenToUsdHelper extends Helper<Signature> {
  @service('token-to-usd') declare tokenToUsdService: TokenToUsdService;
  updateInterval: ReturnType<typeof setInterval> | undefined;
  - tokenAddress: ChainAddress | undefined;
  + lastTokenAddress: ChainAddress | undefined;
  compute(_positional: never, named: NamedArgs): string {
  - let tokenAddress: ChainAddress,
  - tokenAmount: BigNumber,
  - tokenDecimals: number;
  - const tokenQuantity = named.tokenQuantity;
  - if (tokenQuantity) {
  - tokenAddress = tokenQuantity.address;
  - tokenAmount = tokenQuantity.count;
  - tokenDecimals = tokenQuantity.decimals;
  - } else {
  - named = named as NamedArgs;
  - tokenAddress = named.tokenAddress as string;
  - tokenAmount = named.tokenAmount as BigNumber;
  - tokenDecimals = named.tokenDecimals as number;
  + let tokenQuantity = named.tokenQuantity;
  + if (!tokenQuantity) {
  + const { tokenAddress, tokenDecimals, tokenAmount } = named;
  + if (!tokenAddress || !tokenDecimals || !tokenAmount) {
  + throw new Error(
  + 'Must provide tokenQuantity or tokenAddress/tokenDecimals/tokenAmount to token-to-usd helper'
  + );
  + }
  + const token: SelectableToken = {
  + address: tokenAddress,
  + name: 'unknown',
  + symbol: 'unknown',
  + decimals: tokenDecimals,
  + };
  + tokenQuantity = new TokenQuantity(token, tokenAmount);
  }
  - this.ensureTimer(tokenAddress);
  - const usdcAmount = this.computeUsdcAmount(
  - tokenAddress,
  - tokenAmount,
  - tokenDecimals
  - );
  + this.ensureTimer(tokenQuantity.address);
  + const usdcAmount = this.tokenToUsdService.toUsdc(tokenQuantity);
  if (usdcAmount) {
  - return `$ ${nativeUnitsToDecimal([usdcAmount, tokenDecimals, 2])}`;
  + return `$ ${nativeUnitsToDecimal([
  + usdcAmount,
  + tokenQuantity.decimals,
  + 2,
  + ])}`;
  }
  return 'Converting to USD...';
  }
  ensureTimer(tokenAddress: ChainAddress) {
  - if (tokenAddress !== this.tokenAddress) {
  + if (tokenAddress !== this.lastTokenAddress) {
  if (this.updateInterval) {
  clearInterval(this.updateInterval);
  this.updateInterval = undefined;
  }
  - this.tokenAddress = tokenAddress;
  + this.lastTokenAddress = tokenAddress;
  }
  if (!this.updateInterval) {
  this.updateInterval = setInterval(() => {
  @@ -75,21 +77,6 @@ export default class TokenToUsdHelper extends Helper<Signature> {
  }
  }
  - computeUsdcAmount(
  - tokenAddress: ChainAddress,
  - amount: BigNumber,
  - decimals: number
  - ) {
  - const token: SelectableToken = {
  - address: tokenAddress,
  - name: 'unknown',
  - symbol: 'unknown',
  - decimals: decimals,
  - };
  - const tokenQuantity = new TokenQuantity(token, BigNumber.from(amount));
  - return this.tokenToUsdService.toUsdc(tokenQuantity);
  - }
  -
  willDestroy() {
  if (this.updateInterval) {
  clearInterval(this.updateInterval);
  