Skip to content

Commit

Permalink
Merge pull request #31 from CoMakery/feature/prd-2006
Browse files Browse the repository at this point in the history
[PRD-2006] Migration to Alchemy
  • Loading branch information
Alexey1100 authored Sep 19, 2023
2 parents 7590a5e + 2ac0953 commit c854512
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 67 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PROJECT_ID=2
PROJECT_API_KEY=sWo+muXwPNtR7XgX04sBWPE2z1yXIb5G
COMAKERY_SERVER_URL=http://localhost:3000
BLOCKCHAIN_NETWORK=ethereum_rinkeby
INFURA_PROJECT_ID=
BLOCKCHAIN_NETWORK=ethereum_sepolia
ALCHEMY_API_KEY=
ETHEREUM_TOKEN_TYPE=token_release_schedule
ETHEREUM_CONTRACT_ADDRESS=0x9608848FA0063063d2Bb401e8B5efFcb8152Ec65
ETHEREUM_APPROVAL_CONTRACT_ADDRESS=0xe322488096c36edcce397d179e7b1217353884bb
Expand Down
1 change: 0 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const envs = {
projectApiKey: process.env.PROJECT_API_KEY,
comakeryServerUrl: process.env.COMAKERY_SERVER_URL,
purestakeApi: process.env.PURESTAKE_API,
infuraProjectId: process.env.INFURA_PROJECT_ID,
alchemyApiKey: process.env.ALCHEMY_API_KEY,
redisUrl: process.env.REDIS_URL,
emptyQueueDelay: parseInt(process.env.EMPTY_QUEUE_DELAY),
Expand Down
8 changes: 2 additions & 6 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,14 @@
"value": "5"
},
"BLOCKCHAIN_NETWORK": {
"description": "Which network should we use for the hot wallet: algorand, algorand_test, algorand_beta, ethereum, ethereum_ropsten, ethereum_rinkeby",
"description": "Which network should we use for the hot wallet: algorand, algorand_test, algorand_beta, ethereum, ethereum_goerli, ethereum_sepolia",
"required": true,
"value": "ethereum"
},
"MAX_AMOUNT_FOR_TRANSFER": {
"description": "Maximum amount of tokens to send in base units. Unlimited by default",
"required": false
},
"INFURA_PROJECT_ID": {
"description": "Project id generated on Infura. Required to use with ethereum blockchain.",
"required": false
},
"ETHEREUM_TOKEN_TYPE": {
"description": "Ethereum Token Type: erc20, erc20_batch, token_release_schedule. Required to use with ethereum blockchain.",
"required": false
Expand Down Expand Up @@ -98,7 +94,7 @@
"value": "4"
},
"ALCHEMY_API_KEY": {
"description": "API key for Alchemy. Required to use with Solana blockchain.",
"description": "API key for Alchemy. Required to use with Solana and Ethereum blockchain.",
"required": false
},
"OPT_IN_APP": {
Expand Down
2 changes: 1 addition & 1 deletion lib/Blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Blockchain {
this.blockchainNetwork = envs.blockchainNetwork
if (["algorand", "algorand_test", "algorand_beta"].includes(this.blockchainNetwork)) {
this.klass = new AlgorandBlockchain(this.envs)
} else if (["ethereum", "ethereum_ropsten", "ethereum_rinkeby", "ethereum_sepolia", "ethereum_goerli"].includes(this.blockchainNetwork)) {
} else if (["ethereum", "ethereum_sepolia", "ethereum_goerli"].includes(this.blockchainNetwork)) {
this.klass = new EthereumBlockchain(this.envs)
} else if (["solana", "solana_devnet"].includes(this.blockchainNetwork)) {
this.klass = new SolanaBlockchain(this.envs)
Expand Down
2 changes: 1 addition & 1 deletion lib/HotWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class HotWallet {
constructor(network, address, keys, options = {}) {
if (["algorand", "algorand_test", "algorand_beta"].includes(network)) {
this.klass = new AlgorandHotWallet(network, address, keys, options)
} else if (["ethereum", "ethereum_ropsten", "ethereum_rinkeby", "ethereum_sepolia", "ethereum_goerli"].includes(network)) {
} else if (["ethereum", "ethereum_sepolia", "ethereum_goerli"].includes(network)) {
this.klass = new EthereumHotWallet(network, address, keys, options)
} else if (["solana", "solana_devnet"].includes(network)) {
this.klass = new SolanaHotWallet(network, address, keys, options)
Expand Down
12 changes: 4 additions & 8 deletions lib/blockchains/EthereumBlockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class EthereumBlockchain {
constructor(envs) {
this.envs = envs
this.blockchainNetwork = envs.blockchainNetwork
this.web3 = new Web3(new Web3.providers.HttpProvider(`https://${this.chainName}.infura.io/v3/${this.envs.infuraProjectId}`))
this.web3 = new Web3(new Web3.providers.HttpProvider(`https://${this.chainName}.g.alchemy.com/v2/${this.envs.alchemyApiKey}`))
// It is necessary to cache values gotten from blockchain
this.ethBalances = {}
this.tokenBalances = {}
Expand All @@ -25,15 +25,11 @@ class EthereumBlockchain {
get chainName() {
switch (this.blockchainNetwork) {
case 'ethereum':
return 'mainnet'
case 'ethereum_ropsten':
return 'ropsten'
case 'ethereum_rinkeby':
return 'rinkeby'
return 'eth-mainnet'
case 'ethereum_goerli':
return 'goerli'
return 'eth-goerli'
case 'ethereum_sepolia':
return 'sepolia'
return 'eth-sepolia'
default:
console.error("Unknown or unsupported network: ", this.blockchainNetwork)
}
Expand Down
6 changes: 3 additions & 3 deletions lib/hotwalletUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ exports.checkAllVariablesAreSet = function checkAllVariablesAreSet(envs) {
let ethereumEnvs = false

if (envs.ethereumTokenType == 'eth') {
ethereumEnvs = Boolean(envs.infuraProjectId) && Boolean(envs.ethereumTokenSymbol)
ethereumEnvs = Boolean(envs.alchemyApiKey) && Boolean(envs.ethereumTokenSymbol)
} else {
ethereumEnvs = Boolean(envs.infuraProjectId) && Boolean(envs.ethereumTokenSymbol) && Boolean(envs.ethereumContractAddress)
ethereumEnvs = Boolean(envs.alchemyApiKey) && Boolean(envs.ethereumTokenSymbol) && Boolean(envs.ethereumContractAddress)
}

const variables = [
{
isEnvCorrect: ethereumEnvs,
blockchainNetworks: ["ethereum", "ethereum_ropsten", "ethereum_rinkeby", "ethereum_sepolia", "ethereum_goerli"]
blockchainNetworks: ["ethereum", "ethereum_sepolia", "ethereum_goerli"]
},
{
isEnvCorrect: Boolean(envs.purestakeApi),
Expand Down
4 changes: 2 additions & 2 deletions tests/Blockchain.generateNewWallet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ describe("Blockchain generate new wallet test suite", () => {

test('return correct generated keys for ethereum ', async () => {
const ethBlockchain = new hwUtils.EthereumBlockchain({
infuraProjectId: "39f6ad316c5a4b87a0f90956333c3666",
blockchainNetwork: 'ethereum_ropsten'
alchemyApiKey: "alchemy_api_key",
blockchainNetwork: 'ethereum_sepolia'
})
const createAccount = {
address: "0xdc5B966F639a2FDC78F0808E8D17087Ea835f13D",
Expand Down
4 changes: 2 additions & 2 deletions tests/EthereumBlockchain.disableWallet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ describe("EthereumBlockchain.disableWallet", () => {
const ethContractAddress = "0x1d1592c28fff3d3e71b1d29e31147846026a0a37"
const transactionHash = "0xb5c8bd9430b6cc87a0e2fe110ece6bf527fa4f170a4bc8cd032f768fc5219838"
const ethBlockchain = new EthereumBlockchain({
infuraProjectId: "infura_project_id",
blockchainNetwork: 'ethereum_ropsten',
alchemyApiKey: "alchemy_api_key",
blockchainNetwork: 'ethereum_sepolia',
ethereumContractAddress: ethContractAddress
})

Expand Down
8 changes: 4 additions & 4 deletions tests/EthereumBlockchain.getEthBalance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const BigNumber = require('bignumber.js')
describe("EthereumBlockchain check for eth balance", () => {
test('return balance as a BigNumber object', async () => {
const ethBlockchain = new EthereumBlockchain({
infuraProjectId: "39f6ad316c5a4b87a0f90956333c3666",
blockchainNetwork: 'ethereum_ropsten'
alchemyApiKey: "alchemy_api_key",
blockchainNetwork: 'ethereum_sepolia'
})

jest.spyOn(ethBlockchain.web3.eth, "getBalance").mockImplementation(() => { return "995" });
Expand All @@ -16,8 +16,8 @@ describe("EthereumBlockchain check for eth balance", () => {

test('return zero if undefined', async () => {
const ethBlockchain = new EthereumBlockchain({
infuraProjectId: "39f6ad316c5a4b87a0f90956333c3666",
blockchainNetwork: 'ethereum_ropsten'
alchemyApiKey: "alchemy_api_key",
blockchainNetwork: 'ethereum_sepolia'
})

jest.spyOn(ethBlockchain.web3.eth, "getBalance").mockImplementation(() => { return undefined });
Expand Down
12 changes: 6 additions & 6 deletions tests/EthereumBlockchain.getTokenBalance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const BigNumber = require('bignumber.js')
describe("EthereumBlockchain check for token balance", () => {
test('return balance as an object with balances (in base units and in tokens)', async () => {
const ethBlockchain = new EthereumBlockchain({
infuraProjectId: "infura_project_id",
blockchainNetwork: 'ethereum_ropsten',
alchemyApiKey: "alchemy_api_key",
blockchainNetwork: 'ethereum_sepolia',
ethereumContractAddress: "0x1d1592c28fff3d3e71b1d29e31147846026a0a37"
})

Expand All @@ -26,8 +26,8 @@ describe("EthereumBlockchain check for token balance", () => {

test('return zero for unknown contract', async () => {
const ethBlockchain = new EthereumBlockchain({
infuraProjectId: "infura_project_id",
blockchainNetwork: 'ethereum_ropsten',
alchemyApiKey: "alchemy_api_key",
blockchainNetwork: 'ethereum_sepolia',
ethereumContractAddress: "0xB5e3062f536cE503B27CB366529613aa3bE0408e"
})

Expand All @@ -48,8 +48,8 @@ describe("EthereumBlockchain check for token balance", () => {

test('get ethereumApprovalContractAddress balance for Lockup contract', async () => {
const ethBlockchain = new EthereumBlockchain({
infuraProjectId: "infura_project_id",
blockchainNetwork: 'ethereum_rinkeby',
alchemyApiKey: "alchemy_api_key",
blockchainNetwork: 'ethereum_sepolia',
ethereumTokenType: "token_release_schedule",
ethereumContractAddress: "0x9608848fa0063063d2bb401e8b5effcb8152ec65",
ethereumApprovalContractAddress: "0x68ac9A329c688AfBf1FC2e5d3e8Cb6E88989E2cC",
Expand Down
4 changes: 2 additions & 2 deletions tests/EthereumTxValidator.isTransactionValid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const blockchainTransaction = require('./fixtures/ethereumBlockchainTransaction'
describe("EthereumBlockchain.isTransactionValid", () => {
const hwAddress = '0x15b4eda54e7aa56e4ca4fe6c19f7bf9d82eca2fc'
const ethBlockchain = new EthereumBlockchain({
infuraProjectId: "infura_project_id",
blockchainNetwork: 'ethereum_ropsten',
alchemyApiKey: "alchemy_api_key",
blockchainNetwork: 'ethereum_sepolia',
ethereumContractAddress: "0x1d1592c28fff3d3e71b1d29e31147846026a0a37"
})
const txValidator = new EthereumTxValidator({
Expand Down
42 changes: 20 additions & 22 deletions tests/checkAllVariablesAreSet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ describe("Check that all variables are set test suite", () => {
projectApiKey: "project_api_key",
comakeryServerUrl: "http://cmk.server",
purestakeApi: "purestake_api_key",
// infuraProjectId: "infura_project_id", // not required for algorand
redisUrl: "redis://localhost:6379/0",
emptyQueueDelay: 30,
optInApp: 13997710,
Expand All @@ -26,11 +25,11 @@ describe("Check that all variables are set test suite", () => {
projectApiKey: "project_api_key",
comakeryServerUrl: "http://cmk.server",
// purestakeApi: "purestake_api_key", // not required for ethereum
infuraProjectId: "infura_project_id",
alchemyApiKey: "alchemy_api_key",
redisUrl: "redis://localhost:6379/0",
emptyQueueDelay: 30,
// optInApp: 13997710, // not required for ethereum
blockchainNetwork: 'ethereum_ropsten',
blockchainNetwork: 'ethereum_sepolia',
maxAmountForTransfer: 100000000, // optional
ethereumTokenSymbol: "XYZ2",
ethereumContractAddress: "0x1d1592c28fff3d3e71b1d29e31147846026a0a37"
Expand All @@ -44,7 +43,6 @@ describe("Check that all variables are set test suite", () => {
projectApiKey: "project_api_key",
comakeryServerUrl: "http://cmk.server",
// purestakeApi: "purestake_api_key", // not required for solana
// infuraProjectId: "infura_project_id", // not required for solana
alchemyApiKey: "alchemy_key",
redisUrl: "redis://localhost:6379/0",
emptyQueueDelay: 30,
Expand Down Expand Up @@ -152,11 +150,11 @@ describe("Check that all variables are set test suite", () => {
projectApiKey: "project_api_key",
comakeryServerUrl: "http://cmk.server",
purestakeApi: null,
infuraProjectId: "infura_project_id",
alchemyApiKey: "alchemy_api_key",
redisUrl: "redis://localhost:6379/0",
emptyQueueDelay: 30,
optInApp: 13997710,
blockchainNetwork: 'ethereum_ropsten',
blockchainNetwork: 'ethereum_sepolia',
maxAmountForTransfer: 100000000,
ethereumTokenSymbol: "XYZ2",
ethereumContractAddress: "0x1d1592c28fff3d3e71b1d29e31147846026a0a37"
Expand Down Expand Up @@ -214,11 +212,11 @@ describe("Check that all variables are set test suite", () => {
projectId: "1",
projectApiKey: "project_api_key",
comakeryServerUrl: "http://cmk.server",
infuraProjectId: "infura_project_id",
alchemyApiKey: "alchemy_api_key",
redisUrl: "redis://localhost:6379/0",
emptyQueueDelay: 30,
optInApp: null,
blockchainNetwork: 'ethereum_ropsten',
blockchainNetwork: 'ethereum_sepolia',
maxAmountForTransfer: 100000000,
ethereumTokenSymbol: "XYZ2",
ethereumContractAddress: "0x1d1592c28fff3d3e71b1d29e31147846026a0a37"
Expand All @@ -233,7 +231,7 @@ describe("Check that all variables are set test suite", () => {
projectApiKey: "project_api_key",
comakeryServerUrl: "http://cmk.server",
purestakeApi: "purestake_api_key",
infuraProjectId: "infura_project_id",
alchemyApiKey: "alchemy_api_key",
redisUrl: "redis://localhost:6379/0",
emptyQueueDelay: 30,
optInApp: 13997710,
Expand Down Expand Up @@ -266,7 +264,7 @@ describe("Check that all variables are set test suite", () => {
projectApiKey: "project_api_key",
comakeryServerUrl: "http://cmk.server",
purestakeApi: "purestake_api_key",
infuraProjectId: "infura_project_id",
alchemyApiKey: "alchemy_api_key",
redisUrl: "redis://localhost:6379/0",
emptyQueueDelay: 30,
optInApp: 13997710,
Expand All @@ -283,10 +281,10 @@ describe("Check that all variables are set test suite", () => {
projectId: "1",
projectApiKey: "project_api_key",
comakeryServerUrl: "http://cmk.server",
infuraProjectId: "infura_project_id",
alchemyApiKey: "alchemy_api_key",
redisUrl: "redis://localhost:6379/0",
emptyQueueDelay: 30,
blockchainNetwork: 'ethereum_ropsten',
blockchainNetwork: 'ethereum_sepolia',
maxAmountForTransfer: 100000000,
ethereumTokenSymbol: null,
ethereumContractAddress: "0x1d1592c28fff3d3e71b1d29e31147846026a0a37"
Expand All @@ -300,7 +298,7 @@ describe("Check that all variables are set test suite", () => {
projectApiKey: "project_api_key",
comakeryServerUrl: "http://cmk.server",
purestakeApi: "purestake_api_key",
infuraProjectId: "infura_project_id",
alchemyApiKey: "alchemy_api_key",
redisUrl: "redis://localhost:6379/0",
emptyQueueDelay: 30,
optInApp: 13997710,
Expand All @@ -317,10 +315,10 @@ describe("Check that all variables are set test suite", () => {
projectId: "1",
projectApiKey: "project_api_key",
comakeryServerUrl: "http://cmk.server",
infuraProjectId: "infura_project_id",
alchemyApiKey: "alchemy_api_key",
redisUrl: "redis://localhost:6379/0",
emptyQueueDelay: 30,
blockchainNetwork: 'ethereum_ropsten',
blockchainNetwork: 'ethereum_sepolia',
maxAmountForTransfer: 100000000,
ethereumTokenSymbol: "XYZ2",
ethereumContractAddress: null
Expand All @@ -333,10 +331,10 @@ describe("Check that all variables are set test suite", () => {
projectId: "1",
projectApiKey: "project_api_key",
comakeryServerUrl: "http://cmk.server",
infuraProjectId: "infura_project_id",
alchemyApiKey: "alchemy_api_key",
redisUrl: "redis://localhost:6379/0",
emptyQueueDelay: 30,
blockchainNetwork: 'ethereum_ropsten',
blockchainNetwork: 'ethereum_sepolia',
maxAmountForTransfer: 100000000,
ethereumTokenSymbol: "ETH",
ethereumTokenType: 'eth',
Expand All @@ -345,13 +343,13 @@ describe("Check that all variables are set test suite", () => {
expect(hwUtils.checkAllVariablesAreSet(envs)).toBe(true)
})

test('infuraProjectId is null for algorand', async () => {
test('alchemyApiKey is null for algorand', async () => {
const envs = {
projectId: "1",
projectApiKey: "project_api_key",
comakeryServerUrl: "http://cmk.server",
purestakeApi: "purestake_api_key",
infuraProjectId: null,
alchemyApiKey: null,
redisUrl: "redis://localhost:6379/0",
emptyQueueDelay: 30,
optInApp: 13997710,
Expand All @@ -363,15 +361,15 @@ describe("Check that all variables are set test suite", () => {
expect(hwUtils.checkAllVariablesAreSet(envs)).toBe(true)
})

test('infuraProjectId is null for ethereum', async () => {
test('alchemyApiKey is null for ethereum', async () => {
const envs = {
projectId: "1",
projectApiKey: "project_api_key",
comakeryServerUrl: "http://cmk.server",
infuraProjectId: null,
alchemyApiKey: null,
redisUrl: "redis://localhost:6379/0",
emptyQueueDelay: 30,
blockchainNetwork: 'ethereum_ropsten',
blockchainNetwork: 'ethereum_sepolia',
maxAmountForTransfer: 100000000,
ethereumTokenSymbol: "XYZ2",
ethereumContractAddress: "0x1d1592c28fff3d3e71b1d29e31147846026a0a37"
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/ethereumBlockchainTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const blockchainTransaction = {
amount: 5,
nonce: null,
contractAddress: '0xE322488096C36edccE397D179E7b1217353884BB',
network: 'ethereum_rinkeby',
network: 'ethereum_sepolia',
txHash: null,
txRaw: JSON.stringify(txRaw),
status: 'created',
Expand Down
4 changes: 2 additions & 2 deletions tests/hotWalletInitialization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("Hot Wallet initialization suite", () => {
const algoEnvs = envs
algoEnvs.blockchainNetwork = "algorand_test"
const ethEnvs = envs
ethEnvs.blockchainNetwork = "ethereum_ropsten"
ethEnvs.blockchainNetwork = "ethereum_sepolia"

const redisClient = redis.createClient()
const hwRedis = new hwUtils.HotWalletRedis(envs, redisClient)
Expand All @@ -30,7 +30,7 @@ describe("Hot Wallet initialization suite", () => {
privateKeyEncrypted: ""
}
const algorandHW = new hwUtils.HotWallet("algorand_test", "AV52YP3JEHUYVCNDX7RLF3ATQV35UZXOLAZJW5A6ITPJGQY7RPPKRRMHNY", algorandWalletKeys)
const ethHW = new hwUtils.HotWallet("ethereum_ropsten", "0xdc5B966F639a2FDC78F0808E8D17087Ea835f13D", ethWalletKeys)
const ethHW = new hwUtils.HotWallet("ethereum_sepolia", "0xdc5B966F639a2FDC78F0808E8D17087Ea835f13D", ethWalletKeys)

beforeEach(async () => {
await hwRedis.deleteCurrentKey()
Expand Down
Loading

0 comments on commit c854512

Please sign in to comment.