@flipflop-sdk/node
Version:
FlipFlop Node.js SDK for programmatic token operations
122 lines • 5.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setUrc = void 0;
const web3_js_1 = require("@solana/web3.js");
const utils_1 = require("./utils");
const constants_1 = require("./constants");
const spl_token_1 = require("@solana/spl-token");
const config_1 = require("./config");
const setUrc = async (options) => {
// Validate required parameters
if (!options.rpc) {
return {
success: false,
message: "Missing rpc parameter",
};
}
if (!options.urc) {
return {
success: false,
message: "Missing urc parameter",
};
}
if (!options.mint) {
return {
success: false,
message: "Missing mint parameter",
};
}
if (!options.refAccount) {
return {
success: false,
message: "Missing ref-account parameter",
};
}
const rpc = new web3_js_1.Connection(options.rpc, "confirmed");
const urc = options.urc;
const mintAccount = new web3_js_1.PublicKey(options.mint);
try {
// Load keypair and create wallet (keypair-file takes priority)
const refAccount = options.refAccount;
const { program, provider, programId } = await (0, utils_1.initProvider)(rpc, refAccount);
const [referralAccount] = web3_js_1.PublicKey.findProgramAddressSync([
Buffer.from(constants_1.REFERRAL_SEED),
mintAccount.toBuffer(),
refAccount.publicKey.toBuffer(),
], programId);
const [systemConfigAccount] = web3_js_1.PublicKey.findProgramAddressSync([
Buffer.from(constants_1.SYSTEM_CONFIG_SEEDS),
new web3_js_1.PublicKey(config_1.CONFIGS[(0, config_1.getNetworkType)(options.rpc)].systemManagerAccount).toBuffer(),
], programId);
const [codeHash] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(constants_1.REFERRAL_CODE_SEED), Buffer.from(urc)], programId);
const [codeAccount] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(constants_1.CODE_ACCOUNT_SEED), codeHash.toBuffer()], programId);
const [configAccount] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(constants_1.CONFIG_DATA_SEED), mintAccount.toBuffer()], programId);
const codeAccountInfo = await provider.connection.getAccountInfo(codeAccount);
if (codeAccountInfo) {
const codeAccountData = await program.account.codeAccountData.fetch(codeAccount);
if (codeAccountData.referralAccount.toBase58() !==
referralAccount.toBase58()) {
return {
success: false,
message: "❌ Error: Referral code is already assigned to another account",
};
}
}
const referrerAta = await (0, spl_token_1.getAssociatedTokenAddress)(mintAccount, refAccount.publicKey, false, spl_token_1.TOKEN_PROGRAM_ID);
const referrerAtaInfo = await provider.connection.getAccountInfo(referrerAta);
const context = {
mint: mintAccount,
referralAccount: referralAccount,
configAccount,
systemConfigAccount: systemConfigAccount,
payer: refAccount.publicKey,
referrerAta: referrerAta,
codeAccount: codeAccount,
systemProgram: web3_js_1.SystemProgram.programId,
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
};
const tokenMetadata = await (0, utils_1.getMetadataByMint)(rpc, mintAccount);
if (!tokenMetadata.success) {
return {
success: false,
message: `Failed to get token metadata: ${tokenMetadata.message}`,
};
}
const _name = (0, utils_1.cleanTokenName)(tokenMetadata.data.name);
const _symbol = (0, utils_1.cleanTokenName)(tokenMetadata.data.symbol);
const instructionSetReferrerCode = await program.methods
.setReferrerCode(_name, _symbol, codeHash.toBuffer())
.accounts(context)
.instruction();
const transaction = new web3_js_1.Transaction();
if (!referrerAtaInfo) {
transaction.add((0, spl_token_1.createAssociatedTokenAccountInstruction)(refAccount.publicKey, referrerAta, refAccount.publicKey, mintAccount, spl_token_1.TOKEN_PROGRAM_ID));
}
transaction.add(instructionSetReferrerCode);
const tx = await provider.sendAndConfirm(transaction, [refAccount]);
const data = await program.account.tokenReferralData.fetch(referralAccount);
// Return structured data instead of console output
return {
success: true,
data: {
transactionHash: tx,
urc: urc,
mint: mintAccount,
referrer: refAccount.publicKey,
referrerTokenAccount: data.referrerAta,
codeHash: data.codeHash,
usageCount: data.usageCount,
activatedAt: data.activeTimestamp.toNumber(),
},
};
}
catch (error) {
return {
success: false,
message: `Failed to set URC: ${error instanceof Error ? error.message : "Unknown error"}`,
};
}
};
exports.setUrc = setUrc;
//# sourceMappingURL=set-urc.js.map