Consensys / ethql

A GraphQL interface to Ethereum :fire:
Apache License 2.0
622 stars 85 forks source link

Unit conversion: For transaction values #119

Open jormal opened 5 years ago

jormal commented 5 years ago

In issue #45, it fixed that unit conversions for balances. but, for values are still not working.

{
  transaction(hash:"0x1234...") {
    value (unit:ether)
  }
}

I think it also do conversion using web3.utils.fromWei().


I don't know is it right. but, I think it can be conversion by following this.

In, src/core/services/eth-service/index.ts

...
export interface EthService {
    ...
    fetchValue(tx: EthqlTransaction): Promise<string>;
    ...
}
...

In, src/core/services/eth-service/impl/web3-eth-service.ts

...
export class Web3EthService implements EthService {
    ...
    public async fetchValue(tx: EthqlTransaction): Promise<string> {
        return tx && tx.value;
    }
    ...
}
...

In, src/core/model/index.ts

...
export class EthqlTransaction {
    ...
    public readonly value: string;
    ...
    constructor(tx: Transaction, logs?: Log[]) {
        ...
        this.value = value;
        ...
    }
}
...

In, src/core/resolvers/transaction.ts

...
async function value(obj: EthqlTransaction, { unit } /* args */, { services }: EthqlContext): Promise<string> {
    const val = await services.ethService.fetchValue(obj);
    return unit ? Web3.utils.fromWei(val, unit) : val;
}
...

I'm Newbie and I don't know if I'm right.