|
1 | 1 | import { Principal } from '@dfinity/principal';
|
2 | 2 | import { HttpAgent, replica, AgentCanister } from 'ic0';
|
3 | 3 | import utils from './utils';
|
4 |
| -import { ACCOUNT_BALANCE_CALL, LEDGER_CANISTER_ID, ICRC1_FEE_KEY, METADATA_CALL, DEFAULT_SUBACCOUNT } from './iface'; |
| 4 | +import { |
| 5 | + ACCOUNT_BALANCE_CALL, |
| 6 | + LEDGER_CANISTER_ID, |
| 7 | + GOVERNANCE_CANISTER_ID, |
| 8 | + ICRC1_FEE_KEY, |
| 9 | + METADATA_CALL, |
| 10 | + DEFAULT_SUBACCOUNT, |
| 11 | + NeuronInfo, |
| 12 | + ProposalInfo, |
| 13 | + ClaimNeuronParams, |
| 14 | + SetFolloweesParams, |
| 15 | + DissolveDelayParams, |
| 16 | +} from './iface'; |
5 | 17 | import BigNumber from 'bignumber.js';
|
6 | 18 |
|
7 | 19 | export class IcpAgent {
|
@@ -38,6 +50,17 @@ export class IcpAgent {
|
38 | 50 | return ic(Principal.fromUint8Array(LEDGER_CANISTER_ID).toText());
|
39 | 51 | }
|
40 | 52 |
|
| 53 | + /** |
| 54 | + * Retrieves an instance of the governance canister agent. |
| 55 | + * |
| 56 | + * @returns {AgentCanister} An agent interface for interacting with the governance canister. |
| 57 | + */ |
| 58 | + private getGovernanceCanister(): AgentCanister { |
| 59 | + const agent = this.createAgent(); |
| 60 | + const ic = replica(agent, { local: true }); |
| 61 | + return ic(Principal.fromUint8Array(GOVERNANCE_CANISTER_ID).toText()); |
| 62 | + } |
| 63 | + |
41 | 64 | /**
|
42 | 65 | * Fetches the account balance for a given principal ID.
|
43 | 66 | * @param principalId - The principal ID of the account.
|
@@ -87,4 +110,92 @@ export class IcpAgent {
|
87 | 110 | throw new Error(`Error fetching transaction fee: ${errorMessage}`);
|
88 | 111 | }
|
89 | 112 | }
|
| 113 | + |
| 114 | + /** |
| 115 | + * Claims or refreshes a neuron from an account. |
| 116 | + * |
| 117 | + * @param {ClaimNeuronParams} params - Parameters for claiming the neuron |
| 118 | + * @returns {Promise<bigint>} The neuron ID of the claimed neuron |
| 119 | + * @throws {Error} If the neuron cannot be claimed |
| 120 | + */ |
| 121 | + public async claimNeuron(params: ClaimNeuronParams): Promise<bigint> { |
| 122 | + try { |
| 123 | + const governance = this.getGovernanceCanister(); |
| 124 | + const result = await governance.call('claim_or_refresh_neuron_from_account', params); |
| 125 | + return BigInt(result.neuron_id); |
| 126 | + } catch (error) { |
| 127 | + const errorMessage = error instanceof Error ? error.message : 'Unknown error'; |
| 128 | + throw new Error(`Error claiming neuron: ${errorMessage}`); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Retrieves detailed information about a neuron. |
| 134 | + * |
| 135 | + * @param {bigint} neuronId - The ID of the neuron to query |
| 136 | + * @returns {Promise<NeuronInfo>} Detailed information about the neuron |
| 137 | + * @throws {Error} If the neuron information cannot be retrieved |
| 138 | + */ |
| 139 | + public async getNeuronInfo(neuronId: bigint): Promise<NeuronInfo> { |
| 140 | + try { |
| 141 | + const governance = this.getGovernanceCanister(); |
| 142 | + const result = await governance.call('get_neuron_info', { neuron_id: neuronId }); |
| 143 | + return result; |
| 144 | + } catch (error) { |
| 145 | + const errorMessage = error instanceof Error ? error.message : 'Unknown error'; |
| 146 | + throw new Error(`Error getting neuron info for ${neuronId}: ${errorMessage}`); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Sets the followees for a neuron on a specific topic. |
| 152 | + * |
| 153 | + * @param {SetFolloweesParams} params - Parameters for setting followees |
| 154 | + * @returns {Promise<void>} |
| 155 | + * @throws {Error} If the followees cannot be set |
| 156 | + */ |
| 157 | + public async setFollowees(params: SetFolloweesParams): Promise<void> { |
| 158 | + try { |
| 159 | + const governance = this.getGovernanceCanister(); |
| 160 | + await governance.call('set_followees', params); |
| 161 | + } catch (error) { |
| 162 | + const errorMessage = error instanceof Error ? error.message : 'Unknown error'; |
| 163 | + throw new Error(`Error setting followees for neuron ${params.neuronId}: ${errorMessage}`); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Sets the dissolve delay for a neuron. |
| 169 | + * |
| 170 | + * @param {DissolveDelayParams} params - Parameters for setting dissolve delay |
| 171 | + * @returns {Promise<void>} |
| 172 | + * @throws {Error} If the dissolve delay cannot be set |
| 173 | + */ |
| 174 | + public async setDissolveDelay(params: DissolveDelayParams): Promise<void> { |
| 175 | + try { |
| 176 | + const governance = this.getGovernanceCanister(); |
| 177 | + await governance.call('set_dissolve_delay', params); |
| 178 | + } catch (error) { |
| 179 | + const errorMessage = error instanceof Error ? error.message : 'Unknown error'; |
| 180 | + throw new Error(`Error setting dissolve delay for neuron ${params.neuronId}: ${errorMessage}`); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Retrieves information about a specific proposal. |
| 186 | + * |
| 187 | + * @param {bigint} proposalId - The ID of the proposal to query |
| 188 | + * @returns {Promise<ProposalInfo>} Information about the proposal |
| 189 | + * @throws {Error} If the proposal information cannot be retrieved |
| 190 | + */ |
| 191 | + public async getProposal(proposalId: bigint): Promise<ProposalInfo> { |
| 192 | + try { |
| 193 | + const governance = this.getGovernanceCanister(); |
| 194 | + const result = await governance.call('get_proposal_info', { proposal_id: proposalId }); |
| 195 | + return result; |
| 196 | + } catch (error) { |
| 197 | + const errorMessage = error instanceof Error ? error.message : 'Unknown error'; |
| 198 | + throw new Error(`Error getting proposal info for ${proposalId}: ${errorMessage}`); |
| 199 | + } |
| 200 | + } |
90 | 201 | }
|
0 commit comments