Skip to content

Commit ffa6b45

Browse files
josemarinasbanasa44heueristik
authored
fix(sdk): getProtocolVersion function (#77)
* add: getProtocolVersion function * fix: prettier * fix: linting * Update sdk/src/introspection.ts Co-authored-by: Carles <[email protected]> * Update sdk/src/introspection.ts Co-authored-by: Michael Heuer <[email protected]> * Update sdk/test/unit/introspection.test.ts Co-authored-by: Michael Heuer <[email protected]> * remove: constant ADDRESS_ONE * fix: prettier * fix: export of addres one * fix: update getProtocolVersion return comment * chore: add ADDRESS_ONE to constants * feat: add getProtocolVersion * update pacakge.json --------- Co-authored-by: Carles <[email protected]> Co-authored-by: Michael Heuer <[email protected]>
1 parent b3bd6da commit ffa6b45

File tree

4 files changed

+68
-49
lines changed

4 files changed

+68
-49
lines changed

sdk/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@aragon/osx-commons-sdk",
33
"author": "Aragon Association",
4-
"version": "0.0.1-alpha.9",
4+
"version": "0.0.1-alpha.10",
55
"license": "MIT",
66
"main": "dist/index.js",
77
"module": "dist/osx-commons-sdk.esm.js",
@@ -54,11 +54,12 @@
5454
},
5555
"dependencies": {
5656
"@aragon/osx-commons-configs": "^0.4.0",
57+
"@ethersproject/address": "^5.7.0",
5758
"@ethersproject/bignumber": "^5.7.0",
5859
"@ethersproject/contracts": "^5.7.0",
5960
"@ethersproject/hash": "^5.7.0",
61+
"@ethersproject/logger": "^5.7.0",
6062
"@ethersproject/providers": "^5.7.0",
61-
"@ethersproject/address": "^5.7.0",
6263
"ipfs-http-client": "^51.0.0"
6364
}
6465
}

sdk/src/introspection.ts

+16-23
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import {InvalidAddressError} from './errors';
21
import {Interface} from '@ethersproject/abi';
3-
import {isAddress} from '@ethersproject/address';
42
import {BigNumber} from '@ethersproject/bignumber';
53
import {Contract} from '@ethersproject/contracts';
6-
import {JsonRpcProvider} from '@ethersproject/providers';
4+
import {ErrorCode} from '@ethersproject/logger';
5+
6+
// The protocol version number of contracts not having a `getProtocolVersion()` function because they don't inherit from `ProtocolVersion.sol` yet.
7+
export const IMPLICIT_INITIAL_PROTOCOL_VERSION: [number, number, number] = [
8+
1, 0, 0,
9+
];
710

811
/**
912
* Gets the interfaceId of a given interface
@@ -26,31 +29,21 @@ export function getInterfaceId(iface: Interface): string {
2629
* protocolVersion function, it will return [1, 0, 0]
2730
*
2831
* @export
29-
* @param {string} rpc
30-
* @param {string} contractAddress
32+
* @param {Contract} contract
3133
* @return {*} {Promise<[number, number, number]>}
3234
*/
3335
export async function getProtocolVersion(
34-
rpc: string,
35-
contractAddress: string
36+
contract: Contract
3637
): Promise<[number, number, number]> {
37-
if (!isAddress(contractAddress)) {
38-
throw new InvalidAddressError(contractAddress);
39-
}
40-
const provider = new JsonRpcProvider(rpc);
41-
const iface = new Interface([
42-
'function protocolVersion() public pure returns (uint8[3] memory)',
43-
]);
44-
const contract = new Contract(contractAddress, iface, provider);
45-
let version: [number, number, number];
38+
let protocolVersion: [number, number, number];
4639
try {
47-
version = await contract.protocolVersion();
40+
contract.interface.getFunction('protocolVersion');
41+
protocolVersion = await contract.protocolVersion();
4842
} catch (e) {
49-
// version 1.0.0 of the contract does not have a protocolVersion function
50-
// so if we receive an error we cannot differentiate between a call exception
51-
// and a contract that does not have the function. So we assume that is
52-
// a version 1.0.0 contract that does not have the function and return [1, 0, 0]
53-
version = [1, 0, 0];
43+
if ((e as any).code === ErrorCode.INVALID_ARGUMENT) {
44+
return IMPLICIT_INITIAL_PROTOCOL_VERSION;
45+
}
46+
throw e;
5447
}
55-
return version;
48+
return protocolVersion;
5649
}

sdk/test/mocks.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import {FunctionFragment} from '@ethersproject/abi';
2+
import {ErrorCode, Logger} from '@ethersproject/logger';
3+
14
export function mockContractProtocolVersion(
25
version: [number, number, number] = [1, 0, 0],
36
throwException: boolean = false
@@ -8,10 +11,23 @@ export function mockContractProtocolVersion(
811
return {
912
protocolVersion: () => {
1013
if (throwException) {
11-
throw new Error('Error');
14+
const logger = new Logger('5.7.0');
15+
logger.throwError(
16+
'Protocol version not found',
17+
ErrorCode.INVALID_ARGUMENT
18+
);
1219
}
1320
return Promise.resolve(version);
1421
},
22+
interface: {
23+
getFunction: (name: string): FunctionFragment => {
24+
return FunctionFragment.from({
25+
name: name,
26+
type: 'function',
27+
stateMutability: 'pure',
28+
});
29+
},
30+
},
1531
};
1632
});
1733
}

sdk/test/unit/introspection.test.ts

+32-23
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {
2-
InvalidAddressError,
2+
IMPLICIT_INITIAL_PROTOCOL_VERSION,
33
getInterfaceId,
44
getProtocolVersion,
55
} from '../../src';
66
import {ADDRESS_ONE, TEST_HTTP_URI} from '../constants';
7-
import {mockContractProtocolVersion, mockJSONRPCProvider} from '../mocks';
7+
import {mockJSONRPCProvider, mockContractProtocolVersion} from '../mocks';
88
import {Interface} from '@ethersproject/abi';
9+
import {Contract} from '@ethersproject/contracts';
10+
import {JsonRpcProvider} from '@ethersproject/providers';
911

1012
describe('introspection', () => {
1113
describe('getInterfaceId', () => {
@@ -21,33 +23,40 @@ describe('introspection', () => {
2123
});
2224

2325
describe('getProtocolVersion', () => {
24-
it('should return the correct protocol version', async () => {
25-
const expectedVersion: [number, number, number] = [1, 3, 0];
26-
// mock call to the contract
26+
let iface: Interface;
27+
let contract: Contract;
28+
let provider: JsonRpcProvider;
29+
beforeAll(() => {
30+
// mock JSONRPCProvider to return chainId 1 and blockNumber 1
2731
mockJSONRPCProvider();
28-
// mock the call to the contract
29-
mockContractProtocolVersion(expectedVersion);
30-
const version = await getProtocolVersion(TEST_HTTP_URI, ADDRESS_ONE);
31-
expect(version).toEqual(expectedVersion);
3232
});
33-
it('should fail when an invalid address is passed', async () => {
33+
it('should return the correct protocol version', async () => {
34+
// Expected protocol version
3435
const expectedVersion: [number, number, number] = [1, 3, 0];
35-
// mock call to the contract
36-
mockJSONRPCProvider();
37-
// mock the call to the contract
36+
// mock Contract to return the expected protocol version
3837
mockContractProtocolVersion(expectedVersion);
39-
await expect(() =>
40-
getProtocolVersion(TEST_HTTP_URI, '0x')
41-
).rejects.toThrow(new InvalidAddressError('0x'));
38+
// Initialize the contract
39+
provider = new JsonRpcProvider(TEST_HTTP_URI);
40+
iface = new Interface([
41+
'function protocolVersion() public pure returns (uint8[3] memory)',
42+
]);
43+
contract = new Contract(ADDRESS_ONE, iface, provider);
44+
// Call getProtocolVersion
45+
const version = await getProtocolVersion(contract);
46+
expect(version).toEqual(expectedVersion);
4247
});
4348
it('should return [1,0,0] when the call throws an error', async () => {
44-
const expectedVersion: [number, number, number] = [1, 0, 0];
45-
// mock call to the contract
46-
mockJSONRPCProvider();
47-
// mock the call to the contract
48-
mockContractProtocolVersion(expectedVersion, true);
49-
const version = await getProtocolVersion(TEST_HTTP_URI, ADDRESS_ONE);
50-
expect(version).toEqual(expectedVersion);
49+
// mock Contract to throw an error
50+
mockContractProtocolVersion(IMPLICIT_INITIAL_PROTOCOL_VERSION, true);
51+
// Initialize the contract
52+
const iface = new Interface([
53+
'function protocolVersion() public pure returns (uint8[3] memory)',
54+
]);
55+
const provider = new JsonRpcProvider(TEST_HTTP_URI);
56+
const contract = new Contract(ADDRESS_ONE, iface, provider);
57+
// Call getProtocolVersion
58+
const version = await getProtocolVersion(contract);
59+
expect(version).toEqual(IMPLICIT_INITIAL_PROTOCOL_VERSION);
5160
});
5261
});
5362
});

0 commit comments

Comments
 (0)