|
| 1 | +import { AbstractLogger, DummyLogger } from '@rosen-bridge/abstract-logger'; |
| 2 | +import * as ergoLib from 'ergo-lib-wasm-nodejs'; |
| 3 | + |
| 4 | +export class CollateralBoxBuilder { |
| 5 | + private height?: number; |
| 6 | + |
| 7 | + constructor( |
| 8 | + private collateralErgoTree: string, |
| 9 | + private awcNftId: string, |
| 10 | + private rsnAmount: bigint, |
| 11 | + private rsnTokenId: string | undefined, |
| 12 | + private collateralRsn: bigint = 0n, |
| 13 | + private wid: Uint8Array, |
| 14 | + private value: string, |
| 15 | + private logger: AbstractLogger = new DummyLogger(), |
| 16 | + ) {} |
| 17 | + |
| 18 | + /** |
| 19 | + * Increases the locked RSN amount allocated to this watcher. |
| 20 | + * @param amount RSN amount to lock |
| 21 | + * @returns Updated CollateralBoxBuilder instance |
| 22 | + */ |
| 23 | + lockRsn = (amount: bigint): CollateralBoxBuilder => { |
| 24 | + if (amount <= 0n) throw new Error('amount must be positive'); |
| 25 | + |
| 26 | + this.rsnAmount += amount; |
| 27 | + this.logger.debug( |
| 28 | + `locked RSN increased by ${amount}, total=${this.rsnAmount}`, |
| 29 | + ); |
| 30 | + return this; |
| 31 | + }; |
| 32 | + |
| 33 | + /** |
| 34 | + * Decreases the locked RSN amount allocated to this watcher. |
| 35 | + * @param amount RSN amount to unlock |
| 36 | + * @returns Updated CollateralBoxBuilder instance |
| 37 | + */ |
| 38 | + unlockRsn = (amount: bigint): CollateralBoxBuilder => { |
| 39 | + if (amount <= 0n) throw new Error('amount must be positive'); |
| 40 | + |
| 41 | + if (this.rsnAmount < amount) |
| 42 | + throw new Error( |
| 43 | + `locked RSN [${this.rsnAmount}] < unlock amount [${amount}]`, |
| 44 | + ); |
| 45 | + |
| 46 | + this.rsnAmount -= amount; |
| 47 | + this.logger.debug( |
| 48 | + `locked RSN decreased by ${amount}, total=${this.rsnAmount}`, |
| 49 | + ); |
| 50 | + return this; |
| 51 | + }; |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates a CollateralBox from the properties of this CollateralBoxBuilder instance |
| 55 | + * |
| 56 | + * @return {ergoLib.ErgoBoxCandidate} |
| 57 | + */ |
| 58 | + build = (): ergoLib.ErgoBoxCandidate => { |
| 59 | + if (this.value === undefined || this.height === undefined) { |
| 60 | + throw new Error(`value and height must be set before build`); |
| 61 | + } |
| 62 | + |
| 63 | + const boxBuilder = new ergoLib.ErgoBoxCandidateBuilder( |
| 64 | + ergoLib.BoxValue.from_i64(ergoLib.I64.from_str(this.value)), |
| 65 | + ergoLib.Contract.new( |
| 66 | + ergoLib.ErgoTree.from_base16_bytes(this.collateralErgoTree), |
| 67 | + ), |
| 68 | + this.height, |
| 69 | + ); |
| 70 | + |
| 71 | + boxBuilder.set_register_value( |
| 72 | + 4, |
| 73 | + ergoLib.Constant.from_byte_array(this.wid), |
| 74 | + ); |
| 75 | + |
| 76 | + boxBuilder.set_register_value( |
| 77 | + 5, |
| 78 | + ergoLib.Constant.from_i64( |
| 79 | + ergoLib.I64.from_str(this.rsnAmount.toString()), |
| 80 | + ), |
| 81 | + ); |
| 82 | + |
| 83 | + boxBuilder.add_token( |
| 84 | + ergoLib.TokenId.from_str(this.awcNftId), |
| 85 | + ergoLib.TokenAmount.from_i64(ergoLib.I64.from_str('1')), |
| 86 | + ); |
| 87 | + |
| 88 | + if (this.rsnTokenId) { |
| 89 | + boxBuilder.add_token( |
| 90 | + ergoLib.TokenId.from_str(this.rsnTokenId), |
| 91 | + ergoLib.TokenAmount.from_i64( |
| 92 | + ergoLib.I64.from_str(this.collateralRsn.toString()), |
| 93 | + ), |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + return boxBuilder.build(); |
| 98 | + }; |
| 99 | + |
| 100 | + /** Sets the height of the box |
| 101 | + * @param {number} height |
| 102 | + */ |
| 103 | + setHeight = (height: number) => { |
| 104 | + if (height < 1) throw new Error('height must be positive'); |
| 105 | + this.height = height; |
| 106 | + }; |
| 107 | +} |
0 commit comments