Section 9. Create a Direct Order [Agora]

To create a direct order, follow these steps:

  1. Deploy Task Manager, Authz Diamond and RAC (as described in Section 3)
  2. Deploy Trezor Diamond (as described in Section 5)
  3. Deploy Agora Diamond (as described in Section 6)
  4. Create ERC20 Tokens (as described in Section 7)

    Note: You should deploy two tokens, a base token and a quote token.

  5. Create a Pair (as described in Section 8) with your base token and quote token

  6. Giving access to Trezor Diamond. follow these steps:

Step 1: Navigate to Trezor Diamond

From the left-hand side of the page, select the Trezor menu. This will bring up the Deploy Trezor Diamond section.

Step 2: Copy Trezor Diamond Address

From the deployed diamond table, in the Address column, click on the Copy icon.

Step 3: Navigate to Deploy RAC Diamond

From the left-hand side of the page, select the Resource Access Controllermenu. This will bring up the Deploy RAC Diamond section.

Step 4: Giving access to Trezor Diamond

  1. From the deployed diamond table, click on the settings icon
  2. Navigate to the Functions and Permissions tab
  3. Find updateResourceQuota from Function Signature column and click on Add Permissions
  4. Enter the Trezor Diamond Address in Caller Account field to grant access
  5. Click on the Add Permission button
  6. Confirm the transaction in your wallet

Qonsole Panel

7. Add permission to grantRole function in Trezor Diamond

Step 1: Navigate to Trezor Diamond

From the left-hand side of the page, select the Trezor menu.

Step 2: Giving permission to grantRole function

  1. From the deployed diamond table, click on the settings icon
  2. Navigate to the Functions and Permissions tab
  3. Find grantRole from Function Signature column and click on Add Permissions
  4. Click on the Add Permission button
  5. Confirm the transaction in your wallet

Qonsole Panel

8. Make Trezor a Finalizer

Step 1: Navigate to Trezor Diamond

From the left-hand side of the page, select the Trezor menu. This will bring up the Deploy Trezor Diamond section.

Step 2: Copy Trezor Diamond Address

From the deployed diamond table, in the Address column, click on the Copy icon.

Step 3: Navigate to Deploy Task Manager

From the left-hand side of the page, select the Conclave menu. Once it appears, choose the Task Managers submenu. This will bring up the Deploy Task Manager section.

Step 4: Create a new Admin Task

  1. From the Task Manager you have deployed, click on the New… button
  2. Click on Admin Task
  3. Enter “Make Trezor a Finalizer” as the name

Step 5: Approve Admin Task

  1. From the Task Manager you have deployed, click on the Approve… button
  2. Click on Admin Task
  3. Confirm the transaction in your wallet Note: Approval is required from 50% of Admins (Admins specified in Section 3-1, Step 2, Row 3).

Step 6: Make Trezor a Finalizer

  1. From the Task Manager you have deployed, click on the Add…button
  2. Click on Finalizer
  3. Click on Reference Admin Task, select the Admin Task you have deployed
  4. Enter Trezor Diamond Address (which you copy in step 2) in Account field
  5. Click on the Add Finalizer button
  6. Confirm the transaction in your wallet

Qonsole Panel

9. Create and Approve a Task

Step 1: Navigate to Deploy Task Manager

From the left-hand side of the page, select the Conclave menu. Once it appears, choose the Task Managers submenu. This will bring up the Deploy Task Manager section.

Step 2: Create a new Task

  1. From the Task Manager you have deployed, click on the New… button
  2. Click on Task
  3. Enter the desired task Name

Step 3: Approve Task

  1. From the Task Manager you have deployed, click on the Approve… button
  2. Click on Task
  3. Confirm the transaction in your wallet Note: Approval is required from 50% of Admins (Admins specified in Section 3-1, Step 2, Row 3).

Qonsole Panel

10. Giving access to Agora Diamond (to use transferErc20VaultBalance function in Trezor diamond)

Step 1: Navigate to Agora > Orderbooks

From the left-hand side of the page, select the Agora menu. Once it appears, choose the Orderbooks submenu.

Step 2: Copy Agora Diamond Address

From the deployed diamond table, in the Address column, click on the Copy icon.

Step 3: Navigate to Trezor Diamond

From the left-hand side of the page, select the Trezor menu. This will bring up the Deploy Trezor Diamond section.

Step 4: Giving access to Agora Diamond

  1. From the deployed diamond table, click on the settings icon.
  2. Navigate to the Functions and Permissions tab
  3. Find transferErc20VaultBalance from Function Signature column and click on Add Permissions
  4. Enter the Agora Diamond Address in Caller Account field to grant access
  5. Click on the Add Permission button
  6. Confirm the transaction in your wallet

Qonsole Panel

11. Set RAC Props in Agora Diamond

Step 1: Navigate to Resource Access Controller

From the left-hand side of the page, select the Resource Access Controller menu.

Step 2: Copy RAC Diamond Address

From the deployed diamond table, in the Address column, click on the Copy icon.

Step 3: Navigate to Agora Diamond > Orderbooks

From the left-hand side of the page, select the Agora menu. Once it appears, choose the Orderbooks submenu.

Step 4: Giving permission to setAddress function

  1. From the deployed diamond table, click on the settings icon
  2. Navigate to the Functions and Permissions tab
  3. Find setAddress from Function Signature column and click on Add Permissions
  4. Click on the Add Permission button
  5. Confirm the transaction in your wallet

Step 5: Set RAC address Props in Agora Diamond

  1. Navigate to the Facet Settings tab
  2. Navigate to the props facet
  3. Navigate to the Address Props tab
  4. Click on the Set Value button
  5. Enter ‘rac.address’ as Key and paste the RAC Diamond Address (that you have copied in step 2) as Value
  6. Click on the Set Value button
  7. Confirm the transaction in your wallet

Qonsole Panel

Step 6: Giving permission to setInt function

  1. Navigate to the Functions and Permissions tab
  2. Find setInt from Function Signature column and click on Add Permissions
  3. Click on the Add Permission button
  4. Confirm the transaction in your wallet

Step 7: Set RAC integer Props in Agora Diamond

  1. Navigate to the Facet Settings tab
  2. Navigate to the props facet
  3. Navigate to the Integer Props tab (this tab is open by default)
  4. Click on the Set Value button
  5. Enter ‘rac.domain.id’ as Key and ‘999’ as Value
  6. Click on the Set Value button
  7. Confirm the transaction in your wallet

Qonsole Panel

12. Set RAC Props in Trezor Diamond

Step 1: Navigate to Resource Access Controller

From the left-hand side of the page, select the Resource Access Controller menu.

Step 2: Copy RAC Diamond Address

From the deployed diamond table, in the Address column, click on the Copy icon.

Step 3: Navigate to Trezor Diamond

From the left-hand side of the page, select the Trezor menu.

Step 4: Giving permission to setAddress function

  1. From the deployed diamond table, click on the settings icon
  2. Navigate to the Functions and Permissions tab
  3. Find setAddress from Function Signature column and click on Add Permissions
  4. Click on the Add Permission button
  5. Confirm the transaction in your wallet

Step 5: Set RAC address Props in Trezor Diamond

  1. Navigate to the Facet Settings tab
  2. Navigate to the props facet
  3. Navigate to the Address Props tab
  4. Click on the Set Value button
  5. Enter ‘rac.address’ as Key and paste the RAC Diamond Address (that you have copied in step 2) as Value
  6. Click on the Set Value button
  7. Confirm the transaction in your wallet

Qonsole Panel

Step 6: Giving permission to setInt function

  1. Navigate to the Functions and Permissions tab
  2. Find setInt from Function Signature column and click on Add Permissions
  3. Click on the Add Permission button
  4. Confirm the transaction in your wallet

Step 7: Set RAC integer Props in Trezor Diamond

  1. Navigate to the Facet Settings tab
  2. Navigate to the props facet
  3. Navigate to the Integer Props tab (this tab is open by default)
  4. Click on the Set Value button
  5. Enter ‘rac.domain.id’ as Key and ‘999’ as Value
  6. Click on the Set Value button
  7. Confirm the transaction in your wallet

Qonsole Panel

13. Create Ledger in Trezor Diamond

Step 1: Navigate to Trezor Diamond

From the left-hand side of the page, select the Trezor menu.

Step 2: Giving permission to createLedger function

  1. From the deployed diamond table, click on the settings icon
  2. Navigate to the Functions and Permissions tab
  3. Find createLedger from Function Signature column and click on Add Permissions
  4. Click on the Add Permission button
  5. Confirm the transaction in your wallet

Step 3: Create Ledger

  1. Navigate to the Facet Settings tab
  2. Navigate to the ledger-lister facet
  3. Click on the Create Ledger button
  4. Enter ‘Default’ as Name
  5. Click on the Create Ledger button
  6. Confirm the transaction in your wallet

Qonsole Panel

14. Deposit Tokens in Trezor

Step 1: Navigate to Trezor Diamond

From the left-hand side of the page, select the Trezor menu.

Step 2: Deposit Tokens

  1. Navigate to the erc20-vault facet
  2. Navigate to the Deposit tab
  3. Enter your wallet address
  4. Click on Erc20 Token field and select your token
  5. Enter the desired Amount
  6. Click on the Deposit button
  7. Confirm the transaction in your wallet

Qonsole Panel

15. Call createDirectOrder function (Using Deployed Contract Address)

To call a function using the deployed contract's address with Hardhat, you should: 1. Get contract address 2. Setup a new Hardhat script: In your Hardhat project, create a new script file (e.g., scripts/interact.js). 3. Define an async function to call createDirectOrder 4. Run the Script with Hardhat: npx hardhat run scripts/interact.js --network ppr2

→ In *hardhat.config.js**` file add the network settings:

require("@nomiclabs/hardhat-ethers");

ppr2: {
    // find url in: metamask wallet > Settings > Networks > Pulse PPR2
    url: 'https://ppr2.dev.metapolitan.io',
    // accounts: 0x + metamask wallet private key
    accounts: ["0xcb7e81f3c77e5cef80d02a056f3547056e6ce9a42ef18f008a0a6473c8f124b5"], 
    },

→ In common.js file add the network settings:

if (network === 'ppr2'){
    return {
    name: network,
    isGoerli: false,
    isMainnet: true,
    };

→ Sample codes (in scripts/interact.js):

/* eslint-disable no-undef */

async function main() {

const [admin1] = await ethers.getSigners();

//================================= attach diamond contracts

const AgoraEngineDirectOrderManagerFacetContractFactory = await hre.ethers.getContractFactory('AgoraEngineDirectOrderManagerFacet',);
agoraEngineDirectOrderManagerContract = await AgoraEngineDirectOrderManagerFacetContractFactory.attach("0x41b5d3a138b5449778d519d315dfc706db2ac8c4");

const AgoraEngineOrderStatsFacetContractFactory = await hre.ethers.getContractFactory('AgoraEngineOrderStatsFacet',);
agoraEngineOrderStatsContract = await AgoraEngineOrderStatsFacetContractFactory.attach("0x41b5d3a138b5449778d519d315dfc706db2ac8c4");

const RBACFacetContractFactory = await hre.ethers.getContractFactory('RBACFacet',);
trezorRBACContract = await RBACFacetContractFactory.attach("0x5cb3627261e3a33e6a6b38b490265f5bbfc53b12");

//================================== Create Direct Order

// AgoraEngine contract must be a balance manager
await trezorRBACContract.connect(admin1).grantRole(
    4,
    'DEFAULT',
    "0x41b5d3a138b5449778d519d315dfc706db2ac8c4",
    '0x046f8734229b82080d188b9b7247b766dfce75b7f83075eb7cdbf07b03ef6716'
);

const tx =  await agoraEngineDirectOrderManagerContract.connect(admin1).createDirectOrder(
    "0xd3c92c2817246ce95e16873e68ee1133508beb14b325e08ac89bd8c7893c50af", {   
    bid: true,
    directlyFillable: true,
    orderType: 1,
    quantity: 6,
    price: 12 * 10 ** 6,
    slippage: 0,
    expireTs: 0,
    data: ethers.utils.toUtf8Bytes('a good buy'),
    }
);
const receipt = await tx.wait();
const event = receipt.events?.find(
    (event) => event.event === 'DirectOrderCreate'
);
console.log(event);

const getOrders =  await agoraEngineOrderStatsContract.connect(admin1).getOrders(
    '0xd3c92c2817246ce95e16873e68ee1133508beb14b325e08ac89bd8c7893c50af',
    [1, 2, 3]
)
console.log(getOrders);
}

main()
.then(() => process.exit(0))
.catch(error => {
    console.error(error);
    process.exit(1);
});

Note: To call the createDirectOrder function, first you should make Agora Diamond Contract a balance manager by calling grantRole function just once (for the future createDirectOrder calls, remove or comment out the grantRole).

The addresses used in the code are:

  • Deployed Agora diamond contract address: 0x41b5d3a138b5449778d519d315dfc706db2ac8c4
  • Deployed Trezor diamond contract address: 0x5cb3627261e3a33e6a6b38b490265f5bbfc53b12
  • pairId: 0xd3c92c2817246ce95e16873e68ee1133508beb14b325e08ac89bd8c7893c50af