/
index.ts
180 lines (149 loc) · 5.96 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { Buffer } from 'buffer';
import { JWK } from '@learncard/types';
import { Plugin, Wallet } from 'types/wallet';
import { ethers } from 'ethers';
import { EthereumConfig, EthereumPluginMethods, TokenList } from './types';
import {
formatUnits,
parseUnits,
getTokenFromSymbolOrAddress,
getChainIdFromProvider,
} from './helpers';
import hardcodedTokens from './hardcodedTokens';
import { DidMethod } from '@wallet/plugins/didkit/types';
import { Algorithm } from '@wallet/plugins/didkey/types'; // Have to include this in order for getSubjectKeypair to not throw a type error
export * from './types';
const ERC20ABI = require('./erc20.abi.json');
/**
* @group Plugins
*/
export const getEthereumPlugin = (
initWallet: Wallet<
string,
{
getSubjectDid: (type: DidMethod) => string;
getSubjectKeypair: (type?: Algorithm) => JWK;
}
>,
config: EthereumConfig
): Plugin<'Ethereum', EthereumPluginMethods> => {
let { infuraProjectId, network = 'mainnet' } = config;
// Ethers wallet
const secpKeypair = initWallet.pluginMethods.getSubjectKeypair('secp256k1');
const privateKey = Buffer.from(secpKeypair.d, 'base64').toString('hex');
let ethersWallet = new ethers.Wallet(privateKey);
const publicKey: string = ethersWallet.address;
// Provider
const getProvider = () => {
let provider: ethers.providers.Provider;
if (infuraProjectId) {
provider = new ethers.providers.InfuraProvider(network, infuraProjectId);
} else {
provider = ethers.getDefaultProvider(network);
}
ethersWallet = ethersWallet.connect(provider);
return provider;
};
let provider = getProvider();
const getDefaultTokenList = (): TokenList => {
return require('@uniswap/default-token-list/build/uniswap-default.tokenlist.json').tokens.concat(
hardcodedTokens
);
};
const defaultTokenList: TokenList = getDefaultTokenList();
const getTokenAddress = async (tokenSymbolOrAddress: string) => {
return (
await getTokenFromSymbolOrAddress(
tokenSymbolOrAddress,
defaultTokenList,
await getChainIdFromProvider(provider)
)
)?.address;
};
// Core Methods
const getBalance = async (walletAddress = publicKey, tokenSymbolOrAddress = 'ETH') => {
if (!tokenSymbolOrAddress || tokenSymbolOrAddress === 'ETH') {
// check ETH by default
const balance = await provider.getBalance(walletAddress);
const formattedBalance = ethers.utils.formatEther(balance);
return formattedBalance;
}
const tokenAddress = await getTokenAddress(tokenSymbolOrAddress);
if (!tokenAddress) {
throw new Error(`Unable to determine token address for \"${tokenSymbolOrAddress}\"`);
}
const balance = await getErc20TokenBalance(tokenAddress, walletAddress);
return balance;
};
const getErc20TokenBalance = async (
tokenContractAddress: string,
walletPublicAddress = publicKey
) => {
const contract = new ethers.Contract(tokenContractAddress, ERC20ABI, provider);
const balance = await contract.balanceOf(walletPublicAddress);
const formattedBalance = formatUnits(
balance,
tokenContractAddress,
defaultTokenList,
await getChainIdFromProvider(provider)
);
return formattedBalance;
};
return {
pluginMethods: {
getEthereumAddress: () => publicKey,
getBalance: async (_wallet, symbolOrAddress = 'ETH') =>
getBalance(publicKey, symbolOrAddress),
getBalanceForAddress: async (_wallet, walletAddress, symbolOrAddress) =>
getBalance(walletAddress, symbolOrAddress),
transferTokens: async (_wallet, tokenSymbolOrAddress, amount, toAddress) => {
if (tokenSymbolOrAddress === 'ETH') {
const transaction = {
to: toAddress,
// Convert ETH to wei
value: ethers.utils.parseEther(amount.toString()),
};
return (await ethersWallet.sendTransaction(transaction)).hash;
}
const tokenAddress = await getTokenAddress(tokenSymbolOrAddress);
if (!tokenAddress) {
throw new Error(
`Unable to determine token address for \"${tokenSymbolOrAddress}\"`
);
}
const tokenContract = new ethers.Contract(tokenAddress, ERC20ABI, ethersWallet);
// const gas = ethers.utils.formatUnits(await provider.getGasPrice());
return (
await tokenContract.transfer(
toAddress,
await parseUnits(
amount.toString(),
tokenContract.address,
defaultTokenList,
await getChainIdFromProvider(provider)
)
)
).hash;
},
/* Configuration-type methods */
getCurrentNetwork: () => {
return network;
},
changeNetwork: (_wallet, _network: ethers.providers.Networkish) => {
const oldNetwork = network;
try {
network = _network;
provider = getProvider();
} catch (e) {
network = oldNetwork;
provider = getProvider();
throw e;
}
},
addInfuraProjectId: (_wallet, infuraProjectIdToAdd) => {
infuraProjectId = infuraProjectIdToAdd;
provider = getProvider();
},
},
};
};