Bỏ qua tới nội dung chính

Exness hoàn 100% hoa hồng IB

Không khoản nào bị trừ trước khi về tài khoản bạn

EA và indicator miễn phí

XM hoàn 90%, nạp tối thiểu 5 USD

What is a smart contract?

A smart contract is a program stored on a blockchain such as Ethereum. It holds funds and follows fixed rules: when someone calls it and the conditions are met, it acts automatically, with no company in the middle. It cannot see outside data on its own, and a bug can lose money for good.

Nội dung này hiện chỉ có bản tiếng Anh.

5 phút đọcAutoFXRebate

Say 120 people each want to put 1 ETH toward a small game project, but only if it raises enough to get built. Normally a crowdfunding website would hold the money and promise refunds if the goal is missed.

A smart contract can do the same job without the website. The rules are published on a blockchain, and the code itself holds the money.

What a smart contract is

A smart contract is a program stored at an address on a blockchain. It can hold a balance like any account. It has no private key, though, so only its code decides what happens to that balance.

The computer scientist Nick Szabo used the term in the 1990s, long before blockchains existed. Ethereum, launched in 2015, made the idea practical for anyone who can write code.

The name is a little misleading. Most smart contracts involve no legal agreement at all. They are software that moves digital assets according to fixed rules.

How a contract runs

A contract has four parts: its address, its balance, its code and its stored data. The code is split into functions, named actions such as pledge or refund that anyone can ask it to perform.

Nothing happens until someone sends a transaction that calls a function. That person pays the gas fee, in ether, for the work the call takes.

Every validator then runs the same code with the same inputs and must reach the same result. That shared result becomes the new state of the contract.

A wallet sends a call with a gas fee to a contract, which has an address, a balance, code and stored data, and three validators each run the same code.
A contract only acts when a transaction calls it, and the caller pays the gas.

If a call breaks one of the contract's rules, the whole transaction is undone and the contract stays as it was. The caller still pays gas for the work done before the failure.

A worked example: a crowdfunding contract

Here are the rules of an illustrative crowdfunding contract:

  1. The goal is 100 ETH, and the deadline is 30 days after launch.
  2. Anyone can pledge ether before the deadline, and the contract records each pledge.
  3. After the deadline, if the total is 100 ETH or more, the creator can withdraw all of it.
  4. If the total is below 100 ETH, each backer can withdraw their own pledge.

Suppose 120 backers pledge 1 ETH each. The total is 120 ETH, which meets the goal, so the creator's withdraw call succeeds and sends 120 ETH to the creator.

Now suppose only 70 backers pledge 1 ETH each. The total of 70 ETH misses the goal, so the creator's withdraw call fails. Each backer can call refund and get their 1 ETH back, paying a little gas.

A crowdfunding contract with a goal of 100 ETH: if 120 ETH is raised the creator withdraws it, and if 70 ETH is raised the backers are refunded.
The code decides where the money goes, so backers do not have to trust the creator.

No backer has to trust the creator to send refunds. The creator cannot take the 70 ETH, because no line of the code allows it.

That promise holds only if the code says what you think it says. Most users never read it, so in practice they rely on audits and on someone else having checked.

What smart contracts cannot do

A contract only sees data already on its own blockchain. It cannot check a match result or the price of gold by itself.

Projects fill the gap with an oracle, a service that posts outside data onto the blockchain for contracts to read. A contract that depends on an oracle is only as reliable as that oracle.

Outside data such as a gold price or a match result reaches a contract on the blockchain only through an oracle that posts it on-chain.
A contract reads only on-chain data, so outside facts arrive through an oracle.

A contract cannot spot a bad input either. If an oracle reports a wrong price, a lending contract may sell a borrower's collateral at that price, exactly as its code says.

Contracts also never act on their own. When the deadline in the example passes, nothing happens until someone sends a transaction that triggers the next step.

Why fixed code cuts both ways

Once deployed, most contract code cannot be edited. That is the appeal: the rules you read are the rules that run.

It also means a bug stays live. Attackers read public contract code looking for mistakes, and one flaw can drain everything a contract holds.

Some contracts can be upgraded through an admin key. That allows fixes, but whoever holds the key can also change the rules, so you are back to trusting people.

Security audits catch many problems. Audited contracts have still been hacked, so an audit lowers the risk without removing it. In DeFi, where contracts hold large sums, this risk comes with every deposit.

Smart contracts and your wallet

When you use a token in a dApp, your crypto wallet often asks you to sign an approval. It gives a contract permission to move a set amount of that token out of your address.

A malicious contract with an unlimited approval can empty that token from your wallet later. Approval tricks are among the common crypto scams, so read what your wallet asks you to sign and remove approvals you no longer need.

Câu hỏi thường gặp

Are smart contracts legally binding?

Usually they are software, and their legal status depends on the country and the situation. A contract can be part of a legal agreement, but code alone rarely is one.

Can a smart contract be changed after it is deployed?

Most cannot. Some include an upgrade mechanism controlled by an admin key, which allows fixes but also lets the key holder change the rules.

What language are smart contracts written in?

On Ethereum most are written in Solidity and compiled to run on the Ethereum Virtual Machine (EVM). Other blockchains use other languages, such as Rust.

Do smart contracts only exist on Ethereum?

No. Many blockchains support them, and several copy Ethereum's virtual machine so the same code can run there too.

Who pays to run a smart contract?

The account that sends the transaction pays the gas fee. Some apps cover that fee for their users, but a contract never runs for free.

Góp ý

Tài khoản của bạn đang ở sàn nào?

Mỗi sàn một đường chuyển khác nhau, nên chúng tôi đưa bạn tới đúng phần của sàn đó.