Skip to content

Commit

Permalink
feat(connect): implement ethereum rpc call
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasklim committed Oct 26, 2024
1 parent db9824d commit 8ab607c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
63 changes: 63 additions & 0 deletions packages/connect/src/api/blockchainEvmRpcCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { AbstractMethod, Payload } from '../core/AbstractMethod';
import { validateParams } from './common/paramsValidator';
import { ERRORS } from '../constants';
import { CoinInfo } from '../types';
import { initBlockchain, isBackendSupported } from '../backend/BlockchainLink';
import { getCoinInfo } from '../data/coinInfo';

type Params = {
coinInfo: CoinInfo;
identity?: string;
request: Omit<Payload<'blockchainEvmRpcCall'>, 'method' | 'coin'>;
};

export default class BlockchainEvmRpcCall extends AbstractMethod<'blockchainEvmRpcCall', Params> {
init() {
this.useDevice = false;
this.useUi = false;

const { payload } = this;

// validate incoming parameters
validateParams(payload, [
{ name: 'coin', type: 'string', required: true },
{ name: 'identity', type: 'string' },
{ name: 'from', type: 'string' },
{ name: 'to', type: 'string', required: true },
{ name: 'data', type: 'string', required: true },
]);

const coinInfo = getCoinInfo(payload.coin);

if (!coinInfo) {
throw ERRORS.TypedError('Method_UnknownCoin');
}
// validate backend
isBackendSupported(coinInfo);

this.params = {
coinInfo,
identity: payload.identity,
request: {
from: payload.from,
to: payload.to,
data: payload.data,
},
};
}

get info() {
return 'Blockchain Evm Rpc Call';
}

async run() {
const backend = await initBlockchain(
this.params.coinInfo,
postMessage,
this.params.identity,
);
const response = await backend.rpcCall(this.params.request);

return response;
}
}
1 change: 1 addition & 0 deletions packages/connect/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { default as blockchainDisconnect } from './blockchainDisconnect';
export { default as blockchainEstimateFee } from './blockchainEstimateFee';
export { default as blockchainGetAccountBalanceHistory } from './blockchainGetAccountBalanceHistory';
export { default as blockchainGetCurrentFiatRates } from './blockchainGetCurrentFiatRates';
export { default as blockchainEvmRpcCall } from './blockchainEvmRpcCall';
export { default as blockchainGetFiatRatesForTimestamps } from './blockchainGetFiatRatesForTimestamps';
export { default as blockchainGetTransactions } from './blockchainGetTransactions';
export { default as blockchainSetCustomBackend } from './blockchainSetCustomBackend';
Expand Down
4 changes: 4 additions & 0 deletions packages/connect/src/backend/Blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ export class Blockchain {
return this.link.getAccountUtxo(descriptor);
}

rpcCall(params: BlockchainLinkParams<'rpcCall'>) {
return this.link.rpcCall(params);
}

async estimateFee(request: Parameters<typeof this.link.estimateFee>[0]) {
const { blocks } = request;
// cache should be used if there is no specific data (ethereum case) and requested blocks are already cached/downloaded
Expand Down
2 changes: 2 additions & 0 deletions packages/connect/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export const factory = <
blockchainGetFiatRatesForTimestamps: params =>
call({ ...params, method: 'blockchainGetFiatRatesForTimestamps' }),

blockchainEvmRpcCall: params => call({ ...params, method: 'blockchainEvmRpcCall' }),

blockchainDisconnect: params => call({ ...params, method: 'blockchainDisconnect' }),

blockchainEstimateFee: params => call({ ...params, method: 'blockchainEstimateFee' }),
Expand Down
6 changes: 6 additions & 0 deletions packages/connect/src/types/api/blockchainEvmRpcCall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { BlockchainLinkParams, BlockchainLinkResponse } from '@trezor/blockchain-link';
import type { CommonParamsWithCoin, Response } from '../params';

export declare function blockchainEvmRpcCall(
params: CommonParamsWithCoin & BlockchainLinkParams<'rpcCall'>,
): Response<BlockchainLinkResponse<'rpcCall'>>;
3 changes: 3 additions & 0 deletions packages/connect/src/types/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { blockchainDisconnect } from './blockchainDisconnect';
import { blockchainEstimateFee } from './blockchainEstimateFee';
import { blockchainGetAccountBalanceHistory } from './blockchainGetAccountBalanceHistory';
import { blockchainGetCurrentFiatRates } from './blockchainGetCurrentFiatRates';
import { blockchainEvmRpcCall } from './blockchainEvmRpcCall';
import { blockchainGetFiatRatesForTimestamps } from './blockchainGetFiatRatesForTimestamps';
import { blockchainGetTransactions } from './blockchainGetTransactions';
import { blockchainSetCustomBackend } from './blockchainSetCustomBackend';
Expand Down Expand Up @@ -127,6 +128,8 @@ export interface TrezorConnect {
// todo: link docs
blockchainGetCurrentFiatRates: typeof blockchainGetCurrentFiatRates;

blockchainEvmRpcCall: typeof blockchainEvmRpcCall;

// todo: link docs
blockchainGetFiatRatesForTimestamps: typeof blockchainGetFiatRatesForTimestamps;

Expand Down

0 comments on commit 8ab607c

Please sign in to comment.