Creating a Pair
Overview
The factory
contract can create new Astroport pair contracts (and associated LP token contracts) which are tracked in a registry and used as a directory for all pairs.
Currently, Astroport support's the following pool types:
- Constant Product Pools
- Stableswap Pools
Governance may decide to add custom pools that can have any implementation.
Constant Product Pools
Creates an xyk
pair.
create_pair
To create an xyk pair, you need to execute a contract message pointing to the create_pair
endpoint in the factory contract.
create_pair
takes in the pair_type
(xyk
) and a vector containing objects of type AssetInfo
(contract addresses or native denomenations for the pair).
_19{_19 "create_pair": {_19 "pair_type": {_19 "xyk": {}_19 }, _19 "asset_infos": [_19 {_19 "token": {_19 "contract_addr": astroAddress_19 }_19 }, _19 {_19 "token": {_19 "contract_addr": steakAddress_19 }_19 }_19 ]_19 }_19}
feather.js
example
For example, if you want to create a pair on Terra, you can implement this operation using feather.js.
_40const factoryAddress = `terra1z3y69xas85r7egusa0c7m5sam0yk97gsztqmh8f2cc6rr4s4anysudp7k0`;_40const astroAddress = 'terra167dsqkh2alurx997wmycw9ydkyu54gyswe3ygmrs4lwume3vmwks8ruqnv';_40const steakAddress = 'terra1xztnx8mm7dagn4ck3dgylaqucp6h6agw83pmyc29hnplq7355trs78fkcq'_40_40const createPair = new MsgExecuteContract(_40 wallet.key.accAddress('terra'),_40 factoryAddress,_40 {_40 "create_pair": {_40 "pair_type": {_40 "xyk": {}_40 }, _40 "asset_infos": [_40 {_40 "token": {_40 "contract_addr": astroAddress_40 }_40 }, _40 {_40 "token": {_40 "contract_addr": steakAddress_40 }_40 }_40 ]_40 }_40 }_40)_40_40let fee = new Fee(2000000, { uluna: 200000 });_40_40// // BROADCAST TRANSACTION_40let tx = await wallet.createAndSignTx({_40 msgs: [createPair],_40 fee,_40 chainID: 'pisco-1',_40});_40_40let result = await lcd.tx.broadcast(tx, 'pisco-1');_40_40console.log(result);
create_pair
To create an xyk pair, you need to execute a contract message pointing to the create_pair
endpoint in the factory contract.
create_pair
takes in the pair_type
(xyk
) and a vector containing objects of type AssetInfo
(contract addresses or native denomenations for the pair).
feather.js
example
For example, if you want to create a pair on Terra, you can implement this operation using feather.js.
_19{_19 "create_pair": {_19 "pair_type": {_19 "xyk": {}_19 }, _19 "asset_infos": [_19 {_19 "token": {_19 "contract_addr": astroAddress_19 }_19 }, _19 {_19 "token": {_19 "contract_addr": steakAddress_19 }_19 }_19 ]_19 }_19}
Stableswap Pools
Creates a stable
pair.
create_pair
To create a stable pair, you need to execute a contract message pointing to the create_pair
endpoint in the factory contract like we did with xyk
pairs above.
However, there are two key differences:
pair_type
is now defined asstable: {}
init_params
is used to pass in a binary encoded message. In this case, additional pool parameters needed for initializing stableswap pairs such as amplitude.
_24{_24 "create_pair": {_24 "pair_type": {_24 "stable": {}_24 }, _24 "asset_infos": [_24 {_24 "token": {_24 "contract_addr": lunaxAddress_24 }_24 }, _24 {_24 "token": {_24 "contract_addr": steakAddress_24 }_24 }_24 ], _24 "init_params": toBase64(_24 {_24 "amp": 10_24 }_24 )_24 }_24}
Encoding our Msg
To encode our message, there are two common options:
- An online base64 encoder
- A custom function
The code in this section uses a custom function (toBase64
) to display our binary message - this function needs to be defined elsewhere to be used. The actual string representation of our message would be an encoded binary.
_24{_24 "create_pair": {_24 "pair_type": {_24 "stable": {}_24 }, _24 "asset_infos": [_24 {_24 "token": {_24 "contract_addr": lunaxAddress_24 }_24 }, _24 {_24 "token": {_24 "contract_addr": steakAddress_24 }_24 }_24 ], _24 "init_params": toBase64(_24 {_24 "amp": 10_24 }_24 )_24 }_24}
toBase64
The code in this section is an example of using feather.js to define a custom function to encode our init_params
message.
_50const factoryAddress = `terra1z3y69xas85r7egusa0c7m5sam0yk97gsztqmh8f2cc6rr4s4anysudp7k0`;_50const lunaxAddress = 'terra138fmmywk3zvqezaqwnxyv8kyr8k7q4e7latncv0xreej62u9mx0su95h7d';_50const steakAddress = 'terra1xztnx8mm7dagn4ck3dgylaqucp6h6agw83pmyc29hnplq7355trs78fkcq'_50_50const toBase64 = (obj) => {_50 return Buffer.from(JSON.stringify(obj)).toString("base64");_50};_50_50const createStablePair = new MsgExecuteContract(_50 wallet.key.accAddress('terra'),_50 factoryAddress,_50 {_50 "create_pair": {_50 "pair_type": {_50 "stable": {}_50 }, _50 "asset_infos": [_50 {_50 "token": {_50 "contract_addr": lunaxAddress_50 }_50 }, _50 {_50 "token": {_50 "contract_addr": steakAddress_50 }_50 }_50 ], _50 "init_params": toBase64(_50 {_50 "amp": 10_50 }_50 )_50 }_50 }_50)_50_50_50let fee = new Fee(2000000, { uluna: 200000 });_50_50// // BROADCAST TRANSACTION_50let tx = await wallet.createAndSignTx({_50 msgs: [createStablePair],_50 fee,_50 chainID: 'pisco-1',_50});_50_50let result = await lcd.tx.broadcast(tx, 'pisco-1');_50_50console.log(result);
create_pair
To create a stable pair, you need to execute a contract message pointing to the create_pair
endpoint in the factory contract like we did with xyk
pairs above.
However, there are two key differences:
pair_type
is now defined asstable: {}
init_params
is used to pass in a binary encoded message. In this case, additional pool parameters needed for initializing stableswap pairs such as amplitude.
Encoding our Msg
To encode our message, there are two common options:
- An online base64 encoder
- A custom function
The code in this section uses a custom function (toBase64
) to display our binary message - this function needs to be defined elsewhere to be used. The actual string representation of our message would be an encoded binary.
toBase64
The code in this section is an example of using feather.js to define a custom function to encode our init_params
message.
_24{_24 "create_pair": {_24 "pair_type": {_24 "stable": {}_24 }, _24 "asset_infos": [_24 {_24 "token": {_24 "contract_addr": lunaxAddress_24 }_24 }, _24 {_24 "token": {_24 "contract_addr": steakAddress_24 }_24 }_24 ], _24 "init_params": toBase64(_24 {_24 "amp": 10_24 }_24 )_24 }_24}