|
| 1 | +import { getSigningOsmosisClient } from "osmojs"; |
| 2 | +import { getOfflineSignerProto as getOfflineSigner } from "cosmjs-utils"; |
| 3 | +import { chains } from "chain-registry"; |
| 4 | +import { Chain } from "@chain-registry/types"; |
| 5 | +import { readFileSync } from "fs"; |
| 6 | +import { cosmwasm, estimateOsmoFee } from "osmojs"; |
| 7 | +import { DeliverTxResponse } from "@cosmjs/stargate"; |
| 8 | +import { wasmTypes } from "@cosmjs/cosmwasm-stargate/build/modules/wasm/messages"; |
| 9 | +import assert from "assert"; |
| 10 | + |
| 11 | +import { Deployer } from "."; |
| 12 | +import { convert_terra_address_to_hex, extractFromRawLog } from "./terra"; |
| 13 | +import { EncodeObject } from "@cosmjs/proto-signing"; |
| 14 | +import Long from "long"; |
| 15 | + |
| 16 | +export type OsmosisHost = { |
| 17 | + endpoint: string; |
| 18 | +}; |
| 19 | + |
| 20 | +export class OsmosisDeployer implements Deployer { |
| 21 | + private chain: Chain; |
| 22 | + constructor(private endpoint: string, private mnemonic: string) { |
| 23 | + const c = chains.find(({ chain_name }) => chain_name === "osmosis"); |
| 24 | + if (c === undefined) |
| 25 | + throw new Error("Could not find Osmosis in chain registry"); |
| 26 | + this.chain = c; |
| 27 | + } |
| 28 | + |
| 29 | + private async getAccountAddress(): Promise<string> { |
| 30 | + const signer = await getOfflineSigner({ |
| 31 | + mnemonic: this.mnemonic, |
| 32 | + chain: this.chain, |
| 33 | + }); |
| 34 | + |
| 35 | + const accountData = await signer.getAccounts(); |
| 36 | + return accountData[0].address; |
| 37 | + } |
| 38 | + |
| 39 | + private async signAndBroadcast( |
| 40 | + msg: EncodeObject |
| 41 | + ): Promise<DeliverTxResponse> { |
| 42 | + const signer = await getOfflineSigner({ |
| 43 | + mnemonic: this.mnemonic, |
| 44 | + chain: this.chain, |
| 45 | + }); |
| 46 | + |
| 47 | + const client = await getSigningOsmosisClient({ |
| 48 | + rpcEndpoint: this.endpoint, |
| 49 | + signer, |
| 50 | + defaultTypes: wasmTypes, |
| 51 | + }); |
| 52 | + |
| 53 | + const address = await this.getAccountAddress(); |
| 54 | + const fee = await estimateOsmoFee(client, address, [msg], "estimate fee"); |
| 55 | + const rs = await client.signAndBroadcast(address, [msg], fee); |
| 56 | + |
| 57 | + if (rs.code !== 0) { |
| 58 | + console.error(`Transaction failed: ${rs.rawLog}`); |
| 59 | + } else { |
| 60 | + console.log( |
| 61 | + `Broadcasted transaction hash: ${JSON.stringify(rs.transactionHash)}` |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + client.disconnect(); |
| 66 | + |
| 67 | + return rs; |
| 68 | + } |
| 69 | + |
| 70 | + async deployArtifact(artifact: string): Promise<number> { |
| 71 | + const contract_bytes = readFileSync(artifact); |
| 72 | + console.log(`Storing WASM: ${artifact} (${contract_bytes.length} bytes)`); |
| 73 | + |
| 74 | + const storeCode = cosmwasm.wasm.v1.MessageComposer.withTypeUrl.storeCode({ |
| 75 | + sender: await this.getAccountAddress(), |
| 76 | + wasmByteCode: contract_bytes, |
| 77 | + }); |
| 78 | + |
| 79 | + const rs = await this.signAndBroadcast(storeCode); |
| 80 | + if (rs.rawLog === undefined) |
| 81 | + throw new Error("error parsing raw logs: rawLog undefined"); |
| 82 | + |
| 83 | + var codeId: number; |
| 84 | + try { |
| 85 | + // {"key":"code_id","value":"14"} |
| 86 | + const ci = extractFromRawLog(rs.rawLog, "code_id"); |
| 87 | + codeId = parseInt(ci); |
| 88 | + } catch (e) { |
| 89 | + console.error( |
| 90 | + "Encountered an error in parsing deploy code result. Printing raw log" |
| 91 | + ); |
| 92 | + console.error(rs.rawLog); |
| 93 | + throw e; |
| 94 | + } |
| 95 | + |
| 96 | + return codeId; |
| 97 | + } |
| 98 | + |
| 99 | + async instantiate( |
| 100 | + codeId: number, |
| 101 | + inst_msg: string | object, |
| 102 | + label: string |
| 103 | + ): Promise<string> { |
| 104 | + const accAddress = await this.getAccountAddress(); |
| 105 | + const instMsg = |
| 106 | + cosmwasm.wasm.v1.MessageComposer.withTypeUrl.instantiateContract({ |
| 107 | + sender: accAddress, |
| 108 | + admin: accAddress, |
| 109 | + codeId: Long.fromNumber(codeId), |
| 110 | + label, |
| 111 | + msg: Buffer.from(JSON.stringify(inst_msg)), |
| 112 | + funds: [], |
| 113 | + }); |
| 114 | + |
| 115 | + const rs = await this.signAndBroadcast(instMsg); |
| 116 | + if (rs.rawLog === undefined) |
| 117 | + throw new Error("error parsing raw logs: rawLog undefined"); |
| 118 | + |
| 119 | + var address: string = ""; |
| 120 | + try { |
| 121 | + // {"key":"_contract_address","value":"terra1xxx3ps3gm3wceg4g300hvggdv7ga0hmsk64srccffmfy4wvcrugqnlvt8w"} |
| 122 | + address = extractFromRawLog(rs.rawLog, "_contract_address"); |
| 123 | + } catch (e) { |
| 124 | + console.error( |
| 125 | + "Encountered an error in parsing instantiation result. Printing raw log" |
| 126 | + ); |
| 127 | + console.error(rs.rawLog); |
| 128 | + throw e; |
| 129 | + } |
| 130 | + |
| 131 | + console.log( |
| 132 | + `Instantiated ${label} at ${address} (${convert_terra_address_to_hex( |
| 133 | + address |
| 134 | + )})` |
| 135 | + ); |
| 136 | + return address; |
| 137 | + } |
| 138 | + |
| 139 | + async migrate(contract: string, codeId: number): Promise<void> { |
| 140 | + const migrateMsg = |
| 141 | + cosmwasm.wasm.v1.MessageComposer.withTypeUrl.migrateContract({ |
| 142 | + sender: await this.getAccountAddress(), |
| 143 | + contract, |
| 144 | + codeId: Long.fromNumber(codeId), |
| 145 | + msg: Buffer.from( |
| 146 | + JSON.stringify({ |
| 147 | + action: "", |
| 148 | + }) |
| 149 | + ), |
| 150 | + }); |
| 151 | + |
| 152 | + const rs = await this.signAndBroadcast(migrateMsg); |
| 153 | + if (rs.rawLog === undefined) |
| 154 | + throw new Error("error parsing raw logs: rawLog undefined"); |
| 155 | + |
| 156 | + try { |
| 157 | + // {"key":"code_id","value":"13"} |
| 158 | + let resultCodeId = parseInt(extractFromRawLog(rs.rawLog, "code_id")); |
| 159 | + assert.strictEqual(codeId, resultCodeId); |
| 160 | + } catch (e) { |
| 161 | + console.error( |
| 162 | + "Encountered an error in parsing migration result. Printing raw log" |
| 163 | + ); |
| 164 | + console.error(rs.rawLog); |
| 165 | + throw e; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + static fromHostAndMnemonic(host: OsmosisHost, mnemonic: string) { |
| 170 | + return new OsmosisDeployer(host.endpoint, mnemonic); |
| 171 | + } |
| 172 | +} |
0 commit comments