|
1 |
| -import type {JsonObject} from '@blake.regalia/belt'; |
2 | 1 | import type {EncodedGoogleProtobufAny} from '@solar-republic/cosmos-grpc/google/protobuf/any';
|
3 |
| -import type {TxResultTuple, Wallet, WeakSecretAccAddr} from '@solar-republic/neutrino'; |
4 |
| -import type {CwHexLower, WeakUintStr} from '@solar-republic/types'; |
| 2 | +import type {TxResponseTuple, Wallet} from '@solar-republic/neutrino'; |
5 | 3 |
|
6 |
| -import {promisify} from 'node:util'; |
7 |
| -import {gunzip} from 'node:zlib'; |
| 4 | +import {TendermintEventFilter, broadcast_result, create_and_sign_tx_direct} from '@solar-republic/neutrino'; |
8 | 5 |
|
9 |
| -import {base64_to_bytes, bytes_to_hex, bytes_to_text, cast, sha256} from '@blake.regalia/belt'; |
10 |
| -import {queryCosmosBankBalance} from '@solar-republic/cosmos-grpc/cosmos/bank/v1beta1/query'; |
11 |
| -import {encodeGoogleProtobufAny} from '@solar-republic/cosmos-grpc/google/protobuf/any'; |
12 |
| -import {SI_MESSAGE_TYPE_SECRET_COMPUTE_MSG_STORE_CODE, SI_MESSAGE_TYPE_SECRET_COMPUTE_MSG_INSTANTIATE_CONTRACT, encodeSecretComputeMsgStoreCode, encodeSecretComputeMsgInstantiateContract} from '@solar-republic/cosmos-grpc/secret/compute/v1beta1/msg'; |
13 |
| -import {querySecretComputeCodeHashByCodeId, querySecretComputeCodes} from '@solar-republic/cosmos-grpc/secret/compute/v1beta1/query'; |
14 |
| -import {destructSecretRegistrationKey} from '@solar-republic/cosmos-grpc/secret/registration/v1beta1/msg'; |
15 |
| -import {querySecretRegistrationTxKey} from '@solar-republic/cosmos-grpc/secret/registration/v1beta1/query'; |
16 |
| -import {SecretWasm, TendermintEventFilter, TendermintWs, broadcast_result, create_and_sign_tx_direct, exec_fees} from '@solar-republic/neutrino'; |
17 |
| - |
18 |
| -import {X_GAS_PRICE, P_SECRET_LCD, P_SECRET_RPC} from './constants'; |
| 6 | +import {P_SECRET_RPC} from './constants'; |
19 | 7 |
|
20 | 8 | const k_tef = await TendermintEventFilter(P_SECRET_RPC);
|
21 | 9 |
|
22 |
| -export async function exec(k_wallet: Wallet, atu8_msg: EncodedGoogleProtobufAny, xg_gas_limit: bigint): Promise<TxResultTuple> { |
23 |
| - const [atu8_raw, atu8_signdoc, si_txn] = await create_and_sign_tx_direct( |
| 10 | +export async function exec(k_wallet: Wallet, atu8_msg: EncodedGoogleProtobufAny, xg_gas_limit: bigint): Promise<TxResponseTuple> { |
| 11 | + const [atu8_raw, sb16_txn, atu8_signdoc] = await create_and_sign_tx_direct( |
24 | 12 | k_wallet,
|
25 | 13 | [atu8_msg],
|
26 |
| - exec_fees(xg_gas_limit, X_GAS_PRICE, 'uscrt'), |
27 | 14 | xg_gas_limit
|
28 | 15 | );
|
29 | 16 |
|
30 |
| - return await broadcast_result(k_wallet, atu8_raw, si_txn, k_tef); |
31 |
| -} |
32 |
| - |
33 |
| -export async function upload_code(k_wallet: Wallet, atu8_wasm: Uint8Array): Promise<WeakUintStr> { |
34 |
| - let atu8_bytecode = atu8_wasm; |
35 |
| - |
36 |
| - // gzip-encoded; decompress |
37 |
| - if(0x1f === atu8_wasm[0] && 0x8b === atu8_wasm[1]) { |
38 |
| - atu8_bytecode = await promisify(gunzip)(atu8_wasm); |
39 |
| - } |
40 |
| - |
41 |
| - // hash |
42 |
| - const atu8_hash = await sha256(atu8_bytecode); |
43 |
| - const sb16_hash = cast<CwHexLower>(bytes_to_hex(atu8_hash)); |
44 |
| - |
45 |
| - // fetch all uploaded codes |
46 |
| - const [,, g_codes] = await querySecretComputeCodes(P_SECRET_LCD); |
47 |
| - |
48 |
| - // already uploaded |
49 |
| - const g_existing = g_codes?.code_infos?.find(g => g.code_hash! === sb16_hash); |
50 |
| - if(g_existing) { |
51 |
| - console.info(`Found code ID ${g_existing.code_id} already uploaded to network`); |
52 |
| - |
53 |
| - return g_existing.code_id as WeakUintStr; |
54 |
| - } |
55 |
| - |
56 |
| - // upload |
57 |
| - const [xc_code, sx_res, g_meta, atu8_data, h_events] = await exec(k_wallet, encodeGoogleProtobufAny( |
58 |
| - SI_MESSAGE_TYPE_SECRET_COMPUTE_MSG_STORE_CODE, |
59 |
| - encodeSecretComputeMsgStoreCode( |
60 |
| - k_wallet.addr, |
61 |
| - atu8_bytecode |
62 |
| - ) |
63 |
| - ), 30_000000n); |
64 |
| - |
65 |
| - if(xc_code) throw Error(sx_res); |
66 |
| - |
67 |
| - return h_events!['message.code_id'][0] as WeakUintStr; |
68 |
| -} |
69 |
| - |
70 |
| -export async function instantiate_contract(k_wallet: Wallet, sg_code_id: WeakUintStr, h_init_msg: JsonObject): Promise<WeakSecretAccAddr> { |
71 |
| - const [,, g_reg] = await querySecretRegistrationTxKey(P_SECRET_LCD); |
72 |
| - const [atu8_cons_pk] = destructSecretRegistrationKey(g_reg!); |
73 |
| - const k_wasm = SecretWasm(atu8_cons_pk!); |
74 |
| - const [,, g_hash] = await querySecretComputeCodeHashByCodeId(P_SECRET_LCD, sg_code_id); |
75 |
| - |
76 |
| - // @ts-expect-error imported types versioning |
77 |
| - const atu8_body = await k_wasm.encodeMsg(g_hash!.code_hash, h_init_msg); |
78 |
| - |
79 |
| - const [xc_code, sx_res, g_meta, atu8_data, h_events] = await exec(k_wallet, encodeGoogleProtobufAny( |
80 |
| - SI_MESSAGE_TYPE_SECRET_COMPUTE_MSG_INSTANTIATE_CONTRACT, |
81 |
| - encodeSecretComputeMsgInstantiateContract( |
82 |
| - k_wallet.addr, |
83 |
| - null, |
84 |
| - sg_code_id, |
85 |
| - h_init_msg['name'] as string, |
86 |
| - atu8_body |
87 |
| - ) |
88 |
| - ), 10_000_000n); |
89 |
| - |
90 |
| - if(xc_code) { |
91 |
| - const s_error = g_meta?.log ?? sx_res; |
92 |
| - |
93 |
| - // encrypted error message |
94 |
| - const m_response = /(\d+):(?: \w+:)*? encrypted: (.+?): (.+?) contract/.exec(s_error); |
95 |
| - if(m_response) { |
96 |
| - // destructure match |
97 |
| - const [, s_index, sb64_encrypted, si_action] = m_response; |
98 |
| - |
99 |
| - // decrypt ciphertext |
100 |
| - const atu8_plaintext = await k_wasm.decrypt(base64_to_bytes(sb64_encrypted), atu8_body.slice(0, 32)); |
101 |
| - |
102 |
| - throw Error(bytes_to_text(atu8_plaintext)); |
103 |
| - } |
104 |
| - |
105 |
| - throw Error(sx_res); |
106 |
| - } |
107 |
| - |
108 |
| - return h_events!['message.contract_address'][0] as WeakSecretAccAddr; |
| 17 | + return await broadcast_result(k_wallet, atu8_raw, sb16_txn, k_tef); |
109 | 18 | }
|
0 commit comments