Skip to content

Commit

Permalink
sendEIP1559Transaction success
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason Smith committed Jan 13, 2025
1 parent 2183127 commit 4107c55
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
49 changes: 49 additions & 0 deletions networks/ethereum/devnet/__tests__/noethers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,53 @@ describe('sending Tests', () => {
expect(delta).toBe(transferAmount);
});

it('should send ETH from sender to receiver via EIP-1559, and check balances', async () => {
// 1) 先查询发送方和接收方初始余额
const beforeSenderBalance = await signerSender.getBalance();
const beforeReceiverBalance = await signerReceiver.getBalance();

console.log('Sender balance before:', beforeSenderBalance.toString());
console.log('Receiver balance before:', beforeReceiverBalance.toString());

// 2) 准备转账金额 (示例:0.01 ETH)
const valueWei = 10000000000000000n; // 0.01 ETH

// 3) 发送前再次查询发送方余额
const currentSenderBalance = await signerSender.getBalance();
console.log('Sender balance right before sending:', currentSenderBalance.toString());

// 4) 使用 EIP-1559 方式发送交易
const { txHash, wait } = await transfer.sendEIP1559TransactionAutoGasLimit(
receiverAddress,
valueWei
);
expect(txHash).toMatch(/^0x[0-9a-fA-F]+$/);

console.log('EIP-1559 sending txHash:', txHash);

// 5) 等待交易上链并获取回执
const receipt = await wait();
expect(receipt.status).toBe('0x1'); // '0x1' 表示交易成功

// 6) 检查交易后余额
const afterSenderBalance = await signerSender.getBalance();
const afterReceiverBalance = await signerReceiver.getBalance();

console.log('Sender balance after:', afterSenderBalance.toString());
console.log('Receiver balance after:', afterReceiverBalance.toString());

// 7) 验证余额变化
const senderDelta = beforeSenderBalance - afterSenderBalance; // 发送方实际减少的金额
const receiverDelta = afterReceiverBalance - beforeReceiverBalance; // 接收方实际增加的金额

console.log('Sender delta:', senderDelta.toString());
console.log('Receiver delta:', receiverDelta.toString());

// 接收方应增加与转账额相同
expect(receiverDelta).toBe(valueWei);

// 发送方损失的余额应 >= 转账额(多出的部分是 Gas 费)
expect(senderDelta).toBeGreaterThanOrEqual(valueWei);
}, 60000);

});
12 changes: 11 additions & 1 deletion networks/ethereum/src/signers/SignerFromBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,18 @@ export class SignerFromBrowser {
method: 'eth_feeHistory',
params: [1, 'latest', []],
});
const baseFeeHex = feeHistory?.baseFeePerGas;

const baseFeeArray = feeHistory?.baseFeePerGas;
// (可做个防护,确保确实是数组)
if (!Array.isArray(baseFeeArray) || baseFeeArray.length === 0) {
throw new Error(`Invalid feeHistory response: ${JSON.stringify(baseFeeArray)}`);
}

// 2. 取数组最后一个元素(对应最新区块),然后转成 BigInt
const baseFeeHex = baseFeeArray[baseFeeArray.length - 1];
const baseFeePerGas = BigInt(baseFeeHex);

// 3. 返回 baseFee + tip
return baseFeePerGas + maxPriorityFeePerGas;
}
}
17 changes: 14 additions & 3 deletions networks/ethereum/src/signers/SignerFromPrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ export class SignerFromPrivateKey {

const { r, s, recovery } = this.signWithRecovery(msgHash);

// For typed transactions, v = 27 + recovery
const v = 27 + recovery;
// For typed transactions, v = recovery
const v = recovery;
const vHex = this.toHexPadded(v);

// RLP( [chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, accessList, v, r, s] )
Expand Down Expand Up @@ -478,7 +478,18 @@ export class SignerFromPrivateKey {
};
const resp = await axios.post(this.rpcUrl, payload);

const baseFeePerGas = BigInt(resp.data.result.baseFeePerGas);
// 1. 先拿到 baseFeePerGas 数组
const baseFeeArray = resp.data.result.baseFeePerGas;
// (可做个防护,确保确实是数组)
if (!Array.isArray(baseFeeArray) || baseFeeArray.length === 0) {
throw new Error(`Invalid feeHistory response: ${JSON.stringify(baseFeeArray)}`);
}

// 2. 取数组最后一个元素(对应最新区块),然后转成 BigInt
const baseFeeHex = baseFeeArray[baseFeeArray.length - 1];
const baseFeePerGas = BigInt(baseFeeHex);

// 3. 返回 baseFee + tip
return baseFeePerGas + maxPriorityFeePerGas;
}
}

0 comments on commit 4107c55

Please sign in to comment.