# <| Backend Teritori (With VM Arch Linux) |>
## -=¤ Launch the DB and indexer from Linux VM ¤=-
- `systemctl start docker` to launch the daemon
- `docker-compose down -v` to clear the DB
- `docker-compose up -d` to launch the DB
- `go run ./go/cmd/teritori-indexer -indexer-mode data` to launch the indexer
- `go run ./go/cmd/teritori-dapp-backend` to launch the backend
- must edit the network teritori-testnet and .env to listen on your local backend http://localhost:9090
- `ip address show` to see IP address
- `go run ./go/cmd/flush-data -indexer-mode data -target-network-id teritori-testnet` if you want to empty the db
### (Read DB)
- postgres
- 5432
- postgres
- magic
### Config teritori-dapp local indexer and DB run on virtual machine
_Becauuuuse I have a weird env, I use a VM to run the backend (almost) locally. So I have to reach a private IP address. But 127.0.0.1 must be used if you run your backend truelly locally_
- networks/teritori-testnet backendEndpoint http://192.168.xxx.xxx:9090
- networks.json teritori-testnet backendEndpoint http://192.168.xxx.xxx:9090
- .env DB_INDEXER_HOST http://192.168.xxx.xxx:9090
___
## -=¤ Operate on smartcontracts ¤=-
### Modify teritori contracts
- Clone and yarn install https://github.com/TERITORI/teritori-contracts
- Make modifications, then `cargo check` (then fix), and `cargo fmt`
- `make generate.schema`, then commit push
### Optimize teritori contracts Compilation before deploy (get wasm binaries)
- `systemctl start docker` if Docker was not started
- From teritori-dapp :
```
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/optimizer:0.16.0 ./rust/contracts/nft-launchpad
```
- You will can deploy the wasm from /artifacts
https://docs.cosmwasm.com/docs/getting-started/compile-contract/
https://github.com/CosmWasm/rust-optimizer
(You can move the binaries from here to another folder to avoid deploying all binaries from /artifacts)
### Deploy WASM binaries from a folder, on teritori testnet
```echo *.wasm | tr ' ' '\n' | xargs -I % teritorid tx wasm store % --from wallet --node https://rpc.testnet.teritori.com:443 --chain-id teritori-test-7 --gas-prices 0.25utori --gas auto --gas-adjustment 1.3 -y --output json -b block```
### List the contracts on teritori testnet
```teritorid query wasm list-code --node https://rpc.testnet.teritori.com:443 --limit 1000```
### Instantiate the deployed contract cod_id 153, on teritori testnet
```teritorid tx wasm instantiate 153 '{"purchase_price":{"amount":"100","denom":"utori"},"transfer_price":{"amount":"999","denom":"utori"}}' --from wallet --label "contract-name" --node https://rpc.testnet.teritori.com:443 --chain-id teritori-test-7 --gas-prices 0.25utori --gas auto --gas-adjustment 1.3 -y --no-admin```
https://docs.cosmwasm.com/fr/docs/getting-started/interact-with-contract#instantiating-the-contract
#### Then, get the contract 153 address
```wasmd query wasm list-contract-by-code 153 --node https://rpc.testnet.teritori.com:443 --output json```
### Generate clients files, api, ...
- On teritori-dapp, `make generate`
### Useful commands
- `ip address show`
- `chown`
- `sudo docker ps` displays les containers
- `npx tsx` to execute TS script
- `nvm use x.x.x` to set the used nodejs version (Don't forget to add the specified nodejs version before)
___
## -=¤ Redeploy indexer ¤=-
(Scaleway)
After modify the indexer and DB, we need to update the indexer where it's stored, directly on the host.
### Set the env
- Get the access from the Scaleway host and get a SCW_SECRET_KEY
- `docker login rg.nl-ams.scw.cloud/teritori -u nologin --password-stdin <<< ${SCW_SECRET_KEY}`
- `make publish.indexer` from teritori-dapp to generate the new indexer image and send it to the host.
- Get kubeconfig-teritori-testnet.yaml (Or concernend kubeconfig file) from the Kubernetes cluster (DO NOT STORE, IT'S A TOUCHY FILE)
- Clone full-nodes-infra repo and `cd` to it
- `export KUBECONFIG=/full-nodes-infra/testnet/kubeconfig-teritori-testnet.yaml` to set the kubeconfigfile that will be used for the operations
### Operate on the containers
Modify `full-nodes-infra/{NETWORK}/indexer/teritori-testnet-data.yaml` and `teritori-testnet-p2e.yaml`: Modify the `image` hash, it must be the same as mentionned when running `make publish.indexer`
#### Delete the concerned ressources
- `kubectl delete deploy indexer-teritori-testnet-data`
- `kubectl delete deploy indexer-teritori-testnet-p2e`
#### Flush DB using a script (From full-nodes-infra root)
- `./flush-db.sh teritori-testnet data indexerdb`
- `./flush-db.sh teritori-testnet p2e indexerdb`
#### Apply the changes done above
- `kubectl apply -f testnet/indexer/teritori-testnet-data.yaml`
- `kubectl apply -f testnet/indexer/teritori-testnet-p2e.yaml`
#### Verify the containers are ok
- `kubectl logs deploy/indexer-teritori-testnet-data -f`
- `kubectl logs deploy/indexer-teritori-testnet-p2e -f`
#### Update the full-nodes-infra repo
- `git add -p`
- `git commit -m "(relevant message)"`
- `git push`
### Useful commands
- `export VARIABLE_NAME=variable_value` to set reusable variables
- `kubectl get pods` to see the pods
- `kubectl get deploy` to see the last deployements