-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduces callAndPoll utility returning the full undecoded cer…
…tificate from the response
- Loading branch information
Showing
5 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { HttpAgent, fromHex, callAndPoll } from '@dfinity/agent'; | ||
import { expect, describe, it, vi } from 'vitest'; | ||
describe('call and poll', () => { | ||
it('should handle call and poll', async () => { | ||
vi.useRealTimers(); | ||
|
||
const options = { | ||
canisterId: 'tnnnb-2yaaa-aaaab-qaiiq-cai', | ||
methodName: 'inc_read', | ||
agent: await HttpAgent.create({ host: 'https://icp-api.io' }), | ||
arg: fromHex('4449444c0000'), | ||
}; | ||
|
||
const certificate = await callAndPoll(options); | ||
expect(certificate instanceof ArrayBuffer).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Principal } from '@dfinity/principal'; | ||
import { Agent, Certificate, bufFromBufLike, polling } from '..'; | ||
import { AgentError } from '../errors'; | ||
|
||
/** | ||
* Call a canister using the v3 api and either return the response or fall back to polling | ||
* @param options - The options to use when calling the canister | ||
* @param options.canisterId - The canister id to call | ||
* @param options.methodName - The method name to call | ||
* @param options.agent - The agent to use to make the call | ||
* @param options.arg - The argument to pass to the canister | ||
* @returns The certificate response from the canister (which includes the reply) | ||
*/ | ||
export async function callAndPoll(options: { | ||
canisterId: Principal | string; | ||
methodName: string; | ||
agent: Agent; | ||
arg: ArrayBuffer; | ||
}): Promise<ArrayBuffer> { | ||
const { canisterId, methodName, agent, arg } = options; | ||
const cid = Principal.from(options.canisterId); | ||
|
||
const { defaultStrategy } = polling.strategy; | ||
|
||
if (agent.rootKey == null) throw new Error('Agent root key not initialized before making call'); | ||
|
||
const { requestId, response } = await agent.call(cid, { | ||
methodName, | ||
arg, | ||
effectiveCanisterId: cid, | ||
}); | ||
|
||
let certificate: Certificate; | ||
if (response.body && response.body.certificate) { | ||
const cert = response.body.certificate; | ||
// Create certificate to validate the responses | ||
certificate = await Certificate.create({ | ||
certificate: bufFromBufLike(cert), | ||
rootKey: agent.rootKey, | ||
canisterId: Principal.from(canisterId), | ||
}); | ||
} else { | ||
throw new AgentError('unexpected call error: no certificate in response'); | ||
} | ||
// Fall back to polling if we recieve an Accepted response code | ||
if (response.status === 202) { | ||
const pollStrategy = defaultStrategy(); | ||
// Contains the certificate and the reply from the boundary node | ||
const response = await polling.pollForResponse(agent, cid, requestId, pollStrategy); | ||
certificate = response.certificate; | ||
} | ||
|
||
return certificate.rawCert; | ||
} |