In order to ensure easy integration with external partners Speed Markets API is created. API returns all required data to interact with Speed Markets AMM contract. Using Speed Markets API endpoints someone can get data about:
Buy
User claimable markets
Resolve markets (single or multiple markets)
Resolve market with different collateral (single market)
More details about each API endpoint with request/response examples can be found under Postman documentation.
Contract integration
Once all data are fetched from API, the next step is integration with Speed Markets contract. Depending on whether someone wants to buy a position or resolve a market (claim win) integration should be done with Speed Markets AMM contract.
The next sections describe integration with Speed Markets API and Speed Markets contract together with JS code examples.
Buy a UP/DOWN position
Let's say someone wants to buy UP position on the BTC market with a current strike price ($ 63,622.56) in 10 minutes from market creation and with a buy-in amount of 5 sUSD:
Integration with Speed Markets API and Speed Markets AMM contract should include the following steps:
Get a buy parameters for the market from Speed Markets API
Get a Speed Markets AMM contract address for a specific network from Thales contracts
Get a Speed Markets AMM contract ABI from Speed Markets AMM contract repository
Create Speed Markets AMM contract instance
Call createNewMarket or createNewMarketWithDifferentCollateral method on Speed Markets AMM contract with input parameters fetched from Speed Markets API in step #1
The JS code snippet below implements these steps:
constethers=require('ethers');constfetch=require('node-fetch');constdotenv=require('dotenv');// SpeedMarketsAMM contract ABIconst { speedAMMContract } =require('./speedAmmContractAbi.js');dotenv.config();constAPI_URL='https://overtimemarketsv2.xyz'; // base API URLconstNETWORK_ID=10; // optimism network IDconstNETWORK='optimism'; // optimism network// SpeedMarketsAMM contract address on optimismconstAMM_CONTRACT_ADDRESS='0xE16B8a01490835EC1e76bAbbB3Cadd8921b32001'; constASSET='BTC';constDIRECTION='UP';constCOLLATERAL='sUSD';constBUY_IN=5; // 5 sUSDconstDELTA_TIME=600; // 10 min// create instance of Infura provider for optimism networkconstprovider=newethers.providers.InfuraProvider( { chainId:Number(NETWORK_ID), name:NETWORK },process.env.INFURA);// create wallet instance for provided private key and providerconstwallet=newethers.Wallet(process.env.PRIVATE_KEY, provider);// create instance of Speed AMM contractconstspeedAmm=newethers.Contract(AMM_CONTRACT_ADDRESS,speedAMMContract.abi, wallet);constcreateNewMarket=async () => {try { // Get contract method (createNewMarket/createNewMarketWithDifferentCollateral) parameters from Speed Markets API
// for provided asset, direction, buy-in amount, collateral and delta time on optimism networkconstbuyResponse=awaitfetch( `${API_URL}/speed-markets/networks/${NETWORK_ID}/buy/?asset=${ASSET}&direction=${DIRECTION}&buyin=${BUY_IN}&collateral=${COLLATERAL}&deltaTimeSec=${DELTA_TIME}`
);constbuyData=awaitbuyResponse.json();console.log('Buy data', buyData);let tx;if (buyData.methodName =='createNewMarketWithDifferentCollateral') {// call createNewMarketWithDifferentCollateral method on Speed Markets AMM contract tx =awaitspeedAmm.createNewMarketWithDifferentCollateral(buyData.asset,buyData.strikeTime,buyData.delta,buyData.direction,buyData.priceUpdateData,buyData.collateral,buyData.collateralAmount,buyData.isEth,buyData.referrer,buyData.skewImpact, { value:buyData.value, type:2, maxPriorityFeePerGas:10,// 10 wei } ); } else {// call createNewMarket method on Speed Markets AMM contract tx =awaitspeedAmm.createNewMarket(buyData.asset,buyData.strikeTime,buyData.delta,buyData.direction,buyData.buyinAmount,buyData.priceUpdateData,buyData.referrer,buyData.skewImpact, { value:buyData.value, type:2, maxPriorityFeePerGas:10 } // 10 wei ); }// wait for the resultconsttxResult=awaittx.wait();console.log(`Successfully bought from Speed AMM. Transaction hash: ${txResult.transactionHash}`); } catch (e) {console.log('Failed to buy from Speed AMM', e); }};createNewMarket();
Resolve market(s)
Let's say someone wants to claim winnings on twomarkets (resolve markets) in sUSD:
Integration with Speed Markets API and Speed Markets AMM contract should include the following steps:
Get a resolve parameters for the markets from Speed Markets API
Get a Speed Markets AMM contract address for a specific network from Thales contracts
Get a Speed Markets AMM contract ABI from Speed Markets AMM contract repository
Get a user claimable markets from Speed Markets API
Create Speed Markets AMM contract instance
Call resolveMarketsBatch method on Speed Markets AMM contract with input parameters fetched from Speed Markets API in step #1
The JS code snippet below implements these steps:
constethers=require('ethers');constfetch=require('node-fetch');constdotenv=require('dotenv');// SpeedMarketsAMM contract ABIconst { speedAMMContract } =require('./speedAmmContractAbi.js'); dotenv.config();constAPI_URL='https://overtimemarketsv2.xyz'; // base API URLconstNETWORK_ID=10; // optimism network IDconstNETWORK='optimism'; // optimism network// SpeedMarketsAMM contract address on optimismconstAMM_CONTRACT_ADDRESS='0xE16B8a01490835EC1e76bAbbB3Cadd8921b32001'; // Speed markets addresses to resolveconstMARKET_1='0x5ef786087d122b9056f351Cf3B52E5A7B0d5277D'; constMARKET_2='0x2542906FE4701A8c930427c2ff6Db1C948B91571';// create instance of Infura provider for optimism networkconstprovider=newethers.providers.InfuraProvider( { chainId:Number(NETWORK_ID), name:NETWORK },process.env.INFURA);// create wallet instance for provided private key and providerconstwallet=newethers.Wallet(process.env.PRIVATE_KEY, provider);// create instance of Speed AMM contractconstspeedAmm=newethers.Contract(AMM_CONTRACT_ADDRESS,speedAMMContract.abi, wallet);constresolveMarkets=async () => {try {// Get contract method (resolveMarketsBatch) parameters from Speed Markets API// for provided market address on optimism networkconstresolveResponse=awaitfetch(`${API_URL}/speed-markets/networks/${NETWORK_ID}/resolve?markets[]=${MARKET_1}&markets[]=${MARKET_2}` );constresolveData=awaitresolveResponse.json();console.log('Resolve data', resolveData);// call resolveMarketsBatch method on Speed Markets AMM contractconsttx=awaitspeedAmm.resolveMarketsBatch(resolveData.markets,resolveData.priceUpdateData, { value:resolveData.value, type:2, maxPriorityFeePerGas:10,// 10 wei } );// wait for the resultconsttxResult=awaittx.wait();console.log(`Successfully resolved market on Speed AMM. Transaction hash: ${txResult.transactionHash}`); } catch (e) {console.log('Failed to resolve market on Speed AMM', e); }};resolveMarkets();
Resolve market with different collateral
If someone wants to claim winning in different collateral than default one for a network(resolve market) process is the same as previous one, just using appropriate API and contract method. Corresponding API is "Resolve market with different collateral" and contract method resolveMarketWithOfframp. Currently batch is not available for claim with different collateral.