1+ import { DEFAULT_GAS_LIMIT } from "@constants" ;
12import SponsorWallet , { ISponsorWallet } from "models/sponsor-wallet" ;
23import SponsorWalletTransactions , {
34 ISponsorWalletTransaction ,
4- SponsorWalletTransactionStatus ,
5+ SponsorWalletTransactionCollectionName ,
56} from "models/sponsor-wallet-transactions" ;
67import web3 from "utils/get-web3" ;
78import { TransactionConfig } from "web3-core" ;
@@ -44,12 +45,20 @@ export class SponsorWalletService {
4445 return existingTransaction ;
4546 }
4647
47- const nonce = await this . getWalletNextNonce ( wallet . id ) ;
48+ const nonce = await this . getWalletNextNonce ( wallet . _id ) ;
4849
4950 const sender = web3 . eth . accounts . privateKeyToAccount ( wallet . privateKey ) ;
5051
52+ const gasPrice = await web3 . eth . getGasPrice ( ) ;
53+ const gas = await web3 . eth . estimateGas ( transactionConfig ) . catch ( ( e ) => {
54+ console . error ( "estimateGas error" , e ) ;
55+ return DEFAULT_GAS_LIMIT ;
56+ } ) ;
57+
5158 const signedTransaction = await sender . signTransaction ( {
5259 ...transactionConfig ,
60+ gasPrice : web3 . utils . toHex ( gasPrice ) ,
61+ gas : web3 . utils . toHex ( gas ) ,
5362 nonce,
5463 } ) ;
5564
@@ -62,27 +71,15 @@ export class SponsorWalletService {
6271
6372 let walletTransaction = new SponsorWalletTransactions ( {
6473 transferId : transferId ,
65- walletId : wallet . id ,
66- transactionHash : signedTransaction . transactionHash ,
74+ walletId : wallet . _id ,
75+ transaction : {
76+ hash : signedTransaction . transactionHash ,
77+ rawData : signedTransaction . rawTransaction ,
78+ nonce : nonce ,
79+ } ,
6780 status : "pending" ,
6881 } ) ;
6982
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-
8683 return walletTransaction . save ( ) ;
8784 }
8885
@@ -101,31 +98,32 @@ export class SponsorWalletService {
10198 }
10299
103100 transaction . status = receipt . status ? "success" : "failed" ;
101+ transaction . transaction . confirmedHash = receipt . transactionHash ;
104102 await transaction . save ( ) ;
105103 }
106104
107105 private async getWalletNextNonce ( walletId : string ) : Promise < number > {
108- const wallet = await SponsorWallet . findOne ( { id : walletId } ) ;
106+ const wallet = await SponsorWallet . findById ( walletId ) ;
109107 if ( ! wallet ) {
110108 throw new Error ( "Wallet not found" ) ;
111109 }
112- const lastTransaction = await SponsorWalletTransactions . findOne ( {
110+ const [ lastTransaction ] = await SponsorWalletTransactions . find ( {
113111 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- }
112+ } ) . sort ( { "transaction.nonce" : - 1 } ) . limit ( 1 ) ;
123113
124- const transaction = await web3 . eth . getTransaction (
125- lastTransaction . transactionHash
114+ const pendingNonce = await web3 . eth . getTransactionCount (
115+ wallet . address ,
116+ "pending"
126117 ) ;
127118
128- return transaction . nonce + 1 ;
119+ console . log ( "pendingNonce" , pendingNonce ) ;
120+ console . log ( "lastTransaction nonce" , lastTransaction ?. transaction . nonce ) ;
121+
122+ const internalNonce = lastTransaction
123+ ? lastTransaction . transaction . nonce
124+ : - 1 ;
125+
126+ return pendingNonce > internalNonce ? pendingNonce : internalNonce + 1 ;
129127 }
130128
131129 private async getAvailableWalletForSigning ( ) : Promise < ISponsorWallet > {
@@ -146,7 +144,7 @@ export class SponsorWalletService {
146144 return SponsorWallet . aggregate ( [
147145 {
148146 $lookup : {
149- from : "sponsorwallettransactions" ,
147+ from : SponsorWalletTransactionCollectionName ,
150148 localField : "_id" ,
151149 foreignField : "walletId" ,
152150 as : "transactions" ,
@@ -166,7 +164,7 @@ export class SponsorWalletService {
166164 return SponsorWallet . aggregate ( [
167165 {
168166 $lookup : {
169- from : "sponsorwallettransactions" ,
167+ from : SponsorWalletTransactionCollectionName ,
170168 localField : "_id" ,
171169 foreignField : "walletId" ,
172170 as : "transactions" ,
0 commit comments