-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e76e74c
commit 9681289
Showing
5 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import AsyncLock from 'async-lock'; | ||
import { | ||
AbstractSigner, | ||
Provider, | ||
Signer, | ||
TransactionRequest, | ||
TypedDataDomain, | ||
TypedDataField, | ||
} from 'ethers'; | ||
import PendingEthereumTransactionRepository from '../../db/repositories/PendingEthereumTransactionRepository'; | ||
|
||
class SequentialSigner extends AbstractSigner { | ||
private static readonly txLock = 'txLock'; | ||
|
||
private readonly lock = new AsyncLock(); | ||
|
||
constructor(private signer: AbstractSigner) { | ||
super(); | ||
} | ||
|
||
public getAddress = (): Promise<string> => this.signer.getAddress(); | ||
|
||
public connect = (provider: null | Provider): Signer => { | ||
this.signer = this.signer.connect(provider); | ||
return this; | ||
}; | ||
|
||
public signTransaction = async (tx: TransactionRequest): Promise<string> => { | ||
return await this.lock.acquire(SequentialSigner.txLock, async () => { | ||
if (tx.value !== undefined && tx.value !== null) { | ||
const [ourBalance, pendingTxsValue] = await Promise.all([ | ||
this.signer.provider!.getBalance(await this.getAddress()), | ||
PendingEthereumTransactionRepository.getTotalSent(), | ||
]); | ||
|
||
if (ourBalance - pendingTxsValue < BigInt(tx.value)) { | ||
throw new Error('insufficient balance'); | ||
} | ||
} | ||
|
||
if (tx.nonce === undefined || tx.nonce === null) { | ||
tx.nonce = await this.signer.provider!.getTransactionCount( | ||
await this.getAddress(), | ||
); | ||
} | ||
|
||
return await this.signer.signTransaction(tx); | ||
}); | ||
}; | ||
|
||
public signMessage = (message: string | Uint8Array): Promise<string> => | ||
this.signer.signMessage(message); | ||
|
||
public signTypedData = ( | ||
domain: TypedDataDomain, | ||
types: Record<string, Array<TypedDataField>>, | ||
value: Record<string, any>, | ||
): Promise<string> => this.signer.signTypedData(domain, types, value); | ||
} | ||
|
||
export default SequentialSigner; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters