-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
61 lines (53 loc) · 1.99 KB
/
index.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
import { ethers } from "@swisstronik/ethers";
import { abi } from "./TestContract";
const MUMBAI_CONTRACT = "0x2CEc6767FEc921587Ac7A2a35f07b5EFb5088DA0";
const SWTR_CONTRACT = "0x9e7AEfB3eA21DEe24366592B4699e1C7aBD77b80";
const PK = "0x..." // Private key;
function getSigner(isSwisstronik: boolean): ethers.Signer {
const wallet = new ethers.Wallet(PK);
if (isSwisstronik) {
// Construct swisstronik signer
const provider = new ethers.providers.JsonRpcProvider("https://json-rpc.testnet.swisstronik.com")
return wallet.connect(provider)
} else {
// Construct mumbai signer
const provider = new ethers.providers.JsonRpcProvider("https://rpc.ankr.com/polygon_mumbai")
return wallet.connect(provider)
}
}
function getContract(isSwisstronik: boolean): ethers.Contract {
const signer = getSigner(isSwisstronik);
return new ethers.Contract(
isSwisstronik ? SWTR_CONTRACT : MUMBAI_CONTRACT,
abi,
signer
);
}
async function performCall(isSwisstronik: boolean) {
const contract = getContract(isSwisstronik)
const res = await contract.counter();
console.log('call result: ', res)
}
async function performEstimateGas(isSwisstronik: boolean) {
const contract = getContract(isSwisstronik)
const res = await contract.estimateGas.counter();
console.log('estimateGas result: ', res)
}
async function performSendTransaction(isSwisstronik: boolean) {
const signer = getSigner(isSwisstronik)
const contract = getContract(isSwisstronik)
const tx = await contract.populateTransaction.counter();
const res = await signer.sendTransaction(tx);
console.log('sendTransaction result: ', res)
}
async function main() {
console.log("Doing mumbai call...")
await performCall(false)
await performEstimateGas(false)
await performSendTransaction(false)
console.log("Doing swisstronik call...")
await performCall(true)
await performEstimateGas(true)
await performSendTransaction(true)
}
main()