Utility libraries for Solidity smart contract
yarn add -D encoding-helper
const { encodingHelper, hashingHelper, txUtils } = require('encoding-helper');
Convert a string with maximum 10 characters into a 10-bytes hex string. If the input string length is less than 10 characters, the result will be padded with trailing zeros, while with input string greater than 10 characters, the excess will be truncated before encoding.
- Example:
encodingHelper.toBytes10Name("Lorem")
==> '0x4c6f72656d0000000000'
encodingHelper.toBytes10Name("LoremIpsumDolorSit")
==> '0x4c6f72656d497073756d'
Get the first 10-byte string from an address string.
- Example:
encodingHelper.first10('0xECEfEf2074f56ced5A5e68De3Aff44ed5798B35E')
==> '0xecefef2074f56ced5a5e'
Generate a BigNumber id
from a name
, an index
, a token address
, and a product contract address
.
- Example:
encodingHelper.getId('Lorem', 65535, '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972','0x6846ceF88276D453e38B92F4BFe70D29643571B8')
==> BigNumber {
_hex: '0x4c6f72656d0000000000ffffdef0e171272f6f9061636846cef88276d453e38b',
_isBigNumber: true
}
==> '34572686050035833475315598151429145236062344499814420574865813453149958300555'
Get the last 10-byte string from an id
. Usually used for validating the product contract address
.
- Example:
let id = encodingHelper.getId('Lorem', 65535,'0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972','0x6846ceF88276D453e38B92F4BFe70D29643571B8');
encodingHelper.last10(id);
==> '0x6846cef88276d453e38b'
Recover the information used to generate the id
.
- Example:
let id = encodingHelper.getId('Lorem', 65535,'0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972','0x6846ceF88276D453e38B92F4BFe70D29643571B8');
==> BigNumber {
_hex: '0x4c6f72656d0000000000ffffdef0e171272f6f9061636846cef88276d453e38b',
_isBigNumber: true
}
==> '34572686050035833475315598151429145236062344499814420574865813453149958300555'
encodingHelper.decodeId('0x4c6f72656d0000000000ffffdef0e171272f6f9061636846cef88276d453e38b');
==> [
'Lorem', 65535,
'0xdef0e171272f6f906163',
'0x6846cef88276d453e38b'
]
encodingHelper.decodeId('34572686050035833475315598151429145236062344499814420574865813453149958300555');
==> [
'Lorem', 65535,
'0xdef0e171272f6f906163',
'0x6846cef88276d453e38b'
]
encodingHelper.decodeId(id);
==> [
'Lorem', 65535,
'0xdef0e171272f6f906163',
'0x6846cef88276d453e38b'
]
Get the productId generated by combining name
, token
address, and product
address.
If the name
's length is lesser or equal to 10, it is converted to a bytes10 value. If name
is longer than 10 characters, it is hashed using keccak256
function and take the first 10 bytes.
- Example
// Name <= 10 characters
encodingHelper.getProductId('LoremIpsum', '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', '0x6846ceF88276D453e38B92F4BFe70D29643571B8')
==> '0x4c6f72656d497073756d0000def0e171272f6f9061636846cef88276d453e38b'
// Name > 10 characters
encodingHelper.getProductId('LoremIpsumDolorsitAmet', '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', '0x6846ceF88276D453e38B92F4BFe70D29643571B8')
==> '0x872b837047e81dd64a950000def0e171272f6f9061636846cef88276d453e38b'
Return the encoded name (first 10 bytes) from hexId
- Example
encodingHelper.getNameHashFromId('0x872b837047e81dd64a950000def0e171272f6f9061636846cef88276d453e38b')
==> '0x872b837047e81dd64a95'
Return true
if the id
encoded from 6.
can be decoded and matches the value of name
, token
, and product
- Example:
// Name <= 10 characters
encodingHelper.verifyProductId('0x4c6f72656d497073756d0000def0e171272f6f9061636846cef88276d453e38b', 'LoremIpsum', '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', '0x6846ceF88276D453e38B92F4BFe70D29643571B8')
==> true
// Name > 10 characters
encodingHelper.verifyProductId('0x872b837047e81dd64a950000def0e171272f6f9061636846cef88276d453e38b', 'LoremIpsumDolorsitAmet', '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', '0x6846ceF88276D453e38B92F4BFe70D29643571B8')
==> true
// Name > 10 characters and does not match
encodingHelper.verifyProductId('0x872b837047e81dd64a950000def0e171272f6f9061636846cef88276d453e38b', 'LoremIpsumDolorsit', '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', '0x6846ceF88276D453e38B92F4BFe70D29643571B8')
==> false
// Name, token, and product don't match
encodingHelper.verifyProductId('0x872b837047e81dd64a950000def0e171272f6f9061636846cef88276d453e38b', 'LoremIpsumDolorsit', '0x6846ceF88276D453e38B92F4BFe70D29643571B8', '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972')
==> false
Returns the keccak256 hash of the input string. This function is a shortcut to ethers.utils.keccak256(BytesLike)
from ethers.js library.
- Example:
encodingHelper.keccak256("Lorem");
==> '0x53e60f9a9472b58c06a7620335d85ec3c20c4f93c0ac735d7a6622f2ef5b67c6'
This is a short cut to ethers.utils.solidityPack(string[], any[])
.
- Example:
hashingHelper.pack(["address", "string"], ['0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', "LoremIpsum"]);
==> '0xdef0e171272f6f906163a2ab4cd82a3ff85a39724c6f72656d497073756d'
Convert the UTF-8 input
string into a 10-bytes hex string. If the input
string is lesser than or equal to 10 characters in length, the output
is the direct hex-encoding of the input
. If the input
has more than 10 characters, it is hashed using keccak256
and take the first 10 bytes.
- Example
// Input <= 10 characters
encodingHelper.stringToBytes10orHash('LoremIpsum')
==> '0x4c6f72656d497073756d'
// Input > 10 characters
encodingHelper.stringToBytes10orHash('LoremIpsumDolorsitAmet')
==> '0x872b837047e81dd64a95'
Convert the UTF-8 input
string into a hex string representation of a bytes value with specified length
.
The length
must not exceed input
's length.
- Example:
encodingHelper.stringToBytes('Lorem Ipsum dolor sit amet. Consectetur adispicing elit', 10);
==> '0x4c6f72656d2049707375'
Convert a hexString
input into UTF-8 string value and remove any null
characters (\x00
) from the result.
- Example:
encodingHelper.hexToString('0x4c6f72656d2049707375');
==> 'Lorem Ipsu'
encodingHelper.hexToString('0x4c6f72656d0000000000');
==> 'Lorem'
14. encodeCryptoPayload(name: string, index: uint16, token: address, product: address, autoswap: bool | false): string
Encode a payload data that consists of name
, index
, token
, product
, autoswap
into a hex string for Crypto currency deposit.
name
: product nameindex
: account indextoken
: target token addressproduct
: target product addressautoswap
: enable/disable autoswap
encodeCryptoPayload('LOREM', 10, '0x1CBDD079370932F90A19902bA12D20c5D3716833', '0x625384874d83fccfFe55778F72B7C7f4dAb1CdBd', true)
==> '0x4c4f52454d0000000000000a1cbdd079370932f90a19625384874d83fccffe550000000000000000000000001cbdd079370932f90a19902ba12d20c5d3716833000000000000000000000000625384874d83fccffe55778f72b7c7f4dab1cdbd0000000000000000000000000000000000000000000000000000000000000001'
Decode a payload hex string to its original values.
decodeCryptoPayload('0x4c4f52454d0000000000000a1cbdd079370932f90a19625384874d83fccffe550000000000000000000000001cbdd079370932f90a19902ba12d20c5d3716833000000000000000000000000625384874d83fccffe55778f72b7c7f4dab1cdbd0000000000000000000000000000000000000000000000000000000000000001')
==> [
[ 'LOREM', 10, '0x1cbdd079370932f90a19', '0x625384874d83fccffe55' ],
'0x4c4f52454d0000000000000a1cbdd079370932f90a19625384874d83fccffe55',
'0x1CBDD079370932F90A19902bA12D20c5D3716833',
'0x625384874d83fccfFe55778F72B7C7f4dAb1CdBd',
true
]
16. encodeBankPayload(name: string, index: uint16, token: address, product: address, account: address): string
Encode a payload data that consists of name
, index
, token
, product
, account
into a hex string
-
name
: product name -
index
: account index -
token
: target token address -
product
: target product address -
account
: recipient -
Example:
encodingHelper.encodeBankPayload('Lorem', 65535,'0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972','0x6846ceF88276D453e38B92F4BFe70D29643571B8', '0xB34C74d889177ec905FEB41791936BCce1d7A5df')
==> '0x4c6f72656d0000000000ffffdef0e171272f6f9061636846cef88276d453e38b000000000000000000000000def0e171272f6f906163a2ab4cd82a3ff85a39720000000000000000000000006846cef88276d453e38b92f4bfe70d29643571b8000000000000000000000000b34c74d889177ec905feb41791936bcce1d7a5df'
Decode a payload hex string to its original values.
- Example:
let encodedPayload = encodingHelper.encodePayload('Lorem', 0, '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', '0x6846ceF88276D453e38B92F4BFe70D29643571B8', '0xB34C74d889177ec905FEB41791936BCce1d7A5df')
==> '0x4c6f72656d00000000000000def0e171272f6f9061636846cef88276d453e38b000000000000000000000000def0e171272f6f906163a2ab4cd82a3ff85a39720000000000000000000000006846cef88276d453e38b92f4bfe70d29643571b8000000000000000000000000b34c74d889177ec905feb41791936bcce1d7a5df'
encodingHelper.decodePayload(encodedPayload)
==> [
[ 'Lorem', 0, '0xdef0e171272f6f906163', '0x6846cef88276d453e38b' ],
'0x4c6f72656d00000000000000def0e171272f6f9061636846cef88276d453e38b',
'0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972',
'0x6846ceF88276D453e38B92F4BFe70D29643571B8',
'0xB34C74d889177ec905FEB41791936BCce1d7A5df'
]
Calculate a Hash based on current account/contract address
and a name
string.
- Example:
hashingHelper.toRoleHash('0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972', 'DEFAULT_SETTER_ROLE');
==> '0x6346e2e51a432d68c7d12ad11ff4bdf844f7e26e092c0d9787d0397d116098e1'
Convert keyString
string into a 32-bytes Hex string.
- Example:
hashingHelper.toBytes32Key('Lorem Ipsum dolor sit amet. Cons');
==> '0x4c6f72656d20497073756d20646f6c6f722073697420616d65742e20436f6e73'
If the input string is longer than 32 bytes, only the first 32 bytes will be used to create the bytes32 value.
hashingHelper.toBytes32Key('Lorem Ipsum dolor sit amet. Consectetur adispicing elit')
==> '0x4c6f72656d20497073756d20646f6c6f722073697420616d65742e20436f6e73'
Pack the first 10 bytes of the nameString
, the uint16
representation of index
, and the address
string into a bytes32 value.
- Example:
hashingHelper.toBytes32Value('LoremIpsum', 65535, '0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972');
==> '0x4c6f72656d497073756dffffdef0e171272f6f906163a2ab4cd82a3ff85a3972'
Recover the data that was packed in the previous function toBytes32Value
- Example:
hashingHelper.recoverOriginalValues('0x4c6f72656d497073756dffffdef0e171272f6f906163a2ab4cd82a3ff85a3972');
==> [
'LoremIpsum',
65535,
'0xDEF0e171272f6f906163a2ab4cd82a3fF85a3972'
]
Get the revert reason from a failed transaction by the provided txnHash
.
Note: provider
can be a Provider
object or an RPC url
string
- Example:
await txUtils.getRevertReason(provider, '0x7e9e75da5aed836d34d961d61d66b3108115c395cff03aa058fd794e800ba68d')
==> Failed to submit transaction: cannot estimate transaction: AccessControl: account 0xdec0da618b4fc8ff14028a5e27ca7e6a6d6b68d7 is missing role 0x0000000000000000000000000000000000000000000000000000000000000000.
Get the implementation address from a proxy contract with proxyAddress
- Example:
> await txUtils.getImplementationAddress(provider, '0xc4999A3631c017C45B5Ff3363eda6e8B9d81208E')
==> '0xa2BE51DBc0C901b75C7AD6a96a0BF66947ba20ba'