@cityofzion/bs-ethereum 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,86 @@
1
+ import {
2
+ BalanceResponse,
3
+ BlockchainDataService,
4
+ ContractResponse,
5
+ Network,
6
+ Token,
7
+ TransactionResponse,
8
+ TransactionsByAddressParams,
9
+ TransactionsByAddressResponse,
10
+ } from '@cityofzion/blockchain-service'
11
+ import { ethers } from 'ethers'
12
+ import { TOKENS } from './constants'
13
+
14
+ export class RpcBDSEthereum implements BlockchainDataService {
15
+ private readonly network: Network
16
+
17
+ constructor(network: Network) {
18
+ this.network = network
19
+ }
20
+
21
+ async getTransaction(hash: string): Promise<TransactionResponse> {
22
+ const provider = new ethers.providers.JsonRpcProvider(this.network.url)
23
+
24
+ const transaction = await provider.getTransaction(hash)
25
+ if (!transaction || !transaction.blockHash || !transaction.to) throw new Error('Transaction not found')
26
+
27
+ const block = await provider.getBlock(transaction.blockHash)
28
+ if (!block) throw new Error('Block not found')
29
+
30
+ const tokens = TOKENS[this.network.type]
31
+ const token = tokens.find(token => token.symbol === 'ETH')!
32
+
33
+ return {
34
+ block: block.number,
35
+ time: block.timestamp,
36
+ hash: transaction.hash,
37
+ transfers: [
38
+ {
39
+ type: 'token',
40
+ amount: ethers.utils.formatEther(transaction.value),
41
+ contractHash: '-',
42
+ from: transaction.from,
43
+ to: transaction.to,
44
+ token,
45
+ },
46
+ ],
47
+ notifications: [],
48
+ }
49
+ }
50
+
51
+ async getTransactionsByAddress(_params: TransactionsByAddressParams): Promise<TransactionsByAddressResponse> {
52
+ throw new Error("RPC doesn't support get transactions history of address")
53
+ }
54
+
55
+ async getContract(): Promise<ContractResponse> {
56
+ throw new Error("RPC doesn't support contract info")
57
+ }
58
+
59
+ async getTokenInfo(hash: string): Promise<Token> {
60
+ const tokens = TOKENS[this.network.type]
61
+ const token = tokens.find(token => token.hash === hash)
62
+ if (!token) throw new Error('Token not found')
63
+
64
+ return token
65
+ }
66
+
67
+ async getBalance(address: string): Promise<BalanceResponse[]> {
68
+ const provider = new ethers.providers.JsonRpcProvider(this.network.url)
69
+ const balance = await provider.getBalance(address)
70
+
71
+ const tokens = TOKENS[this.network.type]
72
+ const token = tokens.find(token => token.symbol === 'ETH')!
73
+
74
+ return [
75
+ {
76
+ amount: ethers.utils.formatEther(balance),
77
+ token,
78
+ },
79
+ ]
80
+ }
81
+
82
+ async getBlockHeight(): Promise<number> {
83
+ const provider = new ethers.providers.JsonRpcProvider(this.network.url)
84
+ return await provider.getBlockNumber()
85
+ }
86
+ }
@@ -0,0 +1,122 @@
1
+ import { BDSClaimable, BlockchainDataService } from '@cityofzion/blockchain-service'
2
+ import { BitqueryBDSEthereum } from '../BitqueryBDSEthereum'
3
+ import { RpcBDSEthereum } from '../RpcBDSEthereum'
4
+ import { DEFAULT_URL_BY_NETWORK_TYPE } from '../constants'
5
+
6
+ const bitqueryBDSEthereum = new BitqueryBDSEthereum({ type: 'testnet', url: DEFAULT_URL_BY_NETWORK_TYPE.testnet })
7
+ const rpcBDSEthereum = new RpcBDSEthereum({ type: 'testnet', url: DEFAULT_URL_BY_NETWORK_TYPE.testnet })
8
+
9
+ describe('BDSEthereum', () => {
10
+ it.each([rpcBDSEthereum, bitqueryBDSEthereum])(
11
+ 'Should be able to get transaction - %s',
12
+ async (BDSEthereum: BlockchainDataService) => {
13
+ const hash = '0x43fa3015d077a13888409cfbd6228df8900abcd5314ff11ea6ce0c49e8b7c94d'
14
+ const transaction = await BDSEthereum.getTransaction(hash)
15
+
16
+ expect(transaction).toEqual(
17
+ expect.objectContaining({
18
+ block: expect.any(Number),
19
+ hash,
20
+ notifications: [],
21
+ time: expect.any(Number),
22
+ })
23
+ )
24
+ transaction.transfers.forEach(transfer => {
25
+ expect(transfer).toEqual(
26
+ expect.objectContaining({
27
+ from: expect.any(String),
28
+ to: expect.any(String),
29
+ contractHash: expect.any(String),
30
+ amount: expect.any(String),
31
+ type: expect.any(String),
32
+ })
33
+ )
34
+ })
35
+ },
36
+ 10000
37
+ )
38
+
39
+ it.each([bitqueryBDSEthereum])(
40
+ 'Should be able to get transactions of address - %s',
41
+ async (BDSEthereum: BlockchainDataService) => {
42
+ const address = '0x82B5Cd984880C8A821429cFFf89f36D35BaeBE89'
43
+ const response = await BDSEthereum.getTransactionsByAddress({ address: address, page: 1 })
44
+ response.transactions.forEach(transaction => {
45
+ expect(transaction).toEqual(
46
+ expect.objectContaining({
47
+ block: expect.any(Number),
48
+ hash: expect.any(String),
49
+ notifications: [],
50
+ time: expect.any(Number),
51
+ fee: expect.any(String),
52
+ })
53
+ )
54
+
55
+ transaction.transfers.forEach(transfer => {
56
+ expect(transfer).toEqual(
57
+ expect.objectContaining({
58
+ from: expect.any(String),
59
+ to: expect.any(String),
60
+ contractHash: expect.any(String),
61
+ amount: expect.any(String),
62
+ type: expect.any(String),
63
+ })
64
+ )
65
+ })
66
+ })
67
+ },
68
+ 10000
69
+ )
70
+
71
+ it.each([bitqueryBDSEthereum, rpcBDSEthereum])(
72
+ 'Should be able to get eth info - %s',
73
+ async (BDSEthereum: BlockchainDataService) => {
74
+ const hash = '-'
75
+ const token = await BDSEthereum.getTokenInfo(hash)
76
+
77
+ expect(token).toEqual({
78
+ symbol: 'ETH',
79
+ name: 'Ethereum',
80
+ hash: '-',
81
+ decimals: 16,
82
+ })
83
+ }
84
+ )
85
+
86
+ it.each([bitqueryBDSEthereum])(
87
+ 'Should be able to get token info - %s',
88
+ async (BDSEthereum: BlockchainDataService) => {
89
+ const hash = '0xBA62BCfcAaFc6622853cca2BE6Ac7d845BC0f2Dc'
90
+ const token = await BDSEthereum.getTokenInfo(hash)
91
+
92
+ expect(token).toEqual({
93
+ hash: '0xba62bcfcaafc6622853cca2be6ac7d845bc0f2dc',
94
+ name: 'FaucetToken',
95
+ symbol: 'FAU',
96
+ decimals: 18,
97
+ })
98
+ }
99
+ )
100
+
101
+ it.each([bitqueryBDSEthereum, rpcBDSEthereum])(
102
+ 'Should be able to get balance - %s',
103
+ async (BDSEthereum: BlockchainDataService) => {
104
+ const address = '0x82B5Cd984880C8A821429cFFf89f36D35BaeBE89'
105
+ const balance = await BDSEthereum.getBalance(address)
106
+
107
+ balance.forEach(balance => {
108
+ expect(balance).toEqual(
109
+ expect.objectContaining({
110
+ amount: expect.any(String),
111
+ token: {
112
+ hash: expect.any(String),
113
+ name: expect.any(String),
114
+ symbol: expect.any(String),
115
+ decimals: expect.any(Number),
116
+ },
117
+ })
118
+ )
119
+ })
120
+ }
121
+ )
122
+ })
@@ -0,0 +1,123 @@
1
+ import { ethers } from 'ethers'
2
+ import { BSEthereum } from '../BSEthereum'
3
+ import { Account } from '@cityofzion/blockchain-service'
4
+
5
+ let bsEthereum: BSEthereum
6
+ let wallet: ethers.Wallet
7
+ let account: Account
8
+
9
+ describe('BSEthereum', () => {
10
+ beforeAll(() => {
11
+ bsEthereum = new BSEthereum('neo3', { type: 'testnet' })
12
+ wallet = ethers.Wallet.createRandom()
13
+ account = {
14
+ key: wallet.privateKey,
15
+ type: 'privateKey',
16
+ address: wallet.address,
17
+ }
18
+ })
19
+
20
+ it('Should be able to validate an address', () => {
21
+ const validAddress = '0xD81a8F3c3f8b006Ef1ae4a2Fd28699AD7E3e21C5'
22
+ const invalidAddress = 'invalid address'
23
+
24
+ expect(bsEthereum.validateAddress(validAddress)).toBeTruthy()
25
+ expect(bsEthereum.validateAddress(invalidAddress)).toBeFalsy()
26
+ })
27
+
28
+ it('Should be able to validate an encrypted key', async () => {
29
+ const validEncryptedJson = await wallet.encrypt('password')
30
+ const invalidEncryptedJson = '{ invalid: json }'
31
+
32
+ expect(bsEthereum.validateEncrypted(validEncryptedJson)).toBeTruthy()
33
+ expect(bsEthereum.validateEncrypted(invalidEncryptedJson)).toBeFalsy()
34
+ })
35
+
36
+ it('Should be able to validate a private key', () => {
37
+ const validKey = wallet.privateKey
38
+ const invalidKey = 'invalid key'
39
+
40
+ expect(bsEthereum.validateKey(validKey)).toBeTruthy()
41
+ expect(bsEthereum.validateKey(invalidKey)).toBeFalsy()
42
+ })
43
+
44
+ it('Should be able to validate an domain', () => {
45
+ const validDomain = 'alice.eth'
46
+ const invalidDomain = 'invalid domain'
47
+
48
+ expect(bsEthereum.validateNameServiceDomainFormat(validDomain)).toBeTruthy()
49
+ expect(bsEthereum.validateNameServiceDomainFormat(invalidDomain)).toBeFalsy()
50
+ })
51
+
52
+ it('Should be able to generate a account from mnemonic', () => {
53
+ const account = bsEthereum.generateAccountFromMnemonic(wallet.mnemonic.phrase, 0)
54
+
55
+ expect(bsEthereum.validateAddress(account.address)).toBeTruthy()
56
+ expect(bsEthereum.validateKey(account.key)).toBeTruthy()
57
+ })
58
+
59
+ it('Should be able to generate a account from wif', () => {
60
+ const accountFromWif = bsEthereum.generateAccountFromKey(wallet.privateKey)
61
+ expect(accountFromWif).toEqual(account)
62
+ })
63
+
64
+ it('Should be able to decrypt a encrypted key', async () => {
65
+ const password = 'TestPassword'
66
+ const validEncryptedJson = await wallet.encrypt(password)
67
+ const decryptedAccount = await bsEthereum.decrypt(validEncryptedJson, password)
68
+ expect(decryptedAccount).toEqual(account)
69
+ })
70
+
71
+ it.skip('Should be able to calculate transfer fee', async () => {
72
+ const account = bsEthereum.generateAccountFromKey(process.env.TESTNET_PRIVATE_KEY as string)
73
+
74
+ const fee = await bsEthereum.calculateTransferFee({
75
+ senderAccount: account,
76
+ intent: {
77
+ amount: '0.1',
78
+ receiverAddress: '0xFACf5446B71dB33E920aB1769d9427146183aEcd',
79
+ tokenDecimals: 18,
80
+ tokenHash: '0xBA62BCfcAaFc6622853cca2BE6Ac7d845BC0f2Dc',
81
+ },
82
+ })
83
+
84
+ expect(fee).toEqual(expect.any(Number))
85
+ })
86
+
87
+ it.skip('Should be able to transfer a native token', async () => {
88
+ const account = bsEthereum.generateAccountFromKey(process.env.TESTNET_PRIVATE_KEY as string)
89
+
90
+ const transactionHash = await bsEthereum.transfer({
91
+ senderAccount: account,
92
+ intent: {
93
+ amount: '0.00000001',
94
+ receiverAddress: '0xFACf5446B71dB33E920aB1769d9427146183aEcd',
95
+ tokenDecimals: 18,
96
+ tokenHash: '-',
97
+ },
98
+ })
99
+
100
+ expect(transactionHash).toEqual(expect.any(String))
101
+ }, 50000)
102
+
103
+ it.skip('Should be able to transfer a ERC20 token', async () => {
104
+ const account = bsEthereum.generateAccountFromKey(process.env.TESTNET_PRIVATE_KEY as string)
105
+
106
+ const transactionHash = await bsEthereum.transfer({
107
+ senderAccount: account,
108
+ intent: {
109
+ amount: '0.00000001',
110
+ receiverAddress: '0xFACf5446B71dB33E920aB1769d9427146183aEcd',
111
+ tokenDecimals: 18,
112
+ tokenHash: '0xba62bcfcaafc6622853cca2be6ac7d845bc0f2dc',
113
+ },
114
+ })
115
+
116
+ expect(transactionHash).toEqual(expect.any(String))
117
+ }, 50000)
118
+
119
+ it('Should be able to resolve a name service domain', async () => {
120
+ const address = await bsEthereum.resolveNameServiceDomain('alice.eth')
121
+ expect(address).toEqual('0xa974890156A3649A23a6C0f2ebd77D6F7A7333d4')
122
+ }, 10000)
123
+ })
@@ -0,0 +1,46 @@
1
+ import { BitqueryEDSEthereum } from '../BitqueryEDSEthereum'
2
+
3
+ let bitqueryEDSEthereum: BitqueryEDSEthereum
4
+
5
+ describe('FlamingoEDSNeo3', () => {
6
+ beforeAll(() => {
7
+ bitqueryEDSEthereum = new BitqueryEDSEthereum('mainnet')
8
+ })
9
+
10
+ it('Should return a list with prices of tokens using USD', async () => {
11
+ const tokenPriceList = await bitqueryEDSEthereum.getTokenPrices('USD')
12
+
13
+ tokenPriceList.forEach(tokenPrice => {
14
+ expect(tokenPrice).toEqual({
15
+ price: expect.any(Number),
16
+ symbol: expect.any(String),
17
+ })
18
+ })
19
+ })
20
+
21
+ it('Should return a list with prices of tokens using BRL', async () => {
22
+ const tokenPriceListInUSD = await bitqueryEDSEthereum.getTokenPrices('USD')
23
+ const tokenPriceList = await bitqueryEDSEthereum.getTokenPrices('BRL')
24
+
25
+ tokenPriceList.forEach((tokenPrice, index) => {
26
+ expect(tokenPrice.price).toBeGreaterThan(tokenPriceListInUSD[index].price)
27
+ expect(tokenPrice).toEqual({
28
+ price: expect.any(Number),
29
+ symbol: expect.any(String),
30
+ })
31
+ })
32
+ })
33
+
34
+ it('Should return a list with prices of tokens using EUR', async () => {
35
+ const tokenPriceListInUSD = await bitqueryEDSEthereum.getTokenPrices('USD')
36
+ const tokenPriceList = await bitqueryEDSEthereum.getTokenPrices('EUR')
37
+
38
+ tokenPriceList.forEach((tokenPrice, index) => {
39
+ expect(tokenPrice.price).toBeLessThan(tokenPriceListInUSD[index].price)
40
+ expect(tokenPrice).toEqual({
41
+ price: expect.any(Number),
42
+ symbol: expect.any(String),
43
+ })
44
+ })
45
+ })
46
+ })
@@ -0,0 +1,45 @@
1
+ import { GhostMarketNDSEthereum } from '../GhostMarketNDSEthereum'
2
+
3
+ let ghostMarketNDSEthereum: GhostMarketNDSEthereum
4
+
5
+ describe('GhostMarketNDSEthereum', () => {
6
+ beforeAll(() => {
7
+ ghostMarketNDSEthereum = new GhostMarketNDSEthereum('mainnet')
8
+ })
9
+
10
+ it('Get NFT', async () => {
11
+ const nft = await ghostMarketNDSEthereum.getNft({
12
+ contractHash: '0xeb3a9a839dfeeaf71db1b4ed6a8ae0ccb171b227',
13
+ tokenId: '379',
14
+ })
15
+
16
+ expect(nft).toEqual(
17
+ expect.objectContaining({
18
+ id: '379',
19
+ contractHash: '0xeb3a9a839dfeeaf71db1b4ed6a8ae0ccb171b227',
20
+ symbol: 'MOAR',
21
+ collectionImage: expect.any(String),
22
+ collectionName: '"MOAR" by Joan Cornella',
23
+ image: expect.any(String),
24
+ isSVG: expect.any(Boolean),
25
+ name: 'MOAR #379',
26
+ })
27
+ )
28
+ })
29
+
30
+ it('Get NFTS by address', async () => {
31
+ const nfts = await ghostMarketNDSEthereum.getNftsByAddress({
32
+ address: '0xd773c81a4a855556ce2f2372b12272710b95b26c',
33
+ })
34
+ expect(nfts.items.length).toBeGreaterThan(0)
35
+ nfts.items.forEach(nft => {
36
+ expect(nft).toEqual(
37
+ expect.objectContaining({
38
+ symbol: expect.any(String),
39
+ id: expect.any(String),
40
+ contractHash: expect.any(String),
41
+ })
42
+ )
43
+ })
44
+ })
45
+ })
@@ -0,0 +1,8 @@
1
+ [
2
+ {
3
+ "symbol": "ETH",
4
+ "name": "Ethereum",
5
+ "hash": "-",
6
+ "decimals": 16
7
+ }
8
+ ]
@@ -0,0 +1,37 @@
1
+ import { NetworkType, Token } from '@cityofzion/blockchain-service'
2
+ import commom from './assets/tokens/common.json'
3
+
4
+ export type BitqueryNetwork = 'ethereum' | 'goerli'
5
+
6
+ export const TOKENS: Record<NetworkType, Token[]> = {
7
+ mainnet: [...commom],
8
+ testnet: commom,
9
+ custom: commom,
10
+ }
11
+
12
+ export const NATIVE_ASSETS = commom
13
+
14
+ export const DEFAULT_URL_BY_NETWORK_TYPE: Record<NetworkType, string> = {
15
+ mainnet: 'https://ethereum-mainnet-rpc.allthatnode.com',
16
+ testnet: 'https://ethereum-goerli-rpc.allthatnode.com',
17
+ custom: 'http://127.0.0.1:8545',
18
+ }
19
+
20
+ export const BITQUERY_API_KEY = 'BQYMp76Ny15C8ORbI2BOstFUhoMCahLI'
21
+ export const BITQUERY_URL = 'https://graphql.bitquery.io'
22
+ export const BITQUERY_NETWORK_BY_NETWORK_TYPE: Record<Exclude<NetworkType, 'custom'>, BitqueryNetwork> = {
23
+ mainnet: 'ethereum',
24
+ testnet: 'goerli',
25
+ }
26
+
27
+ export const GHOSTMARKET_URL_BY_NETWORK_TYPE: Partial<Record<NetworkType, string>> = {
28
+ mainnet: 'https://api.ghostmarket.io/api/v2',
29
+ testnet: 'https://api-testnet.ghostmarket.io/api/v2',
30
+ }
31
+
32
+ export const GHOSTMARKET_CHAIN_BY_NETWORK_TYPE: Partial<Record<NetworkType, string>> = {
33
+ mainnet: 'eth',
34
+ testnet: 'etht',
35
+ }
36
+
37
+ export const DERIVATION_PATH = "m/44'/60'/0'/0/?"