【Import】
如果我們要從 local 的方式 import 檔案,可以假設 Folder Structure 如下:
├── Import.sol
└── Foo.sol
創建一個名為 Foo.sol
的檔案:
pragma solidity ^0.8.11;
struct Point {
uint x;
uint y;
}
error Unauthorized(address caller);
function add(uint x, uint y) pure returns (uint) {
return x + y;
}
contract Foo {
string public name = "Foo";
}
接下來創建一個名為 Import.sol
的檔案,並且在裡面 import Foo.sol
:
pragma solidity ^0.8.10;
// import Foo.sol from current directory
import "./Foo.sol";
// import {symbol1 as alias, symbol2} from "filename";
import {Unauthorized, add as func, Point} from "./Foo.sol";
contract Import {
// Initialize Foo.sol
Foo public foo = new Foo();
// Test Foo.sol by getting it's name.
function getFooName() public view returns (string memory) {
return foo.name();
}
}
當然也可以繼承其他地方來的合約:
import "./someothercontract.sol";
contract newContract is SomeOtherContract {
}
如果要從 External 的地方像是 GitHub import 可以藉由複製 url 方式 import:
// https://github.com/owner/repo/blob/branch/path/to/Contract.sol
import "https://github.com/owner/repo/blob/branch/path/to/Contract.sol";
// Example import ECDSA.sol from openzeppelin-contract repo, release-v3.3 branch
// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.3/contracts/cryptography/ECDSA.sol
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.3/contracts/cryptography/ECDSA.sol";
Last updated
Was this helpful?