-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGasModelModified.ts
67 lines (62 loc) · 2.04 KB
/
GasModelModified.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
import { ethers, Wallet, Transaction } from "ethers";
import { serialize } from "@ethersproject/transactions";
import { BytesLike } from "@ethersproject/bytes";
import { ExternallyOwnedAccount } from "@ethersproject/abstract-signer";
import { SigningKey } from "@ethersproject/signing-key";
import {
Provider,
TransactionRequest,
TransactionResponse,
} from "@ethersproject/abstract-provider";
import { Deferrable } from "@ethersproject/properties";
export class GasModelSignerModified extends Wallet {
private readonly nodeAddress: string;
private readonly expiration: number;
constructor(
privateKey: BytesLike | ExternallyOwnedAccount | SigningKey,
provider: Provider,
nodeAddress: string,
expiration: number
) {
super(privateKey, provider);
this.nodeAddress = nodeAddress;
this.expiration = expiration;
}
// Populates all fields in a transaction, signs it and sends it to the network
async sendTransaction(
transaction: Deferrable<TransactionRequest>
): Promise<TransactionResponse> {
this._checkProvider("sendTransaction");
const tx = await this.populateTransaction({
...transaction,
});
const signedTx = await this.signTransaction(tx);
const tr = await this.provider.sendTransaction(signedTx);
const receipt = await tr.wait();
console.log(
"address: " + receipt.contractAddress,
"txHash:",
receipt.transactionHash
);
return tr;
}
signTransaction(transaction: TransactionRequest) {
return ethers.utils
.resolveProperties(transaction as Transaction)
.then((tx: Transaction) => {
if (tx.from !== null) {
delete tx.from;
}
const value = ethers.utils.defaultAbiCoder.encode(
["address", "uint256"],
[this.nodeAddress, this.expiration]
);
tx.data = tx.data + value.substring(2);
tx.chainId = 0;
const signature = this._signingKey().signDigest(
ethers.utils.keccak256(serialize(tx))
);
return serialize(tx, signature);
});
}
}