-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add internal transaction submission file
- Loading branch information
Showing
10 changed files
with
238 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/** | ||
* This file contains the underlying implementations for exposed API surface in | ||
* the {@link api/transaction_submission}. By moving the methods out into a separate file, | ||
* other namespaces and processes can access these methods without depending on the entire | ||
* transaction_submission namespace and without having a dependency cycle error. | ||
*/ | ||
|
||
import { AptosConfig } from "../api/aptos_config"; | ||
import { postAptosFullNode } from "../client"; | ||
import { Account } from "../core/account"; | ||
import { AccountAuthenticator } from "../transactions/authenticator/account"; | ||
import { | ||
buildTransaction, | ||
generateTransactionPayload, | ||
generateSignedTransactionForSimulation, | ||
generateSignedTransaction, | ||
sign, | ||
} from "../transactions/transaction_builder/transaction_builder"; | ||
import { GenerateTransactionInput, AnyRawTransaction, SimulateTransactionData } from "../transactions/types"; | ||
import { UserTransactionResponse, PendingTransactionResponse, MimeType } from "../types"; | ||
|
||
/** | ||
* Generates any transaction by passing in the required arguments | ||
* | ||
* @param args.sender The transaction sender's account address as a HexInput | ||
* @param args.data EntryFunctionData | ScriptData | MultiSigData | ||
* @param feePayerAddress optional. For a fee payer (aka sponsored) transaction | ||
* @param secondarySignerAddresses optional. For a multi agent or fee payer (aka sponsored) transactions | ||
* @param args.options optional. GenerateTransactionOptions type | ||
* | ||
* @example | ||
* For a single signer entry function | ||
* move function name, move function type arguments, move function arguments | ||
* ` | ||
* data: { | ||
* function:"0x1::aptos_account::transfer", | ||
* type_arguments:[] | ||
* arguments:[recieverAddress,10] | ||
* } | ||
* ` | ||
* | ||
* @example | ||
* For a single signer script function | ||
* module bytecode, move function type arguments, move function arguments | ||
* ``` | ||
* data: { | ||
* bytecode:"0x001234567", | ||
* type_arguments:[], | ||
* arguments:[recieverAddress,10] | ||
* } | ||
* ``` | ||
* | ||
* @return A raw transaction type (note that it holds the raw transaction as a bcs serialized data) | ||
* ``` | ||
* { | ||
* rawTransaction: Uint8Array, | ||
* secondarySignerAddresses? : Array<AccountAddress>, | ||
* feePayerAddress?: AccountAddress | ||
* } | ||
* ``` | ||
*/ | ||
export async function generateTransaction( | ||
args: { aptosConfig: AptosConfig } & GenerateTransactionInput, | ||
): Promise<AnyRawTransaction> { | ||
const { aptosConfig, sender, data, options, secondarySignerAddresses, feePayerAddress } = args; | ||
const payload = await generateTransactionPayload(data); | ||
const rawTransaction = await buildTransaction({ | ||
aptosConfig, | ||
sender, | ||
payload, | ||
options, | ||
secondarySignerAddresses, | ||
feePayerAddress, | ||
}); | ||
return rawTransaction; | ||
} | ||
|
||
/** | ||
* Sign a transaction that can later be submitted to chain | ||
* | ||
* @param args.signer The signer account to sign the transaction | ||
* @param args.transaction A raw transaction type (note that it holds the raw transaction as a bcs serialized data) | ||
* ``` | ||
* { | ||
* rawTransaction: Uint8Array, | ||
* secondarySignerAddresses? : Array<AccountAddress>, | ||
* feePayerAddress?: AccountAddress | ||
* } | ||
* ``` | ||
* | ||
* @return The signer AccountAuthenticator | ||
*/ | ||
export function signTransaction(args: { signer: Account; transaction: AnyRawTransaction }): AccountAuthenticator { | ||
const accountAuthenticator = sign({ ...args }); | ||
return accountAuthenticator; | ||
} | ||
|
||
/** | ||
* Simulates a transaction before singing it. | ||
* | ||
* @param signerPublicKey The signer pubic key | ||
* @param transaction The raw transaction to simulate | ||
* @param secondarySignersPublicKeys optional. For when the transaction is a multi signers transaction | ||
* @param feePayerPublicKey optional. For when the transaction is a fee payer (aka sponsored) transaction | ||
* @param options optional. A config to simulate the transaction with | ||
*/ | ||
export async function simulateTransaction( | ||
args: { aptosConfig: AptosConfig } & SimulateTransactionData, | ||
): Promise<Array<UserTransactionResponse>> { | ||
const { aptosConfig, transaction, signerPublicKey, secondarySignersPublicKeys, feePayerPublicKey, options } = args; | ||
|
||
const signedTransaction = generateSignedTransactionForSimulation({ | ||
transaction, | ||
signerPublicKey, | ||
secondarySignersPublicKeys, | ||
feePayerPublicKey, | ||
options, | ||
}); | ||
|
||
const { data } = await postAptosFullNode<Uint8Array, Array<UserTransactionResponse>>({ | ||
aptosConfig, | ||
body: signedTransaction, | ||
path: "transactions/simulate", | ||
params: { | ||
estimate_gas_unit_price: args.options?.estimateGasUnitPrice ?? false, | ||
estimate_max_gas_amount: args.options?.estimateMaxGasAmount ?? false, | ||
estimate_prioritized_gas_unit_price: args.options?.estimatePrioritizedGasUnitPrice ?? false, | ||
}, | ||
originMethod: "simulateTransaction", | ||
contentType: MimeType.BCS_SIGNED_TRANSACTION, | ||
}); | ||
return data; | ||
} | ||
|
||
/** | ||
* Submit transaction to chain | ||
* | ||
* @param args.transaction A aptos transaction type | ||
* @param args.senderAuthenticator The account authenticator of the transaction sender | ||
* @param args.secondarySignerAuthenticators optional. For when the transaction is a multi signers transaction | ||
* | ||
* @return PendingTransactionResponse | ||
*/ | ||
export async function submitTransaction(args: { | ||
aptosConfig: AptosConfig; | ||
transaction: AnyRawTransaction; | ||
senderAuthenticator: AccountAuthenticator; | ||
secondarySignerAuthenticators?: { | ||
feePayerAuthenticator?: AccountAuthenticator; | ||
additionalSignersAuthenticators?: Array<AccountAuthenticator>; | ||
}; | ||
}): Promise<PendingTransactionResponse> { | ||
const { aptosConfig } = args; | ||
const signedTransaction = generateSignedTransaction({ ...args }); | ||
const { data } = await postAptosFullNode<Uint8Array, PendingTransactionResponse>({ | ||
aptosConfig, | ||
body: signedTransaction, | ||
path: "transactions", | ||
originMethod: "submitTransaction", | ||
contentType: MimeType.BCS_SIGNED_TRANSACTION, | ||
}); | ||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.