|
| 1 | +import SponsorWallet, { ISponsorWallet } from "models/sponsor-wallet"; |
| 2 | +import SponsorWalletTransactions, { |
| 3 | + ISponsorWalletTransaction, |
| 4 | + SponsorWalletTransactionStatus, |
| 5 | +} from "models/sponsor-wallet-transactions"; |
| 6 | +import web3 from "utils/get-web3"; |
| 7 | +import { TransactionConfig } from "web3-core"; |
| 8 | + |
| 9 | +export class SponsorWalletService { |
| 10 | + public async createSponsorWallet(privateKey: string) { |
| 11 | + const account = web3.eth.accounts.privateKeyToAccount(privateKey); |
| 12 | + |
| 13 | + const balanceInWei = await web3.eth.getBalance(account.address); |
| 14 | + |
| 15 | + const balanceEth = web3.utils.fromWei(balanceInWei, "ether"); |
| 16 | + |
| 17 | + if (Number(balanceEth) < 0.01) { |
| 18 | + throw new Error(`${account.address} Balance is less than 0.01`); |
| 19 | + } |
| 20 | + |
| 21 | + const wallet = new SponsorWallet({ |
| 22 | + address: account.address, |
| 23 | + privateKey, |
| 24 | + }); |
| 25 | + await wallet.save(); |
| 26 | + return wallet.toObject(); |
| 27 | + } |
| 28 | + |
| 29 | + public async sponsorTransaction( |
| 30 | + transferId: string, |
| 31 | + transactionConfig: Omit<TransactionConfig, "nonce"> |
| 32 | + ): Promise<ISponsorWalletTransaction> { |
| 33 | + const wallet = await this.getAvailableWalletForSigning(); |
| 34 | + |
| 35 | + if (!wallet) { |
| 36 | + throw new Error("No Sponsor Wallet available"); |
| 37 | + } |
| 38 | + |
| 39 | + const existingTransaction = await SponsorWalletTransactions.findOne({ |
| 40 | + transferId: transferId, |
| 41 | + }); |
| 42 | + |
| 43 | + if (existingTransaction) { |
| 44 | + return existingTransaction; |
| 45 | + } |
| 46 | + |
| 47 | + const nonce = await this.getWalletNextNonce(wallet.id); |
| 48 | + |
| 49 | + const sender = web3.eth.accounts.privateKeyToAccount(wallet.privateKey); |
| 50 | + |
| 51 | + const signedTransaction = await sender.signTransaction({ |
| 52 | + ...transactionConfig, |
| 53 | + nonce, |
| 54 | + }); |
| 55 | + |
| 56 | + if ( |
| 57 | + signedTransaction.rawTransaction === undefined || |
| 58 | + signedTransaction.transactionHash === undefined |
| 59 | + ) { |
| 60 | + throw new Error("Raw transaction is undefined"); |
| 61 | + } |
| 62 | + |
| 63 | + let walletTransaction = new SponsorWalletTransactions({ |
| 64 | + transferId: transferId, |
| 65 | + walletId: wallet.id, |
| 66 | + transactionHash: signedTransaction.transactionHash, |
| 67 | + status: "pending", |
| 68 | + }); |
| 69 | + |
| 70 | + walletTransaction = await walletTransaction.save(); |
| 71 | + |
| 72 | + let receipt = await web3.eth |
| 73 | + .getTransactionReceipt(signedTransaction.transactionHash) |
| 74 | + .catch(() => undefined); |
| 75 | + let status: SponsorWalletTransactionStatus = "success"; |
| 76 | + |
| 77 | + if (!receipt) { |
| 78 | + receipt = await web3.eth.sendSignedTransaction( |
| 79 | + signedTransaction.rawTransaction |
| 80 | + ); |
| 81 | + status = "pending"; |
| 82 | + } |
| 83 | + |
| 84 | + walletTransaction.status = status; |
| 85 | + |
| 86 | + return walletTransaction.save(); |
| 87 | + } |
| 88 | + |
| 89 | + public async updateSponsorWalletTransactionStatus(transactionHash: string) { |
| 90 | + const transaction = await SponsorWalletTransactions.findOne({ |
| 91 | + transactionHash, |
| 92 | + }); |
| 93 | + |
| 94 | + if (!transaction || transaction.status !== "pending") { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + const receipt = await web3.eth.getTransactionReceipt(transactionHash); |
| 99 | + if (!receipt?.blockNumber) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + transaction.status = receipt.status ? "success" : "failed"; |
| 104 | + await transaction.save(); |
| 105 | + } |
| 106 | + |
| 107 | + private async getWalletNextNonce(walletId: string): Promise<number> { |
| 108 | + const wallet = await SponsorWallet.findOne({ id: walletId }); |
| 109 | + if (!wallet) { |
| 110 | + throw new Error("Wallet not found"); |
| 111 | + } |
| 112 | + const lastTransaction = await SponsorWalletTransactions.findOne({ |
| 113 | + walletId: wallet?.id, |
| 114 | + }).sort({ createdAt: -1 }); |
| 115 | + |
| 116 | + if (!lastTransaction) { |
| 117 | + const nonce = await web3.eth.getTransactionCount( |
| 118 | + wallet.address, |
| 119 | + "pending" |
| 120 | + ); |
| 121 | + return nonce; |
| 122 | + } |
| 123 | + |
| 124 | + const transaction = await web3.eth.getTransaction( |
| 125 | + lastTransaction.transactionHash |
| 126 | + ); |
| 127 | + |
| 128 | + return transaction.nonce + 1; |
| 129 | + } |
| 130 | + |
| 131 | + private async getAvailableWalletForSigning(): Promise<ISponsorWallet> { |
| 132 | + const walletWithNoPendingTransactions = |
| 133 | + await this.getWalletWithNoPendingTransactions(); |
| 134 | + |
| 135 | + if (walletWithNoPendingTransactions.length > 0) { |
| 136 | + return walletWithNoPendingTransactions[0]; |
| 137 | + } |
| 138 | + |
| 139 | + const walletWithLeastPendingTransactions = |
| 140 | + await this.getWalletWithLeastPendingTransactions(); |
| 141 | + |
| 142 | + return walletWithLeastPendingTransactions; |
| 143 | + } |
| 144 | + |
| 145 | + private getWalletWithNoPendingTransactions(): Promise<ISponsorWallet[]> { |
| 146 | + return SponsorWallet.aggregate([ |
| 147 | + { |
| 148 | + $lookup: { |
| 149 | + from: "sponsorwallettransactions", |
| 150 | + localField: "_id", |
| 151 | + foreignField: "walletId", |
| 152 | + as: "transactions", |
| 153 | + }, |
| 154 | + }, |
| 155 | + { |
| 156 | + $match: { |
| 157 | + "transactions.status": { |
| 158 | + $ne: "pending", |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + ]); |
| 163 | + } |
| 164 | + |
| 165 | + private getWalletWithLeastPendingTransactions(): Promise<ISponsorWallet> { |
| 166 | + return SponsorWallet.aggregate([ |
| 167 | + { |
| 168 | + $lookup: { |
| 169 | + from: "sponsorwallettransactions", |
| 170 | + localField: "_id", |
| 171 | + foreignField: "walletId", |
| 172 | + as: "transactions", |
| 173 | + }, |
| 174 | + }, |
| 175 | + { |
| 176 | + $match: { |
| 177 | + "transactions.status": "pending", |
| 178 | + }, |
| 179 | + }, |
| 180 | + { |
| 181 | + $project: { |
| 182 | + id: "$_id", |
| 183 | + address: 1, |
| 184 | + privateKey: 1, |
| 185 | + pendingTransactionCount: { |
| 186 | + $size: "$transactions", |
| 187 | + }, |
| 188 | + }, |
| 189 | + }, |
| 190 | + { |
| 191 | + $sort: { |
| 192 | + pendingTransactionCount: 1, |
| 193 | + }, |
| 194 | + }, |
| 195 | + { |
| 196 | + $limit: 1, |
| 197 | + }, |
| 198 | + ]).then((wallets) => wallets[0]); |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +export default SponsorWalletService; |
0 commit comments