Skip to content

Commit 2a27b77

Browse files
committed
added to get token balance feature
1 parent 16a539e commit 2a27b77

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
11
# amb-query-nodejs
2+
3+
### 1. Authenticated AWS on your local machine
4+
5+
```
6+
export AWS_ACCESS_KEY_ID="{input here}"
7+
export AWS_SECRET_ACCESS_KEY="{input here}"
8+
```
9+
10+
### 2. use node 14 version (using nvm dependency)
11+
```
12+
nvm use 14
13+
```
14+
15+
### 3. install dependencies
16+
```
17+
npm install
18+
```
19+
20+
## AMB Query
21+
22+
### 1. Token Balance
23+
```
24+
node tokenBalance.js
25+
```

package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "amb-query-nodejs",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"@aws-crypto/sha256-js": "^4.0.0",
6+
"@aws-sdk/credential-provider-node": "^3.360.0",
7+
"@aws-sdk/protocol-http": "^3.357.0",
8+
"@aws-sdk/signature-v4": "^3.357.0",
9+
"axios": "^1.4.0"
10+
}
11+
}

tokenBalance.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const axios = require('axios').default;
2+
const SHA256 = require('@aws-crypto/sha256-js').Sha256
3+
const defaultProvider = require('@aws-sdk/credential-provider-node').defaultProvider
4+
const HttpRequest = require('@aws-sdk/protocol-http').HttpRequest
5+
const SignatureV4 = require('@aws-sdk/signature-v4').SignatureV4
6+
7+
// define a signer object with AWS service name, credentials, and region
8+
const signer = new SignatureV4({
9+
credentials: defaultProvider(),
10+
service: 'managedblockchain-query',
11+
region: 'us-east-1',
12+
sha256: SHA256,
13+
});
14+
15+
const queryRequest = async (path, data) => {
16+
//query endpoint
17+
let queryEndpoint = `https://managedblockchain-query.us-east-1.amazonaws.com/${path}`;
18+
19+
// parse the URL into its component parts (e.g. host, path)
20+
const url = new URL(queryEndpoint);
21+
22+
// create an HTTP Request object
23+
const req = new HttpRequest({
24+
hostname: url.hostname.toString(),
25+
path: url.pathname.toString(),
26+
body: JSON.stringify(data),
27+
method: 'POST',
28+
headers: {
29+
'Content-Type': 'application/json',
30+
'Accept-Encoding': 'gzip',
31+
host: url.hostname,
32+
}
33+
});
34+
35+
36+
// use AWS SignatureV4 utility to sign the request, extract headers and body
37+
const signedRequest = await signer.sign(req, { signingDate: new Date() });
38+
39+
try {
40+
//make the request using axios
41+
const response = await axios({...signedRequest, url: queryEndpoint, data: data})
42+
43+
console.log(response.data)
44+
} catch (error) {
45+
console.error('Something went wrong: ', error)
46+
throw error
47+
}
48+
49+
50+
}
51+
52+
53+
let methodArg = 'get-token-balance';
54+
55+
let dataArg = {
56+
// " atBlockchainInstant": {
57+
// "time": 1688071493
58+
// }, // (Optional)
59+
"ownerIdentifier": {
60+
"address": "0x02E6C1d540d17BcD8A47596390E9f1d3d25041d1" // 지갑 혹은 컨트랙트 주소
61+
},
62+
"tokenIdentifier": {
63+
// "contractAddress":"0xA0b86991c6218b36c1d19D4a2e9Eb0cE********", //USDC contract address (Optional)
64+
"network":"ETHEREUM_MAINNET",
65+
"tokenId": "eth"
66+
}
67+
}
68+
69+
//Run the query request.
70+
queryRequest(methodArg, dataArg);

0 commit comments

Comments
 (0)