1- import axios from "axios" ;
2- import fs from "fs" ;
3- import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate" ;
4- import { GasPrice , calculateFee , StdFee } from "@cosmjs/stargate" ;
5- import { DirectSecp256k1HdWallet , makeCosmoshubPath } from "@cosmjs/proto-signing" ;
6- import { Slip10RawIndex } from "@cosmjs/crypto" ;
7- import { toUtf8 , toBase64 } from "@cosmjs/encoding" ;
8- import path from "path" ;
1+ import { calculateFee } from "@cosmjs/stargate"
92
103/*
114 * This is a set of helpers meant for use with @cosmjs/cli
5+ * Look at https://raw.githubusercontent.com/CosmWasm/cw-plus/master/contracts/base-helpers.ts on how to setup a wallet
126 * With these you can easily use the cw20 contract without worrying about forming messages and parsing queries.
137 *
14- * Usage: npx @cosmjs/cli@^0.26 --init https://raw.githubusercontent.com/CosmWasm/cw-plus/master/contracts/cw4-group/helpers.ts
8+ * Usage: npx @cosmjs/cli@^0.26 --init https://raw.githubusercontent.com/CosmWasm/cw-plus/master/contracts/base-helpers.ts --init https://raw.githubusercontent.com/CosmWasm/cw-plus/master/contracts/ cw4-group/helpers.ts
159 *
1610 * Create a client:
1711 * const [addr, client] = await useOptions(pebblenetOptions).setup('password');
@@ -20,10 +14,10 @@ import path from "path";
2014 * await useOptions(pebblenetOptions).recoverMnemonic(password);
2115 *
2216 * Create contract:
23- * const contract = CW4Group(client, pebblenetOptions.fees );
17+ * const contract = CW4Group(client, pebblenetOptions);
2418 *
2519 * Upload contract:
26- * const codeId = await contract.upload(addr);
20+ * const codeId = await contract.upload(addr, pebblenetOptions );
2721 *
2822 * Instantiate contract example:
2923 * const initMsg = {
@@ -39,110 +33,11 @@ import path from "path";
3933 * },
4034 * ]
4135 * };
42- * const instance = await contract.instantiate(addr, codeId, initMsg, 'WORKFORCE1' );
36+ * const instance = await contract.instantiate(addr, codeId, initMsg, 'Potato Coin!', pebblenetOptions );
4337 *
4438 * If you want to use this code inside an app, you will need several imports from https://github.com/CosmWasm/cosmjs
4539*/
4640
47- interface Options {
48- readonly httpUrl : string
49- readonly networkId : string
50- readonly feeToken : string
51- readonly bech32prefix : string
52- readonly hdPath : readonly Slip10RawIndex [ ]
53- readonly faucetUrl ?: string
54- readonly defaultKeyFile : string ,
55- readonly fees : {
56- upload : StdFee ,
57- init : StdFee ,
58- exec : StdFee
59- }
60- }
61-
62- const pebblenetGasPrice = GasPrice . fromString ( "0.01upebble" ) ;
63- const pebblenetOptions : Options = {
64- httpUrl : 'https://rpc.pebblenet.cosmwasm.com' ,
65- networkId : 'pebblenet-1' ,
66- bech32prefix : 'wasm' ,
67- feeToken : 'upebble' ,
68- faucetUrl : 'https://faucet.pebblenet.cosmwasm.com/credit' ,
69- hdPath : makeCosmoshubPath ( 0 ) ,
70- defaultKeyFile : path . join ( process . env . HOME , ".pebblenet.key" ) ,
71- fees : {
72- upload : calculateFee ( 1500000 , pebblenetGasPrice ) ,
73- init : calculateFee ( 500000 , pebblenetGasPrice ) ,
74- exec : calculateFee ( 200000 , pebblenetGasPrice ) ,
75- } ,
76- }
77-
78- interface Network {
79- setup : ( password : string , filename ?: string ) => Promise < [ string , SigningCosmWasmClient ] >
80- recoverMnemonic : ( password : string , filename ?: string ) => Promise < string >
81- }
82-
83- const useOptions = ( options : Options ) : Network => {
84-
85- const loadOrCreateWallet = async ( options : Options , filename : string , password : string ) : Promise < DirectSecp256k1HdWallet > => {
86- let encrypted : string ;
87- try {
88- encrypted = fs . readFileSync ( filename , 'utf8' ) ;
89- } catch ( err ) {
90- // generate if no file exists
91- const wallet = await DirectSecp256k1HdWallet . generate ( 12 , { hdPaths : [ options . hdPath ] , prefix : options . bech32prefix } ) ;
92- const encrypted = await wallet . serialize ( password ) ;
93- fs . writeFileSync ( filename , encrypted , 'utf8' ) ;
94- return wallet ;
95- }
96- // otherwise, decrypt the file (we cannot put deserialize inside try or it will over-write on a bad password)
97- const wallet = await DirectSecp256k1HdWallet . deserialize ( encrypted , password ) ;
98- return wallet ;
99- } ;
100-
101- const connect = async (
102- wallet : DirectSecp256k1HdWallet ,
103- options : Options
104- ) : Promise < SigningCosmWasmClient > => {
105- const clientOptions = {
106- prefix : options . bech32prefix
107- }
108- return await SigningCosmWasmClient . connectWithSigner ( options . httpUrl , wallet , clientOptions )
109- } ;
110-
111- const hitFaucet = async (
112- faucetUrl : string ,
113- address : string ,
114- denom : string
115- ) : Promise < void > => {
116- await axios . post ( faucetUrl , { denom, address} ) ;
117- }
118-
119- const setup = async ( password : string , filename ?: string ) : Promise < [ string , SigningCosmWasmClient ] > => {
120- const keyfile = filename || options . defaultKeyFile ;
121- const wallet = await loadOrCreateWallet ( pebblenetOptions , keyfile , password ) ;
122- const client = await connect ( wallet , pebblenetOptions ) ;
123-
124- const [ account ] = await wallet . getAccounts ( ) ;
125- // ensure we have some tokens
126- if ( options . faucetUrl ) {
127- const tokens = await client . getBalance ( account . address , options . feeToken )
128- if ( tokens . amount === '0' ) {
129- console . log ( `Getting ${ options . feeToken } from faucet` ) ;
130- await hitFaucet ( options . faucetUrl , account . address , options . feeToken ) ;
131- }
132- }
133-
134- return [ account . address , client ] ;
135- }
136-
137- const recoverMnemonic = async ( password : string , filename ?: string ) : Promise < string > => {
138- const keyfile = filename || options . defaultKeyFile ;
139- const wallet = await loadOrCreateWallet ( pebblenetOptions , keyfile , password ) ;
140- return wallet . mnemonic ;
141- }
142-
143- return { setup, recoverMnemonic} ;
144- }
145-
14641interface AdminResponse {
14742 readonly admin ?: string
14843}
@@ -188,12 +83,12 @@ interface CW4GroupInstance {
18883}
18984
19085interface CW4GroupContract {
191- upload : ( txSigner : string ) => Promise < number >
192- instantiate : ( txSigner : string , codeId : number , initMsg : Record < string , unknown > , label : string , admin ?: string ) => Promise < CW4GroupInstance >
86+ upload : ( txSigner : string , options : Options ) => Promise < number >
87+ instantiate : ( txSigner : string , codeId : number , initMsg : Record < string , unknown > , label : string , options : Options , admin ?: string ) => Promise < CW4GroupInstance >
19388 use : ( contractAddress : string ) => CW4GroupInstance
19489}
19590
196- export const CW4Group = ( client : SigningCosmWasmClient , fees : Options [ 'fees' ] ) : CW4GroupContract => {
91+ export const CW4Group = ( client : SigningCosmWasmClient , options : Options ) : CW4GroupContract => {
19792 const use = ( contractAddress : string ) : CW4GroupInstance => {
19893
19994 const admin = async ( ) : Promise < AdminResponse > => {
@@ -217,22 +112,30 @@ export const CW4Group = (client: SigningCosmWasmClient, fees: Options['fees']):
217112 } ;
218113
219114 const updateAdmin = async ( txSigner : string , admin ?: string ) : Promise < string > => {
220- const result = await client . execute ( txSigner , contractAddress , { update_admin : { admin} } , fees . exec ) ;
115+ const fee = calculateFee ( options . fees . exec , options . gasPrice )
116+
117+ const result = await client . execute ( txSigner , contractAddress , { update_admin : { admin} } , fee ) ;
221118 return result . transactionHash ;
222119 }
223120
224121 const updateMembers = async ( txSigner : string , remove : Member [ ] , add : Member [ ] ) : Promise < string > => {
225- const result = await client . execute ( txSigner , contractAddress , { update_members : { remove, add} } , fees . exec ) ;
122+ const fee = calculateFee ( options . fees . exec , options . gasPrice )
123+
124+ const result = await client . execute ( txSigner , contractAddress , { update_members : { remove, add} } , fee ) ;
226125 return result . transactionHash ;
227126 }
228127
229128 const _addHook = async ( txSigner : string , addr : string ) : Promise < string > => {
230- const result = await client . execute ( txSigner , contractAddress , { add_hook : { addr} } , fees . exec ) ;
129+ const fee = calculateFee ( options . fees . exec , options . gasPrice )
130+
131+ const result = await client . execute ( txSigner , contractAddress , { add_hook : { addr} } , fee ) ;
231132 return result . transactionHash ;
232133 }
233134
234135 const _removeHook = async ( txSigner : string , addr : string ) : Promise < string > => {
235- const result = await client . execute ( txSigner , contractAddress , { remove_hook : { addr} } , fees . exec ) ;
136+ const fee = calculateFee ( options . fees . exec , options . gasPrice )
137+
138+ const result = await client . execute ( txSigner , contractAddress , { remove_hook : { addr} } , fee ) ;
236139 return result . transactionHash ;
237140 }
238141
@@ -258,15 +161,17 @@ export const CW4Group = (client: SigningCosmWasmClient, fees: Options['fees']):
258161 return r . data
259162 }
260163
261- const upload = async ( senderAddress : string ) : Promise < number > => {
164+ const upload = async ( senderAddress : string , options : Options ) : Promise < number > => {
262165 const sourceUrl = "https://github.com/CosmWasm/cosmwasm-plus/releases/download/v0.9.0/cw4_group.wasm" ;
263166 const wasm = await downloadWasm ( sourceUrl ) ;
264- const result = await client . upload ( senderAddress , wasm , fees . upload ) ;
167+ const fee = calculateFee ( options . fees . upload , options . gasPrice )
168+ const result = await client . upload ( senderAddress , wasm , fee ) ;
265169 return result . codeId ;
266170 }
267171
268- const instantiate = async ( senderAddress : string , codeId : number , initMsg : Record < string , unknown > , label : string , admin ?: string ) : Promise < CW4GroupInstance > => {
269- const result = await client . instantiate ( senderAddress , codeId , initMsg , label , fees . init , { memo : `Init ${ label } ` , admin } ) ;
172+ const instantiate = async ( senderAddress : string , codeId : number , initMsg : Record < string , unknown > , label : string , options : Options , admin ?: string ) : Promise < CW4GroupInstance > => {
173+ const fee = calculateFee ( options . fees . init , options . gasPrice )
174+ const result = await client . instantiate ( senderAddress , codeId , initMsg , label , fee , { memo : `Init ${ label } ` , admin } ) ;
270175 return use ( result . contractAddress ) ;
271176 }
272177
0 commit comments