Skip to content

Commit 6c71108

Browse files
Merge pull request #58 from web3data/feat/contract
Feat/contract
2 parents 12ad9a2 + 2c37da6 commit 6c71108

File tree

6 files changed

+231
-753
lines changed

6 files changed

+231
-753
lines changed

dist/web3data.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/api.md

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [web3data-js](https://github.com/web3data/web3data-js#readme) *0.5.15*
1+
# [web3data-js](https://github.com/web3data/web3data-js#readme) *0.5.16*
22

33
> A javascript wrapper for accessing amberdata's public API.
44
@@ -699,6 +699,155 @@ Retrieves the blocks specified by its id (number or hash).
699699

700700

701701

702+
### src/contract.js
703+
704+
705+
706+
#### Class: Contract
707+
708+
709+
Contains methods pertaining to the `/contract` endpoint of Amberdata's API.
710+
711+
712+
713+
714+
715+
716+
717+
718+
719+
#### constructor(web3data)
720+
721+
722+
Creates an instance of Contract.
723+
724+
725+
726+
727+
##### Parameters
728+
729+
| Name | Type | Description | |
730+
| ---- | ---- | ----------- | -------- |
731+
| web3data | | The web3data instance. |   |
732+
733+
734+
735+
736+
##### Examples
737+
738+
```javascript
739+
740+
```
741+
742+
743+
##### Returns
744+
745+
746+
- `Void`
747+
748+
749+
750+
751+
752+
#### getDetails(hash)
753+
754+
755+
Retrieves all the detailed information for the specified contract (ABI, bytecode, sourcecode...).
756+
757+
758+
759+
760+
##### Parameters
761+
762+
| Name | Type | Description | |
763+
| ---- | ---- | ----------- | -------- |
764+
| hash | | The address. |   |
765+
766+
767+
768+
769+
##### Examples
770+
771+
```javascript
772+
const details = await web3data.contract.getDetails('0x06012c8cf97bead5deae237070f9587f8e7a266d')
773+
```
774+
775+
776+
##### Returns
777+
778+
779+
- The detailed information for the specified contract.
780+
781+
782+
783+
784+
785+
#### getAbi(hash)
786+
787+
788+
Retrieves the contract's abi.
789+
790+
791+
792+
793+
##### Parameters
794+
795+
| Name | Type | Description | |
796+
| ---- | ---- | ----------- | -------- |
797+
| hash | | The contract address. |   |
798+
799+
800+
801+
802+
##### Examples
803+
804+
```javascript
805+
const abi = await web3data.contract.getAbi('0x06012c8cf97bead5deae237070f9587f8e7a266d')
806+
```
807+
808+
809+
##### Returns
810+
811+
812+
- The abi of the contract.
813+
814+
815+
816+
817+
818+
#### getSourceCode(hash)
819+
820+
821+
Retrieves the contract's source code.
822+
823+
824+
825+
826+
##### Parameters
827+
828+
| Name | Type | Description | |
829+
| ---- | ---- | ----------- | -------- |
830+
| hash | | The contract address. |   |
831+
832+
833+
834+
835+
##### Examples
836+
837+
```javascript
838+
const source = await web3data.contract.getSourceCode('0x06012c8cf97bead5deae237070f9587f8e7a266d')
839+
```
840+
841+
842+
##### Returns
843+
844+
845+
- The source of the contract.
846+
847+
848+
849+
850+
702851
### src/market.js
703852

704853

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "web3data-js",
3-
"version": "0.5.15",
3+
"version": "0.5.16",
44
"description": "A javascript wrapper for accessing amberdata's public API.",
55
"main": "index.js",
66
"browser": "dist/web3data.min.js",

src/contract.js

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,35 @@ const {
22
ERROR_MESSAGE_CONTRACT_NO_ADDRESS: NO_ADDRESS,
33
CONTRACTS_ENDPOINT: ENDPOINT
44
} = require('./constants')
5-
const {is, get} = require('./utils')
5+
const {is, get, throwIf, onFulfilled, onError} = require('./utils')
66

7+
/**
8+
* Contains methods pertaining to the `/contract` endpoint of Amberdata's API.
9+
*/
710
class Contract {
11+
/**
12+
* Creates an instance of Contract.
13+
*
14+
* @param web3data - The web3data instance.
15+
* @example
16+
*/
817
constructor(web3data) {
918
this.web3data = web3data
1019
}
1120

12-
getDetails(hash, filterOptions) {
13-
if (is.notHash(hash)) return Promise.reject(new Error(NO_ADDRESS))
21+
/**
22+
* Retrieves all the detailed information for the specified contract (ABI, bytecode, sourcecode...).
23+
*
24+
* @param hash - The address.
25+
* @returns The detailed information for the specified contract.
26+
* @example const details = await web3data.contract.getDetails('0x06012c8cf97bead5deae237070f9587f8e7a266d')
27+
*/
28+
getDetails(hash) {
29+
throwIf(is.notHash(hash), NO_ADDRESS)
1430
return get(this.web3data, {
1531
pathParam: hash,
16-
endpoint: ENDPOINT,
17-
filterOptions
18-
})
32+
endpoint: ENDPOINT
33+
}).then(onFulfilled, onError)
1934
}
2035

2136
getFunctions(hash, filterOptions) {
@@ -38,43 +53,36 @@ class Contract {
3853
})
3954
}
4055

41-
getAbi(hash, filterOptions) {
42-
if (is.notHash(hash)) return Promise.reject(new Error(NO_ADDRESS))
43-
return get(this.web3data, {
44-
pathParam: hash,
45-
endpoint: ENDPOINT,
46-
subendpoint: 'abi',
47-
filterOptions
48-
})
56+
/**
57+
* Retrieves the contract's abi.
58+
*
59+
* @param hash - The contract address.
60+
* @returns The abi of the contract.
61+
* @example const abi = await web3data.contract.getAbi('0x06012c8cf97bead5deae237070f9587f8e7a266d')
62+
*/
63+
getAbi(hash) {
64+
throwIf(is.notHash(hash), NO_ADDRESS)
65+
return this.getDetails(hash).then(({abi}) => abi)
4966
}
5067

51-
getSourceCode(hash, filterOptions) {
52-
if (is.notHash(hash)) return Promise.reject(new Error(NO_ADDRESS))
68+
/**
69+
* Retrieves the contract's source code.
70+
*
71+
* @param hash - The contract address.
72+
* @returns The source of the contract.
73+
* @example const source = await web3data.contract.getSourceCode('0x06012c8cf97bead5deae237070f9587f8e7a266d')
74+
*/
75+
getSourceCode(hash) {
76+
throwIf(is.notHash(hash), NO_ADDRESS)
5377
return get(this.web3data, {
5478
pathParam: hash,
5579
endpoint: ENDPOINT,
56-
subendpoint: 'source-code',
57-
filterOptions
58-
})
80+
subendpoint: 'source-code'
81+
}).then(onFulfilled, onError)
5982
}
6083

61-
async getCode(hash) {
62-
const response = await this.getDetails(hash)
63-
return new Promise((resolve, reject) => {
64-
if (
65-
is.null(response) ||
66-
is.undefined(response) ||
67-
response.status !== 200
68-
) {
69-
reject(new Error('Failed to retrieve contract code.'))
70-
} else if (!response.payload) {
71-
reject(new Error('Failed to retrieve contract code.'))
72-
} else if (response.payload.bytecode) {
73-
resolve(response.payload.bytecode)
74-
} else {
75-
resolve('0x')
76-
}
77-
})
84+
getCode(hash) {
85+
return this.getDetails(hash).then(details => details.bytecode || '0x')
7886
}
7987
}
8088

test/contract.test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ test.beforeEach(t => {
2121
**********************************/
2222

2323
/*********** Test getDetails() ***********/
24-
test('Successfully gets contract details', async t => {
25-
let response = await t.context.web3data.contract.getDetails(TOKEN_ADDRESS);
26-
t.is(response.status, 200)
27-
});
24+
test('Successfully calls getDetails()', async t => {
25+
const details = await t.context.web3data.contract.getDetails(TOKEN_ADDRESS)
26+
t.true(details.hasProp('abi'))
27+
})
2828
test('throws exception when calling getDetails without hash', async t => {
2929
await t.throwsAsync(async () => {
3030
await t.context.web3data.contract.getDetails()
31-
}, { instanceOf: Error, message: 'No contract address supplied' });
32-
});
31+
}, { instanceOf: Error, message: 'No contract address supplied' })
32+
})
3333

3434
/*********** Test getFunctions() ***********/
3535
test('Successfully gets contract functions', async t => {
@@ -54,9 +54,9 @@ test('throws exception when calling getAudit without hash', async t => {
5454
});
5555

5656
/*********** Test getAbi() ***********/
57-
test('Successfully gets contract abi', async t => {
58-
let response = await t.context.web3data.contract.getAbi(TOKEN_ADDRESS);
59-
t.is(response.status, 200)
57+
test('Successfully calls getAbi()', async t => {
58+
const abi = await t.context.web3data.contract.getAbi(TOKEN_ADDRESS);
59+
t.true(Array.isArray(abi))
6060
});
6161
test('throws exception when calling getAbi without hash', async t => {
6262
await t.throwsAsync(async () => {
@@ -65,11 +65,11 @@ test('throws exception when calling getAbi without hash', async t => {
6565
});
6666

6767
/*********** Test getSourceCode() ***********/
68-
test.skip('Successfully gets contract source code', async t => {
69-
let response = await t.context.web3data.contract.getSourceCode(TOKEN_ADDRESS);
70-
t.is(response.status, 200)
68+
test.only('Successfully gets contract source code', async t => {
69+
const source = await t.context.web3data.contract.getSourceCode(TOKEN_ADDRESS);
70+
t.is(typeof source, 'string')
7171
});
72-
test.skip('throws exception when calling getSourceCode without hash', async t => {
72+
test('throws exception when calling getSourceCode without hash', async t => {
7373
await t.throwsAsync(async () => {
7474
await t.context.web3data.contract.getSourceCode()
7575
}, { instanceOf: Error, message: 'No contract address supplied' });

test/recordings/contract_710824451/recording.har

Lines changed: 22 additions & 701 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)