Home

Swapping tokens

Learn how to implement exchange endpoint in your game.

The API allows you to convert any tokens, using Uniswap V3. Conversion rates are determined via an Automated Market Maker mechanism, and depend on supply and demand of the respective tokens.

To exchange tokens, simply make a POST request to Openfort's exchange API:


_10
https://api.openfort.xyz/v1/exchange

In the body of the request:

  • Include the contract addresses of the desired tokenInAddress and tokenOutAddress tokens
  • Include a fromAddress that contains the address from which the token is swapped.
  • Include an amount to swap, either a desired input and output token amount.

Depending on the "direction" of the conversion, there are two different tradeTypes, which are covered with examples below too. Let's assume we have two tokens IN and OUT:

Converting with an exact input amount#

  • The player wants to swap 100 IN to OUT:
  1. we first need to get the amount of OUT we can expect to get for 100 IN.
  2. then convert it with an EXACT_INPUT to make sure we get the highest amount of OUT for our 100 IN.

Converting with an exact output amount#

  • The player wants to buy 10 OUT:
  1. here we first fetch the required amount of IN to obtain 10 OUT.
  2. then convert it with an EXACT_OUTPUT to make sure get exactly 10 OUT.

Below is a sample cURL command for generating a new wallet for a user with Openfort:

command-line

_10
curl https://api.openfort.xyz/v1/exchange \
_10
-u "$YOUR_SECRET_KEY:" \
_10
-d 'chainId=80002' \
_10
-d 'fromAddress="0x45..."' \
_10
-d 'tokenInAddress="0x45..."' \
_10
-d 'tokenOutAddress="0x45..."' \
_10
-d 'amount="100000"' \
_10
-d 'tradeType=EXACT_INPUT'

A successful response will include the new user object along with their DID and embedded wallet address, like below:

json

_63
{
_63
"id": "tin_c502d628-5bb3-42f2-b8f5-62ba4d71df3a",
_63
"createdAt": 1689869074,
_63
"object": "transactionIntent",
_63
"userOperationHash": "0x25d3...005c",
_63
"userOperation": {
_63
"sender": "0x48930Cd730652bf0B18Ef8c80cD0Fa1Cc72A233E",
_63
"nonce": "0x2",
_63
"initCode": "0x",
_63
"callData": "0xb61d...0000",
_63
"callGasLimit": "0x27863",
_63
"verificationGasLimit": "0x16001",
_63
"preVerificationGas": "0xb818",
_63
"maxFeePerGas": "0x62590091",
_63
"maxPriorityFeePerGas": "0x62590091",
_63
"paymasterAndData": "0x3210...b51c",
_63
"signature": "0x6202...3d1b"
_63
},
_63
"chainId": 80002,
_63
"updatedAt": 1689869074,
_63
"policy": {
_63
"id": "pol_..."
_63
},
_63
"player": {
_63
"id": "pla_..."
_63
},
_63
"account": {
_63
"id": "acc_..."
_63
},
_63
"response": {
_63
"createdAt": 1689869074,
_63
"logs": [
_63
{
_63
"removed": false,
_63
"transactionIndex": 0,
_63
"blockNumber": 44904492,
_63
"transactionHash": "0x25d3...005c",
_63
"address": "0x5FF1...2789",
_63
"topics": [
_63
"0xbb47...f972"
_63
],
_63
"data": "0x",
_63
"logIndex": 0,
_63
"blockHash": "0x8a69...6d59"
_63
}
_63
],
_63
"blockNumber": 8789286,
_63
"transactionHash": "0x25d3...005c",
_63
"to": "0x0576...1B57",
_63
"gasUsed": "336730",
_63
"status": 1
_63
},
_63
"interactions": [
_63
{
_63
"functionName": "mint",
_63
"value": "100000000000000",
_63
"contract": "0x0576...1B57",
_63
"functionArgs": [
_63
"0x63B7...484f"
_63
]
_63
}
_63
]
_63
}

note

Check the Quote Swap endpoint to get a quotation before executing the conversion.