Skip to content

Commit

Permalink
Update SDK to to replace staker with depositOwner
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuba committed Apr 9, 2024
1 parent b4552b1 commit 4e81364
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 42 deletions.
4 changes: 2 additions & 2 deletions dapp/src/web3/relayer-depositor-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class RelayerDepositorProxy<T extends BitcoinDepositor>

if (!extraData) throw new Error("Invalid extra data")

const { staker, referral } =
const { depositOwner, referral } =
this.#bitcoinDepositor.decodeExtraData(extraData)

// TODO: Catch and handle errors + sentry.
Expand All @@ -81,7 +81,7 @@ class RelayerDepositorProxy<T extends BitcoinDepositor>
{
fundingTx,
reveal,
staker: `0x${staker.identifierHex}`,
depositOwner: `0x${depositOwner.identifierHex}`,
referral,
},
)
Expand Down
10 changes: 5 additions & 5 deletions sdk/src/lib/contracts/bitcoin-depositor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DepositorProxy } from "./depositor-proxy"
export { DepositReceipt } from "@keep-network/tbtc-v2.ts"

export type DecodedExtraData = {
staker: ChainIdentifier
depositOwner: ChainIdentifier
referral: number
}

Expand All @@ -24,14 +24,14 @@ export interface BitcoinDepositor extends DepositorProxy {
getTbtcVaultChainIdentifier(): Promise<ChainIdentifier>

/**
* Encodes staker address and referral as extra data.
* @param staker The address to which the stBTC shares will be minted.
* Encodes deposit owner address and referral as extra data.
* @param depositOwner The address to which the stBTC shares will be minted.
* @param referral Data used for referral program.
*/
encodeExtraData(staker: ChainIdentifier, referral: number): Hex
encodeExtraData(depositOwner: ChainIdentifier, referral: number): Hex

/**
* Decodes staker address and referral from extra data.
* Decodes depositOwner address and referral from extra data.
* @param extraData Encoded extra data.
*/
decodeExtraData(extraData: string): DecodedExtraData
Expand Down
26 changes: 14 additions & 12 deletions sdk/src/lib/ethereum/bitcoin-depositor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ class EthereumBitcoinDepositor

if (!extraData) throw new Error("Invalid extra data")

const { staker, referral } = this.decodeExtraData(extraData)
const { depositOwner, referral } = this.decodeExtraData(extraData)

const tx = await this.instance.initializeStake(
const tx = await this.instance.initializeDeposit(
fundingTx,
reveal,
`0x${staker.identifierHex}`,
`0x${depositOwner.identifierHex}`,
referral,
)

Expand All @@ -98,35 +98,37 @@ class EthereumBitcoinDepositor

/**
* @see {BitcoinDepositor#encodeExtraData}
* @dev Packs the data to bytes32: 20 bytes of staker address and 2 bytes of
* @dev Packs the data to bytes32: 20 bytes of deposit owner address and 2 bytes of
* referral, 10 bytes of trailing zeros.
*/
// eslint-disable-next-line class-methods-use-this
encodeExtraData(staker: ChainIdentifier, referral: number): Hex {
const stakerAddress = `0x${staker.identifierHex}`
encodeExtraData(depositOwner: ChainIdentifier, referral: number): Hex {
const depositOwnerAddress = `0x${depositOwner.identifierHex}`

if (!isAddress(stakerAddress) || stakerAddress === ZeroAddress)
throw new Error("Invalid staker address")
if (!isAddress(depositOwnerAddress) || depositOwnerAddress === ZeroAddress)
throw new Error("Invalid deposit owner address")

const encodedData = solidityPacked(
["address", "uint16"],
[stakerAddress, referral],
[depositOwnerAddress, referral],
)

return Hex.from(zeroPadBytes(encodedData, 32))
}

/**
* @see {BitcoinDepositor#decodeExtraData}
* @dev Unpacks the data from bytes32: 20 bytes of staker address and 2
* @dev Unpacks the data from bytes32: 20 bytes of deposit owner address and 2
* bytes of referral, 10 bytes of trailing zeros.
*/
// eslint-disable-next-line class-methods-use-this
decodeExtraData(extraData: string): DecodedExtraData {
const staker = EthereumAddress.from(getAddress(dataSlice(extraData, 0, 20)))
const depositOwner = EthereumAddress.from(
getAddress(dataSlice(extraData, 0, 20)),
)
const referral = Number(dataSlice(extraData, 20, 22))

return { staker, referral }
return { depositOwner, referral }
}
}

Expand Down
1 change: 1 addition & 0 deletions sdk/src/modules/staking/stake-initialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type StakeOptions = {
backoffStepMs: BackoffRetrierParameters[1]
}

// TODO: Rename to `DepositInitialization` to be consistent with the naming.
/**
* Represents an instance of the staking flow. Staking flow requires a few steps
* which should be done to stake BTC.
Expand Down
30 changes: 21 additions & 9 deletions sdk/test/lib/ethereum/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,60 @@ import { EthereumAddress } from "../../../src"
// eslint-disable-next-line import/prefer-default-export
export const extraDataValidTestData: {
testDescription: string
staker: EthereumAddress
depositOwner: EthereumAddress
referral: number
extraData: string
}[] = [
{
testDescription: "staker has leading zeros",
staker: EthereumAddress.from("0x000055d85E80A49B5930C4a77975d44f012D86C1"),
testDescription: "depositOwner has leading zeros",
depositOwner: EthereumAddress.from(
"0x000055d85E80A49B5930C4a77975d44f012D86C1",
),
referral: 6851, // hex: 0x1ac3
extraData:
"0x000055d85e80a49b5930c4a77975d44f012d86c11ac300000000000000000000",
},
{
testDescription: "staker has trailing zeros",
staker: EthereumAddress.from("0x2d2F8BC7923F7F806Dc9bb2e17F950b42CfE0000"),
testDescription: "depositOwner has trailing zeros",
depositOwner: EthereumAddress.from(
"0x2d2F8BC7923F7F806Dc9bb2e17F950b42CfE0000",
),
referral: 6851, // hex: 0x1ac3
extraData:
"0x2d2f8bc7923f7f806dc9bb2e17f950b42cfe00001ac300000000000000000000",
},
{
testDescription: "referral is zero",
staker: EthereumAddress.from("0xeb098d6cDE6A202981316b24B19e64D82721e89E"),
depositOwner: EthereumAddress.from(
"0xeb098d6cDE6A202981316b24B19e64D82721e89E",
),
referral: 0,
extraData:
"0xeb098d6cde6a202981316b24b19e64d82721e89e000000000000000000000000",
},
{
testDescription: "referral has leading zeros",
staker: EthereumAddress.from("0xeb098d6cDE6A202981316b24B19e64D82721e89E"),
depositOwner: EthereumAddress.from(
"0xeb098d6cDE6A202981316b24B19e64D82721e89E",
),
referral: 31, // hex: 0x001f
extraData:
"0xeb098d6cde6a202981316b24b19e64d82721e89e001f00000000000000000000",
},
{
testDescription: "referral has trailing zeros",
staker: EthereumAddress.from("0xeb098d6cDE6A202981316b24B19e64D82721e89E"),
depositOwner: EthereumAddress.from(
"0xeb098d6cDE6A202981316b24B19e64D82721e89E",
),
referral: 19712, // hex: 0x4d00
extraData:
"0xeb098d6cde6a202981316b24b19e64d82721e89e4d0000000000000000000000",
},
{
testDescription: "referral is maximum value",
staker: EthereumAddress.from("0xeb098d6cDE6A202981316b24B19e64D82721e89E"),
depositOwner: EthereumAddress.from(
"0xeb098d6cDE6A202981316b24B19e64D82721e89E",
),
referral: 65535, // max uint16
extraData:
"0xeb098d6cde6a202981316b24b19e64d82721e89effff00000000000000000000",
Expand Down
32 changes: 18 additions & 14 deletions sdk/test/lib/ethereum/tbtc-depositor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe("BitcoinDepositor", () => {
})

describe("revealDeposit", () => {
const staker = EthereumAddress.from(
const depositOwner = EthereumAddress.from(
"0x000055d85E80A49B5930C4a77975d44f012D86C1",
)
const bitcoinFundingTransaction = {
Expand All @@ -79,11 +79,11 @@ describe("BitcoinDepositor", () => {
refundPublicKeyHash: Hex.from("28e081f285138ccbe389c1eb8985716230129f89"),
blindingFactor: Hex.from("f9f0c90d00039523"),
refundLocktime: Hex.from("60bcea61"),
depositor: staker,
depositor: depositOwner,
}
describe("when extra data is defined", () => {
const extraData = {
staker,
depositOwner,
referral: 6851,
hex: Hex.from(
"0x000055d85e80a49b5930c4a77975d44f012d86c11ac300000000000000000000",
Expand Down Expand Up @@ -157,7 +157,7 @@ describe("BitcoinDepositor", () => {
expect(mockedContractInstance.initializeStake).toHaveBeenCalledWith(
btcTxInfo,
revealInfo,
`0x${staker.identifierHex}`,
`0x${depositOwner.identifierHex}`,
referral,
)
expect(result.toPrefixedString()).toBe(mockedTx.toPrefixedString())
Expand All @@ -184,29 +184,29 @@ describe("BitcoinDepositor", () => {

it.each(extraDataValidTestData)(
"$testDescription",
({ staker, referral, extraData }) => {
const result = depositor.encodeExtraData(staker, referral)
({ depositOwner, referral, extraData }) => {
const result = depositor.encodeExtraData(depositOwner, referral)

expect(spyOnSolidityPacked).toHaveBeenCalledWith(
["address", "uint16"],
[`0x${staker.identifierHex}`, referral],
[`0x${depositOwner.identifierHex}`, referral],
)

expect(result.toPrefixedString()).toEqual(extraData)
},
)

describe("when staker is zero address", () => {
const staker = EthereumAddress.from(ZeroAddress)
describe("when deposit owner is zero address", () => {
const depositOwner = EthereumAddress.from(ZeroAddress)

beforeEach(() => {
spyOnSolidityPacked.mockClear()
})

it("should throw an error", () => {
expect(() => {
depositor.encodeExtraData(staker, 0)
}).toThrow("Invalid staker address")
depositor.encodeExtraData(depositOwner, 0)
}).toThrow("Invalid deposit owner address")
expect(spyOnSolidityPacked).not.toHaveBeenCalled()
})
})
Expand All @@ -219,8 +219,12 @@ describe("BitcoinDepositor", () => {

it.each(extraDataValidTestData)(
"$testDescription",
({ staker: expectedStaker, extraData, referral: expectedReferral }) => {
const { staker, referral } = depositor.decodeExtraData(extraData)
({
depositOwner: expectedDepositOwner,
extraData,
referral: expectedReferral,
}) => {
const { depositOwner, referral } = depositor.decodeExtraData(extraData)

expect(spyOnEthersDataSlice).toHaveBeenNthCalledWith(
1,
Expand All @@ -236,7 +240,7 @@ describe("BitcoinDepositor", () => {
22,
)

expect(expectedStaker.equals(staker)).toBeTruthy()
expect(expectedDepositOwner.equals(depositOwner)).toBeTruthy()
expect(expectedReferral).toBe(referral)
},
)
Expand Down

0 comments on commit 4e81364

Please sign in to comment.