Contract Deploy and Interact¶
The AEA erc1155_deploy
and erc1155_client
skills demonstrate an interaction between two AEAs which use a smart contract.
- The
erc1155_deploy
skill deploys the smart contract, creates and mints items. - The
erc1155_client
skill signs a transaction to complete a trustless trade with its counterparty.
Preparation Instructions¶
Dependencies¶
Follow the Preliminaries and Installation sections from the AEA quick start.
Discussion¶
The scope of this guide is demonstrating how you can deploy a smart contract and interact with it using AEAs. In this specific demo, you create two AEAs. One deploys and creates tokens inside a smart contract. The other signs a transaction to complete an atomic swap. The smart contract used is ERC1155 with a one-step atomic swap functionality. This means the trade between the two AEAs can be trustless.
Note
This is only for demonstrative purposes since the AEA deploying the contract also has the ability to mint tokens. In reality, the transfer of tokens from the AEA signing the transaction is worthless.
Demo¶
Create the Deployer AEA¶
Fetch the AEA that will deploy the contract:
Alternatively, create from scratch:
Create the AEA that will deploy the contract.
aea create erc1155_deployer
cd erc1155_deployer
aea add connection fetchai/p2p_libp2p:0.27.5
aea add connection fetchai/soef:0.27.6
aea add connection fetchai/ledger:0.21.5
aea add skill fetchai/erc1155_deploy:0.31.6
aea config set --type dict agent.dependencies \
'{
"aea-ledger-fetchai": {"version": "<2.0.0,>=1.0.0"},
"aea-ledger-ethereum": {"version": "<2.0.0,>=1.0.0"},
"aea-ledger-cosmos": {"version": "<2.0.0,>=1.0.0"}
}'
aea config set agent.default_connection fetchai/p2p_libp2p:0.27.5
aea config set --type dict agent.default_routing \
'{
"fetchai/contract_api:1.1.7": "fetchai/ledger:0.21.5",
"fetchai/ledger_api:1.1.7": "fetchai/ledger:0.21.5",
"fetchai/oef_search:1.1.7": "fetchai/soef:0.27.6"
}'
aea config set --type list vendor.fetchai.connections.p2p_libp2p.cert_requests \
'[{"identifier": "acn", "ledger_id": "ethereum", "not_after": "2023-01-01", "not_before": "2022-01-01", "public_key": "fetchai", "save_path": ".certs/conn_cert.txt"}]'
aea install
aea build
And change the default ledger:
Create a private key for the deployer AEA and add it for Ethereum use:
Create a private key for the P2P connection:
aea generate-key fetchai fetchai_connection_private_key.txt
aea add-key fetchai fetchai_connection_private_key.txt --connection
Finally, certify the key for use by the connections that request that:
Create the Client AEA¶
In another terminal, fetch the client AEA which will receive some tokens from the deployer.
Alternatively, create from scratch:
Create the AEA that will get some tokens from the deployer.
aea create erc1155_client
cd erc1155_client
aea add connection fetchai/p2p_libp2p:0.27.5
aea add connection fetchai/soef:0.27.6
aea add connection fetchai/ledger:0.21.5
aea add skill fetchai/erc1155_client:0.29.6
aea config set --type dict agent.dependencies \
'{
"aea-ledger-fetchai": {"version": "<2.0.0,>=1.0.0"},
"aea-ledger-ethereum": {"version": "<2.0.0,>=1.0.0"},
"aea-ledger-cosmos": {"version": "<2.0.0,>=1.0.0"}
}'
aea config set agent.default_connection fetchai/p2p_libp2p:0.27.5
aea config set --type dict agent.default_routing \
'{
"fetchai/contract_api:1.1.7": "fetchai/ledger:0.21.5",
"fetchai/ledger_api:1.1.7": "fetchai/ledger:0.21.5",
"fetchai/oef_search:1.1.7": "fetchai/soef:0.27.6"
}'
aea config set --type list vendor.fetchai.connections.p2p_libp2p.cert_requests \
'[{"identifier": "acn", "ledger_id": "ethereum", "not_after": "2023-01-01", "not_before": "2022-01-01", "public_key": "fetchai", "save_path": ".certs/conn_cert.txt"}]'
aea install
aea build
And change the default ledger:
Create a private key for the client AEA and add it for Ethereum use:
Create a private key for the P2P connection:
aea generate-key fetchai fetchai_connection_private_key.txt
aea add-key fetchai fetchai_connection_private_key.txt --connection
Finally, certify the key for use by the connections that request that:
Run Ganache¶
Execute the following command to run Ganache:
docker run -p 8545:8545 trufflesuite/ganache-cli:latest --verbose --gasPrice=0 --gasLimit=0x1fffffffffffff --account="$(cat erc1155_deployer/ethereum_private_key.txt),1000000000000000000000" --account="$(cat erc1155_client/ethereum_private_key.txt),1000000000000000000000"
Wait some time for the wealth creation to be mined on Ropsten.
Check your wealth:
You should get 1000000000000000000000
.
Note
If no wealth appears after a while, then try funding the private key directly using a web faucet.
Update SOEF Configurations for both AEAs¶
Update the SOEF configuration in both AEA projects:
Run the AEAs¶
First, run the deployer AEA:
Once you see a message of the form To join its network use multiaddr 'SOME_ADDRESS'
take note of this address.
Alternatively, use aea get-multiaddress fetchai -c -i fetchai/p2p_libp2p:0.27.5 -u public_uri
to retrieve the address. The output will be something like /dns4/127.0.0.1/tcp/9000/p2p/16Uiu2HAm2JPsUX1Su59YVDXJQizYkNSe8JCusqRpLeeTbvY76fE5
.
This is the entry peer address for the local agent communication network created by the deployer.
This AEA then performs the following steps:
- deploys the smart contract
- creates a batch of items in the smart contract
- mints a batch of items in the smart contract
At some point you should see the log output:
At this point, configure the client AEA to connect to the same local ACN created by the deployer by running the following command in the client's terminal, replacing SOME_ADDRESS
with the value you noted above:
aea config set --type dict vendor.fetchai.connections.p2p_libp2p.config \
'{
"delegate_uri": "127.0.0.1:11001",
"entry_peers": ["SOME_ADDRESS"],
"local_uri": "127.0.0.1:9001",
"log_file": "libp2p_node.log",
"public_uri": "127.0.0.1:9001"
}'
Then, run the client AEA:
You will see that after discovery, the two AEAs exchange information about the transaction and the client at the end signs and sends the signature to the deployer AEA to send it to the network.
Note
Transactions on Ropsten can take a significant amount of time! If you run the example a second time, and the previous transaction is still pending, it can lead to a failure.
The warning message Cannot verify whether transaction improves utility. Assuming it does!
can be ignored.
Delete the AEAs¶
When you're done, stop the agents (CTRL+C
), go up a level and delete the AEAs.
Communication¶
This diagram shows the communication between the various entities in this interaction:
sequenceDiagram
participant Search
participant Erc1155_contract
participant Client_AEA
participant Deployer_AEA
participant Blockchain
activate Search
activate Erc1155_contract
activate Client_AEA
activate Deployer_AEA
activate Blockchain
Deployer_AEA->>Blockchain: deployes smart contract
Deployer_AEA->>ERC1155_contract: creates tokens
Deployer_AEA->>ERC1155_contract: mint tokens
Deployer_AEA->>Search: register_service
Client_AEA->>Search: search
Search-->>Client_AEA: list_of_agents
Client_AEA->>Deployer_AEA: call_for_proposal
Deployer_AEA->>Client_AEA: inform_message
Client_AEA->>Deployer_AEA: signature
Deployer_AEA->>Blockchain: send_transaction
Client_AEA->>ERC1155_contract: asks_balance
deactivate Search
deactivate Erc1155_contract
deactivate Client_AEA
deactivate Deployer_AEA
deactivate Blockchain