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 all 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
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/packages/cli/ @mcortesi @asaj
/packages/helm-charts/ @nambrot @timmoreton
/packages/mobile/ @cmcewen @jmrossy
/packages/notification-service/ @jmrossy @cmcewen
/packages/notification-service/ @annakaz @cmcewen
/packages/protocol/ @asaj @m-chrzan
/packages/react-components/ @cmcewen @jmrossy
/packages/walletkit/ @ashishb @yerdua
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"packages": ["packages/*"],
"npmClient": "yarn",
"useWorkspaces": true,
"version": "1.0.0"
"version": "independent"
}
2 changes: 1 addition & 1 deletion packages/attestation-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"@celo/contractkit": "0.1.1",
"@celo/utils": "^0.0.6-beta5",
"@celo/utils": "^0.1.0",
"bignumber.js": "^7.2.0",
"body-parser": "1.19.0",
"debug": "^4.1.1",
Expand Down
3 changes: 2 additions & 1 deletion packages/attestation-service/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"rootDir": "src",
"outDir": "lib"
},
"include": ["src", "types/", "index.d.ts"]
"include": ["src", "types/", "index.d.ts"],
"references": [{ "path": "../utils" }]
}
4 changes: 2 additions & 2 deletions packages/celotool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"license": "Apache-2.0",
"dependencies": {
"@celo/verification-pool-api": "^1.0.0",
"@celo/utils": "^0.0.6-beta5",
"@celo/utils": "^0.1.0",
"@celo/walletkit": "^0.0.14",
"@celo/contractkit": "0.1.1",
"@celo/contractkit": "0.1.5",
"@google-cloud/monitoring": "0.7.1",
"@google-cloud/pubsub": "^0.28.1",
"@google-cloud/storage": "^2.4.3",
Expand Down
3 changes: 2 additions & 1 deletion packages/celotool/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
}
},
"include": ["src/"],
"exclude": ["node_modules/"]
"exclude": ["node_modules/"],
"references": [{ "path": "../utils" }]
}
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"test": "TZ=UTC jest"
},
"dependencies": {
"@celo/utils": "^0.0.6-beta5",
"@celo/utils": "^0.1.0",
"@celo/contractkit": "^0.1.1",
"@oclif/command": "^1",
"@oclif/config": "^1",
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"outDir": "lib",
"esModuleInterop": true
},
"include": ["src"]
"include": ["src"],
"references": [{ "path": "../utils" }]
}
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
3 changes: 2 additions & 1 deletion packages/contractkit/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"rootDir": "src",
"outDir": "lib"
},
"include": ["src", "types/"]
"include": ["src", "types/"],
"references": [{ "path": "../utils" }]
}
4 changes: 2 additions & 2 deletions packages/dappkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"build": "tsc"
},
"dependencies": {
"@celo/contractkit": "0.1.1",
"@celo/utils": "0.0.6-beta5",
"@celo/contractkit": "0.1.5",
"@celo/utils": "^0.1.0",
"expo": "^34.0.1",
"expo-contacts": "6.0.0",
"libphonenumber-js": "^1.7.22"
Expand Down
3 changes: 2 additions & 1 deletion packages/dappkit/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"rootDir": "src",
"outDir": "lib"
},
"include": ["src", "types/"]
"include": ["src", "types/"],
"references": [{ "path": "../utils" }]
}
2 changes: 1 addition & 1 deletion packages/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"dependencies": {
"@celo/client": "2d1d8ca",
"@celo/react-native-sms-retriever": "git+https://github.com/celo-org/react-native-sms-retriever#d3a2fdb",
"@celo/utils": "^0.0.6-beta5",
"@celo/utils": "^0.1.0",
"@celo/walletkit": "^0.0.14",
"@react-native-community/netinfo": "^2.0.4",
"@segment/analytics-react-native": "^1.1.0-beta.1",
Expand Down
3 changes: 2 additions & 1 deletion packages/mobile/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
"target": "es2015",
"strict": true
},
"exclude": ["node_modules", "vendor", ".bundle"]
"exclude": ["node_modules", "vendor", ".bundle"],
"references": [{ "path": "../utils" }]
}
8 changes: 8 additions & 0 deletions packages/notification-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ Next, run this task to build, configure, and start the service:

yarn start:local

## Updating environments

To find a web3 provider for an environment, run

kubectl get svc -n alfajores --context gke_celo-testnet-production_us-west1-a_alfajores

then look for the external IP for one of the tx nodes, for example `alfajores-service-0`.

## Deploying to App Engine

Deploy your app. The project will be built automatically by Google Cloud Build:
Expand Down
7 changes: 3 additions & 4 deletions packages/notification-service/app.integration.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
runtime: nodejs
env: flex
service: integration
runtime: nodejs8
service: integration
instance_class: B4
manual_scaling:
instances: 1
instances: 1
2 changes: 0 additions & 2 deletions packages/notification-service/config/config.alfajores.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ PORT=80
FIREBASE_DB=https://celo-org-mobile-alfajores.firebaseio.com
BLOCKSCOUT_API=https://alfajores-blockscout.celo-testnet.org/api?
NODE_TLS_REJECT_UNAUTHORIZED=0
STABLE_TOKEN_ADDRESS=0xd4b4fcaCAc9e23225680e89308E0a4C41Dd9C6B4
GOLD_TOKEN_ADDRESS=0x11CD75C45638Ec9f41C0e8Df78fc756201E48ff2
DEFAULT_LOCALE=en
NOTIFICATION_TTL_MS=604800000
POLLING_INTERVAL=1000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ PORT=80
FIREBASE_DB=https://celo-org-mobile-alfajoresstaging.firebaseio.com/
BLOCKSCOUT_API=https://alfajoresstaging-blockscout.celo-testnet.org/api?
NODE_TLS_REJECT_UNAUTHORIZED=0
STABLE_TOKEN_ADDRESS=0x2dF4dD6Bd1b26a8503F763506bdB8e7cf165f69E
GOLD_TOKEN_ADDRESS=0x1313e2f3EBef8f0d869EECEb796D55A066eEA863
DEFAULT_LOCALE=es
NOTIFICATION_TTL_MS=604800000
POLLING_INTERVAL=1000
WEB3_PROVIDER_URL=http://35.233.160.250:8545
4 changes: 1 addition & 3 deletions packages/notification-service/config/config.integration.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ PORT=80
FIREBASE_DB=https://celo-org-mobile-int.firebaseio.com
BLOCKSCOUT_API=https://integration-blockscout.celo-testnet.org/api?
NODE_TLS_REJECT_UNAUTHORIZED=0
STABLE_TOKEN_ADDRESS=0x47736AB66b892b0FCCb5c7d69B879C6141F6E80c
GOLD_TOKEN_ADDRESS=0x9102eCD93ac8D66bAc3D397BF52bc57Ee34Bcb87
DEFAULT_LOCALE=en
NOTIFICATION_TTL_MS=604800000
POLLING_INTERVAL=1000
EXCHANGE_POLLING_INTERVAL=1800000
WEB3_PROVIDER_URL=http://34.83.137.48:8545
WEB3_PROVIDER_URL=http://35.233.200.197:8545
4 changes: 1 addition & 3 deletions packages/notification-service/config/config.local.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ PORT=8080
FIREBASE_DB=https://celo-org-mobile-int.firebaseio.com
BLOCKSCOUT_API=https://integration-blockscout.celo-testnet.org/api?
NODE_TLS_REJECT_UNAUTHORIZED=0
STABLE_TOKEN_ADDRESS=0x94a076695b5c4050fbe57ab205e31c18bddb7cb5
GOLD_TOKEN_ADDRESS=0x140ad3e4625160f48208daf47ca7fdff40527f5f
DEFAULT_LOCALE=en
GOOGLE_APPLICATION_CREDENTIALS="./config/serviceAccountKey.json"
NOTIFICATION_TTL_MS=604800000
POLLING_INTERVAL=5000
EXCHANGE_POLLING_INTERVAL=1800000
WEB3_PROVIDER_URL=http://34.83.137.48:8545
WEB3_PROVIDER_URL=http://35.233.200.197:8545
3 changes: 1 addition & 2 deletions packages/notification-service/config/config.pilot.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ PORT=80
FIREBASE_DB=https://celo-org-mobile-pilot.firebaseio.com
BLOCKSCOUT_API=https://pilot-blockscout.celo-testnet.org/api?
NODE_TLS_REJECT_UNAUTHORIZED=0
STABLE_TOKEN_ADDRESS=0x996e24D7791A182f237635018c49E30cdA8FBa5e
GOLD_TOKEN_ADDRESS=0xa69c3D18a74B3FD5F8aDA748428d0bfF8c5387Fe
DEFAULT_LOCALE=en
NOTIFICATION_TTL_MS=604800000
POLLING_INTERVAL=1000
WEB3_PROVIDER_URL=http://35.203.185.89:8545
10 changes: 0 additions & 10 deletions packages/notification-service/config/config.pilotstaging.env

This file was deleted.

10 changes: 0 additions & 10 deletions packages/notification-service/config/config.production.env

This file was deleted.

11 changes: 11 additions & 0 deletions packages/notification-service/config/config.test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ENVIRONMENT=local
PORT=8080
FIREBASE_DB=https://celo-org-mobile-int.firebaseio.com
BLOCKSCOUT_API=https://integration-blockscout.celo-testnet.org/api?
NODE_TLS_REJECT_UNAUTHORIZED=0
DEFAULT_LOCALE=en
GOOGLE_APPLICATION_CREDENTIALS="./config/serviceAccountKey.json"
NOTIFICATION_TTL_MS=604800000
POLLING_INTERVAL=5000
EXCHANGE_POLLING_INTERVAL=1800000
WEB3_PROVIDER_URL=http://35.233.200.197:8545
3 changes: 0 additions & 3 deletions packages/notification-service/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
jest.mock('./src/config', () => ({
...jest.requireActual('./src/config'),
BLOCKSCOUT_API: '',
STABLE_TOKEN_ADDRESS: '',
GOLD_TOKEN_ADDRESS: '',
NOTIFICATIONS_DISABLED: true,
}))
6 changes: 3 additions & 3 deletions packages/notification-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
"scripts": {
"build": "tsc",
"gcp-build": "npm run build",
"test": "export TZ=UTC && jest",
"test": "cp config/config.test.env .env; export TZ=UTC && jest",
"test:watch": "yarn test --watch",
"lint": "tslint -c tslint.json --project tsconfig.json",
"start": "node ./dist/index.js",
"start:local": "npm run build; cp config/config.local.env .env; node --inspect ./dist/index.js",
"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
9 changes: 5 additions & 4 deletions packages/notification-service/src/blockscout/transfers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import BigNumber from 'bignumber.js'
import fetch from 'node-fetch'
import { BLOCKSCOUT_API, GOLD_TOKEN_ADDRESS, STABLE_TOKEN_ADDRESS } from '../config'
import { BLOCKSCOUT_API } from '../config'
import { getLastBlockNotified, sendPaymentNotification, setLastBlockNotified } from '../firebase'
import { removeEmptyValuesFromObject } from '../util/utils'
import { getTokenAddresses, removeEmptyValuesFromObject } from '../util/utils'
import { Log, Response, Transfer } from './blockscout'
import { decodeLogs } from './decode'

Expand Down Expand Up @@ -121,15 +121,16 @@ export async function handleTransferNotifications(): Promise<void> {
return
}

const { goldTokenAddress, stableTokenAddress } = await getTokenAddresses()
const {
transfers: goldTransfers,
latestBlock: goldTransfersLatestBlock,
} = await getLatestTokenTransfers(GOLD_TOKEN_ADDRESS, lastBlockNotified, Currencies.GOLD)
} = await getLatestTokenTransfers(goldTokenAddress, lastBlockNotified, Currencies.GOLD)

const {
transfers: stableTransfers,
latestBlock: stableTransfersLatestBlock,
} = await getLatestTokenTransfers(STABLE_TOKEN_ADDRESS, lastBlockNotified, Currencies.DOLLAR)
} = await getLatestTokenTransfers(stableTokenAddress, lastBlockNotified, Currencies.DOLLAR)

const allTransfers = filterAndJoinTransfers(goldTransfers, stableTransfers)

Expand Down
2 changes: 0 additions & 2 deletions packages/notification-service/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export const FIREBASE_DB = process.env.FIREBASE_DB
const ENFORCE_NOTIFICATIONS = process.env.ENFORCE_LOCAL_NOTIFICATIONS === 'true'
export const NOTIFICATIONS_DISABLED = !ENFORCE_NOTIFICATIONS && ENVIRONMENT === 'local'
export const BLOCKSCOUT_API = process.env.BLOCKSCOUT_API
export const STABLE_TOKEN_ADDRESS = (process.env.STABLE_TOKEN_ADDRESS || '').toLowerCase()
export const GOLD_TOKEN_ADDRESS = (process.env.GOLD_TOKEN_ADDRESS || '').toLowerCase()
export const DEFAULT_LOCALE = process.env.DEFAULT_LOCALE
export const POLLING_INTERVAL = Number(process.env.POLLING_INTERVAL) || 1000
export const NOTIFICATIONS_TTL_MS = Number(process.env.NOTIFICATION_TTL_MS) || 3600 * 1000 * 24 * 7 // 1 week in milliseconds
Expand Down
33 changes: 10 additions & 23 deletions packages/notification-service/src/exchange/exchangeQuery.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ContractKit } 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'
import { writeExchangeRatePair } from '../firebase'
import { getContractKit } from '../util/utils'

// Amounts to estimate the exchange rate, as the rate varies based on transaction size
const SELL_AMOUNTS = {
Expand All @@ -12,34 +11,22 @@ 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]
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 async function getWeb3Instance(): Promise<Web3> {
if (web3 && (await web3.eth.net.isListening())) {
// Already connected
return web3
} else {
const httpProvider = new Web3.providers.HttpProvider(WEB3_PROVIDER_URL)
web3 = new Web3(httpProvider)
return web3
}
}
1 change: 1 addition & 0 deletions packages/notification-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ notificationPolling.run()

if (!WEB3_PROVIDER_URL) {
console.info('No Web3 provider found. Skipping exchange polling.')
console.info('Note that you will need to manually set contract addresses.')
} else {
/**
* Start polling the Exchange contract
Expand Down
Loading