-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
99 lines (86 loc) · 2.65 KB
/
main.js
File metadata and controls
99 lines (86 loc) · 2.65 KB
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
import {
LCDClient,
MnemonicKey,
MsgStoreCode,
MsgInstantiateContract,
MsgExecuteContract,
MsgMigrateContract
} from '@terra-money/terra.js'
import * as fs from 'fs'
async function init() {
const mk = new MnemonicKey({
mnemonic: process.env.MNEMONIC
})
const terra = new LCDClient({
URL: process.env.LCD_URL,
chainID: process.env.CHAIN_ID,
gasPrices: {
[process.env.GAS_DENOM]: process.env.GAS_PRICE
},
isClassic: true
})
const wallet = terra.wallet(mk)
let msg
switch (process.argv[2]) {
case 'store':
msg = new MsgStoreCode(
wallet.key.accAddress,
fs.readFileSync(process.argv[3] || process.env.WASM_FILE).toString('base64')
)
break
case 'instantiate':
msg = new MsgInstantiateContract(
wallet.key.accAddress,
wallet.key.accAddress,
process.argv[3] || process.env.CODE_ID,
{count: 0},
{uluna: process.argv[4] || 1 },
process.argv[5] || process.env.CONTRACT_LABEL
)
break
case 'increment':
msg = new MsgExecuteContract(
wallet.key.accAddress,
process.argv[3] || process.env.CONTRACT_ADDRESS,
{increment: {}}
)
break
case 'decrement':
msg = new MsgExecuteContract(
wallet.key.accAddress,
process.argv[3] || process.env.CONTRACT_ADDRESS,
{decrement: {}}
)
break
case 'migrate':
msg = new MsgMigrateContract(
wallet.key.accAddress,
process.argv[3] || process.env.CONTRACT_ADDRESS,
process.argv[4] || process.env.MIGRATE_ID,
{}
)
break
}
if (msg) {
await broadcastAndLog(terra, wallet, msg)
} else if (process.argv[2] === 'query') {
const result = await terra.wasm.contractQuery(
process.argv[3] || process.env.CONTRACT_ADDRESS,
{get_count: {}}
)
console.log(result)
}
}
async function broadcastAndLog(terra, wallet, msg) {
try {
const tx = await wallet.createAndSignTx({
msgs: [msg],
chainID: process.env.CHAIN_ID
})
const txResult = await terra.tx.broadcastSync(tx, process.env.CHAIN_ID)
console.log(`https://finder.terraclassic.community/mainnet/tx/${txResult.txhash}`)
} catch (e) {
console.error(e)
}
}
init().catch(console.error)