• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Mintbase / mintbase-js / 10492825701

21 Aug 2024 03:27PM CUT coverage: 72.702%. Remained the same
10492825701

push

github

web-flow
@mintbase-js/rpc: add rpcUrl mandatory param (#521)

Co-authored-by: Benjamin Smith <bh2smith@users.noreply.github.com>
Co-authored-by: mintbaseteam <eng@mintbase.io>
Co-authored-by: Till <31419678+tifrel@users.noreply.github.com>

877 of 1390 branches covered (63.09%)

Branch coverage included in aggregate %.

33 of 35 new or added lines in 9 files covered. (94.29%)

1195 of 1460 relevant lines covered (81.85%)

13.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.92
/packages/rpc/src/util.ts
1
import fetch from 'cross-fetch';
21✔
2

3
export const callNearRpc = async ({params, method, rpcUrl}):  Promise<{ result: Record<string, unknown>, error: unknown } | undefined> => {
30✔
4
  return await requestFromNearRpc({
30✔
5
    jsonrpc: '2.0',
6
    id: 'dontcare',
7
    method:method,
8
    params:params,
9
  }, rpcUrl)
10
}
11

12

13
export const requestFromNearRpc = async (
21✔
14
  body: Record<string, unknown>,
15
  rpcUrl: string): Promise<{ result: Record<string, unknown>, error: unknown } | undefined> => {
42✔
16

17

18
   if (!rpcUrl) {
42!
NEW
19
    throw new Error('please add a valid RPC Url, you can check a list of providers on: https://docs.near.org/api/rpc/providers');
×
20
  }
21

22
  try {
42✔
23
    const res = await fetch(rpcUrl, {
42✔
24
      method: 'POST',
25
      body: JSON.stringify(body),
26
      headers: { 'Content-type': 'application/json' },
27
    });
28

29
    return res.json();
42✔
30
  } catch (error) {
NEW
31
    return { result: {}, error };
×
32
  }
33
};
34

35

36
export const callViewMethod = async <T>({
21✔
37
  contractId,
38
  method,
39
  args,
40
  rpcUrl,
41
}: {
42
  contractId: string;
43
  method: string;
44
  args?: Record<string, unknown>;
45
  rpcUrl?: string
46
}): Promise<T> => {
12✔
47
  const args_base64 = args
12!
48
    ? Buffer.from(JSON.stringify(args), 'utf-8').toString('base64')
49
    : '';
50

51
  const res = await requestFromNearRpc({
12✔
52
    jsonrpc: '2.0',
53
    id: 'dontcare',
54
    method: 'query',
55
    params: {
56
      request_type: 'call_function',
57
      finality: 'final',
58
      account_id: contractId,
59
      method_name: method,
60
      args_base64,
61
    },
62
  }, rpcUrl);
63

64
  if (res?.error) {
12!
65
    throw res.error;
×
66
  }
67

68
  const resultBuffer = Buffer.from(res?.result?.result as string);
12!
69
  const parsed = JSON.parse(resultBuffer.toString());
12✔
70
  return parsed as T;
12✔
71
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc