@flashbacktech/flashbackclient 0.1.33 → 0.1.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/stellarv2/client/bucket.d.ts +111 -0
- package/dist/stellarv2/client/bucket.js +272 -0
- package/dist/stellarv2/client/consumer.d.ts +60 -0
- package/dist/stellarv2/client/consumer.js +149 -0
- package/dist/stellarv2/client/deal.d.ts +160 -0
- package/dist/stellarv2/client/deal.js +356 -0
- package/dist/stellarv2/client/funding.d.ts +46 -0
- package/dist/stellarv2/client/funding.js +78 -0
- package/dist/stellarv2/client/index.d.ts +134 -0
- package/dist/stellarv2/client/index.js +240 -0
- package/dist/stellarv2/client/provider.d.ts +72 -0
- package/dist/stellarv2/client/provider.js +178 -0
- package/dist/stellarv2/index.d.ts +6 -6
- package/dist/stellarv2/index.js +5 -5
- package/dist/stellarv2/utils/decorator.d.ts +2 -0
- package/dist/stellarv2/utils/decorator.js +30 -0
- package/dist/stellarv2/utils/lib.d.ts +13 -0
- package/dist/stellarv2/utils/lib.js +26 -0
- package/dist/stellarv2/utils/timing.d.ts +1 -0
- package/dist/stellarv2/utils/timing.js +19 -0
- package/dist/stellarv2/wallet/balance.d.ts +2 -0
- package/dist/stellarv2/wallet/balance.js +9 -0
- package/dist/stellarv2/wallet/batch.d.ts +44 -0
- package/dist/stellarv2/wallet/batch.js +77 -0
- package/dist/stellarv2/wallet/index.d.ts +4 -0
- package/dist/stellarv2/wallet/index.js +20 -0
- package/dist/stellarv2/wallet/token.d.ts +3 -0
- package/dist/stellarv2/wallet/token.js +23 -0
- package/dist/stellarv2/wallet/transaction.d.ts +38 -0
- package/dist/stellarv2/wallet/transaction.js +224 -0
- package/package.json +4 -1
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withSignature = withSignature;
|
|
4
|
+
exports.withSignatureProperty = withSignatureProperty;
|
|
5
|
+
function withSignature(method) {
|
|
6
|
+
return async function (...args) {
|
|
7
|
+
const context = this.getContext();
|
|
8
|
+
if (!context.signTransaction) {
|
|
9
|
+
throw new Error("FlashOnStellarClient: signTransaction method is required for write operations");
|
|
10
|
+
}
|
|
11
|
+
return method.apply(this, args);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
// For arrow functions/properties
|
|
15
|
+
function withSignatureProperty(target, propertyKey) {
|
|
16
|
+
const value = target[propertyKey];
|
|
17
|
+
Object.defineProperty(target, propertyKey, {
|
|
18
|
+
configurable: true,
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get() {
|
|
21
|
+
return async (...args) => {
|
|
22
|
+
const context = this.getContext();
|
|
23
|
+
if (!context.signTransaction) {
|
|
24
|
+
throw new Error("FlashOnStellarClient: signTransaction method is required for write operations");
|
|
25
|
+
}
|
|
26
|
+
return value.apply(this, args);
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type Environment = "node" | "browser";
|
|
2
|
+
/**
|
|
3
|
+
* Detects the current runtime environment
|
|
4
|
+
*/
|
|
5
|
+
export declare function getEnvironment(): Environment;
|
|
6
|
+
/**
|
|
7
|
+
* Checks if the code is running in a browser environment
|
|
8
|
+
*/
|
|
9
|
+
export declare function isBrowser(): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Checks if the code is running in a Node.js environment
|
|
12
|
+
*/
|
|
13
|
+
export declare function isNode(): boolean;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEnvironment = getEnvironment;
|
|
4
|
+
exports.isBrowser = isBrowser;
|
|
5
|
+
exports.isNode = isNode;
|
|
6
|
+
/**
|
|
7
|
+
* Detects the current runtime environment
|
|
8
|
+
*/
|
|
9
|
+
function getEnvironment() {
|
|
10
|
+
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
11
|
+
return "browser";
|
|
12
|
+
}
|
|
13
|
+
return "node";
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Checks if the code is running in a browser environment
|
|
17
|
+
*/
|
|
18
|
+
function isBrowser() {
|
|
19
|
+
return getEnvironment() === "browser";
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Checks if the code is running in a Node.js environment
|
|
23
|
+
*/
|
|
24
|
+
function isNode() {
|
|
25
|
+
return getEnvironment() === "node";
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const sleep: (ms: number) => Promise<void>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sleep = void 0;
|
|
4
|
+
const getGlobalThis = () => {
|
|
5
|
+
if (typeof globalThis !== "undefined")
|
|
6
|
+
return globalThis;
|
|
7
|
+
if (typeof self !== "undefined")
|
|
8
|
+
return self;
|
|
9
|
+
if (typeof window !== "undefined")
|
|
10
|
+
return window;
|
|
11
|
+
if (typeof global !== "undefined")
|
|
12
|
+
return global;
|
|
13
|
+
throw new Error("Unable to locate global object");
|
|
14
|
+
};
|
|
15
|
+
const sleep = (ms) => {
|
|
16
|
+
const globalScope = getGlobalThis();
|
|
17
|
+
return new Promise((resolve) => globalScope.setTimeout(resolve, ms));
|
|
18
|
+
};
|
|
19
|
+
exports.sleep = sleep;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getBalances = getBalances;
|
|
4
|
+
const transaction_1 = require("./transaction");
|
|
5
|
+
async function getBalances(accountAddress, network) {
|
|
6
|
+
const server = (0, transaction_1.getHorizonServer)(network);
|
|
7
|
+
const account = await server.loadAccount(accountAddress);
|
|
8
|
+
return account.balances;
|
|
9
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ClientContext } from "../client/index";
|
|
2
|
+
export type ContractArg = {
|
|
3
|
+
value: string | number | bigint | boolean | null | undefined;
|
|
4
|
+
type: "string" | "symbol" | "address" | "u32" | "i32" | "u64" | "i64" | "bool";
|
|
5
|
+
};
|
|
6
|
+
export type ReadOperation<T> = {
|
|
7
|
+
method: string;
|
|
8
|
+
getArgs: (wallet_address: string, ...extraParams: any[]) => ContractArg[];
|
|
9
|
+
transformResult: (result: any) => T;
|
|
10
|
+
};
|
|
11
|
+
export type BatchedOperation<T> = {
|
|
12
|
+
operation: string;
|
|
13
|
+
params: any[];
|
|
14
|
+
transform: (result: any) => T;
|
|
15
|
+
};
|
|
16
|
+
export type BatchedWriteOperation = {
|
|
17
|
+
operation: string;
|
|
18
|
+
params: any[];
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Executes multiple read operations in a single transaction
|
|
22
|
+
* @param context The client context
|
|
23
|
+
* @param wallet_address The wallet address to use for the operations
|
|
24
|
+
* @param operations Array of operations with their parameters to execute
|
|
25
|
+
* @returns Array of results in the same order as the input operations
|
|
26
|
+
*/
|
|
27
|
+
export declare function batchReadOperations<T>(context: ClientContext, wallet_address: string, operations: BatchedOperation<T>[]): Promise<T[]>;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a read operation with the given method and transform function
|
|
30
|
+
* @param method The contract method to call
|
|
31
|
+
* @param getArgs Function that generates the arguments for the method
|
|
32
|
+
* @param transformResult Function that transforms the raw result into the expected type
|
|
33
|
+
* @returns A ReadOperation object
|
|
34
|
+
*/
|
|
35
|
+
export declare function createReadOperation<T>(method: string, getArgs: (wallet_address: string, ...extraParams: any[]) => ContractArg[], transformResult: (result: any) => T): ReadOperation<T>;
|
|
36
|
+
export declare function createOperationFromMethod<T>(method: string, transformResult: (result: any) => T): ReadOperation<T>;
|
|
37
|
+
/**
|
|
38
|
+
* Executes multiple write operations in a single transaction
|
|
39
|
+
* @param context The client context
|
|
40
|
+
* @param wallet_address The wallet address to use for the operations
|
|
41
|
+
* @param operations Array of operations with their parameters to execute
|
|
42
|
+
* @returns void
|
|
43
|
+
*/
|
|
44
|
+
export declare function batchWriteOperations(context: ClientContext, wallet_address: string, operations: BatchedWriteOperation[]): Promise<void>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.batchReadOperations = batchReadOperations;
|
|
4
|
+
exports.createReadOperation = createReadOperation;
|
|
5
|
+
exports.createOperationFromMethod = createOperationFromMethod;
|
|
6
|
+
exports.batchWriteOperations = batchWriteOperations;
|
|
7
|
+
const transaction_1 = require("./transaction");
|
|
8
|
+
/**
|
|
9
|
+
* Executes multiple read operations in a single transaction
|
|
10
|
+
* @param context The client context
|
|
11
|
+
* @param wallet_address The wallet address to use for the operations
|
|
12
|
+
* @param operations Array of operations with their parameters to execute
|
|
13
|
+
* @returns Array of results in the same order as the input operations
|
|
14
|
+
*/
|
|
15
|
+
async function batchReadOperations(context, wallet_address, operations) {
|
|
16
|
+
const contractCalls = operations.map(({ operation, params }) => ({
|
|
17
|
+
method: operation,
|
|
18
|
+
args: [
|
|
19
|
+
{ value: wallet_address, type: "address" },
|
|
20
|
+
...params.map((param) => ({ value: param, type: "string" })),
|
|
21
|
+
],
|
|
22
|
+
}));
|
|
23
|
+
const response = await (0, transaction_1.prepareTransaction)(context, wallet_address, contractCalls);
|
|
24
|
+
if (!response.isSuccess || !response.isReadOnly) {
|
|
25
|
+
throw new Error("Batch read operation failed");
|
|
26
|
+
}
|
|
27
|
+
const results = Array.isArray(response.result)
|
|
28
|
+
? response.result
|
|
29
|
+
: [response.result];
|
|
30
|
+
return operations.map(({ transform }, index) => transform(results[index]));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates a read operation with the given method and transform function
|
|
34
|
+
* @param method The contract method to call
|
|
35
|
+
* @param getArgs Function that generates the arguments for the method
|
|
36
|
+
* @param transformResult Function that transforms the raw result into the expected type
|
|
37
|
+
* @returns A ReadOperation object
|
|
38
|
+
*/
|
|
39
|
+
function createReadOperation(method, getArgs, transformResult) {
|
|
40
|
+
return {
|
|
41
|
+
method,
|
|
42
|
+
getArgs,
|
|
43
|
+
transformResult,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function createOperationFromMethod(method, transformResult) {
|
|
47
|
+
return createReadOperation(method, (wallet_address, ...extraParams) => {
|
|
48
|
+
// First arg is always wallet_address
|
|
49
|
+
const args = [{ value: wallet_address, type: "address" }];
|
|
50
|
+
// Add any extra params as strings (or we could make this more sophisticated)
|
|
51
|
+
extraParams.forEach((param) => {
|
|
52
|
+
args.push({ value: param, type: "string" });
|
|
53
|
+
});
|
|
54
|
+
return args;
|
|
55
|
+
}, transformResult);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Executes multiple write operations in a single transaction
|
|
59
|
+
* @param context The client context
|
|
60
|
+
* @param wallet_address The wallet address to use for the operations
|
|
61
|
+
* @param operations Array of operations with their parameters to execute
|
|
62
|
+
* @returns void
|
|
63
|
+
*/
|
|
64
|
+
async function batchWriteOperations(context, wallet_address, operations) {
|
|
65
|
+
const contractCalls = operations.map(({ operation, params }) => ({
|
|
66
|
+
method: operation,
|
|
67
|
+
args: [
|
|
68
|
+
{ value: wallet_address, type: "address" },
|
|
69
|
+
...params.map((param) => ({ value: param, type: "string" })),
|
|
70
|
+
],
|
|
71
|
+
}));
|
|
72
|
+
const response = await (0, transaction_1.prepareTransaction)(context, wallet_address, contractCalls);
|
|
73
|
+
if (response.isSuccess && !response.isReadOnly) {
|
|
74
|
+
const signedTxXDR = await context.signTransaction(response.result);
|
|
75
|
+
await (0, transaction_1.sendTransaction)(context, signedTxXDR);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./token"), exports);
|
|
18
|
+
__exportStar(require("./transaction"), exports);
|
|
19
|
+
__exportStar(require("./balance"), exports);
|
|
20
|
+
__exportStar(require("./batch"), exports);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.changeTrustXDR = void 0;
|
|
4
|
+
const stellar_sdk_1 = require("@stellar/stellar-sdk");
|
|
5
|
+
const transaction_1 = require("./transaction");
|
|
6
|
+
const changeTrustXDR = async (network, source, issuerPublikKey, asset_ticker, bRemove) => {
|
|
7
|
+
const server = (0, transaction_1.getServer)(network);
|
|
8
|
+
const account = await server.getAccount(source);
|
|
9
|
+
const asset = new stellar_sdk_1.Asset(asset_ticker, issuerPublikKey);
|
|
10
|
+
const changeParams = bRemove ? { limit: "0" } : {};
|
|
11
|
+
const transaction = new stellar_sdk_1.TransactionBuilder(account, {
|
|
12
|
+
fee: stellar_sdk_1.BASE_FEE,
|
|
13
|
+
networkPassphrase: network.networkPassphrase,
|
|
14
|
+
})
|
|
15
|
+
.addOperation(stellar_sdk_1.Operation.changeTrust({
|
|
16
|
+
asset,
|
|
17
|
+
...changeParams,
|
|
18
|
+
}))
|
|
19
|
+
.setTimeout(30)
|
|
20
|
+
.build();
|
|
21
|
+
return transaction.toXDR();
|
|
22
|
+
};
|
|
23
|
+
exports.changeTrustXDR = changeTrustXDR;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Transaction, Memo, MemoType, Operation, FeeBumpTransaction, Horizon } from "@stellar/stellar-sdk";
|
|
2
|
+
import { rpc } from "@stellar/stellar-sdk";
|
|
3
|
+
import { ClientContext } from "../client";
|
|
4
|
+
export interface StellarNetwork {
|
|
5
|
+
network: string;
|
|
6
|
+
networkPassphrase: string;
|
|
7
|
+
}
|
|
8
|
+
declare const getNetwork: (network: string) => StellarNetwork;
|
|
9
|
+
declare const getPublicKeyFromPrivateKey: (privateKey: string) => string;
|
|
10
|
+
declare const getServer: (network: StellarNetwork) => rpc.Server;
|
|
11
|
+
interface ContractMethodCall {
|
|
12
|
+
method: string;
|
|
13
|
+
args?: Array<{
|
|
14
|
+
value: number | string | bigint | boolean | null | undefined | Array<unknown>;
|
|
15
|
+
type: "u32" | "i32" | "u64" | "i64" | "u128" | "i128" | "string" | "symbol" | "address" | "bool" | "vec";
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
interface ContractMethodResponse {
|
|
19
|
+
isSuccess: boolean;
|
|
20
|
+
isReadOnly: boolean;
|
|
21
|
+
result: string | unknown;
|
|
22
|
+
}
|
|
23
|
+
export declare const getHorizonServer: (network: string) => Horizon.Server;
|
|
24
|
+
export declare const executeWalletTransaction: (context: ClientContext, wallet_address: string, method: string, additionalArgs?: Array<{
|
|
25
|
+
value: string | number | bigint | boolean | null | Array<unknown> | undefined;
|
|
26
|
+
type: "string" | "symbol" | "address" | "u32" | "i32" | "u64" | "i64" | "u128" | "i128" | "bool" | "vec";
|
|
27
|
+
}>) => Promise<ContractMethodResponse>;
|
|
28
|
+
export declare const executeMultiWalletTransactions: (context: ClientContext, wallet_address: string, methods: Array<{
|
|
29
|
+
method: string;
|
|
30
|
+
additionalArgs?: Array<{
|
|
31
|
+
value: string | number | bigint | boolean | null | undefined;
|
|
32
|
+
type: "string" | "symbol" | "address" | "u32" | "i32" | "u64" | "i64" | "u128" | "i128" | "bool" | "vec";
|
|
33
|
+
}>;
|
|
34
|
+
}>) => Promise<ContractMethodResponse>;
|
|
35
|
+
declare const prepareTransaction: (context: ClientContext, address: string, contractCalls: ContractMethodCall | ContractMethodCall[]) => Promise<ContractMethodResponse>;
|
|
36
|
+
declare const signTransaction: (context: ClientContext, xdrToSign: string, privateKey: string) => Promise<Transaction<Memo<MemoType>, Operation[]> | FeeBumpTransaction>;
|
|
37
|
+
declare const sendTransaction: (context: ClientContext, signedTransactionXDR: string, bDebug?: boolean) => Promise<any>;
|
|
38
|
+
export { prepareTransaction, sendTransaction, signTransaction, getNetwork, getPublicKeyFromPrivateKey, getServer, };
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getServer = exports.getPublicKeyFromPrivateKey = exports.getNetwork = exports.signTransaction = exports.sendTransaction = exports.prepareTransaction = exports.executeMultiWalletTransactions = exports.executeWalletTransaction = exports.getHorizonServer = void 0;
|
|
4
|
+
const timing_1 = require("../utils/timing");
|
|
5
|
+
// Polyfill for BigInt JSON serialization
|
|
6
|
+
BigInt.prototype.toJSON = function () {
|
|
7
|
+
return this.toString();
|
|
8
|
+
};
|
|
9
|
+
const stellar_sdk_1 = require("@stellar/stellar-sdk");
|
|
10
|
+
const stellar_sdk_2 = require("@stellar/stellar-sdk");
|
|
11
|
+
const getNetwork = (network) => {
|
|
12
|
+
let networkPassphrase = "";
|
|
13
|
+
switch (network) {
|
|
14
|
+
case "TESTNET":
|
|
15
|
+
networkPassphrase = "Test SDF Network ; September 2015";
|
|
16
|
+
break;
|
|
17
|
+
case "PUBLIC":
|
|
18
|
+
networkPassphrase = "Public Global Stellar Network ; September 2015";
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
return { network, networkPassphrase };
|
|
22
|
+
};
|
|
23
|
+
exports.getNetwork = getNetwork;
|
|
24
|
+
const getPublicKeyFromPrivateKey = (privateKey) => {
|
|
25
|
+
const keypair = stellar_sdk_1.Keypair.fromSecret(privateKey);
|
|
26
|
+
return keypair.publicKey();
|
|
27
|
+
};
|
|
28
|
+
exports.getPublicKeyFromPrivateKey = getPublicKeyFromPrivateKey;
|
|
29
|
+
const getServer = (network) => {
|
|
30
|
+
let serverUrl = "";
|
|
31
|
+
switch (network.network) {
|
|
32
|
+
case "TESTNET":
|
|
33
|
+
serverUrl = "https://soroban-testnet.stellar.org:443";
|
|
34
|
+
break;
|
|
35
|
+
case "PUBLIC":
|
|
36
|
+
serverUrl = "https://rpc.stellar.org:443";
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
const server = new stellar_sdk_2.rpc.Server(serverUrl);
|
|
40
|
+
return server;
|
|
41
|
+
};
|
|
42
|
+
exports.getServer = getServer;
|
|
43
|
+
const TIMEOUT_TRANSACTION = 60;
|
|
44
|
+
const getHorizonServer = (network) => {
|
|
45
|
+
if (network === "TESTNET") {
|
|
46
|
+
return new stellar_sdk_1.Horizon.Server("https://horizon-testnet.stellar.org");
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
return new stellar_sdk_1.Horizon.Server("https://horizon.stellar.org");
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
exports.getHorizonServer = getHorizonServer;
|
|
53
|
+
const executeWalletTransaction = async (context, wallet_address, method, additionalArgs = []) => {
|
|
54
|
+
const response = await prepareTransaction(context, wallet_address, {
|
|
55
|
+
method,
|
|
56
|
+
args: [{ value: wallet_address, type: "address" }, ...additionalArgs],
|
|
57
|
+
});
|
|
58
|
+
if (response.isSuccess) {
|
|
59
|
+
if (response.isReadOnly) {
|
|
60
|
+
return response;
|
|
61
|
+
}
|
|
62
|
+
const signedTxXDR = await context.signTransaction(response.result);
|
|
63
|
+
const sendResponse = await sendTransaction(context, signedTxXDR);
|
|
64
|
+
return {
|
|
65
|
+
isSuccess: true,
|
|
66
|
+
isReadOnly: false,
|
|
67
|
+
result: sendResponse,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
return response;
|
|
71
|
+
};
|
|
72
|
+
exports.executeWalletTransaction = executeWalletTransaction;
|
|
73
|
+
const executeMultiWalletTransactions = async (context, wallet_address, methods) => {
|
|
74
|
+
const contractCalls = methods.map(({ method, additionalArgs = [] }) => ({
|
|
75
|
+
method,
|
|
76
|
+
args: [
|
|
77
|
+
{ value: wallet_address, type: "address" },
|
|
78
|
+
...additionalArgs,
|
|
79
|
+
],
|
|
80
|
+
}));
|
|
81
|
+
const response = await prepareTransaction(context, wallet_address, contractCalls);
|
|
82
|
+
if (response.isSuccess) {
|
|
83
|
+
if (response.isReadOnly) {
|
|
84
|
+
return response;
|
|
85
|
+
}
|
|
86
|
+
const signedTxXDR = await context.signTransaction(response.result);
|
|
87
|
+
const sendResponse = await sendTransaction(context, signedTxXDR);
|
|
88
|
+
return {
|
|
89
|
+
isSuccess: true,
|
|
90
|
+
isReadOnly: false,
|
|
91
|
+
result: sendResponse,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return response;
|
|
95
|
+
};
|
|
96
|
+
exports.executeMultiWalletTransactions = executeMultiWalletTransactions;
|
|
97
|
+
const prepareTransaction = async (context, address, contractCalls) => {
|
|
98
|
+
const contractAddress = context.contractAddress;
|
|
99
|
+
const contract = new stellar_sdk_1.Contract(contractAddress);
|
|
100
|
+
const server = getServer(context.network);
|
|
101
|
+
const sourceAccount = await server.getAccount(address);
|
|
102
|
+
const response = {
|
|
103
|
+
isSuccess: false,
|
|
104
|
+
isReadOnly: false,
|
|
105
|
+
result: "",
|
|
106
|
+
};
|
|
107
|
+
// Ensure contractCalls is an array
|
|
108
|
+
const calls = Array.isArray(contractCalls) ? contractCalls : [contractCalls];
|
|
109
|
+
// Convert raw values to ScVal for all calls
|
|
110
|
+
const convertedCalls = calls.map((call) => {
|
|
111
|
+
const converted = {
|
|
112
|
+
method: call.method,
|
|
113
|
+
args: call.args?.map((arg) => {
|
|
114
|
+
let scVal;
|
|
115
|
+
if (arg.type === "vec") {
|
|
116
|
+
if (arg.value === null) {
|
|
117
|
+
// Handle null vectors as None
|
|
118
|
+
scVal = (0, stellar_sdk_1.nativeToScVal)(null, { type: "vec" });
|
|
119
|
+
}
|
|
120
|
+
else if (Array.isArray(arg.value)) {
|
|
121
|
+
// Handle non-empty vectors
|
|
122
|
+
const vecScVals = arg.value.map((item) => (0, stellar_sdk_1.nativeToScVal)(item.value, { type: item.type }));
|
|
123
|
+
scVal = (0, stellar_sdk_1.nativeToScVal)({ vec: vecScVals }, { type: "vec" });
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
// Fallback for invalid vector values
|
|
127
|
+
scVal = (0, stellar_sdk_1.nativeToScVal)(null, { type: "vec" });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
scVal = (0, stellar_sdk_1.nativeToScVal)(arg.value, { type: arg.type });
|
|
132
|
+
}
|
|
133
|
+
return scVal;
|
|
134
|
+
}) || [],
|
|
135
|
+
};
|
|
136
|
+
return converted;
|
|
137
|
+
});
|
|
138
|
+
const transactionBuilder = new stellar_sdk_1.TransactionBuilder(sourceAccount, {
|
|
139
|
+
fee: stellar_sdk_1.BASE_FEE,
|
|
140
|
+
networkPassphrase: context.network.networkPassphrase,
|
|
141
|
+
});
|
|
142
|
+
// Add all operations to the transaction
|
|
143
|
+
convertedCalls.forEach((call) => {
|
|
144
|
+
transactionBuilder.addOperation(contract.call(call.method, ...call.args));
|
|
145
|
+
});
|
|
146
|
+
const builtTransaction = transactionBuilder
|
|
147
|
+
.setTimeout(TIMEOUT_TRANSACTION)
|
|
148
|
+
.build();
|
|
149
|
+
const sim = await server.simulateTransaction(builtTransaction);
|
|
150
|
+
if (stellar_sdk_2.rpc.Api.isSimulationSuccess(sim)) {
|
|
151
|
+
response.isSuccess = true;
|
|
152
|
+
const result = sim.result && sim.result.retval
|
|
153
|
+
? (0, stellar_sdk_1.scValToNative)(sim.result.retval)
|
|
154
|
+
: undefined;
|
|
155
|
+
const footprint = sim.transactionData.getFootprint();
|
|
156
|
+
const isReadOnly = footprint.readOnly().length > 0 && footprint.readWrite().length === 0;
|
|
157
|
+
if (isReadOnly) {
|
|
158
|
+
response.isReadOnly = true;
|
|
159
|
+
response.result = result;
|
|
160
|
+
return response;
|
|
161
|
+
}
|
|
162
|
+
// For write operations, continue with the normal flow of returning the XDR
|
|
163
|
+
const preparedTransaction = await server.prepareTransaction(builtTransaction);
|
|
164
|
+
response.result = preparedTransaction.toXDR();
|
|
165
|
+
return response;
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
if (stellar_sdk_2.rpc.Api.isSimulationError(sim)) {
|
|
169
|
+
throw new Error(`Transaction simulation error: ${JSON.stringify(sim.error)}`);
|
|
170
|
+
}
|
|
171
|
+
throw new Error("Transaction simulation failed");
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
exports.prepareTransaction = prepareTransaction;
|
|
175
|
+
const signTransaction = async (context, xdrToSign, privateKey) => {
|
|
176
|
+
const preparedTransaction = stellar_sdk_1.TransactionBuilder.fromXDR(xdrToSign, context.network.networkPassphrase);
|
|
177
|
+
const sourceKeypair = stellar_sdk_1.Keypair.fromSecret(privateKey);
|
|
178
|
+
preparedTransaction.sign(sourceKeypair);
|
|
179
|
+
return preparedTransaction;
|
|
180
|
+
};
|
|
181
|
+
exports.signTransaction = signTransaction;
|
|
182
|
+
const sendTransaction = async (context, signedTransactionXDR, bDebug = false) => {
|
|
183
|
+
const server = getServer(context.network);
|
|
184
|
+
const signedTransaction = stellar_sdk_1.TransactionBuilder.fromXDR(signedTransactionXDR, context.network.networkPassphrase);
|
|
185
|
+
// Submit the transaction to the Stellar-RPC server. The RPC server will
|
|
186
|
+
// then submit the transaction into the network for us. Then we will have to
|
|
187
|
+
// wait, polling `getTransaction` until the transaction completes.
|
|
188
|
+
try {
|
|
189
|
+
const sendResponse = await server.sendTransaction(signedTransaction);
|
|
190
|
+
if (sendResponse.status === "PENDING") {
|
|
191
|
+
let getResponse = await server.getTransaction(sendResponse.hash);
|
|
192
|
+
while (getResponse.status === "NOT_FOUND") {
|
|
193
|
+
// See if the transaction is complete
|
|
194
|
+
getResponse = await server.getTransaction(sendResponse.hash);
|
|
195
|
+
// Wait one second
|
|
196
|
+
await (0, timing_1.sleep)(1000);
|
|
197
|
+
}
|
|
198
|
+
if (getResponse.status === "SUCCESS") {
|
|
199
|
+
// Make sure the transaction's resultMetaXDR is not empty
|
|
200
|
+
if (!getResponse.resultMetaXdr) {
|
|
201
|
+
throw new Error("Empty resultMetaXDR in getTransaction response");
|
|
202
|
+
}
|
|
203
|
+
// Find the return value from the contract and return it
|
|
204
|
+
const transactionMeta = getResponse.resultMetaXdr;
|
|
205
|
+
const returnValue = transactionMeta.v3().sorobanMeta()?.returnValue();
|
|
206
|
+
if (returnValue) {
|
|
207
|
+
return (0, stellar_sdk_1.scValToNative)(returnValue);
|
|
208
|
+
}
|
|
209
|
+
return getResponse; // Return the full transaction response
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
throw new Error(`Transaction failed: ${getResponse.resultXdr}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
throw new Error(sendResponse.errorResult?.toString() || "Unknown error");
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
catch (err) {
|
|
220
|
+
// Catch and report any errors we've thrown
|
|
221
|
+
throw new Error(`Transaction sending error: ${JSON.stringify(err)}`);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
exports.sendTransaction = sendTransaction;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flashbacktech/flashbackclient",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.35",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -83,6 +83,9 @@
|
|
|
83
83
|
"stellar": [
|
|
84
84
|
"./dist/stellar/index.d.ts"
|
|
85
85
|
],
|
|
86
|
+
"stellarv2": [
|
|
87
|
+
"./dist/stellarv2/index.d.ts"
|
|
88
|
+
],
|
|
86
89
|
"api": [
|
|
87
90
|
"./dist/api/index.d.ts"
|
|
88
91
|
],
|