-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (117 loc) · 3.22 KB
/
index.js
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
import { SecretNetworkClient, Wallet } from "secretjs";
import * as fs from "fs";
import dotenv from "dotenv";
dotenv.config();
const wallet = new Wallet(process.env.MNEMONIC);
const contract_wasm = fs.readFileSync(
"../contract/target/wasm32-unknown-unknown/release/secret_business_card_demo.wasm"
);
let codeId = 19826;
let contractCodeHash =
"a9664743fb01a8ee2df29f39e382a2e70e87f40dfe33cd40edbeea34deb97835";
let contractAddress = "secret1auclmxkpw28g8qpxe57gqt4t2hgmuuuapa6sta";
const secretjs = new SecretNetworkClient({
chainId: "pulsar-2",
url: "https://api.pulsar.scrttestnet.com",
wallet: wallet,
walletAddress: wallet.address,
});
let upload_contract = async () => {
let tx = await secretjs.tx.compute.storeCode(
{
sender: wallet.address,
wasm_byte_code: contract_wasm,
source: "",
builder: "",
},
{
gasLimit: 4_000_000,
}
);
const codeId = Number(
tx.arrayLog.find((log) => log.type === "message" && log.key === "code_id")
.value
);
console.log("codeId: ", codeId);
const contractCodeHash = (
await secretjs.query.compute.codeHashByCodeId({ code_id: codeId })
).code_hash;
console.log(`Contract hash: ${contractCodeHash}`);
};
// upload_contract();
let instantiate_contract = async () => {
// Create an instance of the Counter contract, providing a starting count
const initMsg = { entropy: "this is my entropy, dude!" };
let tx = await secretjs.tx.compute.instantiateContract(
{
code_id: codeId,
sender: wallet.address,
code_hash: contractCodeHash,
init_msg: initMsg,
label: "Secret Business Card Demo" + Math.ceil(Math.random() * 10000),
},
{
gasLimit: 400_000,
}
);
//Find the contract_address in the logs
const contractAddress = tx.arrayLog.find(
(log) => log.type === "message" && log.key === "contract_address"
).value;
console.log(contractAddress);
};
// instantiate_contract();
let createCard = async () => {
const card_creation_tx = await secretjs.tx.compute.executeContract(
{
sender: wallet.address,
contract_address: contractAddress,
msg: {
create: {
card: { name: "card 0", address: "0", phone: "123456789" },
index: 0,
},
},
code_hash: contractCodeHash,
},
{ gasLimit: 100_000 }
);
console.log(card_creation_tx);
};
// createCard();
let createViewingKey = async () => {
let viewing_key_creation = await secretjs.tx.compute.executeContract(
{
sender: wallet.address,
contract_address: contractAddress,
msg: {
generate_viewing_key: {
index: 0,
},
},
code_hash: contractCodeHash,
},
{ gasLimit: 100_000 }
);
console.log(
viewing_key_creation.arrayLog.find(
(log) => log.type === "wasm" && log.key === "viewing_key"
).value
);
};
// createViewingKey();
let queryCard = async () => {
let business_card_query_tx = await secretjs.query.compute.queryContract({
contract_address: contractAddress,
query: {
get_card: {
wallet: wallet.address,
viewing_key: viewing_key,
index: 0,
},
},
code_hash: contractCodeHash,
});
console.log(business_card_query_tx);
};
// queryCard();