Skip to content

Commit 9273df1

Browse files
committed
Merge branch 'aptos-chain-karan' of github.com:okto-hq/okto-js-sdk-monorepo into development-srijan
2 parents 07403d4 + 016da42 commit 9273df1

17 files changed

+683
-78
lines changed

Diff for: packages/core-js/src/abstract/aptosRawTransaction.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type OktoClient from '@/core/index.js';
2+
import { aptosRawTransaction as useropgen } from '@/userop/aptosRawTransaction.js';
3+
import type { AptosRawTransactionIntentParams } from '@/userop/types.js';
4+
5+
/**
6+
* Execute an Aptos Raw Transaction.
7+
*
8+
* This function initiates the process of executing a raw transaction on the Aptos blockchain
9+
* by encoding the necessary parameters into a User Operation. The operation is then
10+
* submitted through the OktoClient for execution.
11+
*
12+
* @param oc - The OktoClient instance used to interact with the blockchain.
13+
* @param data - The parameters for the Aptos raw transaction.
14+
* @returns A promise that resolves to the transaction ID or operation result.
15+
*/
16+
export async function aptosRawTransaction(
17+
oc: OktoClient,
18+
data: AptosRawTransactionIntentParams,
19+
): Promise<string> {
20+
const userop = await useropgen(oc, data);
21+
const signedUserOp = await oc.signUserOp(userop);
22+
const res = await oc.executeUserOp(signedUserOp);
23+
return res;
24+
}

Diff for: packages/core-js/src/abstract/rawTransaction.ts renamed to packages/core-js/src/abstract/evmRawTransaction.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type OktoClient from '@/core/index.js';
2-
import { evmRawTransaction as useropgen } from '@/userop/rawTransaction.js';
3-
import type { RawTransactionIntentParams } from '@/userop/types.js';
2+
import { evmRawTransaction as useropgen } from '@/userop/evmRawTransaction.js';
3+
import type { EVMRawTransactionIntentParams } from '@/userop/types.js';
44

55
/**
66
* Do a Raw Transaction on any EVM Chain.
77
*/
88
export async function evmRawTransaction(
99
oc: OktoClient,
10-
data: RawTransactionIntentParams,
10+
data: EVMRawTransactionIntentParams,
1111
): Promise<string> {
1212
const userop = await useropgen(oc, data);
1313
const signedUserOp = await oc.signUserOp(userop);

Diff for: packages/core-js/src/abstract/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export { nftTransfer } from './nftTransfer.js';
2-
export { evmRawTransaction } from './rawTransaction.js';
2+
export { evmRawTransaction } from './evmRawTransaction.js';
33
export { tokenTransfer } from './tokenTransfer.js';
4+
export { aptosRawTransaction } from './aptosRawTransaction.js';
5+
export { nftCreateCollection } from './nftCollectionCreation.js';
6+
export { nftMint } from './nftMint.js';
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type OktoClient from '@/core/index.js';
2+
import { nftCreateCollection as useropgen } from '@/userop/nftCollectionCreation.js';
3+
import type { NftCreateCollectionParams } from '@/userop/types.js';
4+
5+
/**
6+
* Create an NFT Collection.
7+
*
8+
* This function initiates the process of creating an NFT collection by encoding
9+
* the necessary parameters into a User Operation. The operation is then
10+
* submitted through the OktoClient for execution.
11+
*
12+
* @param oc - The OktoClient instance used to interact with the blockchain.
13+
* @param data - The parameters for creating the NFT collection (caip2Id, name, uri, data with attributes, symbol, type, description).
14+
* @returns A promise that resolves to the transaction ID or operation result.
15+
*/
16+
export async function nftCreateCollection(
17+
oc: OktoClient,
18+
data: NftCreateCollectionParams,
19+
): Promise<string> {
20+
const userop = await useropgen(oc, data);
21+
const signedUserOp = await oc.signUserOp(userop);
22+
const res = await oc.executeUserOp(signedUserOp);
23+
return res;
24+
}

Diff for: packages/core-js/src/abstract/nftMint.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type OktoClient from '@/core/index.js';
2+
import { nftMint as useropgen } from '@/userop/nftMint.js';
3+
import type { NftMintParams } from '@/userop/types.js';
4+
5+
/**
6+
* Mint an NFT.
7+
*
8+
* This function initiates the process of minting an NFT by encoding
9+
* the necessary parameters into a User Operation. The operation is then
10+
* submitted through the OktoClient for execution.
11+
*
12+
* @param oc - The OktoClient instance used to interact with the blockchain.
13+
* @param data - The parameters for minting an NFT (caip2Id, nftName, collectionAddress, uri, and additional data).
14+
* @returns A promise that resolves to the transaction ID or operation result.
15+
*/
16+
export async function nftMint(
17+
oc: OktoClient,
18+
data: NftMintParams,
19+
): Promise<string> {
20+
const userop = await useropgen(oc, data);
21+
const signedUserOp = await oc.signUserOp(userop);
22+
const res = await oc.executeUserOp(signedUserOp);
23+
return res;
24+
}

Diff for: packages/core-js/src/userop/aptosRawTransaction.ts

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import GatewayClientRepository from '@/api/gateway.js';
2+
import type OktoClient from '@/core/index.js';
3+
import { BaseError } from '@/errors/base.js';
4+
import { getChains } from '@/explorer/chain.js';
5+
import type { Address, UserOp } from '@/types/core.js';
6+
import { Constants } from '@/utils/index.js';
7+
import { generateUUID, nonceToBigInt } from '@/utils/nonce.js';
8+
import {
9+
encodeAbiParameters,
10+
encodeFunctionData,
11+
parseAbiParameters,
12+
stringToBytes,
13+
toHex,
14+
} from 'viem';
15+
import { INTENT_ABI } from './abi.js';
16+
import type {
17+
AptosRawTransaction,
18+
AptosRawTransactionIntentParams,
19+
} from './types.js';
20+
import {
21+
AptosRawTransactionIntentParamsSchema,
22+
validateSchema,
23+
} from './userOpInputValidator.js';
24+
25+
/**
26+
* Creates a user operation for Aptos Raw Transaction.
27+
*
28+
* @param oc - The OktoClient instance used to interact with the blockchain.
29+
* @param data - The parameters for the Aptos raw transaction.
30+
* @returns The User Operation (UserOp) for the Aptos raw transaction.
31+
*/
32+
export async function aptosRawTransaction(
33+
oc: OktoClient,
34+
data: AptosRawTransactionIntentParams,
35+
feePayerAddress?: Address,
36+
): Promise<UserOp> {
37+
if (!oc.isLoggedIn()) {
38+
throw new BaseError('User not logged in');
39+
}
40+
validateSchema(AptosRawTransactionIntentParamsSchema, data);
41+
42+
if (!feePayerAddress) {
43+
feePayerAddress = Constants.FEE_PAYER_ADDRESS;
44+
}
45+
46+
const nonce = generateUUID();
47+
48+
const jobParametersAbiType = '(string caip2Id, bytes[] transactions)';
49+
const gsnDataAbiType = `(bool isRequired, string[] requiredNetworks, ${jobParametersAbiType}[] tokens)`;
50+
51+
const chains = await getChains(oc);
52+
const currentChain = chains.find(
53+
(chain) => chain.caipId.toLowerCase() === data.caip2Id.toLowerCase(),
54+
);
55+
56+
if (!currentChain) {
57+
throw new BaseError(`Chain Not Supported`, {
58+
details: `${data.caip2Id} is not supported for this client`,
59+
});
60+
}
61+
62+
if (!data.caip2Id.toLowerCase().startsWith('aptos:')) {
63+
throw new BaseError('Invalid chain for Aptos transaction', {
64+
details: `${data.caip2Id} is not an Aptos chain`,
65+
});
66+
}
67+
68+
const transactionsBytes = data.transactions.map((transaction) => {
69+
const aptosTransaction: AptosRawTransaction = {
70+
function: transaction.function,
71+
typeArguments: transaction.typeArguments || [],
72+
functionArguments: transaction.functionArguments || [],
73+
};
74+
return toHex(stringToBytes(JSON.stringify(aptosTransaction)));
75+
});
76+
77+
const jobparam = encodeAbiParameters(
78+
parseAbiParameters(jobParametersAbiType),
79+
[
80+
{
81+
caip2Id: data.caip2Id,
82+
transactions: transactionsBytes,
83+
},
84+
],
85+
);
86+
87+
const calldata = encodeAbiParameters(
88+
parseAbiParameters('bytes4, address, uint256, bytes'),
89+
[
90+
Constants.EXECUTE_USEROP_FUNCTION_SELECTOR,
91+
oc.env.jobManagerAddress,
92+
Constants.USEROP_VALUE,
93+
encodeFunctionData({
94+
abi: INTENT_ABI,
95+
functionName: Constants.FUNCTION_NAME,
96+
args: [
97+
toHex(nonceToBigInt(nonce), { size: 32 }),
98+
oc.clientSWA,
99+
oc.userSWA,
100+
feePayerAddress,
101+
encodeAbiParameters(
102+
parseAbiParameters('(bool gsnEnabled, bool sponsorshipEnabled)'),
103+
[
104+
{
105+
gsnEnabled: currentChain.gsnEnabled ?? false,
106+
sponsorshipEnabled: currentChain.sponsorshipEnabled ?? false,
107+
},
108+
],
109+
),
110+
encodeAbiParameters(parseAbiParameters(gsnDataAbiType), [
111+
{
112+
isRequired: false,
113+
requiredNetworks: [],
114+
tokens: [],
115+
},
116+
]),
117+
jobparam,
118+
Constants.INTENT_TYPE.RAW_TRANSACTION,
119+
],
120+
}),
121+
],
122+
);
123+
124+
const gasPrice = await GatewayClientRepository.getUserOperationGasPrice(oc);
125+
126+
const userOp: UserOp = {
127+
sender: oc.userSWA,
128+
nonce: toHex(nonceToBigInt(nonce), { size: 32 }),
129+
paymaster: oc.env.paymasterAddress,
130+
callGasLimit: toHex(Constants.GAS_LIMITS.CALL_GAS_LIMIT),
131+
verificationGasLimit: toHex(Constants.GAS_LIMITS.VERIFICATION_GAS_LIMIT),
132+
preVerificationGas: toHex(Constants.GAS_LIMITS.PRE_VERIFICATION_GAS),
133+
maxFeePerGas: gasPrice.maxFeePerGas,
134+
maxPriorityFeePerGas: gasPrice.maxPriorityFeePerGas,
135+
paymasterPostOpGasLimit: toHex(
136+
Constants.GAS_LIMITS.PAYMASTER_POST_OP_GAS_LIMIT,
137+
),
138+
paymasterVerificationGasLimit: toHex(
139+
Constants.GAS_LIMITS.PAYMASTER_VERIFICATION_GAS_LIMIT,
140+
),
141+
callData: calldata,
142+
paymasterData: await oc.paymasterData({
143+
nonce: nonce,
144+
validUntil: new Date(Date.now() + 6 * Constants.HOURS_IN_MS),
145+
}),
146+
};
147+
148+
return userOp;
149+
}

Diff for: packages/core-js/src/userop/rawTransaction.ts renamed to packages/core-js/src/userop/evmRawTransaction.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ import {
1414
toHex,
1515
} from 'viem';
1616
import { INTENT_ABI } from './abi.js';
17-
import type { EVMRawTransaction, RawTransactionIntentParams } from './types.js';
17+
import type {
18+
EVMRawTransaction,
19+
EVMRawTransactionIntentParams,
20+
} from './types.js';
1821
import {
19-
RawTransactionIntentParamsSchema,
22+
EvmRawTransactionIntentParamsSchema,
2023
validateSchema,
2124
} from './userOpInputValidator.js';
2225

@@ -25,14 +28,14 @@ import {
2528
*/
2629
export async function evmRawTransaction(
2730
oc: OktoClient,
28-
data: RawTransactionIntentParams,
31+
data: EVMRawTransactionIntentParams,
2932
feePayerAddress?: Address,
3033
): Promise<UserOp> {
3134
if (!oc.isLoggedIn()) {
3235
throw new BaseError('User not logged in');
3336
}
3437

35-
validateSchema(RawTransactionIntentParamsSchema, data);
38+
validateSchema(EvmRawTransactionIntentParamsSchema, data);
3639

3740
if (!feePayerAddress) {
3841
feePayerAddress = Constants.FEE_PAYER_ADDRESS;

Diff for: packages/core-js/src/userop/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export { nftTransfer } from './nftTransfer.js';
2-
export { evmRawTransaction } from './rawTransaction.js';
2+
export { evmRawTransaction } from './evmRawTransaction.js';
33
export { tokenTransfer } from './tokenTransfer.js';
4+
export { nftCreateCollection } from './nftCollectionCreation.js';
5+
export { nftMint } from './nftMint.js';
6+
export { aptosRawTransaction } from './aptosRawTransaction.js';

0 commit comments

Comments
 (0)