Skip to content

Commit

Permalink
Add unit tests for getDeposits
Browse files Browse the repository at this point in the history
Also here we update the `getDepositsByOwner` from `AcreSubgraphApi` -
add the `amountToDeposit` and `initialAmount` fields and we should use
the initial amount regardless of the deposit state.
  • Loading branch information
r-czajkowski committed May 22, 2024
1 parent c2518f3 commit 1c159b7
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 26 deletions.
6 changes: 4 additions & 2 deletions sdk/src/lib/api/AcreSubgraphApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type DepositDataResponse = {
type Deposit = {
depositKey: string
txHash: string
amount: bigint
initialAmount: bigint
amountToDeposit: bigint
status: DepositStatus
}

Expand Down Expand Up @@ -70,7 +71,8 @@ export default class AcreSubgraphApi extends HttpApi {
return {
depositKey: id,
txHash,
amount: BigInt(amountToDeposit ?? initialDepositAmount),
initialAmount: BigInt(initialDepositAmount),
amountToDeposit: BigInt(amountToDeposit ?? 0),
type: "deposit",
status,
}
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/modules/staking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class StakingModule {
)

const amount = toSatoshi(
depositFromSubgraph?.amount ?? deposit.initialAmount,
depositFromSubgraph?.initialAmount ?? deposit.initialAmount,
)

return {
Expand Down
6 changes: 4 additions & 2 deletions sdk/test/lib/api/acre-subgraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ const expectedDepositData = [
depositKey:
"0x35a0ef1f1b41e5dbe56f5c02c9902ca4317f587e352d9fc8859af4e569b80c1f",
txHash: "6349459ccb07419a7184577adf7a992216a214792e45ad2a689127d14c7a4cb9",
amount: BigInt("1200000000000000"),
initialAmount: BigInt("1200000000000000"),
amountToDeposit: BigInt(0),
type: "deposit",
status: DepositStatus.Initialized,
},
{
depositKey:
"0x73661e3ee3c6c30988800e5fedc081f29c6540505383fcfcd172fd10f3a73139",
txHash: "6bca75ba55334c25064e7bf5333a3b39ed5bb73fb17e73ea9e55e6294e3fbf65",
amount: BigInt("536361040000000"),
initialAmount: BigInt("1040000000000000"),
amountToDeposit: BigInt("536361040000000"),
type: "deposit",
status: DepositStatus.Finalized,
},
Expand Down
128 changes: 107 additions & 21 deletions sdk/test/modules/staking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
EthereumAddress,
DepositFees,
DepositFee,
DepositStatus,
} from "../../src"
import * as satoshiConverter from "../../src/lib/utils/satoshi-converter"
import { MockAcreContracts } from "../utils/mock-acre-contracts"
Expand Down Expand Up @@ -107,29 +108,32 @@ describe("Staking", () => {
const bitcoinProvider = new MockBitcoinProvider()
const orangeKit = new MockOrangeKitSdk()
const tbtc = new MockTbtc()
const acreSubgraph = new AcreSubgraphApi("test")

const { bitcoinDepositorAddress, predictedEthereumDepositorAddress } =
stakingModuleData.initializeDeposit

bitcoinProvider.getAddress.mockResolvedValue(
stakingModuleData.initializeDeposit.bitcoinDepositorAddress,
)

orangeKit.predictAddress = jest
.fn()
.mockResolvedValue(`0x${predictedEthereumDepositorAddress.identifierHex}`)

const staking: StakingModule = new StakingModule(
contracts,
bitcoinProvider,
// @ts-expect-error Error: Property '#private' is missing in type
// 'MockOrangeKitSdk' but required in type 'OrangeKitSdk'.
orangeKit,
tbtc,
{} as AcreSubgraphApi,
acreSubgraph,
)

describe("initializeStake", () => {
const {
mockedDepositBTCAddress,
bitcoinDepositorAddress,
predictedEthereumDepositorAddress,
referral,
extraData,
} = stakingModuleData.initializeDeposit
const { mockedDepositBTCAddress, referral, extraData } =
stakingModuleData.initializeDeposit
// TODO: Rename to `depositor`.
const staker = predictedEthereumDepositorAddress

Expand Down Expand Up @@ -171,12 +175,6 @@ describe("Staking", () => {

tbtc.initiateDeposit = jest.fn().mockReturnValue(mockedDeposit)

orangeKit.predictAddress = jest
.fn()
.mockResolvedValue(
`0x${predictedEthereumDepositorAddress.identifierHex}`,
)

result = await staking.initializeStake(
referral,
bitcoinRecoveryAddress,
Expand Down Expand Up @@ -298,13 +296,13 @@ describe("Staking", () => {
)
})

describe("when the message has not been signed yet", () => {
it("should throw an error", async () => {
await expect(result.stake()).rejects.toThrow(
"Sign message first",
)
})
})
// describe("when the message has not been signed yet", () => {
// it("should throw an error", async () => {
// await expect(result.stake()).rejects.toThrow(
// "Sign message first",
// )
// })
// })

describe("when message has already been signed", () => {
let depositId: string
Expand Down Expand Up @@ -466,4 +464,92 @@ describe("Staking", () => {
})
})
})

describe("getDeposits", () => {
const finalizedDeposit = {
subgraph: {
depositKey:
"0x73661e3ee3c6c30988800e5fedc081f29c6540505383fcfcd172fd10f3a73139",
txHash:
"6bca75ba55334c25064e7bf5333a3b39ed5bb73fb17e73ea9e55e6294e3fbf65",
initialAmount: BigInt("1040000000000000"),
amountToDeposit: BigInt("536361040000000"),
type: "deposit",
status: DepositStatus.Finalized,
},
tbtc: {
depositKey:
"0x73661e3ee3c6c30988800e5fedc081f29c6540505383fcfcd172fd10f3a73139",
txHash:
"6bca75ba55334c25064e7bf5333a3b39ed5bb73fb17e73ea9e55e6294e3fbf65",
initialAmount: BigInt("1040000000000000"),
status: DepositStatus.Finalized,
},
}

const queuedDeposit = {
txHash:
"96c40dcd6ef25bd27f926dc396bb8f8e7365b4746870e2d186ff001d6693a3ff",
depositKey:
"0xc6c428b14cf7c3116603b355ad3ddf029ea5119a2699090c80f99b68dd40fea4",
outputIndex: 0,
initialAmount: BigInt("1050000000000000"),
status: DepositStatus.Queued,
}

const spyOnSubgraphGetDeposits = jest
.spyOn(acreSubgraph, "getDepositsByOwner")
.mockResolvedValueOnce([finalizedDeposit.subgraph])

const expectedDeposits = [
{
id: queuedDeposit.depositKey,
txHash: queuedDeposit.txHash,
amount: 105000n,
status: DepositStatus.Queued,
},
{
id: finalizedDeposit.subgraph.depositKey,
txHash: finalizedDeposit.subgraph.txHash,
amount: 104000n,
status: DepositStatus.Finalized,
},
]

let result: Awaited<ReturnType<StakingModule["getDeposits"]>>

beforeAll(async () => {
tbtc.getDepositsByOwner = jest
.fn()
.mockResolvedValue([queuedDeposit, finalizedDeposit.tbtc])

result = await staking.getDeposits()
})

it("should get the bitcoin address from bitcoin provider", () => {
expect(bitcoinProvider.getAddress).toHaveBeenCalled()
})

it("should get Ethereum depositor owner address", () => {
expect(orangeKit.predictAddress).toHaveBeenCalledWith(
bitcoinDepositorAddress,
)
})

it("should get deposits from subgraph", () => {
expect(spyOnSubgraphGetDeposits).toHaveBeenCalledWith(
predictedEthereumDepositorAddress,
)
})

it("should get deposits from tbtc module", () => {
expect(tbtc.getDepositsByOwner).toHaveBeenCalledWith(
predictedEthereumDepositorAddress,
)
})

it("should return correct data", () => {
expect(result).toStrictEqual(expectedDeposits)
})
})
})

0 comments on commit 1c159b7

Please sign in to comment.