Skip to content

Commit 0139a27

Browse files
committed
Update payload to adapt new aptos transaction payload interface
1 parent 10fa512 commit 0139a27

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

typescript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"typescript": "^4.6.4"
2727
},
2828
"dependencies": {
29-
"aptos": "^1.3.7",
29+
"aptos": "^1.3.10",
3030
"big-integer": "^1.6.51",
3131
"elliptic": "^6.5.4",
3232
"json-stable-stringify": "^1.0.1",

typescript/src/builtinFuncs.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { HexString } from "aptos";
55
import stringify from "json-stable-stringify";
66
import { StructInfoType } from "./parserRepo";
77
import { ActualStringClass } from "./nativeFuncs";
8+
import { AccountAddress } from "aptos/dist/transaction_builder/aptos_types";
89

910
export function abortCode(code: any) {
1011
if (code instanceof U64) {
@@ -256,15 +257,24 @@ export function payloadArg(val: any) {
256257
return val.value.toString();
257258
}
258259
else {
259-
throw new Error("Only expect U8, U64, or U128 for integer types");
260+
throw new Error("Only expect U8, U64, or U128 for UnsignedInt types");
260261
}
261262
}
263+
else if (Array.isArray(val) && val.every(v => v instanceof U8)) {
264+
// For U8[]
265+
return u8ArrayArg(val);
266+
}
262267
else if (val instanceof HexString) {
263268
return val.toShortString();
264269
}
265-
else if (typeof val === 'boolean') {
270+
else if (['boolean', 'string', 'number', 'bigint'].includes(typeof val)) {
271+
// 1. return as it is for js primitive types
272+
// 2. to make sure function payloadArg is idempotent when called more than once
266273
return val
267274
}
275+
else if (val instanceof AccountAddress) {
276+
return val;
277+
}
268278
else if(val.typeTag instanceof StructTag) {
269279
const tag = val.typeTag as StructTag;
270280
if (tag.address.toShortString() === '0x1' && tag.module === 'string' && tag.name === 'String') {
@@ -277,7 +287,7 @@ export function payloadArg(val: any) {
277287
}
278288
}
279289
else {
280-
throw new Error(`Unexpected value type: ${typeof val}`);
290+
throw new Error(`Unexpected value ${val}`);
281291
}
282292
}
283293

typescript/src/txSender.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { AptosClient, AptosAccount, Types, TxnBuilderTypes, HexString, BCS } from "aptos";
2-
import { TypeTagParser } from "aptos/dist/transaction_builder/builder_utils";
3-
import { UserTransaction, WriteSetChange_WriteResource } from "aptos/dist/generated";
4-
import { AccountAddress, Identifier, ModuleId, EntryFunction } from "aptos/dist/transaction_builder/aptos_types";
2+
import { TransactionPayload_EntryFunctionPayload, UserTransaction, WriteSetChange_WriteResource } from "aptos/dist/generated";
53
import { AptosParserRepo } from "./parserRepo";
6-
import { AtomicTypeTag, StructTag, TypeTag } from "./typeTag";
4+
import { StructTag } from "./typeTag";
75
import { U128, U64, U8 } from "./builtinTypes";
8-
import { ActualStringClass, serializeMoveValue, serializeMoveValueWithoutTag, serializeVector } from ".";
6+
import { ActualStringClass, payloadArg } from ".";
97

108
type AcceptedScriptFuncArgType = any[] | U8 | U64 | U128 | HexString | boolean | ActualStringClass;
119

@@ -15,22 +13,17 @@ export function buildPayload(
1513
funcName: string,
1614
typeArguments: string[],
1715
args: AcceptedScriptFuncArgType[],
18-
): TxnBuilderTypes.TransactionPayloadEntryFunction {
16+
): TransactionPayload_EntryFunctionPayload {
1917

20-
const bytes = args.map(arg => {
21-
const serializer = new BCS.Serializer();
22-
serializeMoveValueWithoutTag(serializer, arg);
23-
return serializer.getBytes();
24-
});
18+
const _function = `${moduleAddress.toString()}::${moduleName}::${funcName}`;
19+
const _arguments = args.map(a => payloadArg(a));
2520

26-
27-
const scriptFunction = new EntryFunction(
28-
new ModuleId(new AccountAddress(moduleAddress.toUint8Array()), new Identifier(moduleName)),
29-
new Identifier(funcName),
30-
typeArguments.map(str => new TypeTagParser(str).parseTypeTag()),
31-
bytes,
32-
);
33-
return new TxnBuilderTypes.TransactionPayloadEntryFunction(scriptFunction);
21+
return {
22+
type: "entry_function_payload",
23+
function: _function,
24+
type_arguments: typeArguments,
25+
arguments: _arguments,
26+
};
3427
}
3528

3629
export async function sendPayloadTx(

0 commit comments

Comments
 (0)