github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/docs/js-api.md (about) 1 # JS API 2 3 Burrow has a JavaScript API for communicating with a [Hyperledger Burrow](https://github.com/hyperledger/burrow) server, which implements the GRPC spec. 4 5 ## Prerequisites 6 7 - Burrow version 0.20 or higher 8 - Node.js version 7 or higher 9 10 You can check the installed version of Node.js with the command: 11 12 ```bash 13 $ node --version 14 ``` 15 16 If your distribution of Linux has a version older than 6 then you can update it. 17 18 ## Install 19 20 ``` bash 21 $ yarn install @hyperledger/burrow 22 ``` 23 24 ## Usage 25 26 You will need to know the <IP Address>:<PORT> of the burrow instance you wish to connect to. If running locally this will be 'localhost' and the default port, which is '10997'. DO NOT INCLUDE A PROTOCOL. 27 28 The main class is `Burrow`. A standard `Burrow` instance is created like this: 29 30 ```JavaScript 31 const monax = require('@monax/burrow'); 32 var burrowURL = "<IP address>:<PORT>"; // localhost:10997 if running locally on default port 33 var account = 'ABCDEF01234567890123'; // address of the account to use for signing, hex string representation 34 var options = {objectReturn: true}; 35 var burrow = monax.createInstance(burrowURL, account, options); 36 ``` 37 38 The parameters for `createInstance` is the server URL as a string or as an object `{host:<IP Address>, port:<PORT>}`. An account in the form of a hex-encoded address must be provided. 39 40 > Note: the instance of burrow you are connecting to must have the associated key (if you want local signing you should be running a local node of burrow. Other local signing options might be made available at a later point). 41 42 And finally an optional options object. Allowed options are: 43 44 * objectReturn: If True, communicating with contracts an object returns an object of the form: `{values:{...}, raw:[]}` where the values objects attempts to name the returns based on the abi and the raw is the decoded array of return values. If False just the array of decoded return values is returned. 45 46 47 ## API Reference 48 49 There are bindings for all the GRPC methods. All functions are on the form `function(param1, param2, ... [, callback])`, where the callback is a function on the form `function(error, data)`. The `data` object is the same as you would get by calling the corresponding RPC method directly. If no callback is provided, a promise will be returned instead. If calling a response streaming GRPC call, the callback is not optional and will be called with `data` anytime it is recieved. 50 51 The structure of the library is such that there are lower-level access to the GRPC services and higher level wrappers of these services. The structure of the library is outlined below 52 53 ### Burrow 54 55 The table below links to the reference schema for either the protobuf files governing the component or the Javascript interface. 56 57 | Component Name | Accessor | 58 | :----------- | :--------------- | 59 | Transactions | [Burrow.transact](https://github.com/hyperledger/burrow/blob/main/protobuf/rpctransact.proto) | 60 | Queries | [Burrow.query](https://github.com/hyperledger/burrow/blob/main/protobuf/rpcquery.proto) | 61 | EventStream | [Burrow.eventStream](https://github.com/hyperledger/burrow/blob/main/protobuf/rpcevents.proto) | 62 | Events | [Burrow.events](https://github.com/hyperledger/burrow/blob/main/lib/events.js) | 63 | NameReg | [Burrow.namereg](https://github.com/hyperledger/burrow/blob/main/lib/namereg.js) | 64 65 | Contracts | [Burrow.contracts](https://github.com/hyperledger/burrow/blob/main/lib/contractManager.js) | 66 67 ### GRPC Access Components 68 69 Burrow provides access to three GRPC services; Transactions, Queries, and ExecutionEvents in the form of automatically generated code endpoints. Below details how to access these methods and links to the request and return objects defined in the protobuf specs. The format for all calls is `function(object[, callback])` The callback is optional for non-streaming endpoints in which case a promise will be returned. 70 71 #### Sending Funds and Creating Accounts 72 73 In Burrow an account must already exist in order to be used as a input account (the sender of a transaction). An account can be created once the chain is running (accounts at genesis can be described in the genesis document in the accounts section) in the following ways: 74 75 1. Issuing a `SendTx` to the address to be created (see below) - where the input account must have both the `Send` and `CreateAccount` permission. 76 2. Sending value to the address to created from a smart contract using the CALL opcode - where the caller contract must have the `CreateAccount` permission. 77 3. Issuing a `GovTx` where accounts can be created/updated in bulk provided the input has the `Root` permission. 78 79 The conventional way to create an new account to use as an input for transactions is the following: 80 81 First create a key - you will want to create an account for which you have access to the private key controlling that account (as defined by the address of the public key): 82 83 ```shell 84 # Create a new key against the key store of a locally running Burrow (or burrow keys standalone server): 85 $ address=$(burrow keys gen -n --name NewKey) 86 87 # The address will be printed to stdout so the above captures it in $address, you can also list named keys: 88 $ burrow keys list 89 Address:"6075EADD0C7A33EE6153F3FA1B21E4D80045FCE2" KeyName:"NewKey" 90 ``` 91 92 Note creating the key _does not_ create the account - it just generates a key pair in your local key store (it is not in anyway known the blockchain network). 93 94 Now we would like to use a `SendTx` to create the address, here's how to do that in Javscript: 95 96 ```javascript 97 // Using account and burrow defined in snippet from [Usage](#usage) 98 99 // Address we want to create 100 var addressToCreate = "6075EADD0C7A33EE6153F3FA1B21E4D80045FCE2" 101 102 // The amount we send is arbitrary 103 var amount = 20 104 105 client.transact.SendTxSync( 106 { 107 Inputs: [{ 108 Address: Buffer.from(account, 'hex'), 109 Amount: amount 110 }], 111 Outputs: [{ 112 Address: Buffer.from(addressToCreate, 'hex'), 113 Amount: amount 114 }] 115 }) 116 .then(txe => console.log(txe)) 117 .catch(err => console.error(err)) 118 ``` 119 120 The return `txe` (short for `TxExecution`) logged to the console in the `then` block contains the history and fingerprint of the `SendTx` execution. You can see an example of this in [basic app](example/basic-app/app.js). 121 122 #### NameReg access 123 124 Here is an example of usage in setting and getting a name: 125 126 ```javascript 127 var setPayload = { 128 Input: { 129 Address: Buffer.from(account, 'hex'), 130 Amount: 50000 131 }, 132 Name: "DOUG", 133 Data: "Marmot", 134 Fee: 5000 135 } 136 137 var getPayload = {Name: "DOUG"} 138 139 // Using a callback 140 client.transact.NameTxSync(setPayload, function (error, data) { 141 if (error) throw error; // or something more sensible 142 // data object contains detailed information of the transaction execution. 143 144 // Get a name this time using a promise 145 client.query.GetName(getPayload) 146 .then((data) => { 147 console.log(data); 148 }) // should print "Marmot" 149 .catch((error) => { 150 throw error; 151 }) 152 }) 153 154 ``` 155 156 #### Transactions 157 158 `burrow.transact` provides access to the burrow GRPC service `rpctransact`. As a GRPC wrapper all the endpoints take a data argument and an optional callback. The format of the data object is specified in the [rpctransact protobuf file](./protobuf/rpctransact.proto). A note on RPC naming, any method which ends in `Sync` will wait until the transaction generated is included in a block. Any `Async` method will return a receipt of the transaction immediately but does not guarantee it has been included. `Sim` methods request that the transaction be simulated and the result returned as if it had been executed. SIMULATED CALLS DO NOT GET COMMITTED AND DO NOT CHANGE STATE. 159 160 | Method | Passed | Returns | 161 | :----- | :--------- | :---- | 162 | burrow.transact.BroadcastTxSync | [TxEnvelopeParam](https://github.com/hyperledger/burrow/blob/main/protobuf/rpctransact.proto#L74-L79) | [TxExecution](https://github.com/hyperledger/burrow/blob/develop/protobuf/exec.proto#L34-L56) | 163 | burrow.transact.BroadcastTxASync | [TxEnvelopeParam](https://github.com/hyperledger/burrow/blob/main/protobuf/rpctransact.proto#L74-L79) | [Receipt](https://github.com/hyperledger/burrow/blob/develop/protobuf/txs.proto#L38-L47) | 164 | burrow.transact.SignTx | [TxEnvelopeParam](https://github.com/hyperledger/burrow/blob/main/protobuf/rpctransact.proto#L74-L79) | [TxEnvelope](https://github.com/hyperledger/burrow/blob/develop/protobuf/rpctransact.proto#L70-L72) | 165 | burrow.transact.FormulateTx | [PayloadParam](https://github.com/hyperledger/burrow/blob/main/protobuf/rpctransact.proto#L64-L68) | [TxEnvelope](https://github.com/hyperledger/burrow/blob/develop/protobuf/rpctransact.proto#L70-L72) | 166 | burrow.transact.CallTxSync | [CallTx](https://github.com/hyperledger/burrow/blob/main/protobuf/payload.proto#L53-L66) | [TxExecution](https://github.com/hyperledger/burrow/blob/develop/protobuf/exec.proto#L34-L56) | 167 | burrow.transact.CallTxAsync | [CallTx](https://github.com/hyperledger/burrow/blob/main/protobuf/payload.proto#L53-L66) | [Receipt](https://github.com/hyperledger/burrow/blob/develop/protobuf/txs.proto#L38-L47) | 168 | burrow.transact.CallTxSim | [CallTx](https://github.com/hyperledger/burrow/blob/main/protobuf/payload.proto#L53-L66) | [TxExecution](https://github.com/hyperledger/burrow/blob/develop/protobuf/exec.proto#L34-L56) | 169 | burrow.transact.SendTxSync | [SendTx](https://github.com/hyperledger/burrow/blob/main/protobuf/payload.proto#L69-L76) | [TxExecution](https://github.com/hyperledger/burrow/blob/develop/protobuf/exec.proto#L34-L56) | 170 | burrow.transact.SendTxAsync | [SendTx](https://github.com/hyperledger/burrow/blob/main/protobuf/payload.proto#L69-L76) | [Receipt](https://github.com/hyperledger/burrow/blob/develop/protobuf/txs.proto#L38-L47) | 171 | burrow.transact.NameTxSync | [NameTx](https://github.com/hyperledger/burrow/blob/main/protobuf/payload.proto#L88-L98) | [TxExecution](https://github.com/hyperledger/burrow/blob/develop/protobuf/exec.proto#L34-L56) | 172 | burrow.transact.NameTxAsync | [NameTx](https://github.com/hyperledger/burrow/blob/main/protobuf/payload.proto#L88-L98) | [Receipt](https://github.com/hyperledger/burrow/blob/develop/protobuf/txs.proto#L38-L47) | 173 174 175 #### Queries 176 177 `Burrow.query` provides access to the burrow GRPC service `rpcquery`. As a GRPC wrapper all the endpoints take a data argument and an optional callback. The format of the data object is specified in the [protobuf files](https://github.com/hyperledger/burrow/tree/main/js/protobuf). Note that "STREAM" functions take a callback `function(error, data)` which is mandatory and is called any time data is returned. For list Accounts the queryable tags are Address, PublicKey, Sequence, Balance, Code, Permissions (Case sensitive). As an example you can get all accounts with a balance greater than 1000 by `burrow.query.ListAccounts('Balance > 1000', callback)`. Multiple tag criteria can be combined using 'AND' and 'OR' for an example of a combined query see [here](https://github.com/hyperledger/burrow/blob/develop/protobuf/rpcevents.proto#L87). Similarly for ListNames, the avaible tags are Name, Data, Owner and Exires (once again case sensitive) use is identical to List accounts. 178 179 | Method | Passed | Returns | Notes | 180 | :----- | :--------- | :---- | :------- | 181 | burrow.query.GetAccount | [GetAccountParam](https://github.com/hyperledger/burrow/blob/main/protobuf/rpcquery.proto#L25-L27) | [ConcreteAccount](https://github.com/hyperledger/burrow/blob/develop/protobuf/acm.proto#L23-L31) | | 182 | burrow.query.ListAccounts | [ListAccountsParam](https://github.com/hyperledger/burrow/blob/main/protobuf/rpcquery.proto#L29-L31) | [ConcreteAccount](https://github.com/hyperledger/burrow/blob/develop/protobuf/acm.proto#L23-L31) | STREAM | 183 | burrow.query.GetNameParam | [GetNameParam](https://github.com/hyperledger/burrow/blob/main/protobuf/rpcquery.proto#L33-L35) | [Entry](https://github.com/hyperledger/burrow/blob/develop/protobuf/names.proto#L22-L32) | | 184 | burrow.query.ListNames | [ListNamesParam](https://github.com/hyperledger/burrow/blob/main/protobuf/rpcquery.proto#L37-L39) | [Entry](https://github.com/hyperledger/burrow/blob/develop/protobuf/names.proto#L22-L32) | STREAM| 185 186 #### EventStream 187 188 NB: When listening to contract events it is easier to use the contract interface (described below) 189 190 `Burrow.executionEvents` provides access to the burrow GRPC service `ExecutionEvents`. As a GRPC wrapper all the endpoints take a data argument and an optional callback. The format of the data object is specified in the [protobuf files](https://github.com/hyperledger/burrow/tree/main/js/protobuf). Note that "STREAM" functions take a callback `function(error, data)` which is mandatory and is called any time data is returned. 191 192 | Method | Passed | Returns | Notes | 193 | :----- | :--------- | :---- | :------- | 194 | burrow.executionEvents.GetBlock | [GetBlockRequest](https://github.com/hyperledger/burrow/blob/main/protobuf/rpcevents.proto#L37-L42) | [BlockExecution](https://github.com/hyperledger/burrow/blob/develop/protobuf/exec.proto#L20-L27) | | 195 | burrow.executionEvents.GetBlocks | [BlocksRequest](https://github.com/hyperledger/burrow/blob/main/protobuf/rpcevents.proto#L51-L89) | [BlockExecution](https://github.com/hyperledger/burrow/blob/develop/protobuf/exec.proto#L20-L27) | STREAM | 196 | burrow.executionEvents.GetTx | [GetTxRequest](https://github.com/hyperledger/burrow/blob/main/protobuf/rpcevents.proto#L44-L49) | [TxExecution](https://github.com/hyperledger/burrow/blob/develop/protobuf/exec.proto#L34-L56) | | 197 | burrow.executionEvents.GetTxs | [BlocksRequest](https://github.com/hyperledger/burrow/blob/main/protobuf/rpcevents.proto#L51-L89) | [GetTxsResponse](https://github.com/hyperledger/burrow/blob/develop/protobuf/rpcevents.proto#L96-L99) | STREAM | 198 | burrow.executionEvents.GetEvents | [BlocksRequest](https://github.com/hyperledger/burrow/blob/main/protobuf/rpcevents.proto#L51-L89) | [GetEventsResponse](https://github.com/hyperledger/burrow/blob/develop/protobuf/rpcevents.proto#L91-L94) | STREAM | 199 200 201 *** 202 203 ### High-Level Components 204 205 In addition to direct access to the grpc services, the burrow object also provides access to *three* higher level components which wrap the low level access for convenience. These are ```.namereg```, ```.events```, and ```.contracts```. All high level components use the account provided during creation of the burrow instance for constructing transactions. Component ```contracts``` is the most important of the three, as events and namereg are really just helpful wrappers. 206 207 208 #### 1. Namereg 209 210 `burrow.namereg` is a convenience wrapper for setting and getting entries from the name registry. 211 212 ##### burrow.namereg.get 213 ```burrow.namereg.get(name[,callback])``` 214 215 Gets an entry stored at the name. It returns a promise if callback not provided. 216 ###### Parameters 217 1. `String` - Name you wish to retrieve from the namereg 218 2. `function` - (optional) Function to call upon completion of form `function(error, data)`. 219 ###### Returns 220 `Object` - The return data object is of the form: 221 222 ```javascript 223 { 224 Name: (registered name) (string) 225 Owner: (address of name owner) (buffer) 226 Data: (stored data) (string) 227 Expires: (block at which entry expires) (int) 228 } 229 ``` 230 231 ##### burrow.namereg.set 232 ```burrow.namereg.set(name, data, lease[, callback])``` 233 234 Sets an entry in the namereg. It returns a promise if callback not provided. 235 ###### Parameters 236 1. `String` - The name you wish to register 237 2. `String` - The string data you wish to store at the registered name (longer string = larger fee) 238 3. `int` - The number of blocks to register the name for (more blocks = larger fee) 239 4. `function` - (optional) Function to call upon completion of form `function(error, data)`. 240 ###### Returns 241 `TxExecution` - The return data object is a [TxExecution](https://github.com/hyperledger/burrow/blob/main/protobuf/exec.proto#L34-L56). 242 ###### Example 243 244 ```javascript 245 // Using a callback 246 client.namereg.set("DOUG", "Marmot", 5000, function (error, data) { 247 if (error) throw error; // or something more sensible 248 // data object contains detailed information of the transaction execution. 249 250 // Get a name this time using a promise 251 client.namereg.get("DOUG") 252 .then((data) => { 253 console.log(data); 254 }) // Should print "Marmot" 255 .catch((error) => { 256 throw error; 257 }) 258 }) 259 ``` 260 261 > Note: this example is nearly identical to the example above except that the objects are not explicitly constructed by you. 262 263 264 265 #### 2. Events 266 267 `burrow.events` contains convenience wrappers for streaming executionEvents. 268 269 ##### burrow.events.listen 270 ```burrow.events.listen(query, options, callback)``` 271 272 Listens to execution events which satisfy the filter query. 273 ###### Parameters 274 1. `String` - a pegjs querystring for filtering the returned events see [here]() for grammar specification 275 2. `Object` - Currently unused. pass `{}` 276 3. `function` - Signature of `function(error, data)` mandatory 277 ###### Returns 278 `GetEventsResponse` - The return data object is a [GetEventsResponse](https://github.com/hyperledger/burrow/blob/main/protobuf/rpcevents.proto#L91-L94) 279 280 281 282 ##### burrow.events.subContractEvents 283 ```burrow.events.subContractEvents(address, signature, options, callback)``` 284 285 Listens to EVM event executions from specific contract. 286 ###### Parameters 287 1. `String` - hex string of the contract address of interest 288 2. `String` - event abi signature 289 3. `Object` - Currently unused. pass `{}` 290 4. `function` - Signature of `function(error, data)` mandatory. 291 ###### Returns 292 `GetEventsResponse` - The return data object is a [GetEventsResponse](https://github.com/hyperledger/burrow/blob/main/protobuf/rpcevents.proto#L91-L94) 293 294 295 296 #### 3. Contracts 297 298 `burrow.contracts` is arguably the most important component of the burrow it exposes two functions, `.deploy` and `.new` both of which return a Contract interface object (sometimes refered to as contract object). The difference between them is that `new` simply creates an interface to a contract and `deploy` will first create an instance and then deploy a copy of it to the blockchain. 299 300 301 ##### burrow.contracts.deploy 302 ```burrow.contracts.deploy(abi, bytecode, params... [, callback])``` 303 304 Deploys a contract and returns a contract interface either to the callback or a promise once deploy is successful. It returns a promise if callback not provided. 305 306 When the contract interface object is created via deploy, the default address is set to the address of the deployed contract (which can be accessed as contract.address). This interface object can still be used as a generic interface but care must be taken to use the `.at()` and `.atSim()` versions of functions. 307 308 ###### Parameters 309 1. `Object` - the object corresponding to the json ABI of the contract you wish to interface with. 310 2. `String` - Hex encoded string of bytecode of the contract to deploy 311 3. `params` - arguments to the constructor function (if there are any) 312 4. `function` - (optional) Format of `function(error, contract)` where contract is the contract interface object. 313 ###### Returns 314 `Object` - The return data object is a contract interface, which refers to the contract which is deployed at `contract.address`. (This functionality used to be called `new`.) 315 316 ##### burrow.contracts.new 317 ```burrow.contracts.address(address)``` 318 319 Returns a new contract interface object, without having to pass in the ABI. The ABI is retrieved from burrow; the contract must have been deployed with burrow deploy 0.28.0 or later. 320 321 ###### Parameters 322 3. `String` - Hex encoded address of the default contract you want the interface to access 323 ###### Returns 324 `Object` - The return data object is a contract interface. 325 326 ##### burrow.contracts.new 327 ```burrow.contracts.new(abi, [bytecode[, address]])``` 328 329 Returns a new contract interface object. All you really need to create an interface is the abi, however you can also include the bytecode of the contract. If you do so you can create new contracts of this type by calling `contract._constructor(...)` which will deploy a new contract and return its address. If you provide an address, then this will be the default contract address used however you can also omit this at be sure to use the `.at()` and `.atSim()` versions of functions. Also note you must provide bytecode is you wish to provide address, though bytecode argument can be null. 330 331 ###### Parameters 332 1. `Object` - the object corresponding to the json ABI of the contract you wish to interface with. 333 2. `String` - Hex encoded string of bytecode of the contract to deploy 334 3. `String` - (optional) Hex encoded address of the default contract you want the interface to access 335 ###### Returns 336 `Object` - The return data object is a contract interface. 337 338 339 #### 3.1. Contract interface object 340 341 The contract interface object allows for easy access of solidity contract function calls and subscription to events. When created, javascript functions for all functions specified in the abi are generated. All of these functions have the same form `Contract.functionname(params...[, callback])`, where `params` are the arguments to the contract constructor. Arguments of the "bytes" type should be properly hex encoded before passing, to avoid improper encoding. If a callback is not provided a promise is returned. 342 343 > Note: if the burrow object was created with ```{objectReturn: True}``` the return from these function calls is formatted as `{values:{...}, raw:[]}` otherwise an array of decoded values is provided. The values object names the decoded values according to the abi spec, if a return value has no name it won't be included in the values object and must be retrieved from its position on the raw array. 344 345 346 In the case of a REVERT op-code being called in the contract function call, an error will be passed with the revert string as the `.message` field. These errors can be distinguished from other errors as the `.code` field will be `ERR_EXECUTION_REVERT`. 347 348 In addition to the standard function call, there are three other forms: `contract.functionname.sim`, `contract.functionname.at`, `contract.functionname.atSim`. 349 350 351 ##### contract.functionname.sim 352 ```contract.functionname.sim(params... [, callback])``` 353 354 The "Sim" forms will force a simulated call so that does not change state. Although, the data returned is identical to what would have been returned if the call had been submitted. Useful for querying data or checking if a transaction passes some tests. 355 ###### Parameters 356 1. `params` - the arguments to the function (if there are any) 357 2. `function`- (optional) Function to call upon completion of form `function(error, data)`. 358 359 360 361 ##### contract.functionname.at 362 ```contract.functionname.at(address, params... [, callback])``` 363 364 The "at" forms allow you to specify which contract you wish to submit the transaction to. This allows you to use a single contract interface instance to access any contract with the same abi. Useful if for example there is a factory contract on the chain and you wish to connect to any of its children. The at forms MUST be used if a default address was not provided or created. 365 ###### Parameters 366 1. `String` - Hex encoded address of the default contract you want the interface to access 367 2. `params` - the arguments to the function (if there are any) 368 3. `function`- (optional) Function to call upon completion of form `function(error, data)`. 369 370 371 372 ##### contract.functionname.atSim 373 ```contract.functionname.at(address, params... [, callback])``` 374 375 376 ###### Parameters 377 1. `String` - Hex encoded address of the default contract you want the interface to access 378 2. `params` - the arguments to the function (if there are any) 379 3. `function`- (optional) Function to call upon completion of form `function(error, data)` 380 381 382 ##### contract._constructor 383 ```contract._constructor(params... [, callback])``` 384 385 Deploys a new contract from the same interface (no need to create a new interface object via .deploy). Once completed it will return the created contract's address. 386 387 388 ###### Parameters 389 1. `params` - the arguments to the function (if there are any) 390 3. `function`- (optional) Function to call upon completion of form `function(error, data)`. 391 ###### Returns 392 `String` - The return data String is the created contract's address. 393 394 395 #### 3.2. Encoding and Decoding Params 396 397 Occassionally you may wish to encode the parameters to a function call but not actually make the call. The most common use of this is in forwarding contracts which take pre-encoded arguments along with function signature bytes and then call another function passing that data for specifying the call. 398 399 The Contract interface object supports this use case through `Contract.functionname.encode(...args)` which will return a hex string with the encoded arguments. This functionality is also available through `Monax.utils.encode(abi, functionname, ...args)`. In addition the complement also exists, `Contract.functionname.decode(data)` will produce the return object as if the data was just returned from a call. 400 401 402 #### 3.3. Contract Events 403 404 ##### contract.eventname 405 ```contract.eventname(callback)``` 406 407 The contract interface object exposes subscription to Solidity events under event's name. 408 where the provided callback with be passed an error and data of the form: 409 410 ###### Parameters 411 1. `function` - Function to call upon completion of form `function(error, data)`. The data object has the following form: 412 ``` 413 { 414 event: [fulleventname], 415 address: [address of contract emitting event], 416 args: {argname: argvalue} 417 } 418 ``` 419 420 421 ##### contract.eventname.at 422 ```contract.eventname.at(address, callback)``` 423 424 Similarly to functions' contract it is possible to start listening to a non-default contract address. 425 426 ###### Parameters 427 1. `String` - hex string of the contract address of interest 428 2. `function` - Function to call upon completion of form `function(error, data)`. The data object has the following form: 429 ``` 430 { 431 event: [fulleventname], 432 address: [address of contract emitting event], 433 args: {argname: argvalue} 434 } 435 ``` 436 437 438 439 440 ### Example: 441 442 The following contract is a simple contract which takes a "name" to the constructor and also has a function `getName` which returns the name. 443 444 ````solidity 445 pragma solidity ^0.4.18; 446 contract SimpleContract { 447 448 string private name; 449 450 function SimpleContract(string _newName) public { 451 name = _newName; 452 } 453 454 function getName() public constant returns (string thename) { 455 return name; 456 } 457 458 } 459 ```` 460 461 Here I provide an example of communicating to the contract above from start to finish: 462 463 ```javascript 464 const monax = require('@monax/burrow'); 465 const assert = require('assert'); 466 467 var burrowURL = "<IP address>:<PORT>"; // localhost:10997 if running locally on default port 468 var account = 'ABCDEF01234567890123'; // address of the account to use for signing, hex string representation 469 var options = {objectReturn: false}; 470 var burrow = monax.createInstance(burrowURL, account, options); 471 472 // Get the contractABIJSON from somewhere such as solc 473 var abi = json.parse(contractABIJSON) // Get the contractABIJSON from somewhere such as solc 474 var bytecode = contractBytecode // Get this from somewhere such as solc 475 476 477 478 // I'm going to use new to create a contract interface followed by a double direct call to the _constructor to deploy two contracts 479 const contract = burrow.contracts.new(abi, bytecode); 480 return Promise.all( // Deployment of two contracts 481 [ 482 contract._constructor('contract1'), 483 contract._constructor('contract2') 484 ] 485 ).then( ([address1, address2]) => { // Collection of contracts' addresses 486 console.log(address1 + " - contract1"); 487 console.log(address2 + " - contract2"); 488 return Promise.all( // Execution of getName functions 489 [ 490 contract.getName.at(address1), // Using the .at() to specify the second deployed contract 491 contract.getName.at(address2) 492 ] 493 ).then( ([name1, name2]) => { // Collection of contracts' names 494 console.log(address1 + " - " + name1); 495 assert.equal(name1, 'contract1'); 496 console.log(address2 + " - " + name2); 497 assert.equal(name2, 'contract2'); 498 }); 499 }); 500 ``` 501 502 <!-- 503 504 ## Documentation 505 506 Generate documentation using the command `yarn run doc`. 507 508 ## Testing 509 510 To test the library against pre-recorded vectors: 511 512 ``` 513 yarn test 514 ``` 515 516 To test the library against Burrow while recording vectors: 517 518 ``` 519 TEST=record yarn test 520 ``` 521 522 To test Burrow against pre-recorded vectors without exercising the library: 523 524 ``` 525 TEST=server yarn test 526 ``` 527 528 ## Debugging 529 530 Debugging information will display on `stderr` if the library is run with `NODE_DEBUG=monax` in the environment. --> 531