首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >区块链开发训练营

区块链开发训练营
EN

Ethereum用户
提问于 2022-11-13 11:22:46
回答 1查看 41关注 0票数 0

在区块链上部署智能合同时,我面临一个问题

代码语言:javascript
复制
TypeError: Cannot read properties of undefined (reading 'args')
at main (/mnt/d/Practice/LEarn/BlockChain/Trading/blockchain-developer-bootcamp/scripts/2_seed-exchange.js:102:30)

下面是导致错误的文件

代码语言:javascript
复制
require("truffle-test-utils").init();
const config = require("../src/config.json");

const tokens = (n) => {
return ethers.utils.parseUnits(n.toString(), "ether");
};

const wait = (seconds) => {
const milliseconds = seconds * 1000;
return new Promise((resolve) => setTimeout(resolve, milliseconds));
};

async function main() {
// Fetch accounts from wallet - these are unlocked
const accounts = await ethers.getSigners();

// Fetch network
const { chainId } = await ethers.provider.getNetwork();
console.log("Using chainId:", chainId);

// Fetch deployed tokens
const DApp = await ethers.getContractAt(
  "Token",
  config[chainId].DApp.address
);
console.log(`Dapp Token fetched: ${DApp.address}\n`);

const mETH = await ethers.getContractAt(
  "Token",
  config[chainId].mETH.address
);
console.log(`mETH Token fetched: ${mETH.address}\n`);

const mDAI = await ethers.getContractAt(
  "Token",
  config[chainId].mDAI.address
);
console.log(`mDAI Token fetched: ${mDAI.address}\n`);

// Fetch the deployed exchange
const exchange = await ethers.getContractAt(
  "Exchange",
  config[chainId].exchange.address
);
console.log(`Exchange fetched: ${exchange.address}\n`);

// Give tokens to account[1]
const sender = accounts[0];
const receiver = accounts[1];
let amount = tokens(10000);

// user1 transfers 10,000 mETH...
let transaction, result;
transaction = await mETH.connect(sender).transfer(receiver.address, amount);
console.log(
  `Transferred ${amount} tokens from ${sender.address} to ${receiver.address}\n`
);

// Set up exchange users
const user1 = accounts[0];
const user2 = accounts[1];
amount = tokens(10000);

// user1 approves 10,000 Dapp...
transaction = await DApp.connect(user1).approve(exchange.address, amount);
await transaction.wait();
console.log(`Approved ${amount} tokens from ${user1.address}`);

// user1 deposits 10,000 DApp...
transaction = await exchange
  .connect(user1)
  .depositToken(DApp.address, amount);
await transaction.wait();
console.log(`Deposited ${amount} Ether from ${user1.address}\n`);

// User 2 Approves mETH
transaction = await mETH.connect(user2).approve(exchange.address, amount);
await transaction.wait();
console.log(`Approved ${amount} tokens from ${user2.address}`);

// User 2 Deposits mETH
transaction = await exchange
  .connect(user2)
  .depositToken(mETH.address, amount);
await transaction.wait();
console.log(`Deposited ${amount} tokens from ${user2.address}\n`);

/////////////////////////////////////////////////////////////
// Seed a Cancelled Order
//

// User 1 makes order to get tokens
let orderId;
transaction = await exchange
  .connect(user1)
  .makeOrder(mETH.address, tokens(100), DApp.address, tokens(5));
result = await transaction.wait();
console.log(`Made order from ${user1.address}`);

// User 1 cancels order
orderId = result.events[0].args.id;
transaction = await exchange.connect(user1).cancelOrder(orderId);
result = await transaction.wait();
console.log(`Cancelled order from ${user1.address}\n`);

// Wait 1 second
await wait(1);

/////////////////////////////////////////////////////////////
// Seed Filled Orders
//

// User 1 makes order
transaction = await exchange
  .connect(user1)
  .makeOrder(mETH.address, tokens(100), DApp.address, tokens(10));
result = await transaction.wait();
console.log(`Made order from ${user1.address}`);

// User 2 fills order
orderId = result.events[0].args.id; // <= error
transaction = await exchange.connect(user2).fillOrder(orderId);
result = await transaction.wait();
console.log(`Filled order from ${user1.address}\n`);

// Wait 1 second
await wait(1);

// User 1 makes another order
transaction = await exchange.makeOrder(
  mETH.address,
  tokens(50),
  DApp.address,
  tokens(15)
);
result = await transaction.wait();
console.log(`Made order from ${user1.address}`);

// User 2 fills another order
orderId = result.events[0].args.id;
transaction = await exchange.connect(user2).fillOrder(orderId);
result = await transaction.wait();
console.log(`Filled order from ${user1.address}\n`);

// Wait 1 second
await wait(1);

// User 1 makes final order
transaction = await exchange
  .connect(user1)
  .makeOrder(mETH.address, tokens(200), DApp.address, tokens(20));
result = await transaction.wait();
console.log(`Made order from ${user1.address}`);

// User 2 fills final order
orderId = result.events[0].args.id;
transaction = await exchange.connect(user2).fillOrder(orderId);
result = await transaction.wait();
console.log(`Filled order from ${user1.address}\n`);

// Wait 1 second
await wait(1);
// three orders upto now

/////////////////////////////////////////////////////////////
// Seed Open Orders
//

// User 1 makes 10 orders
for (let i = 1; i <= 10; i++) {
  transaction = await exchange
    .connect(user1)
    .makeOrder(mETH.address, tokens(10 * i), DApp.address, tokens(10));
  result = await transaction.wait();

  console.log(`Made order from ${user1.address}`);

  // Wait 1 second
  await wait(1);
}

// User 2 makes 10 orders
for (let i = 1; i <= 10; i++) {
  transaction = await exchange
    .connect(user2)
    .makeOrder(DApp.address, tokens(10), mETH.address, tokens(10 * i));
  result = await transaction.wait();

  console.log(`Made order from ${user2.address}`);

  // Wait 1 second
  await wait(1);
}
}

main()
.then(() => process.exit(0))
.catch((error) => {
  console.error(error);
  process.exit(1);
});
EN

回答 1

Ethereum用户

发布于 2022-11-15 11:27:46

我没有看到您的智能契约,但是看起来这个函数makeOrder不会发出事件。要检查这一点,您可以直接在智能协议中或在错误行之前检查它,只需运行console.log(result.events)并查看返回的数据。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/139304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档