stellar-plus
Version:
beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain
159 lines (158 loc) • 9.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const index_1 = require("../../../index");
const account_1 = require("../../../stellar-plus/account");
const asset_1 = require("../../../stellar-plus/asset");
const channel_accounts_1 = require("../../../stellar-plus/channel-accounts");
const network_1 = require("../../../stellar-plus/network");
const stellar_test_ledger_1 = require("../../../stellar-plus/test/stellar-test-ledger");
const regex_1 = require("../../../stellar-plus/utils/regex");
const utils_1 = require("../../../tests/utils");
describe('Channel Accounts Plugin Use Case: ', () => {
const logLevel = 'TRACE';
const stellarTestLedger = new stellar_test_ledger_1.StellarTestLedger({
logLevel,
network: stellar_test_ledger_1.TestLedgerNetwork.LOCAL,
containerImageVersion: stellar_test_ledger_1.SupportedImageVersions.LASTEST,
});
let networkConfig;
beforeAll(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const container = yield stellarTestLedger.start();
expect(container).toBeDefined();
networkConfig = (0, network_1.CustomNet)(yield stellarTestLedger.getNetworkConfiguration());
expect(networkConfig.horizonUrl).toBeDefined();
expect(networkConfig.networkPassphrase).toBeDefined();
expect(networkConfig.rpcUrl).toBeDefined();
expect(networkConfig.friendbotUrl).toBeDefined();
}));
afterAll(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
yield stellarTestLedger.stop();
yield stellarTestLedger.destroy();
}));
describe('Given an issuer with lumens and a user', () => {
let issuer;
let user;
let userExpectedBalance;
let channels;
let classicCAPlugin;
let sorobanCAPlugin;
let feeBumpPlugin;
beforeAll(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
issuer = new account_1.DefaultAccountHandler({ networkConfig });
user = new account_1.DefaultAccountHandler({ networkConfig });
yield issuer.initializeWithFriendbot();
yield user.initializeWithFriendbot();
userExpectedBalance = 0;
}));
it('Can use the channel accounts helper tool to initiate 10 channels', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
channels = yield channel_accounts_1.ChannelAccounts.openChannels({
networkConfig,
numberOfChannels: 10,
txInvocation: (0, utils_1.simpleTxInvocation)(issuer),
sponsor: issuer,
});
expect(channels.length).toBe(10);
}));
it('Can prepare a FeebumpWrapper plugin to cover all fees', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const feebumpHeader = (0, utils_1.simpleTxInvocationToFeebump)((0, utils_1.simpleTxInvocation)(issuer));
feeBumpPlugin = new index_1.StellarPlus.Utils.Plugins.submitTransaction.FeeBumpWrapperPlugin(feebumpHeader);
expect(feeBumpPlugin).toBeDefined();
}));
describe('A Classic Asset Handler initialized with the issuer account handler and the plugin', () => {
let classicAsset;
beforeAll(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
classicCAPlugin = new index_1.StellarPlus.Utils.Plugins.classicTransaction.ClassicChannelAccountsPlugin({ channels });
expect(classicCAPlugin).toBeDefined();
classicAsset = new asset_1.ClassicAssetHandler({
code: 'SPLUSCLA',
issuerAccount: issuer,
networkConfig,
options: {
classicTransactionPipeline: {
plugins: [classicCAPlugin, feeBumpPlugin],
},
},
});
expect(classicAsset).toBeDefined();
}));
it('should be able to use the channels to run an add trustline and mint transaction', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
userExpectedBalance += 100;
yield expect(classicAsset.addTrustlineAndMint(Object.assign({ to: user.getPublicKey(), amount: 100 }, (0, utils_1.simpleTxInvocation)(user)))).toResolve();
yield expect(classicAsset.balance(user.getPublicKey())).resolves.toBe(userExpectedBalance);
}));
it('should be able to use the channels to run 50 mint transactions in parallel', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const promises = [];
userExpectedBalance += 10 * 50;
for (let i = 0; i < 50; i++) {
promises.push(expect(classicAsset.mint(Object.assign({ to: user.getPublicKey(), amount: 10 }, (0, utils_1.simpleTxInvocation)(issuer)))).toResolve());
}
yield Promise.all(promises);
yield expect(classicAsset.balance(user.getPublicKey())).resolves.toBe(userExpectedBalance);
}));
});
describe('A SAC Asset Handler initialized with the issuer account handler and the plugin', () => {
let sacAsset;
beforeAll(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
userExpectedBalance = 0;
classicCAPlugin = new index_1.StellarPlus.Utils.Plugins.classicTransaction.ClassicChannelAccountsPlugin({ channels });
sorobanCAPlugin = new index_1.StellarPlus.Utils.Plugins.sorobanTransaction.SorobanChannelAccountsPlugin({ channels });
expect(classicCAPlugin).toBeDefined();
expect(sorobanCAPlugin).toBeDefined();
sacAsset = new asset_1.SACHandler({
code: 'SPLUSSAC',
issuerAccount: issuer,
networkConfig,
options: {
classicTransactionPipeline: {
plugins: [classicCAPlugin, feeBumpPlugin],
},
sorobanTransactionPipeline: {
plugins: [sorobanCAPlugin, feeBumpPlugin],
},
},
});
expect(sacAsset).toBeDefined();
}));
it('should be able to use the channels to run an add trustline and mint transaction', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
userExpectedBalance += 100;
yield expect(sacAsset.classicHandler.addTrustlineAndMint(Object.assign({ to: user.getPublicKey(), amount: 100 }, (0, utils_1.simpleTxInvocation)(user)))).toResolve();
yield expect(sacAsset.classicHandler.balance(user.getPublicKey())).resolves.toBe(userExpectedBalance);
}));
it('should be able to use the channels to run 50 mint transactions in parallel', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const promises = [];
userExpectedBalance += 10 * 50;
for (let i = 0; i < 50; i++) {
promises.push(expect(sacAsset.classicHandler.mint(Object.assign({ to: user.getPublicKey(), amount: 10 }, (0, utils_1.simpleTxInvocation)(issuer)))).toResolve());
}
yield Promise.all(promises);
yield expect(sacAsset.classicHandler.balance(user.getPublicKey())).resolves.toBe(userExpectedBalance);
}));
it('should be able to wrap the asset in an SAC contract', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
yield expect(sacAsset.wrapAndDeploy((0, utils_1.simpleTxInvocation)(issuer))).toResolve();
expect(sacAsset.sorobanTokenHandler.getContractId()).toMatch(regex_1.contractIdRegex);
}));
describe('Given the asset has been wrapped and deployed with the SAC', () => {
it('should be able to use the channels to run 50 transfer soroban transactions in parallel', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const promises = [];
for (let i = 0; i < 50; i++) {
promises.push(expect(sacAsset.sorobanTokenHandler.transfer(Object.assign({ from: user.getPublicKey(), to: user.getPublicKey(), amount: BigInt(1) }, (0, utils_1.simpleTxInvocation)(user)))).toResolve());
}
yield Promise.all(promises);
yield expect(sacAsset.sorobanTokenHandler.balance(Object.assign({ id: user.getPublicKey() }, (0, utils_1.simpleTxInvocation)(user)))).resolves.toBe(BigInt(userExpectedBalance * 10 ** 7));
}));
});
});
describe('After all transactions', () => {
it('Can use the channel accounts helper tool to close 10 channels', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
yield expect(channel_accounts_1.ChannelAccounts.closeChannels({
networkConfig,
txInvocation: (0, utils_1.simpleTxInvocation)(issuer),
sponsor: issuer,
channels,
})).toResolve();
}));
});
});
});