|  | 
|  | 1 | +--- | 
|  | 2 | +id: setup | 
|  | 3 | +title: Setting up the Advanced Cross-Contract Calls Project | 
|  | 4 | +sidebar_label: Project Setup | 
|  | 5 | +description: "Get the advanced cross-contract calls example project running locally with all necessary dependencies and test contracts." | 
|  | 6 | +--- | 
|  | 7 | + | 
|  | 8 | +import Tabs from "@theme/Tabs"; | 
|  | 9 | +import TabItem from "@theme/TabItem"; | 
|  | 10 | + | 
|  | 11 | +Let's set up the project environment with the main contract and test contracts. | 
|  | 12 | + | 
|  | 13 | +## Obtaining the Project | 
|  | 14 | + | 
|  | 15 | +```bash | 
|  | 16 | +git clone https://github.com/near-examples/cross-contract-calls | 
|  | 17 | +cd cross-contract-calls | 
|  | 18 | +``` | 
|  | 19 | + | 
|  | 20 | +Choose your language: | 
|  | 21 | + | 
|  | 22 | +<Tabs> | 
|  | 23 | +  <TabItem value="js" label="JavaScript" default> | 
|  | 24 | + | 
|  | 25 | +```bash | 
|  | 26 | +cd contract-advanced-ts | 
|  | 27 | +npm install | 
|  | 28 | +``` | 
|  | 29 | + | 
|  | 30 | +  </TabItem> | 
|  | 31 | +  <TabItem value="rust" label="Rust"> | 
|  | 32 | + | 
|  | 33 | +```bash | 
|  | 34 | +cd contract-advanced-rs | 
|  | 35 | +cargo check | 
|  | 36 | +``` | 
|  | 37 | + | 
|  | 38 | +  </TabItem> | 
|  | 39 | +</Tabs> | 
|  | 40 | + | 
|  | 41 | +## Project Structure | 
|  | 42 | + | 
|  | 43 | +``` | 
|  | 44 | +├── tests/                    # Test environment | 
|  | 45 | +│   └── external-contracts/   # Pre-compiled contracts | 
|  | 46 | +│       ├── counter.wasm      # Simple counter | 
|  | 47 | +│       ├── guest-book.wasm   # Message storage | 
|  | 48 | +│       └── hello-near.wasm   # Greeting contract | 
|  | 49 | +├── src/                      # Main contract code | 
|  | 50 | +│   ├── batch_actions.*       # Batch operations | 
|  | 51 | +│   ├── multiple_contracts.*  # Parallel execution | 
|  | 52 | +│   └── similar_contracts.*   # Same-type calls | 
|  | 53 | +└── README.md | 
|  | 54 | +``` | 
|  | 55 | + | 
|  | 56 | +## Test Contracts Overview | 
|  | 57 | + | 
|  | 58 | +### Hello NEAR Contract | 
|  | 59 | +- `get_greeting()` - Returns current greeting | 
|  | 60 | +- `set_greeting(message: string)` - Updates greeting | 
|  | 61 | + | 
|  | 62 | +### Counter Contract   | 
|  | 63 | +- `get_num()` - Returns current count | 
|  | 64 | +- `increment()` - Increases by 1 | 
|  | 65 | +- `decrement()` - Decreases by 1 | 
|  | 66 | + | 
|  | 67 | +### Guest Book Contract | 
|  | 68 | +- `add_message(text: string)` - Adds message | 
|  | 69 | +- `get_messages()` - Returns all messages | 
|  | 70 | + | 
|  | 71 | +## Building the Contract | 
|  | 72 | + | 
|  | 73 | +<Tabs> | 
|  | 74 | +  <TabItem value="js" label="JavaScript" default> | 
|  | 75 | + | 
|  | 76 | +```bash | 
|  | 77 | +npm run build | 
|  | 78 | +# Output: build/cross_contract.wasm | 
|  | 79 | +``` | 
|  | 80 | + | 
|  | 81 | +  </TabItem> | 
|  | 82 | +  <TabItem value="rust" label="Rust"> | 
|  | 83 | + | 
|  | 84 | +```bash | 
|  | 85 | +cargo near build | 
|  | 86 | +# Output: target/wasm32-unknown-unknown/release/cross_contract.wasm | 
|  | 87 | +``` | 
|  | 88 | + | 
|  | 89 | +  </TabItem> | 
|  | 90 | +</Tabs> | 
|  | 91 | + | 
|  | 92 | +## Running Tests | 
|  | 93 | + | 
|  | 94 | +Verify everything works: | 
|  | 95 | + | 
|  | 96 | +<Tabs> | 
|  | 97 | +  <TabItem value="js" label="JavaScript" default> | 
|  | 98 | + | 
|  | 99 | +```bash | 
|  | 100 | +npm test | 
|  | 101 | +``` | 
|  | 102 | + | 
|  | 103 | +  </TabItem> | 
|  | 104 | +  <TabItem value="rust" label="Rust"> | 
|  | 105 | + | 
|  | 106 | +```bash | 
|  | 107 | +cargo test | 
|  | 108 | +``` | 
|  | 109 | + | 
|  | 110 | +  </TabItem> | 
|  | 111 | +</Tabs> | 
|  | 112 | + | 
|  | 113 | +Expected output: | 
|  | 114 | +``` | 
|  | 115 | +✓ Test batch actions | 
|  | 116 | +✓ Test multiple contracts   | 
|  | 117 | +✓ Test similar contracts | 
|  | 118 | +``` | 
|  | 119 | + | 
|  | 120 | +## Contract Initialization | 
|  | 121 | + | 
|  | 122 | +The main contract needs external contract addresses: | 
|  | 123 | + | 
|  | 124 | +<Tabs> | 
|  | 125 | +  <TabItem value="js" label="JavaScript" default> | 
|  | 126 | + | 
|  | 127 | +```typescript | 
|  | 128 | +@NearBindgen({}) | 
|  | 129 | +export class CrossContractCall { | 
|  | 130 | +  hello_account: AccountId; | 
|  | 131 | +  counter_account: AccountId; | 
|  | 132 | +  guestbook_account: AccountId; | 
|  | 133 | + | 
|  | 134 | +  @initialize({}) | 
|  | 135 | +  init({ | 
|  | 136 | +    hello_account, | 
|  | 137 | +    counter_account, | 
|  | 138 | +    guestbook_account, | 
|  | 139 | +  }: { | 
|  | 140 | +    hello_account: AccountId; | 
|  | 141 | +    counter_account: AccountId; | 
|  | 142 | +    guestbook_account: AccountId; | 
|  | 143 | +  }) { | 
|  | 144 | +    this.hello_account = hello_account; | 
|  | 145 | +    this.counter_account = counter_account; | 
|  | 146 | +    this.guestbook_account = guestbook_account; | 
|  | 147 | +  } | 
|  | 148 | +} | 
|  | 149 | +``` | 
|  | 150 | + | 
|  | 151 | +  </TabItem> | 
|  | 152 | +  <TabItem value="rust" label="Rust"> | 
|  | 153 | + | 
|  | 154 | +```rust | 
|  | 155 | +#[near_bindgen] | 
|  | 156 | +impl Contract { | 
|  | 157 | +    #[init] | 
|  | 158 | +    pub fn init( | 
|  | 159 | +        hello_account: AccountId, | 
|  | 160 | +        counter_account: AccountId, | 
|  | 161 | +        guestbook_account: AccountId, | 
|  | 162 | +    ) -> Self { | 
|  | 163 | +        Self { | 
|  | 164 | +            hello_account, | 
|  | 165 | +            counter_account, | 
|  | 166 | +            guestbook_account, | 
|  | 167 | +        } | 
|  | 168 | +    } | 
|  | 169 | +} | 
|  | 170 | +``` | 
|  | 171 | + | 
|  | 172 | +  </TabItem> | 
|  | 173 | +</Tabs> | 
|  | 174 | + | 
|  | 175 | +## Deploy Your Contract | 
|  | 176 | + | 
|  | 177 | +```bash | 
|  | 178 | +# Create your account | 
|  | 179 | +near account create-account sponsor-by-faucet-service xcc.YOUR_NAME.testnet autogenerate-new-keypair save-to-keychain network-config testnet create | 
|  | 180 | + | 
|  | 181 | +# Deploy with initialization | 
|  | 182 | +near contract deploy xcc.YOUR_NAME.testnet use-file ./build/cross_contract.wasm with-init-call init json-args '{ | 
|  | 183 | +  "hello_account":"hello.near-examples.testnet", | 
|  | 184 | +  "counter_account":"counter.near-examples.testnet", | 
|  | 185 | +  "guestbook_account":"guestbook.near-examples.testnet" | 
|  | 186 | +}' prepaid-gas '100.0 Tgas' attached-deposit '0 NEAR' network-config testnet sign-with-keychain send | 
|  | 187 | +``` | 
|  | 188 | + | 
|  | 189 | +Now let's explore [batch actions](2-batch-actions.md) in the next chapter! | 
0 commit comments