@unique.vc/udm.js
Version:
UDM - Unique Delegation Manager is a simple yet efficient solution allowing to bridge self-custody and user experience, have best from both worlds and potentially extend the reach of the industry.
194 lines (193 loc) • 8.35 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkIfDelegationExists = exports.getDelegationAddress = exports.getAllRepresentative = exports.getAllMasters = exports.getMastersForPublicKey = exports.getRepresentativeForPublicKey = exports.getAllMastersTokenAccountsWithSpecifiedMint = exports.checkNumberOfTokenAccounts = void 0;
const web3_js_1 = require("@solana/web3.js");
const constants_1 = require("./common/constants");
const checkNumberOfTokenAccounts = (connection, representative, master, mint) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
try {
let mastersTokenAccounts;
const tokenAccountsForPublicKey = yield connection.getParsedTokenAccountsByOwner(representative, {
mint: mint,
});
try {
mastersTokenAccounts = (_a = (yield (0, exports.getAllMastersTokenAccountsWithSpecifiedMint)(connection, representative, master, mint))) === null || _a === void 0 ? void 0 : _a.value.length;
}
catch (error) {
mastersTokenAccounts = undefined;
}
return {
numberOfRepresentativeTokenAccounts: tokenAccountsForPublicKey.value.length,
numberOfMasterTokenAccounts: mastersTokenAccounts,
};
}
catch (error) {
console.log(error);
throw error;
}
});
exports.checkNumberOfTokenAccounts = checkNumberOfTokenAccounts;
const getAllMastersTokenAccountsWithSpecifiedMint = (connection, representative, master, mint) => __awaiter(void 0, void 0, void 0, function* () {
try {
const program = (0, constants_1.programFactory)(connection);
const [delegation] = web3_js_1.PublicKey.findProgramAddressSync([constants_1.authorizeSeed, master.toBuffer(), representative.toBuffer()], program.programId);
const delegationInfo = yield program.account.delegation.fetch(delegation);
if (!delegationInfo) {
throw new Error('Delegation does not exist');
}
if (!delegationInfo.authorised) {
throw new Error('You need to confirm delegation in order to represent this master');
}
const tokenAccounts = yield connection.getParsedTokenAccountsByOwner(master, {
mint: mint,
});
return tokenAccounts;
}
catch (error) {
console.log(error);
throw error;
}
});
exports.getAllMastersTokenAccountsWithSpecifiedMint = getAllMastersTokenAccountsWithSpecifiedMint;
const getRepresentativeForPublicKey = (publicKey, connection) => __awaiter(void 0, void 0, void 0, function* () {
try {
const representations = [];
const program = (0, constants_1.programFactory)(connection);
const allDelegationsAccounts = yield program.account.delegation.all([
{
memcmp: {
offset: 8,
bytes: publicKey.toBase58(),
},
},
]);
allDelegationsAccounts.forEach((item) => {
item.account.authorised &&
representations.push({
authorised: item.account.authorised,
master: item.account.master,
representative: item.account.representative,
});
});
return representations;
}
catch (error) {
console.log(error);
throw error;
}
});
exports.getRepresentativeForPublicKey = getRepresentativeForPublicKey;
const getMastersForPublicKey = (publicKey, connection) => __awaiter(void 0, void 0, void 0, function* () {
try {
const representations = [];
const program = (0, constants_1.programFactory)(connection);
const allDelegationsAccounts = yield program.account.delegation.all([
{
memcmp: {
offset: 8 + 32,
bytes: publicKey.toBase58(),
},
},
]);
allDelegationsAccounts.forEach((item) => {
item.account.authorised &&
representations.push({
authorised: item.account.authorised,
master: item.account.master,
representative: item.account.representative,
});
});
return representations;
}
catch (error) {
console.log(error);
throw error;
}
});
exports.getMastersForPublicKey = getMastersForPublicKey;
const getAllMasters = (connection) => __awaiter(void 0, void 0, void 0, function* () {
try {
const representations = [];
const program = (0, constants_1.programFactory)(connection);
const allDelegationsAccounts = yield program.account.delegation.all();
allDelegationsAccounts.forEach((item) => {
item.account.authorised &&
representations.push({
authorised: item.account.authorised,
master: item.account.master,
representative: item.account.representative,
});
});
return representations;
}
catch (error) {
console.log(error);
throw error;
}
});
exports.getAllMasters = getAllMasters;
const getAllRepresentative = (connection) => __awaiter(void 0, void 0, void 0, function* () {
try {
const representations = [];
const program = (0, constants_1.programFactory)(connection);
const allDelegationsAccounts = yield program.account.delegation.all();
allDelegationsAccounts.forEach((item) => {
item.account.authorised &&
representations.push({
authorised: item.account.authorised,
master: item.account.master,
representative: item.account.representative,
});
});
return representations;
}
catch (error) {
console.log(error);
throw error;
}
});
exports.getAllRepresentative = getAllRepresentative;
const getDelegationAddress = (connection, master, representative) => {
const program = (0, constants_1.programFactory)(connection);
const [delegation] = web3_js_1.PublicKey.findProgramAddressSync([constants_1.authorizeSeed, master.toBuffer(), representative.toBuffer()], program.programId);
return delegation;
};
exports.getDelegationAddress = getDelegationAddress;
const checkIfDelegationExists = (connection, master, representative) => __awaiter(void 0, void 0, void 0, function* () {
try {
const program = (0, constants_1.programFactory)(connection);
const delegationInfo = yield program.account.delegation.fetch((0, exports.getDelegationAddress)(connection, master, representative));
if (!delegationInfo) {
throw new Error('Delegation does not exist');
}
if (!delegationInfo.authorised) {
throw new Error('Delegation not authorized');
}
return {
authorised: delegationInfo.authorised,
master: delegationInfo.master,
representative: delegationInfo.representative,
};
}
catch (error) {
throw error;
}
});
exports.checkIfDelegationExists = checkIfDelegationExists;
exports.default = {
getMastersForPublicKey: exports.getMastersForPublicKey,
getRepresentativeForPublicKey: exports.getRepresentativeForPublicKey,
getAllMasters: exports.getAllMasters,
getAllRepresentative: exports.getAllRepresentative,
getAllMastersTokenAccountsWithSpecifiedMint: exports.getAllMastersTokenAccountsWithSpecifiedMint,
checkNumberOfTokenAccounts: exports.checkNumberOfTokenAccounts,
};