Skip to content

Commit 7ca69b9

Browse files
committed
fix(sdk-coin-icp): update testnet ledger canister details
Ticket: WIN-7912 TICKET: WIN-7912
1 parent 2959566 commit 7ca69b9

File tree

6 files changed

+42
-12
lines changed

6 files changed

+42
-12
lines changed

modules/sdk-coin-icp/src/icp.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
TssVerifyAddressOptions,
2121
VerifyTransactionOptions,
2222
} from '@bitgo/sdk-core';
23-
import { coins, BaseCoin as StaticsBaseCoin } from '@bitgo/statics';
23+
import { coins, NetworkType, BaseCoin as StaticsBaseCoin } from '@bitgo/statics';
2424
import { Principal } from '@dfinity/principal';
2525
import axios from 'axios';
2626
import BigNumber from 'bignumber.js';
@@ -30,6 +30,7 @@ import * as mpc from '@bitgo/sdk-lib-mpc';
3030
import {
3131
CurveType,
3232
LEDGER_CANISTER_ID,
33+
TESTNET_LEDGER_CANISTER_ID,
3334
PayloadsData,
3435
PUBLIC_NODE_REQUEST_ENDPOINT,
3536
PublicNodeSubmitResponse,
@@ -249,7 +250,9 @@ export class Icp extends BaseCoin {
249250

250251
private getPublicNodeBroadcastEndpoint(): string {
251252
const nodeUrl = this.getPublicNodeUrl();
252-
const principal = Principal.fromUint8Array(LEDGER_CANISTER_ID);
253+
const ledgerCanisterId =
254+
this._staticsCoin.network.type === NetworkType.TESTNET ? TESTNET_LEDGER_CANISTER_ID : LEDGER_CANISTER_ID;
255+
const principal = Principal.fromUint8Array(ledgerCanisterId);
253256
const canisterIdHex = principal.toText();
254257
const endpoint = `${nodeUrl}${PUBLIC_NODE_REQUEST_ENDPOINT}${canisterIdHex}/call`;
255258
return endpoint;
@@ -263,7 +266,7 @@ export class Icp extends BaseCoin {
263266
*/
264267
protected async getAccountBalance(publicKeyHex: string): Promise<BigNumber> {
265268
const principalId = utils.getPrincipalIdFromPublicKey(publicKeyHex).toText();
266-
const agent = new IcpAgent(this.getPublicNodeUrl());
269+
const agent = new IcpAgent(this.getPublicNodeUrl(), this._staticsCoin);
267270
return agent.getBalance(principalId);
268271
}
269272

@@ -277,7 +280,7 @@ export class Icp extends BaseCoin {
277280
* @throws Will propagate any errors encountered while communicating with the ICP node.
278281
*/
279282
protected async getFeeData(): Promise<BigNumber> {
280-
const agent = new IcpAgent(this.getPublicNodeUrl());
283+
const agent = new IcpAgent(this.getPublicNodeUrl(), this._staticsCoin);
281284
return await agent.getFee();
282285
}
283286

modules/sdk-coin-icp/src/lib/icpAgent.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import { Principal } from '@dfinity/principal';
22
import { HttpAgent, replica, AgentCanister } from 'ic0';
33
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+
TESTNET_LEDGER_CANISTER_ID,
8+
ICRC1_FEE_KEY,
9+
METADATA_CALL,
10+
DEFAULT_SUBACCOUNT,
11+
} from './iface';
512
import BigNumber from 'bignumber.js';
13+
import { NetworkType, BaseCoin as StaticsBaseCoin } from '@bitgo/statics';
614

715
export class IcpAgent {
816
private readonly host: string;
17+
private readonly staticsCoin: Readonly<StaticsBaseCoin>;
918

10-
constructor(host: string) {
19+
constructor(host: string, staticsCoin: Readonly<StaticsBaseCoin>) {
1120
this.host = host;
21+
this.staticsCoin = staticsCoin;
1222
}
1323

1424
/**
@@ -35,7 +45,9 @@ export class IcpAgent {
3545
private getLedger(): AgentCanister {
3646
const agent = this.createAgent();
3747
const ic = replica(agent, { local: true });
38-
return ic(Principal.fromUint8Array(LEDGER_CANISTER_ID).toText());
48+
const ledgerCanisterId =
49+
this.staticsCoin.network.type === NetworkType.TESTNET ? TESTNET_LEDGER_CANISTER_ID : LEDGER_CANISTER_ID;
50+
return ic(Principal.fromUint8Array(ledgerCanisterId).toText());
3951
}
4052

4153
/**

modules/sdk-coin-icp/src/lib/iface.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
export const MAX_INGRESS_TTL = 5 * 60 * 1000_000_000; // 5 minutes in nanoseconds
77
export const PERMITTED_DRIFT = 60 * 1000_000_000; // 60 seconds in nanoseconds
88
export const LEDGER_CANISTER_ID = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 2, 1, 1]); // Uint8Array value for "00000000000000020101" and the string value is "ryjl3-tyaaa-aaaaa-aaaba-cai"
9+
export const TESTNET_LEDGER_CANISTER_ID = new Uint8Array([0, 0, 0, 0, 1, 0, 130, 251, 1, 1]); // Uint8Array value for "00000000010082fb0101" and the string value is "xafvr-biaaa-aaaai-aql5q-cai"
910
export const ROOT_PATH = 'm/0';
1011
export const ACCOUNT_BALANCE_CALL = 'icrc1_balance_of';
1112
export const PUBLIC_NODE_REQUEST_ENDPOINT = '/api/v3/canister/';
@@ -36,7 +37,11 @@ export enum MethodName {
3637
}
3738

3839
export enum Network {
39-
ID = '00000000000000020101', // ICP does not have different network IDs for mainnet and testnet
40+
ID = '00000000000000020101',
41+
}
42+
43+
export enum TestNetwork {
44+
ID = '00000000000000020101',
4045
}
4146

4247
export interface IcpTransactionData {

modules/sdk-coin-icp/src/lib/transferBuilder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ export class TransferBuilder extends TransactionBuilder {
3838
}
3939
this.validateTransaction(this._transaction);
4040
this.buildIcpTransactionData();
41-
const unsignedTransactionBuilder = new UnsignedTransactionBuilder(this._transaction.icpTransaction);
41+
const unsignedTransactionBuilder = new UnsignedTransactionBuilder(
42+
this._transaction.icpTransaction,
43+
this._coinConfig
44+
);
4245
const payloadsData = await unsignedTransactionBuilder.getUnsignedTransaction();
4346
this._transaction.payloadsData = payloadsData;
4447
return this._transaction;

modules/sdk-coin-icp/src/lib/unsignedTransactionBuilder.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ import {
1010
MAX_INGRESS_TTL,
1111
PERMITTED_DRIFT,
1212
LEDGER_CANISTER_ID,
13+
TESTNET_LEDGER_CANISTER_ID,
1314
} from './iface';
1415
import utils from './utils';
16+
import { NetworkType, BaseCoin as StaticsBaseCoin } from '@bitgo/statics';
1517

1618
export class UnsignedTransactionBuilder {
1719
private _icpTransactionPayload: IcpTransaction;
18-
constructor(icpTransactionPayload: IcpTransaction) {
20+
private readonly _staticsCoin: Readonly<StaticsBaseCoin>;
21+
22+
constructor(icpTransactionPayload: IcpTransaction, staticsCoin: Readonly<StaticsBaseCoin>) {
1923
this._icpTransactionPayload = icpTransactionPayload;
24+
this._staticsCoin = staticsCoin;
2025
}
2126

2227
async getUnsignedTransaction(): Promise<PayloadsData> {
@@ -104,7 +109,9 @@ export class UnsignedTransactionBuilder {
104109
async getUpdate(sendArgs: SendArgs, publicKeyHex: string): Promise<HttpCanisterUpdate> {
105110
const principalId = utils.getPrincipalIdFromPublicKey(publicKeyHex).toUint8Array();
106111
const senderBlob = Buffer.from(principalId);
107-
const canisterIdBuffer = Buffer.from(LEDGER_CANISTER_ID);
112+
const ledgerCanisterId =
113+
this._staticsCoin.network.type === NetworkType.TESTNET ? TESTNET_LEDGER_CANISTER_ID : LEDGER_CANISTER_ID;
114+
const canisterIdBuffer = Buffer.from(ledgerCanisterId);
108115
const args = await utils.toArg(sendArgs);
109116
const update: HttpCanisterUpdate = {
110117
canister_id: canisterIdBuffer,

modules/sdk-core/src/bitgo/environments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ const testnetBase: EnvironmentTemplate = {
363363
flrExplorerBaseUrl: 'https://coston2-explorer.flare.network',
364364
xdcExplorerBaseUrl: 'https://api.etherscan.io/v2',
365365
sgbExplorerBaseUrl: 'https://coston-explorer.flare.network',
366-
icpNodeUrl: 'https://exchanges.testnet.dfinity.network',
366+
icpNodeUrl: 'https://ic0.app',
367367
monExplorerBaseUrl: 'https://api.etherscan.io/v2',
368368
worldExplorerBaseUrl: 'https://sepolia.worldscan.org/',
369369
somniaExplorerBaseUrl: 'https://shannon-explorer.somnia.network/',

0 commit comments

Comments
 (0)