Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: encodeScriptFunctionByResolve support arg type of { Vector: 'Add…
…ress' }, example: 0x1::TransferScripts::batch_peer_to_peer_v2
  • Loading branch information
wk3368 committed May 18, 2022
1 parent 605767f commit 1e7525b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file, as of versi
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.5] - 2022-05-18

- Fix: encodeScriptFunctionByResolve support arg type of { Vector: 'Address' }, example: 0x1::TransferScripts::batch_peer_to_peer_v2

## [2.1.4] - 2022-05-17

- Fix #42 #43: transaction generated by encodePackage will fail when dry_run_raw on chain
Expand Down
49 changes: 43 additions & 6 deletions src/encoding/index.spec.ts
Expand Up @@ -308,9 +308,17 @@ test("encodeScriptFunctionByResolve5", async () => {
}, 10000);

test("encodeScriptFunctionByResolve6", async () => {
const address = '0xedb4a7199ae49f76991614CF4C39c585'
const privateKey = '0a4a0fe4985df2590ac59c208775f36438a47193ce6eeb197964d8a8f8a6a1f9'
const STC_SCALLING_FACTOR = 1000000000
const addressArray = ["0x22a19240709CB17ec9523252AA17B997"];
const amountArray = [11 * STC_SCALLING_FACTOR];
const addressArray = [
'0xFB400ab8753213Cb40c286E740534Ab9',
];
const amountArray = [];
addressArray.forEach(() => {
amountArray.push(0.1 * STC_SCALLING_FACTOR)
});
// console.log({ addressArray, amountArray })
const functionId = '0x1::TransferScripts::batch_peer_to_peer_v2'
const typeArgs = ['0x1::STC::STC']
const args = [addressArray, amountArray]
Expand All @@ -320,10 +328,39 @@ test("encodeScriptFunctionByResolve6", async () => {

const se = new BcsSerializer();
scriptFunction.serialize(se);
const payloadInHex = toHexString(se.getBytes());
const hexExpected = "0x02000000000000000000000000000000010f5472616e73666572536372697074731562617463685f706565725f746f5f706565725f76320107000000000000000000000000000000010353544303535443000212011022a19240709cb17ec9523252aa17b997110100aea68f020000000000000000000000";
expect(payloadInHex).toBe(hexExpected);
}, 10000);
// const payloadInHex = toHexString(se.getBytes());
// const hexExpected = "0x02000000000000000000000000000000010f5472616e73666572536372697074731562617463685f706565725f746f5f706565725f76320107000000000000000000000000000000010353544303535443000212011022a19240709cb17ec9523252aa17b997110100aea68f020000000000000000000000";
// expect(payloadInHex).toBe(hexExpected);
const provider = new JsonRpcProvider(nodeUrl);
const senderSequenceNumber = await provider.getSequenceNumber(address)
const chainId = 251;
const nowSeconds = await provider.getNowSeconds();
// console.log({ senderSequenceNumber, nowSeconds })
const rawUserTransaction = generateRawUserTransaction(
address,
scriptFunction,
10000000, //maxGasAmount
1, // gasUnitPrice
senderSequenceNumber,
nowSeconds + 43200,
chainId,
);

const rawUserTransactionHex = bcsEncode(rawUserTransaction)
// console.log({ rawUserTransactionHex })

const signedUserTransactionHex = await signRawUserTransaction(
privateKey,
rawUserTransaction,
);

const txn = await provider.sendTransaction(signedUserTransactionHex);
console.log(txn);
const txnInfo = await txn.wait(1);
// console.log(txnInfo);

expect(txnInfo.status).toBe('Executed');
}, 60000);

test("decoding txn payload", () => {
const payloadInHex = "0x02000000000000000000000000000000010f5472616e73666572536372697074730c706565725f746f5f7065657201070000000000000000000000000000000103535443035354430003101df9157f14b0041eed18dcc56520d82900100060d743dd500b000000000000000000";
Expand Down
3 changes: 2 additions & 1 deletion src/utils/tx.ts
Expand Up @@ -293,7 +293,8 @@ function serializeWithType(
if (type.Vector.Vector === 'U8') {
se.serializeBytes(fromHexString(sub))
} else if (type.Vector === 'Address') {
se.serializeBytes(arrayify(sub))
const accountAddress = addressToSCS(sub)
accountAddress.serialize(se)
} else if (type.Vector) {
// array of other types: vector<u8>
se[`serialize${ type.Vector }`](sub);
Expand Down

0 comments on commit 1e7525b

Please sign in to comment.