Skip to content

Commit

Permalink
fix typing on ABI argument types
Browse files Browse the repository at this point in the history
Now the ABI is properly handled, where it doesn't have to be forced
  • Loading branch information
gregnazario committed Feb 13, 2024
1 parent 94fde99 commit fe9c7ae
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 83 deletions.
4 changes: 2 additions & 2 deletions src/internal/transactionSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export async function buildTransactionPayload(
payload = await generateTransactionPayload(data);
} else if ("multisigAddress" in data) {
if (data.abi) {
payload = generateTransactionPayloadWithABI(data);
payload = generateTransactionPayloadWithABI({ abi: data.abi, ...data });
} else {
generateTransactionPayloadData = {
aptosConfig,
Expand All @@ -124,7 +124,7 @@ export async function buildTransactionPayload(
payload = await generateTransactionPayload(generateTransactionPayloadData);
}
} else if (data.abi) {
payload = generateTransactionPayloadWithABI(data);
payload = generateTransactionPayloadWithABI({ abi: data.abi, ...data });
} else {
generateTransactionPayloadData = {
aptosConfig,
Expand Down
17 changes: 7 additions & 10 deletions src/transactions/transactionBuilder/transactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ import {
MultiAgentTransaction,
InputScriptData,
InputSimulateTransactionData,
InputGenerateTransactionPayloadData,
InputEntryFunctionData,
InputMultiSigData,
InputMultiSigDataWithRemoteABI,
InputEntryFunctionDataWithRemoteABI,
InputGenerateTransactionPayloadDataWithRemoteABI,
InputSubmitTransactionData,
InputGenerateTransactionPayloadDataWithABI,
InputEntryFunctionDataWithABI,
InputMultiSigDataWithABI,
} from "../types";
import { convertArgument, fetchEntryFunctionAbi, standardizeTypeTags } from "./remoteAbi";
import { memoizeAsync } from "../../utils/memoize";
Expand Down Expand Up @@ -120,15 +120,12 @@ export async function generateTransactionPayload(
return generateTransactionPayloadWithABI({ abi: functionAbi, ...args });
}

export function generateTransactionPayloadWithABI(args: InputEntryFunctionData): TransactionPayloadEntryFunction;
export function generateTransactionPayloadWithABI(args: InputMultiSigData): TransactionPayloadMultiSig;
export function generateTransactionPayloadWithABI(args: InputEntryFunctionDataWithABI): TransactionPayloadEntryFunction;
export function generateTransactionPayloadWithABI(args: InputMultiSigDataWithABI): TransactionPayloadMultiSig;
export function generateTransactionPayloadWithABI(
args: InputGenerateTransactionPayloadData,
args: InputGenerateTransactionPayloadDataWithABI,
): AnyTransactionPayloadInstance {
if (isScriptDataInput(args)) {
return generateTransactionPayloadScript(args);
}
const functionAbi = args.abi!;
const functionAbi = args.abi;
const { moduleAddress, moduleName, functionName } = getFunctionParts(args.function);

// Ensure that all type arguments are typed properly
Expand Down
10 changes: 10 additions & 0 deletions src/transactions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ export type InputEntryFunctionData = {
abi?: EntryFunctionABI;
};

export type InputGenerateTransactionPayloadDataWithABI = InputEntryFunctionDataWithABI | InputMultiSigDataWithABI;

export type InputEntryFunctionDataWithABI = Omit<InputEntryFunctionData, "abi"> & {
abi: EntryFunctionABI;
};

export type InputMultiSigDataWithABI = {
multisigAddress: AccountAddressInput;
} & InputEntryFunctionDataWithABI;

export type InputEntryFunctionDataWithRemoteABI = InputEntryFunctionData & { aptosConfig: AptosConfig };
/**
* The data needed to generate a Multi Sig payload
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/transaction/generateTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ describe("generate transaction", () => {
data: {
function: "0x1::aptos_account::transfer",
functionArguments: [recieverAccounts[0].accountAddress, 1],
},
abi: {
typeParameters: [],
parameters: [new TypeTagAddress(), new TypeTagU64()],
abi: {
typeParameters: [],
parameters: [new TypeTagAddress(), new TypeTagU64()],
},
},
});
expect(transaction.rawTransaction instanceof RawTransaction).toBeTruthy();
Expand Down
50 changes: 21 additions & 29 deletions tests/e2e/transaction/transactionBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,42 +103,34 @@ describe("transaction builder", () => {
};

test("it generates a multi sig transaction payload", async () => {
const payload = generateTransactionPayloadWithABI(
{
multisigAddress: Account.generate().accountAddress,
function: `${contractPublisherAccount.accountAddress}::transfer::transfer`,
functionArguments: ["0x1", "0x1"],
},
functionAbi,
);
const payload = generateTransactionPayloadWithABI({
multisigAddress: Account.generate().accountAddress,
function: `${contractPublisherAccount.accountAddress}::transfer::transfer`,
functionArguments: ["0x1", "0x1"],
abi: functionAbi,
});
expect(payload instanceof TransactionPayloadMultiSig).toBeTruthy();
});
test("it generates an entry function transaction payload", async () => {
const payload = generateTransactionPayloadWithABI(
{
function: `${contractPublisherAccount.accountAddress}::transfer::transfer`,
functionArguments: ["0x1", "0x1"],
},
functionAbi,
);
const payload = generateTransactionPayloadWithABI({
function: `${contractPublisherAccount.accountAddress}::transfer::transfer`,
functionArguments: ["0x1", "0x1"],
abi: functionAbi,
});
expect(payload instanceof TransactionPayloadEntryFunction).toBeTruthy();
});
test("it generates an entry function transaction payload with mixed arguments", async () => {
const payload = generateTransactionPayloadWithABI(
{
function: `${contractPublisherAccount.accountAddress}::transfer::transfer`,
functionArguments: [AccountAddress.ONE, "0x1"],
},
functionAbi,
);
const payload = generateTransactionPayloadWithABI({
function: `${contractPublisherAccount.accountAddress}::transfer::transfer`,
functionArguments: [AccountAddress.ONE, "0x1"],
abi: functionAbi,
});
expect(payload instanceof TransactionPayloadEntryFunction).toBeTruthy();
const payload2 = generateTransactionPayloadWithABI(
{
function: `${contractPublisherAccount.accountAddress}::transfer::transfer`,
functionArguments: ["0x1", AccountAddress.ONE],
},
functionAbi,
);
const payload2 = generateTransactionPayloadWithABI({
function: `${contractPublisherAccount.accountAddress}::transfer::transfer`,
functionArguments: ["0x1", AccountAddress.ONE],
abi: functionAbi,
});
expect(payload2 instanceof TransactionPayloadEntryFunction).toBeTruthy();
});
});
Expand Down
44 changes: 6 additions & 38 deletions tests/e2e/transactionManagement/transactionWorker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,43 +73,13 @@ describe("transactionWorker", () => {
function: "0x1::aptos_account::transfer",
functionArguments: [recipient.accountAddress, 1],
};
const payloads = [...Array(5).fill(txn)];

// start transactions worker
const transactionWorker = new TransactionWorker(aptosConfig, sender);
transactionWorker.start();

// push transactions to queue
for (const payload of payloads) {
transactionWorker.push(payload);
}

// stop transaction worker for testing purposes.
setTimeout(async () => {
transactionWorker.stop();
const accountData = await aptos.getAccountInfo({ accountAddress: sender.accountAddress });
// call done() when all asynchronous operations are finished
done();
// expect sender sequence number to be 5
expect(accountData.sequence_number).toBe("5");
}, 1000 * 30);
},
longTestTimeout,
);

test(
"submits 5 transactions to chain for a single account with ABI",
(done) => {
// Specify the number of assertions expected
expect.assertions(1);

// create 5 transactions
const txn: InputGenerateTransactionPayloadData = {
// create 5 transactions with ABI
const txnWithAbi: InputGenerateTransactionPayloadData = {
function: "0x1::aptos_account::transfer",
functionArguments: [recipient.accountAddress, 1],
abi: { typeParameters: [], parameters: [new TypeTagAddress(), new TypeTagU64()] },
};
const payloads = [...Array(5).fill(txn)];
const payloads = [...Array(5).fill(txn), ...Array(5).fill(txnWithAbi)];

// start transactions worker
const transactionWorker = new TransactionWorker(aptosConfig, sender);
Expand All @@ -126,12 +96,10 @@ describe("transactionWorker", () => {
const accountData = await aptos.getAccountInfo({ accountAddress: sender.accountAddress });
// call done() when all asynchronous operations are finished
done();
// expect sender sequence number to be 5
expect(accountData.sequence_number).toBe("5");
}, 1000 * 30);
// expect sender sequence number to be 10
expect(accountData.sequence_number).toBe("10");

// Ensure all transactions were successful
setTimeout(async () => {
// Check all are successful
const txns = await aptos.getAccountTransactions({ accountAddress: sender.accountAddress });
txns.forEach((userTxn) => {
if (userTxn.type === TransactionResponseType.User) {
Expand Down

0 comments on commit fe9c7ae

Please sign in to comment.