-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add internal transaction submission file #11
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,7 +19,12 @@ import { getLedgerInfo } from "../../internal/general"; | |||||
import { getGasPriceEstimation } from "../../internal/transaction"; | ||||||
import { HexInput, SigningScheme } from "../../types"; | ||||||
import { NetworkToChainId } from "../../utils/apiEndpoints"; | ||||||
import { DEFAULT_MAX_GAS_AMOUNT, DEFAULT_TXN_EXP_SEC_FROM_NOW } from "../../utils/const"; | ||||||
import { | ||||||
DEFAULT_MAX_GAS_AMOUNT, | ||||||
DEFAULT_TXN_EXP_SEC_FROM_NOW, | ||||||
RAW_TRANSACTION_SALT, | ||||||
RAW_TRANSACTION_WITH_DATA_SALT, | ||||||
} from "../../utils/const"; | ||||||
import { | ||||||
AccountAuthenticator, | ||||||
AccountAuthenticatorEd25519, | ||||||
|
@@ -180,12 +185,10 @@ export async function generateRawTransaction(args: { | |||||
* When we call our `generateTransaction` function with the relevant type properties, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* Typescript can infer the return type based on the appropriate function overload. | ||||||
*/ | ||||||
export async function generateTransaction( | ||||||
args: GenerateSingleSignerRawTransactionArgs, | ||||||
): Promise<SingleSignerTransaction>; | ||||||
export async function generateTransaction(args: GenerateFeePayerRawTransactionArgs): Promise<FeePayerTransaction>; | ||||||
export async function generateTransaction(args: GenerateMultiAgentRawTransactionArgs): Promise<MultiAgentTransaction>; | ||||||
export async function generateTransaction(args: GenerateRawTransactionArgs): Promise<AnyRawTransaction>; | ||||||
export async function buildTransaction(args: GenerateSingleSignerRawTransactionArgs): Promise<SingleSignerTransaction>; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much better name! |
||||||
export async function buildTransaction(args: GenerateFeePayerRawTransactionArgs): Promise<FeePayerTransaction>; | ||||||
export async function buildTransaction(args: GenerateMultiAgentRawTransactionArgs): Promise<MultiAgentTransaction>; | ||||||
export async function buildTransaction(args: GenerateRawTransactionArgs): Promise<AnyRawTransaction>; | ||||||
/** | ||||||
* Generates a transaction based on the provided arguments | ||||||
* | ||||||
|
@@ -208,7 +211,7 @@ export async function generateTransaction(args: GenerateRawTransactionArgs): Pro | |||||
* } | ||||||
* ``` | ||||||
*/ | ||||||
export async function generateTransaction(args: GenerateRawTransactionArgs): Promise<AnyRawTransaction> { | ||||||
export async function buildTransaction(args: GenerateRawTransactionArgs): Promise<AnyRawTransaction> { | ||||||
const { aptosConfig, sender, payload, options, secondarySignerAddresses, feePayerAddress } = args; | ||||||
// generate raw transaction | ||||||
const rawTxn = await generateRawTransaction({ | ||||||
|
@@ -246,8 +249,14 @@ export async function generateTransaction(args: GenerateRawTransactionArgs): Pro | |||||
|
||||||
/** | ||||||
* Simluate a transaction before signing and submit to chain | ||||||
* @param args | ||||||
* @returns | ||||||
* | ||||||
* @param args.transaction A aptos transaction type to sign | ||||||
* @param args.signerPublicKey The signer public key | ||||||
* @param args.secondarySignersPublicKeys optional. The secondart signers public keys if multi signers transaction | ||||||
* @param args.feePayerPublicKey optional. The fee payer public key is a fee payer (aka sponsored) transaction | ||||||
* @param args.options optional. SimulateTransactionOptions | ||||||
* | ||||||
* @returns A signed serialized transaction that can be simulated | ||||||
*/ | ||||||
export function generateSignedTransactionForSimulation(args: SimulateTransactionData): Uint8Array { | ||||||
const { signerPublicKey, transaction, secondarySignersPublicKeys, feePayerPublicKey } = args; | ||||||
|
@@ -340,7 +349,7 @@ export function generateSignedTransactionForSimulation(args: SimulateTransaction | |||||
* | ||||||
* @return The signer AccountAuthenticator | ||||||
*/ | ||||||
export function signTransaction(args: { signer: Account; transaction: AnyRawTransaction }): AccountAuthenticator { | ||||||
export function sign(args: { signer: Account; transaction: AnyRawTransaction }): AccountAuthenticator { | ||||||
const { signer, transaction } = args; | ||||||
|
||||||
const transactionToSign = deriveTransactionType(transaction); | ||||||
|
@@ -505,9 +514,6 @@ export function generateMultiSignersSignedTransaction( | |||||
); | ||||||
} | ||||||
|
||||||
const RAW_TRANSACTION_SALT = "APTOS::RawTransaction"; | ||||||
const RAW_TRANSACTION_WITH_DATA_SALT = "APTOS::RawTransactionWithData"; | ||||||
|
||||||
export function getSigningMessage(rawTxn: AnyRawTransactionInstance): Uint8Array { | ||||||
const hash = sha3Hash.create(); | ||||||
|
||||||
|
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is different than buildTransaction, or should it also be named buildTransaction?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is different. buildTransaction is a transaction_builder function, generateTransaction is a transaction_submission function