0.3.2 • Published 23 days ago
@soulwallet/decoder v0.3.2
Table of Contents
Installing
Using npm:
$ npm install @soulwallet/decoder
Using yarn:
$ yarn add @soulwallet/decoder
Using pnpm:
$ pnpm add @soulwallet/decoder
Once the package is installed, you can import the library using import
approach:
import { Decoder } from "@soulwallet/decoder";
Example
import { ethers } from "ethers";
import { ABI_SoulWallet } from "@soulwallet/abi";
import { Decoder } from "@soulwallet/decoder";
async function main(): Promise<void> {
let callData = '0x';
const abi = new ethers.Interface(ABI_SoulWallet);
let to: string[] = [
'0xccaE58775FcBc01E0bB965Cc153363CD93e2d0f4',
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
];
let value: string[] = [
'11',
'12'
];
// encode erc20 transfer
const erc20Interface = new ethers.Interface([
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]);
let data: string[] = [
'0x32',
erc20Interface.encodeFunctionData("transfer", ['0x4521C9aD6A3D4230803aB752Ed238BE11F8B342F', '0xf'])
];
if (to.length > 1) {
if (value[0] !== '0x0') {
callData = abi.encodeFunctionData("executeBatch(address[],uint256[],bytes[])", [to, value, data]);
} else {
callData = abi.encodeFunctionData("executeBatch(address[],bytes[])", [to, data]);
}
} else {
callData = abi.encodeFunctionData("execute", [to[0], value[0], data[0]]);
}
const ret = await Decoder.decode(1, '0x1', '0x2', callData);
if (ret.isErr()) {
throw new Error(
ret.ERR.message
);
}
if (ret.OK.length !== 2) {
throw new Error('result length error');
}
console.log(ret.OK);
}
- output
[
{
"from": "0x1",
"to": "0xccaE58775FcBc01E0bB965Cc153363CD93e2d0f4",
"value": 11
},
{
"from": "0x1",
"method": {
"bytes4": "0xa9059cbb",
"name": "transfer",
"params": [
"0x4521C9aD6A3D4230803aB752Ed238BE11F8B342F",
15
],
"text": "transfer(address,uint256)"
},
"to": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
"toInfo": {
"address": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
"chainId": 1,
"decimals": 6,
"logoURI": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48/logo.png",
"name": "USDCoin",
"symbol": "USDC"
}
}
]
License
ISC
0.3.2
23 days ago
0.3.1
24 days ago
0.3.0
2 months ago
0.2.0
3 months ago
0.1.5
5 months ago
0.1.4
6 months ago
0.1.3
6 months ago
0.1.2
7 months ago
0.1.1
9 months ago
0.0.13
9 months ago
0.0.12
9 months ago
0.0.11
9 months ago
0.0.10
9 months ago
0.0.9
9 months ago
0.0.8
10 months ago
0.0.7
10 months ago
0.0.6
10 months ago
0.0.5
10 months ago
0.0.4
10 months ago
0.0.3
10 months ago
0.0.2
10 months ago
0.0.1
10 months ago