Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use contractkit in notification service #1118

Merged
merged 22 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/contractkit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@celo/contractkit",
"version": "0.1.1",
"version": "0.1.6",
"description": "Celo's ContractKit to interact with Celo network",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand All @@ -25,7 +25,8 @@
"lint": "tslint -c tslint.json --project ."
},
"dependencies": {
"@celo/utils": "^0.0.6-beta5",
"@0x/subproviders": "^5.0.0",
"@celo/utils": "0.1.0",
"@types/debug": "^4.1.5",
"bignumber.js": "^7.2.0",
"debug": "^4.1.1",
Expand Down
12 changes: 6 additions & 6 deletions packages/notification-service/app.integration.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
runtime: nodejs
env: flex
service: integration
instance_class: B4
manual_scaling:
instances: 1
runtime: nodejs8
service: integration
instance_class: F4
automatic_scaling:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this when I first created the service and found that automatic scaling would always eventually kill the service since it wasn't receiving any inbound network requests. Seems like app engine is kind of hard wired to run web servers and not well suited to other kinds of apps...
It's possible this issue has since been fixed but please test that before changing this.
Also worth it to double check that we have alerting for when the service goes down

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the version that I deployed has been running for a while - do you remember when it would get killed? https://console.cloud.google.com/appengine/instances?project=celo-org-mobile&instancesTablesize=20&versionId=20190926t191656

It also looks like we have an uptime check https://app.google.stackdriver.com/uptime/efe3d6cc3966ae961e398dd5b73d2094?project=celo-testnet

Does this seem okay to you then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use automatic scaling if min and max instances are both set to 1?

min_instances: 1
max_instances: 1
4 changes: 2 additions & 2 deletions packages/notification-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"deploy": "./deploy.sh"
},
"dependencies": {
"@celo/utils": "^0.0.6-beta5",
"@celo/walletkit": "^0.0.14",
"@celo/contractkit": "0.1.5",
"@celo/utils": "0.1.0",
"async-polling": "^0.2.1",
"bignumber.js": "^7.2.0",
"dotenv": "^6.0.0",
Expand Down
31 changes: 16 additions & 15 deletions packages/notification-service/src/exchange/exchangeQuery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ContractKit, newKitFromWeb3 } from '@celo/contractkit'
import { CURRENCY_ENUM } from '@celo/utils'
import { ContractUtils } from '@celo/walletkit' // To be updated to contractkit when new version is published on npm
import BigNumber from 'bignumber.js'
import Web3 from 'web3'
import { WEB3_PROVIDER_URL } from '../config'
Expand All @@ -12,34 +12,35 @@ const SELL_AMOUNTS = {
}

export async function handleExchangeQuery() {
const web3Instance = await getWeb3Instance()
const contractKitInstance = await getContractKit()
const fetchTime = Date.now().toString()
const [dollarMakerRate, goldMakerRate] = await Promise.all([
getExchangeRate(CURRENCY_ENUM.DOLLAR, web3Instance),
getExchangeRate(CURRENCY_ENUM.GOLD, web3Instance),
getExchangeRate(CURRENCY_ENUM.DOLLAR, contractKitInstance),
getExchangeRate(CURRENCY_ENUM.GOLD, contractKitInstance),
])

writeExchangeRatePair(CURRENCY_ENUM.DOLLAR, dollarMakerRate.toString(), fetchTime)
writeExchangeRatePair(CURRENCY_ENUM.GOLD, goldMakerRate.toString(), fetchTime)
}

export async function getExchangeRate(makerToken: CURRENCY_ENUM, web3Instance: Web3) {
const rate = await ContractUtils.getExchangeRate(
web3Instance,
makerToken,
SELL_AMOUNTS[makerToken]
export async function getExchangeRate(makerToken: CURRENCY_ENUM, contractKitInstance: ContractKit) {
const exchange = await contractKitInstance.contracts.getExchange()
const rate = await exchange.getExchangeRate(
SELL_AMOUNTS[makerToken],
makerToken === CURRENCY_ENUM.GOLD
)
return rate
}

let web3: Web3
export function getWeb3Instance(): Web3 {
if (web3 && web3.eth.net.isListening()) {
let contractKit: ContractKit
export function getContractKit(): ContractKit {
if (contractKit && contractKit.isListening()) {
// Already connected
return web3
return contractKit
} else {
const httpProvider = new Web3.providers.HttpProvider(WEB3_PROVIDER_URL)
web3 = new Web3(httpProvider)
return web3
const web3 = new Web3(httpProvider)
contractKit = newKitFromWeb3(web3)
return contractKit
}
}
Loading