EIP1559与传统Gas定价模型转账逻辑

传统的gas定价模型(Txn Type===0)

20230104-112422.jpg

import Web3 from 'web3';
import { AbiItem } from 'web3-utils/types/index.d';
import erc20 from '../abis/erc20.json';
import { FeeMarketEIP1559Transaction as Tx } from '@ethereumjs/tx';
import { Common, Chain, Hardfork } from '@ethereumjs/common';
import config from '../constants/service.config';
const webSocketProvider = (rpcUrl: string) => {
  return new Web3.providers.HttpProvider(rpcUrl);
};
const provider = (rpcUrl: string = config.alchemyEthBaseUrl) => {
  return new Web3(webSocketProvider(rpcUrl));
};
const transfer = async (
  { ...rest }: any
) => {
  const {
    privateKey,
    contractAddress,
    tokenAddress,
    recipientAddress,
    rpcUrl,
    senderAddress,
    amount,
    ...args
  } = rest;
  const web3 = provider();
  const { toHex, toWei, hexToNumber } = web3.utils;
  const gasPrice = await web3.eth.getGasPrice();
  try {
    const nounce = await web3.eth.getTransactionCount(senderAddress, 'pending');
    let rawTx = {};
    if (['eth', 'matic'].includes(tokenAddress.toLowerCase())) {
      // 以太坊主币
      rawTx = {
        to: recipientAddress,
        value: toHex(toWei(String(amount))),
      };
    } else {
      // 符合erc20规范的代币
      const contract = new web3.eth.Contract(erc20 as AbiItem[], tokenAddress, {
        from: senderAddress,
      });
      const transferEvent = contract.methods.transfer(
        recipientAddress,
        toHex(toWei(String(amount)))
      );
      const limit = await transferEvent.estimateGas(); // gas limit
      rawTx = {
        to: tokenAddress,
        gasLimit: toHex(Math.ceil(limit * 1.2)),
        data: transferEvent.encodeABI(),
      };
    }
    /**
     * 组装交易数据:
     * to: 
     *    如果是以太坊主币,to是收款人地址
     *    如果是erc20代码,to是协议地址,收款人地址在合约方法:contrack.methods.transfer中定义
     * value:
     *    如果是以太坊主币,value是转账金额
     *    如果是erc20代码,value是`toHex(0)`,转账金额在合约方法:contrack.methods.transfer中定义
     * maxPriorityFeePerGas
     *    Txn Type=2所必需,提高优先级,给予矿工的小费
     * maxFeePerGas
     *    Txn Type=2所必需,提高优先级,给予矿工小费的最大值
    */
    rawTx = {
      from: senderAddress,
      nonce: toHex(nounce),
      gasLimit: toHex(21000), //"21000", 
      gasPrice: toHex(gasPrice),
      value: '0x00', //"10000000",//'0x00',
      ...rawTx,
    };
    /**
     * 以太坊资源的通用实现类;
     *    创建以太坊链和分叉的实例,实现EIP1559功能需要选择London硬分叉
     *    new Common({ chain: Chain.Mainnet, eips: [1559] })
    */
    const common = new Common({
      chain: Chain.Goerli,
      hardfork: Hardfork.London,
    });
    const unsignedTx = Tx.fromTxData(rawTx, { common });
    const secretKey = Buffer.from(privateKey.slice(2), 'hex');
    const signedTx = unsignedTx.sign(secretKey);
    const serializedTx = signedTx.serialize();
    const signedTransactionData = '0x' + serializedTx.toString('hex');
    web3.eth
      .sendSignedTransaction(signedTransactionData, async function (err, hash) {
        if (!err) {
          /**
           * gasfee = `${web3.utils.fromWei(
            (Number((rawTx as any).gasLimit) * Number(gasPrice)).toString()
          )}${wallet.unit}`;
          */
          console.log('-----hash-------', hash);
          const url = `https://goerli.etherscan.io/tx/${hash}`;
          const accepted = `https://etherscan.io/api?module=localchk&action=txexist&txhash=${hash}`;
          console.log('TX Link', url);
          console.log('Accepted', accepted);
        } else {
          console.log('-----error', err);
        }
      })
      .on('receipt', (receipt) => {
        console.log('TX receipt', receipt);
        //             blockHash: ""
        // blockNumber: 
        // contractAddress: null
        // cumulativeGasUsed: 
        // effectiveGasPrice: 
        // from: ""
        // gasUsed: 21000
        // logs: []
        // logsBloom: ""
        // status: true
        // to: ""
        // transactionHash: ""
        // transactionIndex: 
        // type: "0x0"
        const { gasUsed, status } = receipt;
        /**
         * gasfee = `${web3.utils.fromWei(
          (gasUsed * Number(gasPrice)).toString()
        )}${wallet.unit}`;
        */
      });
  } catch (e) {
    console.log(e);
  }
};

伦敦硬分叉EIP1559(Txn Type===2)

20230104-112437.jpg

import Web3 from 'web3';
import { AbiItem } from 'web3-utils/types/index.d';
import erc20 from '../abis/erc20.json';
import { FeeMarketEIP1559Transaction as Tx } from '@ethereumjs/tx';
import { Common, Chain, Hardfork } from '@ethereumjs/common';
import config from '../constants/service.config';
const webSocketProvider = (rpcUrl: string) => {
  return new Web3.providers.HttpProvider(rpcUrl);
};
const provider = (rpcUrl: string = config.alchemyEthBaseUrl) => {
  return new Web3(webSocketProvider(rpcUrl));
};
const transfer = async (
  { ...rest }: any
) => {
  const {
    privateKey,
    contractAddress,
    tokenAddress,
    recipientAddress,
    rpcUrl,
    senderAddress,
    amount,
    ...args
  } = rest;
  const web3 = provider();
  const { toHex, toWei, hexToNumber } = web3.utils;
  const gasPrice = await web3.eth.getGasPrice();
  try {
    const nounce = await web3.eth.getTransactionCount(senderAddress, 'pending');
    let rawTx = {};
    if (['eth', 'matic'].includes(tokenAddress.toLowerCase())) {
      // 以太坊主币
      rawTx = {
        to: recipientAddress,
        value: toHex(toWei(String(amount))),
      };
    } else {
      // 符合erc20规范的代币
      const contract = new web3.eth.Contract(erc20 as AbiItem[], tokenAddress, {
        from: senderAddress,
      });
      const transferEvent = contract.methods.transfer(
        recipientAddress,
        toHex(toWei(String(amount)))
      );
      const limit = await transferEvent.estimateGas(); // gas limit
      rawTx = {
        to: tokenAddress,
        gasLimit: toHex(Math.ceil(limit * 1.2)),
        data: transferEvent.encodeABI(),
      };
    }
    // maxPriorityFeePerGas: Txn Type === 2所需要的字段
    const maxPriorityFeePerGasRes = request.post(config.alchemyEthBaseUrl, {
      id: 1,
      jsonrpc: '2.0',
      method: 'eth_maxPriorityFeePerGas',
    });
    const maxPriorityFeePerGas = ((await maxPriorityFeePerGasRes) as any)
      .result;
    const getBlockRes = web3.eth.getBlock('latest');
    const baseFeePerGas = ((await getBlockRes) as any).baseFeePerGas - 1;
    /**
     * 组装交易数据:
     * to: 
     *    如果是以太坊主币,to是收款人地址
     *    如果是erc20代码,to是协议地址,收款人地址在合约方法:contrack.methods.transfer中定义
     * value:
     *    如果是以太坊主币,value是转账金额
     *    如果是erc20代码,value是`toHex(0)`,转账金额在合约方法:contrack.methods.transfer中定义
     * maxPriorityFeePerGas
     *    Txn Type=2所必需,提高优先级,给予矿工的小费
     * maxFeePerGas
     *    Txn Type=2所必需,提高优先级,给予矿工小费的最大值
    */
    rawTx = {
      from: senderAddress,
      nonce: toHex(nounce),
      gasLimit: toHex(21000), //"21000",
      maxPriorityFeePerGas: maxPriorityFeePerGas,
      maxFeePerGas: toHex(
        2 * baseFeePerGas + hexToNumber(maxPriorityFeePerGas)
      ),
      value: '0x00', //"10000000",//'0x00',
      ...rawTx,
    };
    /**
     * 以太坊资源的通用实现类;
     *    创建以太坊链和分叉的实例,实现EIP1559功能需要选择London硬分叉
     *    new Common({ chain: Chain.Mainnet, eips: [1559] })
    */
    const common = new Common({
      chain: Chain.Goerli,
      hardfork: Hardfork.London,
    });
    const unsignedTx = Tx.fromTxData(rawTx, { common });
    const secretKey = Buffer.from(privateKey.slice(2), 'hex');
    const signedTx = unsignedTx.sign(secretKey);
    const serializedTx = signedTx.serialize();
    const signedTransactionData = '0x' + serializedTx.toString('hex');
    web3.eth
      .sendSignedTransaction(signedTransactionData, async function (err, hash) {
        if (!err) {
          /**
           * gasfee = `${web3.utils.fromWei(
            (Number((rawTx as any).gasLimit) * Number(gasPrice)).toString()
          )}${wallet.unit}`;
          */
          console.log('-----hash-------', hash);
          const url = `https://goerli.etherscan.io/tx/${hash}`;
          const accepted = `https://etherscan.io/api?module=localchk&action=txexist&txhash=${hash}`;
          console.log('TX Link', url);
          console.log('Accepted', accepted);
        } else {
          console.log('-----error', err);
        }
      })
      .on('receipt', (receipt) => {
        console.log('TX receipt', receipt);
        // blockHash: ""
        // blockNumber: 
        // contractAddress: null
        // cumulativeGasUsed: 
        // effectiveGasPrice: 
        // from: ""
        // gasUsed: 21000
        // logs: []
        // logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
        // status: true
        // to: ""
        // transactionHash: ""
        // transactionIndex: 
        // type: "0x0"
        const { gasUsed, status } = receipt;
        /**
         * gasfee = `${web3.utils.fromWei(
          (gasUsed * Number(gasPrice)).toString()
        )}${wallet.unit}`;
        */
      });
  } catch (e) {
    console.log(e);
  }
};

外部签名

import Web3 from 'web3';
import { bufArrToArr, toBuffer, bigIntToHex, addHexPrefix } from '@ethereumjs/util'
...
// signatures是外部服务签名
const { toHex, toWei, toDecimal } = web3.utils;
const { r_hex: r, s_hex: s, recovery_id_hex } = signatures
const chainId = unsignedTx.common.chainId()
const v = chainId === undefined
  ? BigInt(toDecimal(addHexPrefix(recovery_id_hex)) + 27)
  : BigInt(toDecimal(addHexPrefix(recovery_id_hex)) + 35) + BigInt(chainId) * BigInt(2)
const toSignTx = {
  nonce: unsignedTx.nonce,
  gasPrice: unsignedTx.gasPrice,
  gasLimit: unsignedTx.gasLimit,
  to: unsignedTx.to,
  value: unsignedTx.value,
  data: unsignedTx.data,
  v,
  r: addHexPrefix(r),
  s: addHexPrefix(s),
}
const signedTx = Tx.fromTxData(toSignTx, { common })
const serializeSignedTx = signedTx.serialize()
const signedTxHex = addHexPrefix(serializeSignedTx.toString('hex'))

web3.eth.sendSignedTransaction(signedTxHex, async function (err, hash) {
    if (!err) {
      console.log('-----hash-------', hash);
      const url = `https://goerli.etherscan.io/tx/${hash}`;
      const accepted = `https://etherscan.io/api?module=localchk&action=txexist&txhash=${hash}`;
      console.log('TX Link', url);
      console.log('Accepted', accepted);
    } else {
       console.log('-----error', err);
    }
})

1.2k 声望
71 粉丝
0 条评论
推荐阅读
web3从入门到实战-理论篇
中心化网络已经帮助数十亿人融入了互联网,并在其上创建了稳定、可靠的基础设施。 与此同时,少数中心化巨头几乎垄断了互联网,甚至可以为所欲为。Web3 是摆脱这一困境的方案。 不同于科技巨头垄断的传统互联网,...

MangoGoing4阅读 1.9k评论 2

封面图
从0系列(第一篇)---NFT平台的底层关系及注册流程
nft的价值不在现实世界而在区块链和虚拟世界。nft的平台,功能在nft内容审核,替内容创造者生成区块链上证明也就是nft,价值在在流量,流量带来的信用凭证。

一句话1阅读 2.3k

代码走读(第一篇)---ethereumj之Repository快照功能
ethereum有个replayBlock功能,就是将块放到指定的世界状态中,重新执行一次,获取执行结果。这个功能依赖于能够获得指定块高时的世界状态,具体就是根据块hash获取块所在的Repository快照。

一句话阅读 824

4EVERLAND 因对 Dfinity 生态系统的贡献而获得 Dfinity 资助
DFINITY 开发者资助计划旨在通过为有前途的开发者和团队提供支持来促进互联网计算机生态系统的发展,使全球更多的开发者能够访问和使用互联网计算机。

4EVERLAND社区阅读 627

封面图
每月明星计划(12 月),ECHO:我们的意见万岁!
在 11 月的 MSP 竞赛中,被选中的项目Owlando以其先进的 UGC Metaverse 概念及其与朋友创建、交流和玩耍的有趣方式引起了评委的注意,最终将结果构建到 NFT 保存中。在12月的评选中,基于Arweave去中心化社交参与...

4EVERLAND社区阅读 597

封面图
4EVERLAND | AWS月度明星计划(一月):新年最好的开始
MSP I 的最后一个月,即 MSP I January,现在可以申请了!我们很高兴看到本月将有哪些项目脱颖而出!在 MSP I 的最后一个月,4EVERLAND 将与我们的合作伙伴一起努力,努力得出一个好的结论。

4EVERLAND社区阅读 515

封面图
1.2k 声望
71 粉丝
宣传栏