Skip to content

Commit

Permalink
handle EvmAddress when passed to addAddress as a ContractFunctionPara…
Browse files Browse the repository at this point in the history
…meter (#1819)

Signed-off-by: Petar Tonev <[email protected]>
  • Loading branch information
petreze authored Aug 25, 2023
1 parent 51e722d commit 88f6ae4
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/contract/ContractFunctionParameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import BigNumber from "bignumber.js";
import * as util from "../util.js";
import { defaultAbiCoder } from "@ethersproject/abi";
import { arrayify } from "@ethersproject/bytes";
import EvmAddress from "../EvmAddress.js";

export default class ContractFunctionParameters {
constructor() {
Expand Down Expand Up @@ -1274,49 +1275,60 @@ export default class ContractFunctionParameters {
}

/**
* @param {string} value
* @param {string | EvmAddress} value
* @returns {ContractFunctionParameters}
*/
addAddress(value) {
// Allow `0x` prefix
if (value.length !== 40 && value.length !== 42) {
throw new Error(
"`address` type requires parameter to be 40 or 42 characters"
);
let address;
if (typeof value === "string") {
// Allow `0x` prefix
if (value.length !== 40 && value.length !== 42) {
throw new Error(
"`address` type requires parameter to be 40 or 42 characters"
);
}
address = value;
} else {
address = value.toString();
}

const par =
value.length === 40
? hex.decode(value)
: hex.decode(value.substring(2));
address.length === 40
? hex.decode(address)
: hex.decode(address.substring(2));

this._selector.addAddress();

return this._addParam(par, false);
}

/**
* @param {string[]} value
* @param {string[] | EvmAddress[]} value
* @returns {ContractFunctionParameters}
*/
addAddressArray(value) {
/**
* @type {Uint8Array[]}
*/
const par = [];

// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const [_, entry] of value.entries()) {
if (entry.length !== 40 && entry.length !== 42) {
throw new Error(
"`address` type requires parameter to be 40 or 42 characters"
);
let address;
if (typeof entry === "string") {
if (entry.length !== 40 && entry.length !== 42) {
throw new Error(
"`address` type requires parameter to be 40 or 42 characters"
);
}
address = entry;
} else {
address = entry.toString();
}

const buf =
entry.length === 40
? hex.decode(entry)
: hex.decode(entry.substring(2));
address.length === 40
? hex.decode(address)
: hex.decode(address.substring(2));

par.push(buf);
}
Expand Down Expand Up @@ -1683,6 +1695,7 @@ function argumentToBytes(param, ty) {
case ArgumentType.uint256: {
let paramToHex = param.toString(16);

// @ts-ignore
if (param > 0 || param == 0) {
paramToHex = "0x" + paramToHex;
} else {
Expand Down

0 comments on commit 88f6ae4

Please sign in to comment.