0% found this document useful (0 votes)
16 views3 pages

Tự luận 6 - Blockchain

The document discusses Truffle contract deployment and interaction syntax like deployer.deploy(), deployer.link(), deployer.then(), sending transactions to a smart contract, and getting the result of transactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Tự luận 6 - Blockchain

The document discusses Truffle contract deployment and interaction syntax like deployer.deploy(), deployer.link(), deployer.then(), sending transactions to a smart contract, and getting the result of transactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question 1: Can you explain the syntax "deployer.deploy(contract, args. . .

,
options)"? and give an example?
The syntax deployer.deploy(contract, args..., options) is used in Truffle migration
scripts to deploy smart contracts to the blockchain network.
Here's an example to illustrate the usage of this syntax:
// In a Truffle migration script (e.g., 2_deploy_contract.js)
const MyContract = artifacts.require("MyContract");
module.exports = function(deployer) {
// Deploying MyContract with constructor arguments and deployment options
deployer.deploy(MyContract, arg1, arg2, { gas: 5000000, from: accounts[0] });
};
Question 2: Can you explain the syntax "deployer.link(library, destinations)"?
and give an example?
The syntax deployer.link(library, destinations) is used in Truffle migration scripts to
link a library contract to one or more contract deployments.
Here is an illustrative example:
// In a Truffle migration script (e.g., 2_deploy_contract.js)
const LibraryContract = artifacts.require("LibraryContract");
const MyContract = artifacts.require("MyContract");
module.exports = function(deployer) {
// Deploying the library contract
deployer.deploy(LibraryContract);
// Linking the library contract to the MyContract
deployer.link(LibraryContract, MyContract);
// Deploying MyContract
deployer.deploy(MyContract);
};
Question 3: Can you explain the syntax "deployer.then(function() {. . . })"? and
give an example?
The syntax deployer.then(function() { ... }) is used in Truffle migration scripts to
execute asynchronous tasks after contract deployment has completed.
Here is an illustrative example:
// In a Truffle migration script (e.g., 2_deploy_contract.js)
const MyContract = artifacts.require("MyContract");
module.exports = function(deployer) {
// Deploying MyContract
deployer.deploy(MyContract)
.then(function() {
// Additional tasks to execute after deployment
console.log("MyContract has been deployed successfully!");
});
};
Question 4: Can you explain the result of executed commands below:
truffle(develop)> let accounts = await web3.eth.getAccounts()
truffle(develop)> instance.sendCoin(accounts[1], 10, {from:
accounts[0]})
 `let accounts = await web3.eth.getAccounts()`: This command requests to
get a list of Ethereum accounts on the current blockchain network. `await`
ensures that we wait when the account list is returned before allocating it to the
`account` variant.
 `instance.sendCoin(accounts[1], 10, {from:accounts[0]})`:This command
sends a transaction to the `instance` smart contract to call the `sendCoin`
function. This transaction transfers 10 units of currency from the account at
position 0 in the list of accounts `accounts' to the account at position 1 in the
list. This assumes that the `instance` contract has a `sendCoin` function
developed and that this function accepts two parameters: the receiving address
and the amount.
Question 5: Can you explain the result of executed commands below:
truffle(develop)> let result = await contract.sendCoin(accounts[1],
10, {from: ˓→accounts[0]})
truffle(develop)> result
The commands you've executed involve interacting with a smart contract using
Truffle's console (truffle(develop)).
 let result = await contract.sendCoin(accounts[1], 10, {from:
accounts[0]}): This command sends a transaction to the smart contract
method sendCoin. It transfers 10 units of currency from the account at
accounts[0] to the account at accounts[1]. The await keyword is used to
wait for the transaction to be mined and for the result to be returned
before assigning it to the variable result.
 result: This command prints the value of the result variable, which
holds the result of the transaction sent in the previous command.

You might also like