Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Nov 17, 2024
1 parent 94759ee commit cbffabf
Show file tree
Hide file tree
Showing 19 changed files with 596 additions and 754 deletions.
174 changes: 102 additions & 72 deletions memo.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
var assert = require("assert");
const { Header, FieldsV0, EncodingFormat, OpCode, EncodeToBytes } = require('./memo');
const { Web3 } = require('web3');
const {
Header,
FieldsV0,
EncodingFormat,
OpCode,
EncodeToBytes,
} = require("./memo");
const { Web3 } = require("web3");
const web3 = new Web3();

// Test data
Expand All @@ -9,80 +15,104 @@ const payload = new TextEncoder().encode("a payload");
const revertAddress = "tb1q6rufg6myrxurdn0h57d2qhtm9zfmjw2mzcm05q";

// Test case for memo ABI encoding
function testMemoAbi() {
// Create memo header
const header = new Header(EncodingFormat.EncodingFmtABI, OpCode.DepositAndCall);

// Create memo fields
const fields = new FieldsV0(receiver, payload, revertAddress);

// Encode standard memo
const encodedMemo = EncodeToBytes(header, fields);
const encodedMemoHex = web3.utils.bytesToHex(encodedMemo).slice(2);

// Expected output
const expectedHex = '5a001007' + // header
'000000000000000000000000ea9808f0ac504d1f521b5bbdfc33e6f1953757a7' + // receiver
'0000000000000000000000000000000000000000000000000000000000000060' + // payload offset
'00000000000000000000000000000000000000000000000000000000000000a0' + // revertAddress offset
'0000000000000000000000000000000000000000000000000000000000000009' + // payload length
'61207061796c6f61640000000000000000000000000000000000000000000000' + // payload
'000000000000000000000000000000000000000000000000000000000000002a' + // revertAddress length
'746231713672756667366d7972787572646e3068353764327168746d397a666d6a77326d7a636d30357100000000000000000000000000000000000000000000'; // revertAddress

// Compare with expected output
assert.strictEqual(encodedMemoHex, expectedHex, 'ABI encoding failed: encoded bytes do not match expected');

console.log("Test passed: testMemoAbi");
}
const testMemoAbi = () => {
// Create memo header
const header = new Header(
EncodingFormat.EncodingFmtABI,
OpCode.DepositAndCall
);

// Create memo fields
const fields = new FieldsV0(receiver, payload, revertAddress);

// Encode standard memo
const encodedMemo = EncodeToBytes(header, fields);
const encodedMemoHex = web3.utils.bytesToHex(encodedMemo).slice(2);

// Expected output
const expectedHex =
"5a001007" + // header
"000000000000000000000000ea9808f0ac504d1f521b5bbdfc33e6f1953757a7" + // receiver
"0000000000000000000000000000000000000000000000000000000000000060" + // payload offset
"00000000000000000000000000000000000000000000000000000000000000a0" + // revertAddress offset
"0000000000000000000000000000000000000000000000000000000000000009" + // payload length
"61207061796c6f61640000000000000000000000000000000000000000000000" + // payload
"000000000000000000000000000000000000000000000000000000000000002a" + // revertAddress length
"746231713672756667366d7972787572646e3068353764327168746d397a666d6a77326d7a636d30357100000000000000000000000000000000000000000000"; // revertAddress

// Compare with expected output
assert.strictEqual(
encodedMemoHex,
expectedHex,
"ABI encoding failed: encoded bytes do not match expected"
);

console.log("Test passed: testMemoAbi");
};

// Test case for memo compact short encoding
function testMemoCompactShort() {
// Create memo header
const header = new Header(EncodingFormat.EncodingFmtCompactShort, OpCode.DepositAndCall);

// Create memo fields
const fields = new FieldsV0(receiver, payload, revertAddress);

// Encode standard memo
const encodedMemo = EncodeToBytes(header, fields);
const encodedMemoHex = web3.utils.bytesToHex(encodedMemo).slice(2);

// Expected output
const expectedHex = '5a011007' + // header
'ea9808f0ac504d1f521b5bbdfc33e6f1953757a7' + // receiver
'0961207061796c6f6164' + // payload
'2a746231713672756667366d7972787572646e3068353764327168746d397a666d6a77326d7a636d303571'; // revertAddress

// Compare with expected output
assert.strictEqual(encodedMemoHex, expectedHex, 'Compact short encoding failed: encoded bytes do not match expected');

console.log("Test passed: testMemoCompactShort");
}
const testMemoCompactShort = () => {
// Create memo header
const header = new Header(
EncodingFormat.EncodingFmtCompactShort,
OpCode.DepositAndCall
);

// Create memo fields
const fields = new FieldsV0(receiver, payload, revertAddress);

// Encode standard memo
const encodedMemo = EncodeToBytes(header, fields);
const encodedMemoHex = web3.utils.bytesToHex(encodedMemo).slice(2);

// Expected output
const expectedHex =
"5a011007" + // header
"ea9808f0ac504d1f521b5bbdfc33e6f1953757a7" + // receiver
"0961207061796c6f6164" + // payload
"2a746231713672756667366d7972787572646e3068353764327168746d397a666d6a77326d7a636d303571"; // revertAddress

// Compare with expected output
assert.strictEqual(
encodedMemoHex,
expectedHex,
"Compact short encoding failed: encoded bytes do not match expected"
);

console.log("Test passed: testMemoCompactShort");
};

// Test case for memo compact long encoding
function testMemoCompactLong() {
// Create memo header
const header = new Header(EncodingFormat.EncodingFmtCompactLong, OpCode.DepositAndCall);

// Create memo fields
const fields = new FieldsV0(receiver, payload, revertAddress);

// Encode standard memo
const encodedMemo = EncodeToBytes(header, fields);
const encodedMemoHex = web3.utils.bytesToHex(encodedMemo).slice(2);

// Expected output
const expectedHex = '5a021007' + // header
'ea9808f0ac504d1f521b5bbdfc33e6f1953757a7' + // receiver
'090061207061796c6f6164' + // payload
'2a00746231713672756667366d7972787572646e3068353764327168746d397a666d6a77326d7a636d303571'; // revertAddress

// Compare with expected output
assert.strictEqual(encodedMemoHex, expectedHex, 'Compact long encoding failed: encoded bytes do not match expected');

console.log("Test passed: testMemoCompactLong");
}
const testMemoCompactLong = () => {
// Create memo header
const header = new Header(
EncodingFormat.EncodingFmtCompactLong,
OpCode.DepositAndCall
);

// Create memo fields
const fields = new FieldsV0(receiver, payload, revertAddress);

// Encode standard memo
const encodedMemo = EncodeToBytes(header, fields);
const encodedMemoHex = web3.utils.bytesToHex(encodedMemo).slice(2);

// Expected output
const expectedHex =
"5a021007" + // header
"ea9808f0ac504d1f521b5bbdfc33e6f1953757a7" + // receiver
"090061207061796c6f6164" + // payload
"2a00746231713672756667366d7972787572646e3068353764327168746d397a666d6a77326d7a636d303571"; // revertAddress

// Compare with expected output
assert.strictEqual(
encodedMemoHex,
expectedHex,
"Compact long encoding failed: encoded bytes do not match expected"
);

console.log("Test passed: testMemoCompactLong");
};

// Run the test cases
testMemoAbi();
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/encodeToBytes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Web3 } from "web3";
import { ethers } from "ethers";
import { Web3 } from "web3";
import { isAddress } from "web3-validator";

// Memo identifier byte
Expand Down Expand Up @@ -137,4 +137,4 @@ const encodeDataCompact = (
return Buffer.concat([encodedLength, data]);
};

export { Header, FieldsV0, EncodingFormat, OpCode, encodeToBytes };
export { encodeToBytes, EncodingFormat, FieldsV0, Header, OpCode };
9 changes: 5 additions & 4 deletions test/memo.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Web3 from "web3";

import {
Header,
FieldsV0,
encodeToBytes,
EncodingFormat,
FieldsV0,
Header,
OpCode,
encodeToBytes,
} from "../packages/client/src/encodeToBytes";
import Web3 from "web3";

const web3 = new Web3();

Expand Down
2 changes: 2 additions & 0 deletions typechain-types/@openzeppelin/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
import type * as interfaces from "./interfaces";
export type { interfaces };
import type * as token from "./token";
export type { token };
Loading

0 comments on commit cbffabf

Please sign in to comment.