-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.CallInvokeContract.ts
96 lines (85 loc) · 3.94 KB
/
2.CallInvokeContract.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
// interact with a contract that is already deployed on devnet.
// launch with npx ts-node src/2.CallInvokeContract.ts
// Coded with Starknet.js v5.21.0
import { Provider, RpcProvider, SequencerProvider, Contract, Account, json, BigNumberish, num, encode, constants } from "starknet";
import { account2TestnetAddress, account2TestnetPrivateKey, junoNMtestnet } from "./private/A1priv";
import { account4MainnetAddress, account4MainnetPrivateKey } from "./private/mainPriv";
import fs from "fs";
// 👇👇👇
// 🚨🚨🚨 Launch starknet-devnet-rs with '--seed 0' before using this script.
// 👆👆👆
interface BinaryFile {
size_bytes: BigNumberish,
bits_len: BigNumberish,
len: BigNumberish,
numbers: BigNumberish[],
}
interface BinaryJson {
size_bytes: BigNumberish,
bits_len: BigNumberish,
numbers: string[],
}
async function main() {
const network: string = "testnet" // 🚨 "devnet" or "testnet" or "mainnet".
let provider: Provider | SequencerProvider | RpcProvider;
let privateKey0: BigNumberish;
let account0Address: BigNumberish;
let account0: Account;
switch (network) {
case "devnet":
provider = new SequencerProvider({ baseUrl: "http://127.0.0.1:5050" });
// for Starknet-devnet
// privateKey0 = "0xe3e70682c2094cac629f6fbed82c07cd";
// account0Address = "0x7e00d496e324876bbc8531f2d9a82bf154d1a04a50218ee74cdd372f75a551a";
// for starknet-devnet-rs
privateKey0 = "0x71d7bb07b9a64f6f78ac4c816aff4da9";
account0Address = "0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691";
account0 = new Account(provider, account0Address, privateKey0);
break;
case "testnet":
provider = new RpcProvider({ nodeUrl: junoNMtestnet });
privateKey0 = account2TestnetPrivateKey;
account0Address = account2TestnetAddress;
account0 = new Account(provider, account0Address, privateKey0, "1");
break;
case "mainnet":
provider = new RpcProvider({ nodeUrl: "https://json-rpc.starknet-mainnet.public.lavanet.xyz" });
privateKey0 = account4MainnetPrivateKey;
account0Address = account4MainnetAddress;
account0 = new Account(provider, account0Address, privateKey0, "1");
break;
default:
throw new Error("wrong network name.")
break;
}
console.log('existing account connected.\n');
// Connect the deployed Test instance in devnet
const testAddress = "0x154a66175310f89c9908835fb85d012b5c42a74c9404d29b4152eec552ca8c"; // modify in accordance with result of script 1
// was 0x7dac368af2e1f96f0d72241ded49b5b433103bfdd65fc3663f01376f4ee2615
const compiledTest = json.parse(fs.readFileSync("./cairo/storage_felts.sierra.json").toString("ascii"));
const myTestContract = new Contract(compiledTest.abi, testAddress, provider);
console.log('Test Contract connected at =', myTestContract.address, "\n", myTestContract.functions);
// Interactions with the contract with call & invoke
myTestContract.connect(account0);
const sizeBytes = await myTestContract.get_size_bytes();
console.log("sizeBytes=", sizeBytes);
const bits_len = await myTestContract.get_bits_len_bytes();
console.log("bits_len=", bits_len);
const fil = await myTestContract.get_file();
console.log("file=", fil);
let hexArray = fil.numbers.map((numb: bigint) => encode.addHexPrefix(numb.toString(16).padStart(64, '0')));
let jsonF: BinaryJson = {
size_bytes: fil.size_bytes,
bits_len: fil.bits_len,
numbers: hexArray
}
console.log("json=", jsonF);
fs.writeFileSync('./kingRead.json', json.stringify(jsonF, undefined, 2));
console.log('✅ Test completed.');
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});