@ic-wallet-kit/hpl
Version:
Ic middleware wallet HPL protocol
337 lines (336 loc) • 13.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IngressActorWrapper = void 0;
const agent_1 = require("@dfinity/agent");
const principal_1 = require("@dfinity/principal");
const ingress_did_1 = require("../../candid/ingress/ingress.did");
const ingressActor_error_1 = require("../../errors/ingressActor.error");
class IngressActorWrapper {
actor;
logger;
constructor(actor, logger) {
this.actor = actor;
this.logger = logger;
}
static create(agent, canisterId, logger) {
const actor = IngressActorWrapper.getIngressActor(agent, canisterId);
return new IngressActorWrapper(actor, logger);
}
async getAccounts() {
try {
const accounts = await this.actor.nAccounts();
return accounts;
}
catch (e) {
if (e.result?.reject_message === "Unknown caller") {
return BigInt(0);
}
throw new ingressActor_error_1.IngressActorError("get.accounts", e.message);
}
}
async getVirtualAccounts() {
try {
const virtualAccounts = await this.actor.nVirtualAccounts();
return virtualAccounts;
}
catch (e) {
if (e.result?.reject_message === "Unknown caller") {
return BigInt(0);
}
throw new ingressActor_error_1.IngressActorError("get.virtual.accounts", e.message);
}
}
async getFtAssets() {
try {
const ftAssets = await this.actor.nFtAssets();
return ftAssets;
}
catch (e) {
throw new ingressActor_error_1.IngressActorError("get.ftAssets", e.message);
}
}
async getAllAccountsInfo(accounts) {
try {
const accountInfo = await this.actor.accountInfo({
idRange: [BigInt(0), [accounts - BigInt(1)]]
});
const result = accountInfo.map((ai) => {
return {
accountId: ai[0],
accountType: ai[1]
};
});
return result;
}
catch (e) {
if (e.result?.reject_message === "Unknown caller") {
return [];
}
throw new ingressActor_error_1.IngressActorError("get.all.accounts.info", e.message);
}
}
async getAccountInfo(account) {
try {
const accountInfo = await this.actor.accountInfo({
id: account
});
return accountInfo;
}
catch (e) {
if (e.result?.reject_message === "Unknown caller") {
return [];
}
throw new ingressActor_error_1.IngressActorError("get.accountInfo", e.message);
}
}
async getFtAssetInfo(ftAssets) {
try {
const ftInfo = await this.actor.ftInfo({
idRange: [BigInt(0), [ftAssets - BigInt(1)]]
});
return this.parseFtInfo(ftInfo);
}
catch (e) {
throw new ingressActor_error_1.IngressActorError("get.ftInfo", e.message);
}
}
async getAllVirtualAccountInfo(virtualAccounts) {
try {
const virtualAccountInfo = await this.actor.virtualAccountInfo({
idRange: [BigInt(0), [virtualAccounts - BigInt(1)]]
});
const result = virtualAccountInfo.map((vai) => {
return {
virtualAccountId: vai[0],
virtualAccountInfo: {
accountType: vai[1][0],
principal: vai[1][1].toString()
}
};
});
return result;
}
catch (e) {
throw new ingressActor_error_1.IngressActorError("get.virtualAccountInfo", e.message);
}
}
async getState(virtualAccounts, ftAssets, accounts, remotesToLook) {
try {
const state = await this.actor.state({
ftSupplies: ftAssets > BigInt(0) ? [{ idRange: [BigInt(0), [ftAssets - BigInt(1)]] }] : [],
virtualAccounts: virtualAccounts > BigInt(0) ? [{ idRange: [BigInt(0), [virtualAccounts - BigInt(1)]] }] : [],
accounts: accounts > BigInt(0) ? [{ idRange: [BigInt(0), [accounts - BigInt(1)]] }] : [],
remoteAccounts: remotesToLook,
});
return this.parseState(state);
}
catch (e) {
throw new ingressActor_error_1.IngressActorError("get.state", e.message);
}
}
async getVirtualAccountState(virtualAccountId) {
let result;
try {
const stateResult = await this.actor.state({
ftSupplies: [],
virtualAccounts: [{ id: virtualAccountId }],
accounts: [],
remoteAccounts: [],
});
const state = this.parseState(stateResult);
result = state.virtualAccounts.find((vt) => vt.virtualAccountId === virtualAccountId);
}
catch (e) {
throw new ingressActor_error_1.IngressActorError("get.virtualAccountState", e.message);
}
if (!result) {
throw new ingressActor_error_1.IngressActorError("get.virtualAccountState.not.found", "VirtualAccount not found");
}
return result;
}
async getAdminState() {
try {
const state = await this.actor.adminState({
ftSupplies: [],
virtualAccounts: [],
accounts: [{ idRange: [BigInt(0), []] }],
remoteAccounts: [],
});
return this.parseState(state);
}
catch (e) {
throw new ingressActor_error_1.IngressActorError("get.admin.state", e.message);
}
}
async openAccount(ftAssetId) {
try {
const response = await this.actor.openAccounts([{
ft: ftAssetId
}]);
if (response.ok && response.ok.first) {
return response.ok.first;
}
if (response.err?.InvalidArguments) {
throw new ingressActor_error_1.IngressActorError("open.account.error.invalid.arguments", response.err.InvalidArguments);
}
if (response.err?.NoSpaceForPrincipal) {
throw new ingressActor_error_1.IngressActorError("open.account.error.no.space.for.principal", response.err.NoSpaceForPrincipal);
}
if (response.err?.NoSpaceForSubaccount) {
throw new ingressActor_error_1.IngressActorError("open.account.error.no.space.for.subaccount", response.err.NoSpaceForSubaccount);
}
throw new ingressActor_error_1.IngressActorError("open.account", "open account actor error");
}
catch (e) {
throw new ingressActor_error_1.IngressActorError("open.account", e.message);
}
}
async openVirtualAccount(assetId, accountId, accessByPrincipal, amount, expiration) {
try {
const response = await this.actor.openVirtualAccounts([
[
{ ft: assetId },
accessByPrincipal,
{ ft: amount },
accountId,
expiration ? expiration : BigInt(0),
],
]);
if (response.ok && response.ok.first) {
return response.ok.first;
}
if (response.err?.InvalidArguments) {
throw new ingressActor_error_1.IngressActorError("open.virtual.account.error.invalid.arguments", response.err.InvalidArguments);
}
if (response.err?.NoSpaceForAccount) {
throw new ingressActor_error_1.IngressActorError("open.virtual.account.error.no.space.for.account", response.err.NoSpaceForAccount);
}
throw new ingressActor_error_1.IngressActorError("open.virtual.account", "open virtualAccount actor error");
}
catch (e) {
this.logger.logError(e, "IngressActorWrapper openVirtualAccount");
throw new ingressActor_error_1.IngressActorError("open.virtual.account", e.message);
}
}
async updateVirtualAccount(virtualAccountId, accountId, amount, expiration) {
try {
const response = await this.actor.updateVirtualAccounts([
[
virtualAccountId,
{
backingAccount: [accountId],
state: [{ ft_set: amount }],
expiration: expiration ? [expiration] : [],
},
],
]);
if (response.ok && response.ok[0] && response.ok[0].ft) {
return response.ok[0].ft;
}
if (response.err?.InvalidArguments) {
throw new ingressActor_error_1.IngressActorError("update.virtual.account.error.invalid.arguments", response.err.InvalidArguments);
}
if (response.err?.InsufficientFunds) {
throw new ingressActor_error_1.IngressActorError("update.virtual.account.error.insufficient.funds", response.err.InsufficientFunds);
}
if (response.err?.DeletedVirtualAccount) {
throw new ingressActor_error_1.IngressActorError("update.virtual.account.error.deleted", response.err.DeletedVirtualAccount);
}
throw new ingressActor_error_1.IngressActorError("update.virtual.account", "update virtualAccount actor error");
}
catch (e) {
throw new ingressActor_error_1.IngressActorError("update.virtual.account", e.message);
}
}
async deleteVirtualAccounts(virtualAccountId) {
try {
const response = await this.actor.deleteVirtualAccounts([virtualAccountId]);
if (response.ok?.length > 0 && response.ok) {
return response.ok;
}
if (response.err?.InvalidArguments) {
throw new ingressActor_error_1.IngressActorError("delete.virtual.account.error.invalid.arguments", response.err.InvalidArguments);
}
if (response.err?.DeletedVirtualAccount) {
throw new ingressActor_error_1.IngressActorError("delete.virtual.account.error", response.err.DeletedVirtualAccount);
}
throw new ingressActor_error_1.IngressActorError("delete.virtual.account", "Delete virtualAccount actor error");
}
catch (e) {
throw new ingressActor_error_1.IngressActorError("delete.virtual.account", e.message);
}
}
async remoteAccountInfo(selector) {
try {
const remoteAccountInfo = await this.actor.remoteAccountInfo(selector);
return remoteAccountInfo;
}
catch (e) {
throw new ingressActor_error_1.IngressActorError("get.remoteAccountInfo", e.message);
}
}
async feeRatio() {
try {
const feeConstant = await this.actor.feeRatio();
return feeConstant;
}
catch (e) {
throw new ingressActor_error_1.IngressActorError("get.fee.ratio", e.message);
}
}
parseState(state) {
const result = {
ftSupplies: state.ftSupplies.map((sup) => {
return {
assetId: sup[0],
ftSupply: sup[1]
};
}),
accounts: state.accounts.map((acc) => {
return {
accountId: acc[0],
accountState: acc[1]
};
}),
virtualAccounts: state.virtualAccounts.map((acc) => {
return {
virtualAccountId: acc[0],
accountState: acc[1][0],
accountId: acc[1][1],
time: acc[1][2]
};
}),
remoteAccounts: state.remoteAccounts.map((ra) => {
return {
remotePrincipal: ra[0][0].toString(),
remoteAccountId: ra[0][1],
accountState: ra[1][0],
time: ra[1][1]
};
})
};
return result;
}
parseFtInfo(ftAssets) {
const result = [];
ftAssets.forEach((ftAsset) => {
const item = {
assetId: ftAsset[0],
controller: ftAsset[1].controller.toString(),
decimals: ftAsset[1].decimals,
description: ftAsset[1].description
};
result.push(item);
});
return result;
}
static getIngressActor(agent, canister) {
const canisterId = principal_1.Principal.fromText(canister);
const ingressActor = agent_1.Actor.createActor(ingress_did_1.idlFactory, {
agent,
canisterId
});
return ingressActor;
}
}
exports.IngressActorWrapper = IngressActorWrapper;