# Service Script

## How to run Service Script

1. Download the [scads-service](https://github.com/zoinkswap/zoinks-service) folder on git repository.&#x20;

> -> git clone <https://github.com/zoinkswap/zoinks-service.git>

2\. Navigate to the source folder.

3\. Install packages

> -> npm install

![](https://3720878023-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHHrNo8gwQXtJkd0dunVR%2Fuploads%2FvqQ5VXE7QG9D3LQPK9kN%2Fservice-script-install.png?alt=media\&token=4e82ff83-f166-4413-bec6-23fbb237d369)

4\. Run index.js

> -> node index.js

![](https://3720878023-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHHrNo8gwQXtJkd0dunVR%2Fuploads%2FOTMDuWsMh8flVHfL9R6v%2Fservice-script-run.png?alt=media\&token=10b14a1a-e042-484a-bea2-b8169efa8ef2)

## Service Script Architecture

1. index.js
2. controller/service.controller.js
3. package.json

### Start point - index.js

![](https://3720878023-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHHrNo8gwQXtJkd0dunVR%2Fuploads%2F9OVg9e0vuY0UAyyg9wvc%2Fservice-script-index.png?alt=media\&token=9e8d1987-c9fd-492c-ad96-4069ee36b1dd)

There are 2 paragraph for test/production mode

#### Test mode

```
// test mode
const ret1 = service.checkRewards();
const ret2 = service.checkPulse();
```

#### Production mode

```
// production mode
 cron.schedule("* * * * *", function () {
   service.checkRewards();
 });

 cron.schedule("0 2,14 * * *", function () {
   service.checkPulse();
 });
```

### Main script - controller.js

![](https://3720878023-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHHrNo8gwQXtJkd0dunVR%2Fuploads%2Fv8XNGuTckHwYs21RngpZ%2Fservice-script-controller.png?alt=media\&token=0341299a-5309-4c48-9564-e6ba9eb7f993)

#### Pool addresses

```
const pulseAddress = "0x2B0C377aDC12cFbb79a55D060eE3d7180C07e2bB";
const cakePulseAddress = "0x9D699DA95EA0AC833D01C965DB174c79FbcDDd78";
const masterAddress = "0x6CDA12D62E49F1BE7812a9EEeB59B43345b651A0";
const zoinksPoolAddress = "0xFec39c2B3FB9f606fC757972906f91f5Fe4348AA";
const snacksPoolAddress = "0xaE42213e2f73752208c7731cb98FEEd4F7813De0";
```

#### Wallet connect

```
const privateKey1 = "";
const privateKey2 = "";

const wallet1 = new ethers.Wallet(privateKey1, provider);
const wallet2 = new ethers.Wallet(privateKey2, provider);
```

#### Check Rewards

```
const account = await wallet1.connect(provider);

  const ifaceSnacksPool = new ethers.utils.Interface(snackspool_abi);
  const ifaceZoinksPool = new ethers.utils.Interface(zoinkspool_abi);
  const ifaceMaster = new ethers.utils.Interface(master_abi);

  const snackspool_data = ifaceSnacksPool.encodeFunctionData("updatePool", []);
  const snackspool_txObjs = {
    from: account.address,
    to: snacksPoolAddress,
    value: 0,
    gasLimit: 504264, // 100000
    gasPrice: 50000000000,
    data: snackspool_data,
  };
  await account.sendTransaction(snackspool_txObjs).then((transaction) => {
    try {
      console.dir(transaction);

      console.log("snacks pool update");
    } catch (err) {
      console.log("snacks pool update failed: " + err);
    }
  });

  const zoinkspool_data = ifaceZoinksPool.encodeFunctionData("updatePool", []);
  const zoinkspool_txObjs = {
    from: account.address,
    to: zoinksPoolAddress,
    value: 0,
    gasLimit: 504264, // 100000
    gasPrice: 50000000000,
    data: zoinkspool_data,
  };
  await account.sendTransaction(zoinkspool_txObjs).then((transaction) => {
    try {
      console.dir(transaction);

      console.log("zoinks pool update");
    } catch (err) {
      console.log("zoinks pool update failed: " + err);
    }
  });
  
  const master_data = ifaceMaster.encodeFunctionData("massUpdatePools", []);
  const master_txObjs = {
    from: account.address,
    to: masterAddress,
    value: 0,
    gasLimit: 504264, // 100000
    gasPrice: 50000000000,
    data: master_data,
  };
  await account.sendTransaction(master_txObjs).then((transaction) => {
    try {
      console.dir(transaction);

      console.log("Master(Farms) update");
    } catch (err) {
      console.log("Master(Farms) update failed: " + err);
    }
  });
```

#### Check Pulse

```
const account = await wallet2.connect(provider);

  const ifacePulse = new ethers.utils.Interface(pulse_abi);
  const ifaceCakePulse = new ethers.utils.Interface(cakepulse_abi);

  const pulse_data = ifacePulse.encodeFunctionData("circulate", []);
  const pulse_txObjs = {
    from: account.address,
    to: pulseAddress,
    value: 0,
    gasLimit: 504264, // 100000
    gasPrice: 50000000000,
    data: pulse_data,
  };
  await account.sendTransaction(pulse_txObjs).then((transaction) => {
    try {
      console.dir(transaction);

      console.log("Pulse circulate");
    } catch (err) {
      console.log("Pulse circulate failed: " + err);
    }
  });

  const cakepulse_data = ifaceCakePulse.encodeFunctionData("circulate", []);
  const cakepulse_txObjs = {
    from: account.address,
    to: cakePulseAddress,
    value: 0,
    gasLimit: 504264, // 100000
    gasPrice: 50000000000,
    data: cakepulse_data,
  };
  await account.sendTransaction(cakepulse_txObjs).then((transaction) => {
    try {
      console.dir(transaction);

      console.log("CakePulse circulate");
    } catch (err) {
      console.log("CakePulse circulate failed: " + err);
    }
  });
```

### Package.json

```
{
  "name": "zoinksservice",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ethers": "^5.6.1",
    "node-cron": "^3.0.0",
    "web3": "^1.7.1"
  }
}
```
