正确使用artifacts.require?
Proper use of artifacts.require?
我想了解 artifacts.require
应该如何使用。我已经看到标准段落将其描述为用于迁移和测试。由此我推断,全局范围的 artifacts
及其方法 require
在进行迁移或 运行 测试时由 truffle 可执行工具自动定义。但是,我正在处理一些在任何迁移或测试上下文之外使用 artifacts.require
的代码,而这些代码只需要执行通常的 at
和 new
。但是,在此上下文中,未定义对象 artifacts
。
我这里有正确的图片吗?这是对 artifacts.require
的适当使用吗?如果是这样,必须做什么才能使其在迁移和测试之外定义?
感谢任何建议!
artifacts.require 真的不应该在测试之外使用。这是定义的地方:https://github.com/trufflesuite/truffle-core/blob/3e96337c32aaae6885105661fd1a6792ab4494bf/lib/test.js#L240
在生产代码中,您应该使用 truffle-contract https://github.com/trufflesuite/truffle-contract
将已编译的合约加载到您的应用程序中
这是一个简短的示例(来自 http://truffleframework.com/docs/getting_started/packages-npm#within-javascript-code 并参见
http://truffleframework.com/docs/getting_started/contracts#making-a-transaction )
var contract = require("truffle-contract");
var contractJson = require("example-truffle-library/build/contracts/SimpleNameRegistry.json");
var SimpleNameRegistry = contract(contractJson);
SimpleNameRegistry
.deployed()
.then(function(instance) {
return instance.setRegistry(address);
})
.then(function(result) {
// If this callback is called, the transaction was successfully processed.
alert("Transaction successful!")
})
.catch(function(e) {
// There was an error! Handle it.
});
我想了解 artifacts.require
应该如何使用。我已经看到标准段落将其描述为用于迁移和测试。由此我推断,全局范围的 artifacts
及其方法 require
在进行迁移或 运行 测试时由 truffle 可执行工具自动定义。但是,我正在处理一些在任何迁移或测试上下文之外使用 artifacts.require
的代码,而这些代码只需要执行通常的 at
和 new
。但是,在此上下文中,未定义对象 artifacts
。
我这里有正确的图片吗?这是对 artifacts.require
的适当使用吗?如果是这样,必须做什么才能使其在迁移和测试之外定义?
感谢任何建议!
artifacts.require 真的不应该在测试之外使用。这是定义的地方:https://github.com/trufflesuite/truffle-core/blob/3e96337c32aaae6885105661fd1a6792ab4494bf/lib/test.js#L240
在生产代码中,您应该使用 truffle-contract https://github.com/trufflesuite/truffle-contract
将已编译的合约加载到您的应用程序中这是一个简短的示例(来自 http://truffleframework.com/docs/getting_started/packages-npm#within-javascript-code 并参见 http://truffleframework.com/docs/getting_started/contracts#making-a-transaction )
var contract = require("truffle-contract");
var contractJson = require("example-truffle-library/build/contracts/SimpleNameRegistry.json");
var SimpleNameRegistry = contract(contractJson);
SimpleNameRegistry
.deployed()
.then(function(instance) {
return instance.setRegistry(address);
})
.then(function(result) {
// If this callback is called, the transaction was successfully processed.
alert("Transaction successful!")
})
.catch(function(e) {
// There was an error! Handle it.
});