-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: decouple transfer execution from generator
- Loading branch information
1 parent
464c6b9
commit 3d7f891
Showing
6 changed files
with
315 additions
and
216 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { JsonRpcDatasource } from ".." | ||
import { Inscriber } from "../transactions/Inscriber" | ||
import { | ||
BRC20TransferOptions, | ||
GetBRC20BalancesOptions, | ||
HasEnoughBalanceOptions, | ||
ValidateBRC20TransferOptions | ||
} from "./types" | ||
|
||
export class BRC20TransferBase extends Inscriber { | ||
protected tick: string | ||
protected amount = 0 | ||
|
||
protected _destinationAddress: string | ||
|
||
constructor({ address, pubKey, destinationAddress, feeRate, network, tick, amount }: BRC20TransferOptions) { | ||
super({ | ||
autoAdjustment: false, | ||
network, | ||
address, | ||
changeAddress: address, | ||
destinationAddress: address, // IMPORTANT to keep destination as owner address | ||
publicKey: pubKey, | ||
feeRate, | ||
postage: 1000, | ||
mediaType: "<temp-type>", // Set on payload creation | ||
mediaContent: "<temp-content>" // Set on payload creation | ||
}) | ||
|
||
this._destinationAddress = destinationAddress | ||
|
||
this.tick = tick | ||
this.amount = amount | ||
} | ||
|
||
static async validateTransferOptions({ amount, tick, datasource, network }: ValidateBRC20TransferOptions) { | ||
if (isNaN(amount) || amount <= 0) { | ||
throw new Error("Invalid amount") | ||
} | ||
|
||
datasource = datasource || new JsonRpcDatasource({ network }) | ||
const token = await datasource.getToken({ tick }) | ||
if (!token) { | ||
throw new Error("Invalid token") | ||
} | ||
} | ||
|
||
static async getBalances({ address, tick, datasource, network }: GetBRC20BalancesOptions) { | ||
datasource = datasource || new JsonRpcDatasource({ network }) | ||
const balances = await datasource.getAddressTokens({ | ||
address: address | ||
}) | ||
const balance = balances.find((b) => b.tick === tick) | ||
if (!balance) { | ||
throw new Error("Owner does not have any balance for this token") | ||
} | ||
|
||
return { | ||
total: balance.total, | ||
available: balance.available, | ||
transferable: balance.transferable | ||
} | ||
} | ||
|
||
static async hasEnoughTransferableBalance({ address, amount, tick, datasource, network }: HasEnoughBalanceOptions) { | ||
const { transferable } = await BRC20TransferBase.getBalances({ address, tick, datasource, network }) | ||
return transferable >= amount | ||
} | ||
|
||
static async hasEnoughOverallBalance({ address, amount, tick, datasource, network }: HasEnoughBalanceOptions) { | ||
const { available } = await BRC20TransferBase.getBalances({ address, tick, datasource, network }) | ||
return available >= amount | ||
} | ||
} |
Oops, something went wrong.