Skip to content

Commit

Permalink
feat(blockchain-link): implement ethereum rpc call
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasklim authored and dev-pvl committed Oct 21, 2024
1 parent f64440f commit b8c9d0a
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/blockchain-link-types/src/blockbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
GetFiatRatesForTimestampsParams,
GetFiatRatesTickersListParams,
EstimateFeeParams,
EthereumCallParams,
AccountInfoParams,
} from './params';
import type { AccountBalanceHistory, FiatRatesBySymbol, TokenStandard } from './common';
Expand Down Expand Up @@ -210,6 +211,7 @@ declare function FSend(
params: GetFiatRatesForTimestampsParams,
): Promise<FiatRatesForTimestamp>;
declare function FSend(method: 'estimateFee', params: EstimateFeeParams): Promise<Fee>;
declare function FSend(method: 'rpcCall', params: EthereumCallParams): Promise<{ data: string }>;
declare function FSend(
method: 'subscribeAddresses',
params: { addresses: string[] },
Expand Down
1 change: 1 addition & 0 deletions packages/blockchain-link-types/src/constants/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const GET_ACCOUNT_UTXO = 'm_get_account_utxo';
export const GET_TRANSACTION = 'm_get_transaction';
export const GET_TRANSACTION_HEX = 'm_get_transaction_hex';
export const ESTIMATE_FEE = 'm_estimate_fee';
export const ETHEREUM_CALL = 'm_ethereum_call';
export const SUBSCRIBE = 'm_subscribe';
export const UNSUBSCRIBE = 'm_unsubscribe';
export const PUSH_TRANSACTION = 'm_push_tx';
Expand Down
1 change: 1 addition & 0 deletions packages/blockchain-link-types/src/constants/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const GET_ACCOUNT_BALANCE_HISTORY = 'r_get_account_balance_history';
export const GET_TRANSACTION = 'r_get_transaction';
export const GET_TRANSACTION_HEX = 'r_get_transaction_hex';
export const ESTIMATE_FEE = 'r_estimate_fee';
export const ETHEREUM_CALL = 'r_ethereum_call';
export const SUBSCRIBE = 'r_subscribe';
export const UNSUBSCRIBE = 'r_unsubscribe';
export const PUSH_TRANSACTION = 'r_push_tx';
Expand Down
7 changes: 7 additions & 0 deletions packages/blockchain-link-types/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
GetFiatRatesForTimestampsParams,
GetFiatRatesTickersListParams,
EstimateFeeParams,
EthereumCallParams,
AccountInfoParams,
} from './params';

Expand Down Expand Up @@ -77,6 +78,11 @@ export interface EstimateFee {
payload: EstimateFeeParams;
}

export interface EthereumCall {
type: typeof MESSAGES.ETHEREUM_CALL;
payload: EthereumCallParams;
}

export interface Subscribe {
type: typeof MESSAGES.SUBSCRIBE;
payload:
Expand Down Expand Up @@ -144,6 +150,7 @@ export type Message =
| ChannelMessage<GetAccountBalanceHistory>
| ChannelMessage<GetFiatRatesTickersList>
| ChannelMessage<EstimateFee>
| ChannelMessage<EthereumCall>
| ChannelMessage<Subscribe>
| ChannelMessage<Unsubscribe>
| ChannelMessage<PushTransaction>;
6 changes: 6 additions & 0 deletions packages/blockchain-link-types/src/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export interface EstimateFeeParams {
};
}

export interface EthereumCallParams {
from: string;
to: string;
data: string;
}

export interface AccountInfoParams {
descriptor: string; // address or xpub
details?: 'basic' | 'tokens' | 'tokenBalances' | 'txids' | 'txs'; // depth, default: 'basic'
Expand Down
8 changes: 8 additions & 0 deletions packages/blockchain-link-types/src/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ export interface EstimateFee {
}[];
}

export interface EthereumCall {
type: typeof RESPONSES.ETHEREUM_CALL;
payload: {
data: string;
};
}

export interface Subscribe {
type: typeof RESPONSES.SUBSCRIBE;
payload: { subscribed: boolean };
Expand Down Expand Up @@ -173,6 +180,7 @@ export type Response =
| ChannelMessage<GetFiatRatesForTimestamps>
| ChannelMessage<GetFiatRatesTickersList>
| ChannelMessage<EstimateFee>
| ChannelMessage<EthereumCall>
| ChannelMessage<Subscribe>
| ChannelMessage<Unsubscribe>
| ChannelMessage<Notification>
Expand Down
9 changes: 9 additions & 0 deletions packages/blockchain-link/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ class BlockchainLink extends TypedEmitter<Events> {
});
}

ethereumCall(
payload: MessageTypes.EthereumCall['payload'],
): Promise<ResponseTypes.EthereumCall['payload']> {
return this.sendMessage({
type: MESSAGES.ETHEREUM_CALL,
payload,
});
}

/**
* Subscribe for live changes in
* - blockchain i.e new blocks mined.
Expand Down
12 changes: 12 additions & 0 deletions packages/blockchain-link/src/workers/blockbook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ const estimateFee = async (request: Request<MessageTypes.EstimateFee>) => {
} as const;
};

const ethereumCall = async (request: Request<MessageTypes.EthereumCall>) => {
const api = await request.connect();
const resp = await api.ethereumCall(request.payload);

return {
type: RESPONSES.ETHEREUM_CALL,
payload: resp,
} as const;
};

const onNewBlock = ({ post }: Context, event: BlockNotification) => {
post({
id: -1,
Expand Down Expand Up @@ -417,6 +427,8 @@ const onRequest = (request: Request<MessageTypes.Message>) => {
return getFiatRatesTickersList(request);
case MESSAGES.ESTIMATE_FEE:
return estimateFee(request);
case MESSAGES.ETHEREUM_CALL:
return ethereumCall(request);
case MESSAGES.PUSH_TRANSACTION:
return pushTransaction(request);
case MESSAGES.SUBSCRIBE:
Expand Down
5 changes: 5 additions & 0 deletions packages/blockchain-link/src/workers/blockbook/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
AccountInfoParams,
EstimateFeeParams,
AccountBalanceHistoryParams,
EthereumCallParams,
} from '@trezor/blockchain-link-types/src/params';

import { BaseWebsocket } from '../baseWebsocket';
Expand Down Expand Up @@ -126,6 +127,10 @@ export class BlockbookAPI extends BaseWebsocket<BlockbookEvents> {
return this.send('estimateFee', payload);
}

ethereumCall(payload: EthereumCallParams) {
return this.send('rpcCall', payload);
}

getCurrentFiatRates(payload: GetCurrentFiatRates['payload']) {
return this.send('getCurrentFiatRates', payload);
}
Expand Down

0 comments on commit b8c9d0a

Please sign in to comment.