0.1.7 • Published 4 months ago

@soulwallet_test/decoder v0.1.7

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
4 months ago

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.1.7

4 months ago

0.1.6

4 months ago

0.1.5

4 months ago

0.1.2

7 months ago

0.1.1

7 months ago

0.1.0

8 months ago