Skip to content

Commit

Permalink
Merge pull request #258 from gnosis/feature/add-chainInfo-types
Browse files Browse the repository at this point in the history
Added getChainInfo method and types from safe-react-gateway-sdk
  • Loading branch information
DaniSomoza authored Nov 24, 2021
2 parents 7331a88 + d9ae749 commit 48f9874
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 3 deletions.
13 changes: 13 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"mode": "exit",
"tag": "next",
"initialVersions": {
"@gnosis.pm/cra-template-safe-app": "4.0.1",
"@gnosis.pm/safe-apps-provider": "0.9.0",
"@gnosis.pm/safe-apps-react-sdk": "4.0.6",
"@gnosis.pm/safe-apps-sdk": "6.0.0",
"@gnosis.pm/safe-apps-web3-react": "0.6.5",
"@gnosis.pm/safe-apps-web3modal": "5.0.0"
},
"changesets": []
}
5 changes: 5 additions & 0 deletions .changeset/yellow-planes-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gnosis.pm/safe-apps-sdk': minor
---

Adding typing for retrieving network information in the getSafeInfo method
1 change: 1 addition & 0 deletions packages/safe-apps-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"license": "MIT",
"dependencies": {
"@changesets/cli": "^2.16.0",
"@gnosis.pm/safe-react-gateway-sdk": "^2.5.6",
"ethers": "^5.4.7"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/safe-apps-sdk/src/communication/methods.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum Methods {
sendTransactions = 'sendTransactions',
rpcCall = 'rpcCall',
getChainInfo = 'getChainInfo',
getSafeInfo = 'getSafeInfo',
getTxBySafeTxHash = 'getTxBySafeTxHash',
getSafeBalances = 'getSafeBalances',
Expand Down
19 changes: 18 additions & 1 deletion packages/safe-apps-sdk/src/safe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import { ethers } from 'ethers';
import { EIP_1271_INTERFACE, EIP_1271_BYTES_INTERFACE, MAGIC_VALUE_BYTES, MAGIC_VALUE } from './signatures';
import { Methods } from '../communication/methods';
import { RPC_CALLS } from '../eth/constants';
import { Communicator, SafeInfo, SafeBalances, GetBalanceParams, RPCPayload, TransactionConfig } from '../types';
import {
Communicator,
SafeInfo,
ChainInfo,
SafeBalances,
GetBalanceParams,
RPCPayload,
TransactionConfig,
} from '../types';

class Safe {
private readonly communicator: Communicator;
Expand All @@ -11,6 +19,15 @@ class Safe {
this.communicator = communicator;
}

async getChainInfo(): Promise<ChainInfo> {
const response = await this.communicator.send<Methods.getChainInfo, undefined, ChainInfo>(
Methods.getChainInfo,
undefined,
);

return response.data;
}

async getInfo(): Promise<SafeInfo> {
const response = await this.communicator.send<Methods.getSafeInfo, undefined, SafeInfo>(
Methods.getSafeInfo,
Expand Down
40 changes: 39 additions & 1 deletion packages/safe-apps-sdk/src/safe/safe.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import SDK from '../sdk';
import { SafeInfo } from '../types';
import { SafeInfo, ChainInfo } from '../types';
import { Methods } from '../communication/methods';

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
Expand Down Expand Up @@ -355,4 +355,42 @@ describe('Safe Apps SDK safe methods', () => {
);
});
});

describe('SDK.safe.getChainInfo', () => {
test('Should send a valid message to the interface', () => {
sdkInstance.safe.getChainInfo();

expect(postMessageSpy).toHaveBeenCalledWith(expect.objectContaining({ method: Methods.getChainInfo }), '*');
});

test('should resolve the correct ChainInfo types', async () => {
const safeInfoSpy = jest.spyOn(sdkInstance.safe, 'getChainInfo');
safeInfoSpy.mockImplementationOnce(
(): Promise<ChainInfo> =>
Promise.resolve({
chainName: 'rinkeby',
chainId: '4',
shortName: 'rin',
nativeCurrency: {
name: 'ether',
symbol: 'eth',
decimals: 18,
logoUri: 'ethUri',
},
}),
);
const chainInfo = await sdkInstance.safe.getChainInfo();
expect(chainInfo).toMatchObject({
chainName: 'rinkeby',
chainId: '4',
shortName: 'rin',
nativeCurrency: {
name: 'ether',
symbol: 'eth',
decimals: 18,
logoUri: 'ethUri',
},
});
});
});
});
3 changes: 2 additions & 1 deletion packages/safe-apps-sdk/src/types/messaging.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Methods } from '../communication/methods';
import { SafeInfo, SendTransactionsResponse } from './sdk';
import { SafeInfo, ChainInfo, SendTransactionsResponse } from './sdk';
import { GatewayTransactionDetails, SafeBalances } from './gateway';

export type RequestId = string;
Expand All @@ -10,6 +10,7 @@ export interface MethodToResponse {
[Methods.sendTransactions]: SendTransactionsResponse;
[Methods.rpcCall]: unknown;
[Methods.getSafeInfo]: SafeInfo;
[Methods.getChainInfo]: ChainInfo;
[Methods.getTxBySafeTxHash]: GatewayTransactionDetails;
[Methods.getSafeBalances]: SafeBalances[];
[Methods.signMessage]: SendTransactionsResponse;
Expand Down
6 changes: 6 additions & 0 deletions packages/safe-apps-sdk/src/types/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { ChainInfo as _ChainInfo } from '@gnosis.pm/safe-react-gateway-sdk';

export type ChainInfo = Pick<_ChainInfo, 'chainName' | 'chainId' | 'shortName' | 'nativeCurrency'>;

export { NativeCurrency } from '@gnosis.pm/safe-react-gateway-sdk';

export type BaseTransaction = {
to: string;
value: string;
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,13 @@
"@ethersproject/properties" "^5.5.0"
"@ethersproject/strings" "^5.5.0"

"@gnosis.pm/safe-react-gateway-sdk@^2.5.6":
version "2.5.6"
resolved "https://registry.yarnpkg.com/@gnosis.pm/safe-react-gateway-sdk/-/safe-react-gateway-sdk-2.5.6.tgz#7b8713bb16a542c4af27d6e1f2d8925a21aaeb51"
integrity sha512-flM/i80oFwSRlW0U8HE7QurwwPpNTUjBv7gZNAjgOpGhub51r7Uf1LtSm89Mnxnh2mtj4Whl8ZC4RMoXdV3yvw==
dependencies:
isomorphic-unfetch "^3.1.0"

"@humanwhocodes/config-array@^0.5.0":
version "0.5.0"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.5.0.tgz#1407967d4c6eecd7388f83acf1eaf4d0c6e58ef9"
Expand Down Expand Up @@ -5020,6 +5027,14 @@ isobject@^3.0.1:
resolved "https://registry.yarnpkg.com/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df"
integrity sha1-TkMekrEalzFjaqH5yNHMvP2reN8=

isomorphic-unfetch@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/isomorphic-unfetch/-/isomorphic-unfetch-3.1.0.tgz#87341d5f4f7b63843d468438128cb087b7c3e98f"
integrity sha512-geDJjpoZ8N0kWexiwkX8F9NkTsXhetLPVbZFQ+JTW239QNOwvB0gniuR1Wc6f0AMTn7/mFGyXvHTifrCp/GH8Q==
dependencies:
node-fetch "^2.6.1"
unfetch "^4.2.0"

isstream@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"
Expand Down Expand Up @@ -8289,6 +8304,11 @@ unbox-primitive@^1.0.1:
has-symbols "^1.0.2"
which-boxed-primitive "^1.0.2"

unfetch@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/unfetch/-/unfetch-4.2.0.tgz#7e21b0ef7d363d8d9af0fb929a5555f6ef97a3be"
integrity sha512-F9p7yYCn6cIW9El1zi0HI6vqpeIvBsr3dSuRO6Xuppb1u5rXpCPmMvLSyECLhybr9isec8Ohl0hPekMVrEinDA==

unique-filename@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/unique-filename/-/unique-filename-1.1.1.tgz#1d69769369ada0583103a1e6ae87681b56573230"
Expand Down

0 comments on commit 48f9874

Please sign in to comment.