Skip to content

Commit

Permalink
optimise code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason Smith committed Jan 13, 2025
1 parent 4107c55 commit 8a7930d
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 19 deletions.
15 changes: 3 additions & 12 deletions networks/ethereum/devnet/__tests__/noethers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,17 @@ describe('sending Tests', () => {
});

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
Expand All @@ -169,28 +165,23 @@ describe('sending Tests', () => {

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

// 5) 等待交易上链并获取回执
const receipt = await wait();
expect(receipt.status).toBe('0x1'); // '0x1' 表示交易成功
expect(receipt.status).toBe('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; // 接收方实际增加的金额
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);

Expand Down
3 changes: 0 additions & 3 deletions networks/ethereum/src/signers/SignerFromBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,13 @@ export class SignerFromBrowser {
});

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;
}
}
4 changes: 0 additions & 4 deletions networks/ethereum/src/signers/SignerFromPrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,18 +478,14 @@ export class SignerFromPrivateKey {
};
const resp = await axios.post(this.rpcUrl, payload);

// 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 8a7930d

Please sign in to comment.