* Invokes a chaincode method with write capabilities (submit transaction). * Changes made during invocation are persisted to the state. * * @template T - Expected return type of the invocation * @param method - Name of the contract method to invoke * @param args - Arguments to pass to
(
method: string,
...args: (string | { serialize: () => string })[]
)
| 166 | * @throws Error if the invocation fails |
| 167 | */ |
| 168 | public async invoke<T = InvokeResponse>( |
| 169 | method: string, |
| 170 | ...args: (string | { serialize: () => string })[] |
| 171 | ): Promise<T> { |
| 172 | const argsSerialized = args.map((arg) => (typeof arg === "string" ? arg : arg.serialize())); |
| 173 | const newWrites = {}; |
| 174 | const stub = new TestChaincodeStub([method, ...argsSerialized], this.state, newWrites); |
| 175 | stub.mockCreator(this.callingUserMsp, this.callingUser); |
| 176 | const rawResponse = await this.chaincode.Invoke(stub); |
| 177 | |
| 178 | if (rawResponse.status === 200) { |
| 179 | const stringResponse = rawResponse.payload.toString(); |
| 180 | this.writes.push(newWrites); |
| 181 | return JSON.parse(stringResponse) as T; |
| 182 | } else { |
| 183 | throw rawResponse.message; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Queries a chaincode method with read-only access (evaluate transaction). |