Skip to content

Commit 9110e55

Browse files
committed
v4.4.0
1 parent 1b147ed commit 9110e55

File tree

8 files changed

+199
-3
lines changed

8 files changed

+199
-3
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@azuro-org/toolkit",
3-
"version": "4.3.3",
3+
"version": "4.4.0",
44
"description": "Set of helpers to work with Azuro protocol",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/config.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ export const ODDS_DECIMALS = 12
1010
export const MARGIN_DECIMALS = 12
1111
export const MIN_LIVE_BET_AMOUNT = 1
1212

13+
export const LIVE_BET_DATA_TYPES = {
14+
ClientBetData: [
15+
{ name: 'attention', type: 'string' },
16+
{ name: 'affiliate', type: 'address' },
17+
{ name: 'core', type: 'address' },
18+
{ name: 'amount', type: 'uint128' },
19+
{ name: 'nonce', type: 'uint256' },
20+
{ name: 'conditionId', type: 'uint256' },
21+
{ name: 'outcomeId', type: 'uint64' },
22+
{ name: 'minOdds', type: 'uint64' },
23+
{ name: 'expiresAt', type: 'uint256' },
24+
{ name: 'chainId', type: 'uint256' },
25+
{ name: 'relayerFeeAmount', type: 'uint256' },
26+
],
27+
} as const
28+
29+
export const LIVE_TYPED_DATA_DOMAIN_NAME = 'Live Betting'
30+
export const LIVE_TYPED_DATA_DOMAIN_VERSION = '1.0.0'
31+
1332
export const deBridgeUrl = 'https://api.dln.trade/v1.0'
1433
export const deBridgeTxUrl = 'https://stats-api.dln.trade/api'
1534

src/global.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
1+
import { type Address } from 'viem'
2+
3+
import { type ChainId } from './config'
4+
5+
16
export type Selection = {
27
outcomeId: string
38
conditionId: string
49
coreAddress: string
510
}
611

712
export type WaveId = number | 'active'
13+
14+
export type LiveBet = {
15+
attention: string
16+
affiliate: Address
17+
core: Address
18+
amount: string
19+
chainId: ChainId
20+
conditionId: string
21+
outcomeId: number
22+
minOdds: string
23+
nonce: string
24+
expiresAt: number
25+
relayerFeeAmount: string
26+
}

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export { groupByConditionId } from './utils/groupByConditionId'
2121
export { setupContracts, type Contracts } from './utils/setupContracts'
2222
export { getApiEndpoint, getLiveGraphqlEndpoint, getPrematchGraphqlEndpoint, getSocketEndpoint } from './utils/getEndpoints'
2323
export { getFreeBets, FreeBetStatus, type FreeBet } from './utils/getFreebets'
24+
export { getLiveBetTypedData } from './utils/getLiveBetTypedData'
25+
export { createLiveBet, type CreateLiveBetResponse, LiveBetState } from './utils/createLiveBet'
26+
export { getLiveBet, type GetLiveBetResponse } from './utils/getLiveBet'
2427

2528
// wave
2629
export { getWaveLevels, WaveLevelName, type WaveLevelData, type WaveLevelsResponse } from './utils/wave/getWaveLevels'

src/utils/createLiveBet.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { type Address, type Hex } from 'viem'
2+
3+
import { type ChainId, chainsData, liveSupportedChains } from '../config'
4+
import { type LiveBet } from '../global'
5+
6+
7+
export enum LiveBetState {
8+
Created = 'Created',
9+
Pending = 'Pending',
10+
Sent = 'Sent',
11+
Accepted = 'Accepted',
12+
Rejected = 'Rejected'
13+
}
14+
15+
export type CreateLiveBetResponse = {
16+
id: string
17+
state: LiveBetState
18+
errorMessage?: string
19+
}
20+
21+
type Props = {
22+
chainId: ChainId
23+
account: Address
24+
bet: LiveBet
25+
signature: Hex
26+
}
27+
28+
export const createLiveBet = async (props: Props) => {
29+
const { chainId, account, bet, signature } = props
30+
31+
if (!liveSupportedChains.includes(chainId)) {
32+
throw new Error('provided chainId is not supported for live bet')
33+
}
34+
35+
const { api, environment } = chainsData[chainId]
36+
37+
const order = { bet }
38+
39+
const signedBet = {
40+
environment,
41+
bettor: account.toLowerCase(),
42+
data: order,
43+
bettorSignature: signature,
44+
}
45+
46+
const response = await fetch(`${api}/orders`, {
47+
method: 'POST',
48+
headers: {
49+
'Accept': 'application/json',
50+
'Content-Type': 'application/json',
51+
},
52+
body: JSON.stringify(signedBet),
53+
})
54+
55+
if (response.status === 404) {
56+
return null
57+
}
58+
59+
if (!response.ok) {
60+
throw new Error(`Status ${response.status}: ${response.statusText}`)
61+
}
62+
63+
const data: CreateLiveBetResponse = await response.json()
64+
65+
return data
66+
}

src/utils/getLiveBet.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { chainsData, liveSupportedChains, type ChainId } from '../config'
2+
import { type CreateLiveBetResponse } from './createLiveBet'
3+
4+
5+
export type GetLiveBetResponse = {
6+
txHash: string
7+
odds: string
8+
betId: string
9+
} & CreateLiveBetResponse
10+
11+
type Props = {
12+
chainId: ChainId
13+
orderId: string
14+
}
15+
16+
export const getLiveBet = async ({ chainId, orderId }: Props) => {
17+
if (!liveSupportedChains.includes(chainId)) {
18+
throw new Error('provided chainId is not supported for live bet')
19+
}
20+
21+
const { api } = chainsData[chainId]
22+
23+
const response = await fetch(`${api}/orders/${orderId}`)
24+
25+
if (response.status === 404) {
26+
return null
27+
}
28+
29+
if (!response.ok) {
30+
throw new Error(`Status ${response.status}: ${response.statusText}`)
31+
}
32+
33+
const data: GetLiveBetResponse = await response.json()
34+
35+
return data
36+
}

src/utils/getLiveBetTypedData.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { type SignTypedDataParameters, type Address, type TypedDataDomain } from 'viem'
2+
3+
import {
4+
type ChainId,
5+
chainsData,
6+
liveSupportedChains,
7+
LIVE_BET_DATA_TYPES,
8+
LIVE_TYPED_DATA_DOMAIN_NAME,
9+
LIVE_TYPED_DATA_DOMAIN_VERSION,
10+
} from '../config'
11+
import { type LiveBet } from '../global'
12+
13+
14+
type Props = {
15+
chainId: ChainId
16+
account: Address
17+
bet: LiveBet
18+
}
19+
20+
export const getLiveBetTypedData = ({ account, chainId, bet }: Props): SignTypedDataParameters => {
21+
if (!liveSupportedChains.includes(chainId)) {
22+
throw new Error('provided chainId is not supported for live bet')
23+
}
24+
25+
const { contracts } = chainsData[chainId]
26+
27+
const EIP712Domain: TypedDataDomain = {
28+
name: LIVE_TYPED_DATA_DOMAIN_NAME,
29+
version: LIVE_TYPED_DATA_DOMAIN_VERSION,
30+
chainId,
31+
verifyingContract: contracts.liveCore!.address,
32+
}
33+
34+
return {
35+
account: account,
36+
domain: EIP712Domain,
37+
primaryType: 'ClientBetData',
38+
types: LIVE_BET_DATA_TYPES,
39+
message: {
40+
attention: bet.attention,
41+
affiliate: bet.affiliate,
42+
core: bet.core,
43+
amount: BigInt(bet.amount),
44+
nonce: BigInt(bet.nonce),
45+
conditionId: BigInt(bet.conditionId),
46+
outcomeId: BigInt(bet.outcomeId),
47+
minOdds: BigInt(bet.minOdds),
48+
expiresAt: BigInt(bet.expiresAt),
49+
chainId: BigInt(bet.chainId),
50+
relayerFeeAmount: BigInt(bet.relayerFeeAmount),
51+
},
52+
}
53+
}

0 commit comments

Comments
 (0)