Skip to content

Commit

Permalink
adding aws service file and moving aws config file
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitverma007 committed Feb 9, 2019
1 parent 70117a3 commit 4a4c682
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
107 changes: 107 additions & 0 deletions src/services/aws/aws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const AWS = require('aws-sdk')
const REWARDS_TABLE = 'Rewards'
const defaultConfig = require('../../../static/aws-config.json')

export default class {
constructor (delegateHash, config = defaultConfig) {
this.delegateHash = delegateHash
this.setupAws(config)
}

setupAws (config) {
AWS.config.update(config)
this.ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'})
this.docClient = new AWS.DynamoDB.DocumentClient()
}

paramsForSimpleLookup (tableName, key, value, indexName, comparison = '=') {
let params = {
TableName: tableName,
KeyConditionExpression: '#key ' + comparison + ' :value',
ExpressionAttributeNames: {
'#key': key
},
ExpressionAttributeValues: {
':value': value
}
}

if (indexName) {
params['IndexName'] = indexName
}

return params
}

paramsForDoubleLookup (tableName, key1, value1, key2, value2, indexName) {
let params = {
TableName: tableName,
KeyConditionExpression: '#key1 = :value1 AND #key2 = :value2',
ExpressionAttributeNames: {
'#key1': key1,
'#key2': key2
},
ExpressionAttributeValues: {
':value1': value1,
':value2': value2
}
}

if (indexName) {
params['IndexName'] = indexName
}

return params
}

paramsForScan (tableName, attributes) {
let params = {
TableName: tableName,
ProjectionExpression: attributes
}

return params
}

async createTable (params) {
return this.ddb.createTable(params).promise().then(function (data) {
console.log('Created Table: ', data)
}).catch(function (err) {
console.log('Error Occured: ', err)
})
}

async putItem (params) {
return this.ddb.putItem(params).promise().then(function (data) {
console.log('Inserted Data, unproccesed: ', data)
}).catch(function (err) {
console.log('Error Occured: ', err)
})
}

async put (params) {
return this.docClient.put(params).promise().then(function (data) {
console.log('Inserted Data, unproccesed: ', data)
}).catch(function (err) {
console.log('Error Occured: ', err)
})
}

async getAllSnapshotsData () {
const params = this.paramsForScan('SnapshotData', 'cycleNumber, snapshotBlockNumber, snapshotNumber')
return this.docClient.scan(params).promise().then(function (data) {
let snapshotData = {}
for (let i = 0; i < data.Items.length; i++) {
snapshotData[data.Items[i].cycleNumber] = data.Items[i].snapshotBlockNumber
}
return snapshotData
}).catch(function (err) {
console.log('Error Occured: ', err)
})
}

async getRewardsDataForDelegate (cycle, delegateHash = this.delegateHash) {
const params = this.paramsForSimpleLookup(REWARDS_TABLE, 'delegate', delegateHash)
return this.docClient.query(params).promise()
}
}
2 changes: 1 addition & 1 deletion aws-config.json → static/aws-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"endpoint": "https://dynamodb.us-west-2.amazonaws.com",
"accessKeyId": "",
"secretAccessKey": ""
}
}

0 comments on commit 4a4c682

Please sign in to comment.