Skip to content

Commit

Permalink
fix to instantiate contract address and include output log
Browse files Browse the repository at this point in the history
  • Loading branch information
ltfschoen committed Oct 6, 2024
1 parent 587a3f1 commit 06c0367
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ make build

* OPTIONAL - optimize contract code

* Upload and Instantiate
```
yarn run secret:clean:uploadContract
yarn run secret:start:uploadContract
yarn run secret:clean:instantiateContract
yarn run secret:start:instantiateContract
```
* View logs at ./logs/instantiateOutput.log
* View on Secret Testnet block explorer at https://testnet.ping.pub/secret/

## Documentation

Expand Down
86 changes: 86 additions & 0 deletions logs/instantiateOutput.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
Starting deployment...
codeId: 10593
CODE_HASH: 5ea3b37c0f3b06a621e58327bd7235fd8c323a4a595b095914c5fd2a5511dc93
params: {
codeId: '10593',
contractCodeHash: '5ea3b37c0f3b06a621e58327bd7235fd8c323a4a595b095914c5fd2a5511dc93'
}
Instantiating contract...
tx: {
height: 7098192,
timestamp: '',
transactionHash: 'C5419257F2ED564093D27F1B73F4EAC4EAABC950EBE590C6B7C9C7B9CC434DEC',
code: 0,
codespace: '',
info: '',
tx: {
'@type': '/cosmos.tx.v1beta1.Tx',
body: {
messages: [Array],
memo: '',
timeout_height: '0',
extension_options: [],
non_critical_extension_options: []
},
auth_info: { signer_infos: [Array], fee: [Object] },
signatures: [
'J1Ei+KHikPOXjRfuP1b22UYlxdHJM6TpntR8KFmAEmBPRW2CFou8WFO9Nubg/vAR/bC0cKybR7d9+Rkb77rjNQ=='
]
},
rawLog: '[{"events":[{"type":"instantiate","attributes":[{"key":"contract_address","value":"secret19kk5wcmxp2alr356emljyr8xp90atndxy3r2c9"},{"key":"code_id","value":"10593"}]},{"type":"message","attributes":[{"key":"action","value":"/secret.compute.v1beta1.MsgInstantiateContract"},{"key":"module","value":"compute"},{"key":"sender","value":"secret1am2mk908456twwgehmaua85s6laar4yr3awzzj"},{"key":"contract_address","value":"secret19kk5wcmxp2alr356emljyr8xp90atndxy3r2c9"}]},{"type":"wasm","attributes":[{"key":"contract_address","value":"secret19kk5wcmxp2alr356emljyr8xp90atndxy3r2c9"}]}]}]',
jsonLog: [ { events: [Array], msg_index: 0 } ],
arrayLog: [
{
msg: 0,
type: 'instantiate',
key: 'contract_address',
value: 'secret19kk5wcmxp2alr356emljyr8xp90atndxy3r2c9'
},
{ msg: 0, type: 'instantiate', key: 'code_id', value: '10593' },
{
msg: 0,
type: 'message',
key: 'action',
value: '/secret.compute.v1beta1.MsgInstantiateContract'
},
{ msg: 0, type: 'message', key: 'module', value: 'compute' },
{
msg: 0,
type: 'message',
key: 'sender',
value: 'secret1am2mk908456twwgehmaua85s6laar4yr3awzzj'
},
{
msg: 0,
type: 'message',
key: 'contract_address',
value: 'secret19kk5wcmxp2alr356emljyr8xp90atndxy3r2c9'
},
{
msg: 0,
type: 'wasm',
key: 'contract_address',
value: 'secret19kk5wcmxp2alr356emljyr8xp90atndxy3r2c9'
}
],
events: [
{ type: 'coin_spent', attributes: [Array] },
{ type: 'coin_received', attributes: [Array] },
{ type: 'transfer', attributes: [Array] },
{ type: 'message', attributes: [Array] },
{ type: 'tx', attributes: [Array] },
{ type: 'tx', attributes: [Array] },
{ type: 'tx', attributes: [Array] },
{ type: 'message', attributes: [Array] },
{ type: 'instantiate', attributes: [Array] },
{ type: 'wasm', attributes: [Array] },
{ type: 'message', attributes: [Array] }
],
data: [
<Buffer 0a 2d 73 65 63 72 65 74 31 39 6b 6b 35 77 63 6d 78 70 32 61 6c 72 33 35 36 65 6d 6c 6a 79 72 38 78 70 39 30 61 74 6e 64 78 79 33 72 32 63 39>
],
gasUsed: 43476,
gasWanted: 400000,
ibcResponses: []
}
SECRET_ADDRESS: secret19kk5wcmxp2alr356emljyr8xp90atndxy3r2c9
49 changes: 32 additions & 17 deletions packages/secret-upload-contract/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ async function main () {

console.log('balance: ', balance);

type INIT_MSG = {
gateway_address: String,
gateway_hash: String,
gateway_key: String,
count: Number,
};

type CODE_PARAMS = {
codeId: String | undefined,
contractCodeHash: String | undefined,
Expand All @@ -60,21 +67,25 @@ async function main () {

let codeId: String | undefined;
let contractCodeHash: String | undefined;

let tx = await secretjs.tx.compute.storeCode(
{
sender: wallet.address,
wasm_byte_code: contract_wasm,
source: "",
builder: "",
},
{
gasLimit: 5_000_000,
}
);
let tx: any;
try {
tx = await secretjs.tx.compute.storeCode(
{
sender: wallet.address,
wasm_byte_code: contract_wasm,
source: "",
builder: "",
},
{
gasLimit: 5_000_000,
}
);
} catch (e) {
console.log('error: ', e);
}

codeId = String(
tx?.arrayLog?.find((log) => log?.type === "message" && log?.key === "code_id")?.value
tx?.arrayLog?.find((log: any) => log?.type === "message" && log?.key === "code_id")?.value
);

console.log("codeId: ", codeId);
Expand All @@ -91,18 +102,21 @@ async function main () {
};

let instantiate_contract = async (params: CODE_PARAMS) => {
console.log('params: ', params)
let contractAddress;

if (!params.codeId || !params.contractCodeHash) {
throw new Error("codeId or contractCodeHash is not set.");
}
console.log("Instantiating contract...");

let initMsg = {
let initMsg: INIT_MSG = {
gateway_address: gatewayAddress,
gateway_hash: gatewayHash,
gateway_key: gatewayPublicKeyBytes,
count: 1,
};

let tx = await secretjs.tx.compute.instantiateContract(
{
code_id: params.codeId.toString(),
Expand All @@ -115,6 +129,7 @@ async function main () {
gasLimit: 400_000,
}
);
console.log('tx: ', tx)

//Find the contract_address in the logs
contractAddress = tx?.arrayLog?.find(
Expand All @@ -127,9 +142,9 @@ async function main () {
};

// Chain the execution using promises
upload_contract()
.then((res) => {
instantiate_contract(res);
await upload_contract()
.then(async (res) => {
await instantiate_contract(res);
})
.catch((error) => {
console.error("Error:", error);
Expand Down

0 comments on commit 06c0367

Please sign in to comment.