-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo.ts
162 lines (142 loc) · 5.33 KB
/
demo.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
import {
createPublicClient,
createWalletClient,
http,
parseEther,
parseGwei,
} from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { celoAlfajores } from "viem/chains";
import "dotenv/config"; // use to read private key from environment variable
const PRIVATE_KEY = process.env.PRIVATE_KEY
/**
* Boilerplate to create a viem client
*/
const account = privateKeyToAccount(`0x${PRIVATE_KEY}`);
const publicClient = createPublicClient({
chain: celoAlfajores,
transport: http(),
});
const walletClient = createWalletClient({
chain: celoAlfajores, // Celo testnet
transport: http(),
});
/**
* Transation type: 0 (0x00)
* Name: "Legacy"
* Description: Ethereum legacy transaction
*/
async function demoLegacyTransactionType() {
console.log(`Initiating legacy transaction...`);
const transactionHash = await walletClient.sendTransaction({
account, // Sender
to: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", // Recipient (illustrative address)
value: parseEther("0.01"), // 0.01 CELO
gasPrice: parseGwei("20"), // Special field for legacy transaction type
});
const transactionReceipt = await publicClient.waitForTransactionReceipt({
hash: transactionHash,
});
printFormattedTransactionReceipt(transactionReceipt);
}
/**
* Transaction type: 2 (0x02)
* Name: "Dynamic fee"
* Description: Ethereum EIP-1559 transaction
*/
async function demoDynamicFeeTransactionType() {
console.log(`Initiating dynamic fee (EIP-1559) transaction...`);
const transactionHash = await walletClient.sendTransaction({
account, // Sender
to: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", // Recipient (illustrative address)
value: parseEther("0.01"), // 0.01 CELO
maxFeePerGas: parseGwei("10"), // Special field for dynamic fee transaction type (EIP-1559)
maxPriorityFeePerGas: parseGwei("10"), // Special field for dynamic fee transaction type (EIP-1559)
});
const transactionReceipt = await publicClient.waitForTransactionReceipt({
hash: transactionHash,
});
printFormattedTransactionReceipt(transactionReceipt);
}
/**
* Transaction type: 123 (0x7b)
* Name: "Dynamic fee"
* Description: Celo dynamic fee transaction (with custom fee currency)
*/
async function demoFeeCurrencyTransactionType() {
console.log(`Initiating custom fee currency transaction...`);
const transactionHash = await walletClient.sendTransaction({
account, // Sender
to: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", // Recipient (illustrative address)
value: parseEther("0.01"), // 0.01 CELO
feeCurrency: "0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1", // cUSD fee currency
maxFeePerGas: parseGwei("10"), // Denominated in the token the fee is paid in
maxPriorityFeePerGas: parseGwei("10"), // Denominated in the token the fee is paid in
});
const transactionReceipt = await publicClient.waitForTransactionReceipt({
hash: transactionHash,
});
printFormattedTransactionReceipt(transactionReceipt);
}
// /**
// * Transaction type: 122 (0x7a)
// * Name: "Celo Denominated Easy Fee Transaction"
// * Description: Celo dynamic fee transaction (with custom fee currency)
// */
// async function demoCeloDenominatedFeeCurrencyTransactionType() {
// console.log(`Initiating Celo denominated fee currency transaction...`);
// const transactionHash = await walletClient.sendTransaction({
// account, // Sender
// to: "0x70997970c51812dc3a010c7d01b50e0d17dc79c8", // Recipient (illustrative address)
// value: parseEther("0.01"), // 0.01 CELO
// feeCurrency: "0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1", // cUSD fee currency
// maxFeePerGas: parseGwei("10"), // Denominated in CELO but paid in the fee currency
// maxPriorityFeePerGas: parseGwei("10"), // Denominated in CELO but paid in the fee currency
// maxFeeInFeeCurrency: parseGwei("10") // Calculated as maxFeePerGas * gasLimit * conversionRateFromCELOtoToken
// });
// const transactionReceipt = await publicClient.waitForTransactionReceipt({
// hash: transactionHash,
// });
// printFormattedTransactionReceipt(transactionReceipt);
// }
function printFormattedTransactionReceipt(transactionReceipt: any) {
const {
blockHash,
blockNumber,
contractAddress,
cumulativeGasUsed,
effectiveGasPrice,
from,
gasUsed,
logs,
logsBloom,
status,
to,
transactionHash,
transactionIndex,
type,
feeCurrency,
gatewayFee,
gatewayFeeRecipient
} = transactionReceipt;
const filteredTransactionReceipt = {
type,
status,
transactionHash,
from,
to
};
console.log(`Transaction details:`, filteredTransactionReceipt, `\n`);
}
// Wrap all demos in an async function to await their completion
async function runDemosSequentially() {
// Run each demo and await its completion
await demoLegacyTransactionType();
await demoDynamicFeeTransactionType();
await demoFeeCurrencyTransactionType();
}
// Run the demos sequentially
runDemosSequentially().catch((err) => {
// Handle any errors that might occur in the demos
console.error("An error occurred:", err);
});