@mean-dao/ddca
Version:
Typescript library to interact with the Decentralized DCA program
83 lines (82 loc) • 4.19 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.calculateActionFees = void 0;
const web3_js_1 = require("@solana/web3.js");
const _1 = require(".");
const calculateActionFees = (connection, action, swapsCount) => __awaiter(void 0, void 0, void 0, function* () {
let recentBlockhash = yield connection.getRecentBlockhash(connection.commitment);
let lamportsPerSignatureFee = recentBlockhash.feeCalculator.lamportsPerSignature;
let signaturesCount = 0;
let maxTotalRentExcemptInLamports = 0;
let totalAmountNeededForsSwapsInLamports = 0;
let flatFeeInLamports = 0;
let percentFee = 0;
const ddcaAccountSizeInBytes = 500; //TODO: calculate dynamically
const tokenAccountSizeInBytes = 165; //TODO: calculate dynamically
const minimumAccountSizeInBytes = 128; //Solana min account size (aka metadata)
/*
Note:
getMinimumBalanceForRentExemption(size_A)
+ getMinimumBalanceForRentExemption(size_B)
!= getMinimumBalanceForRentExemption(size_A + size_B)
There is a 128 bytes in metadata that is not included in the size param passed to
getMinimumBalanceForRentExemption
*/
switch (action) {
case _1.DDCA_ACTIONS.create: {
signaturesCount = 2; // owner + temp wrap account
maxTotalRentExcemptInLamports =
yield connection.getMinimumBalanceForRentExemption(ddcaAccountSizeInBytes + 3 * (tokenAccountSizeInBytes + minimumAccountSizeInBytes) + minimumAccountSizeInBytes); // 1 account + 3 token accounts + 1 wake account
totalAmountNeededForsSwapsInLamports = swapsCount * 20000000; //20 million
flatFeeInLamports = 0;
percentFee = 0;
break;
}
case _1.DDCA_ACTIONS.addFunds: {
signaturesCount = 2; // owner + temp wrap account
maxTotalRentExcemptInLamports = 0;
totalAmountNeededForsSwapsInLamports = swapsCount * 20000000; //20 million
flatFeeInLamports = 0;
percentFee = 0;
break;
}
case _1.DDCA_ACTIONS.withdraw: {
signaturesCount = 1;
maxTotalRentExcemptInLamports =
yield connection.getMinimumBalanceForRentExemption(tokenAccountSizeInBytes + 3 * (tokenAccountSizeInBytes + minimumAccountSizeInBytes)); // 4 token accounts
flatFeeInLamports = 0;
totalAmountNeededForsSwapsInLamports = 0;
percentFee = 0.5;
break;
}
case _1.DDCA_ACTIONS.close: {
signaturesCount = 1;
maxTotalRentExcemptInLamports =
yield connection.getMinimumBalanceForRentExemption(tokenAccountSizeInBytes + 3 * (tokenAccountSizeInBytes + minimumAccountSizeInBytes)); // 4 token accounts
flatFeeInLamports = 0;
totalAmountNeededForsSwapsInLamports = 0;
percentFee = 0.5;
break;
}
default: {
throw Error("Invalid DDCA action: " + action);
}
}
return {
maxBlockchainFee: (maxTotalRentExcemptInLamports + lamportsPerSignatureFee * signaturesCount) / web3_js_1.LAMPORTS_PER_SOL,
totalScheduledSwapsFees: totalAmountNeededForsSwapsInLamports / web3_js_1.LAMPORTS_PER_SOL,
flatFee: flatFeeInLamports,
percentFee: percentFee,
maxFeePerSwap: _1.MAX_FEE_PER_SWAP_IN_LAMPORTS / web3_js_1.LAMPORTS_PER_SOL
};
});
exports.calculateActionFees = calculateActionFees;