Skip to content

Commit

Permalink
Added new getChainInfo method
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniSomoza committed Nov 24, 2021
1 parent 05a7e77 commit c90b4dd
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 77 deletions.
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
106 changes: 39 additions & 67 deletions 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 @@ -38,12 +38,6 @@ describe('Safe Apps SDK safe methods', () => {
safeAddress: '0x9C6FEA0B2eAc5b6D8bBB6C30401D42aA95398190',
owners: [],
threshold: 1,
chainInfo: {
chainName: 'rinkeby',
chainId: 4,
shortName: 'rin',
nativeCurrency: 'eth',
},
}),
);
// to test message/hash I signed a test message on rinkeby
Expand All @@ -66,12 +60,6 @@ describe('Safe Apps SDK safe methods', () => {
safeAddress: '0x9C6FEA0B2eAc5b6D8bBB6C30401D42aA95398190',
owners: [],
threshold: 1,
chainInfo: {
chainName: 'rinkeby',
chainId: 4,
shortName: 'rin',
nativeCurrency: 'eth',
},
}),
);
const message = '0x617070726f76652072756770756c6c0000000000000000000000000000000000'; // ethers.utils.formatBytes32String('approve rugpull')
Expand Down Expand Up @@ -107,12 +95,6 @@ describe('Safe Apps SDK safe methods', () => {
safeAddress: '0x9C6FEA0B2eAc5b6D8bBB6C30401D42aA95398190',
owners: [],
threshold: 1,
chainInfo: {
chainName: 'rinkeby',
chainId: 4,
shortName: 'rin',
nativeCurrency: 'eth',
},
}),
);
rpcCallSpy.mockImplementationOnce(() =>
Expand All @@ -139,12 +121,6 @@ describe('Safe Apps SDK safe methods', () => {
safeAddress: '0x9C6FEA0B2eAc5b6D8bBB6C30401D42aA95398190',
owners: [],
threshold: 1,
chainInfo: {
chainName: 'rinkeby',
chainId: 4,
shortName: 'rin',
nativeCurrency: 'eth',
},
}),
);
rpcCallSpy.mockImplementationOnce(() => Promise.reject(new Error('Hash not approved')));
Expand All @@ -165,12 +141,6 @@ describe('Safe Apps SDK safe methods', () => {
safeAddress: '0x9C6FEA0B2eAc5b6D8bBB6C30401D42aA95398190',
owners: [],
threshold: 1,
chainInfo: {
chainName: 'rinkeby',
chainId: 4,
shortName: 'rin',
nativeCurrency: 'eth',
},
}),
);
const message = '0x617070726f76652072756770756c6c0000000000000000000000000000000000'; // ethers.utils.formatBytes32String('approve rugpull')
Expand Down Expand Up @@ -206,12 +176,6 @@ describe('Safe Apps SDK safe methods', () => {
safeAddress: '0x9C6FEA0B2eAc5b6D8bBB6C30401D42aA95398190',
owners: [],
threshold: 1,
chainInfo: {
chainName: 'rinkeby',
chainId: 4,
shortName: 'rin',
nativeCurrency: 'eth',
},
}),
);
rpcCallSpy.mockImplementationOnce(() =>
Expand All @@ -238,12 +202,6 @@ describe('Safe Apps SDK safe methods', () => {
safeAddress: '0x9C6FEA0B2eAc5b6D8bBB6C30401D42aA95398190',
owners: [],
threshold: 1,
chainInfo: {
chainName: 'rinkeby',
chainId: 4,
shortName: 'rin',
nativeCurrency: 'eth',
},
}),
);
rpcCallSpy.mockImplementationOnce(() => Promise.reject(new Error('Hash not approved')));
Expand Down Expand Up @@ -277,12 +235,6 @@ describe('Safe Apps SDK safe methods', () => {
safeAddress: '0x9C6FEA0B2eAc5b6D8bBB6C30401D42aA95398190',
owners: [],
threshold: 1,
chainInfo: {
chainName: 'rinkeby',
chainId: 4,
shortName: 'rin',
nativeCurrency: 'eth',
},
}),
);

Expand Down Expand Up @@ -320,12 +272,6 @@ describe('Safe Apps SDK safe methods', () => {
safeAddress: '0x9C6FEA0B2eAc5b6D8bBB6C30401D42aA95398190',
owners: [],
threshold: 1,
chainInfo: {
chainName: 'rinkeby',
chainId: 4,
shortName: 'rin',
nativeCurrency: 'eth',
},
}),
);
// @ts-expect-error ts fails to infer the return type because of a private method
Expand Down Expand Up @@ -353,12 +299,6 @@ describe('Safe Apps SDK safe methods', () => {
safeAddress: '0x9C6FEA0B2eAc5b6D8bBB6C30401D42aA95398190',
owners: [],
threshold: 1,
chainInfo: {
chainName: 'rinkeby',
chainId: 4,
shortName: 'rin',
nativeCurrency: 'eth',
},
}),
);
// @ts-expect-error ts fails to infer the return type because of a private method
Expand Down Expand Up @@ -388,12 +328,6 @@ describe('Safe Apps SDK safe methods', () => {
safeAddress: '0x9C6FEA0B2eAc5b6D8bBB6C30401D42aA95398190',
owners: [],
threshold: 1,
chainInfo: {
chainName: 'rinkeby',
chainId: 4,
shortName: 'rin',
nativeCurrency: 'eth',
},
}),
);
// @ts-expect-error ts fails to infer the return type because of a private method
Expand Down Expand Up @@ -421,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
14 changes: 6 additions & 8 deletions packages/safe-apps-sdk/src/types/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { NativeCurrency, ChainInfo as _ChainInfo } from '@gnosis.pm/safe-react-gateway-sdk';

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

export { NativeCurrency };

export type BaseTransaction = {
to: string;
value: string;
Expand Down Expand Up @@ -27,17 +33,9 @@ export type SendTransactionsResponse = {
safeTxHash: string;
};

export type ChainInfo = {
chainName: string;
chainId: number;
shortName: string;
nativeCurrency: string;
};

export type SafeInfo = {
safeAddress: string;
chainId: number;
threshold: number;
owners: string[];
chainInfo: ChainInfo;
};
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 c90b4dd

Please sign in to comment.