-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: transaction template #808
base: master
Are you sure you want to change the base?
Changes from 1 commit
cbb5490
9755aa8
a342ab7
edb3894
250105d
90dab94
13efa80
fec87f6
fff192b
d46c571
6f91f9e
3a3b9ea
30f1764
3b2ae37
7099194
edd8234
d120896
a50620a
ee5edf4
ab0b78e
49ad6d3
674aa2d
c7e968c
61b9502
ba4110d
1d8cb3f
b2cd834
d82aa19
33591c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
import Input from '../../models/input'; | ||
import Output from '../../models/output'; | ||
import transactionUtils from '../../utils/transaction'; | ||
import { DEFAULT_TX_VERSION, NATIVE_TOKEN_UID } from '../../constants'; | ||
import { CREATE_TOKEN_TX_VERSION, DEFAULT_TX_VERSION, NATIVE_TOKEN_UID } from '../../constants'; | ||
|
||
export interface TokenBalance { | ||
tokens: OutputValueType; | ||
|
@@ -22,63 +22,63 @@ | |
balance: Record<string, TokenBalance>; | ||
|
||
constructor() { | ||
this.balance = {}; | ||
} | ||
|
||
getTokenBalance(token: string): TokenBalance { | ||
if (!this.balance[token]) { | ||
this.balance[token] = { | ||
tokens: 0n, | ||
mint_authorities: 0, | ||
melt_authorities: 0, | ||
}; | ||
} | ||
|
||
return this.balance[token]; | ||
} | ||
|
||
setTokenBalance(token: string, balance: TokenBalance) { | ||
this.balance[token] = balance; | ||
} | ||
|
||
addInput(tx: IHistoryTx, index: number) { | ||
if (tx.outputs.length <= index) { | ||
throw new Error('Index does not exist on tx outputs'); | ||
} | ||
const output = tx.outputs[index]; | ||
const { token } = output; | ||
const balance = this.getTokenBalance(token); | ||
|
||
if (transactionUtils.isAuthorityOutput(output)) { | ||
if (transactionUtils.isMint(output)) { | ||
balance.mint_authorities += 1; | ||
} | ||
|
||
if (transactionUtils.isMelt(output)) { | ||
balance.melt_authorities += 1; | ||
} | ||
} else { | ||
balance.tokens += output.value; | ||
} | ||
|
||
this.setTokenBalance(token, balance); | ||
} | ||
|
||
addOutput(amount: OutputValueType, token: string) { | ||
const balance = this.getTokenBalance(token); | ||
balance.tokens -= amount; | ||
this.setTokenBalance(token, balance); | ||
} | ||
|
||
addOutputAuthority(count: number, token: string, authority: 'mint'|'melt') { | ||
const balance = this.getTokenBalance(token); | ||
if (authority === 'mint') { | ||
balance.mint_authorities -= count; | ||
} | ||
if (authority === 'melt') { | ||
balance.melt_authorities -= count; | ||
} | ||
this.setTokenBalance(token, balance); | ||
} | ||
} | ||
|
||
|
@@ -108,58 +108,76 @@ | |
debug: boolean; | ||
|
||
constructor(logger?: ILogger, debug: boolean = false) { | ||
this.inputs = []; | ||
this.outputs = []; | ||
this.tokens = []; | ||
this.version = DEFAULT_TX_VERSION; | ||
this.signalBits = 0; | ||
this.balance = new TxBalance(); | ||
this.vars = {}; | ||
this._logs = []; | ||
this._logger = logger ?? getDefaultLogger(); | ||
this.debug = debug; | ||
} | ||
|
||
log(message: string): void { | ||
this._logs.push(message); | ||
if (this.debug) { | ||
this._logger.info(message); | ||
} | ||
} | ||
|
||
get logArray(): string[] { | ||
return this._logs; | ||
} | ||
|
||
useCreateTxContext() { | ||
if (this.tokens.length !== 0) { | ||
throw new Error(`Trying to build a create token tx with ${this.tokens.length} tokens on the array`); | ||
} | ||
this.version = CREATE_TOKEN_TX_VERSION; | ||
} | ||
|
||
getTokenDataFor(token: string, useCreatedToken: boolean) { | ||
if (useCreatedToken) { | ||
this.useCreateTxContext(); | ||
return 1; | ||
} | ||
return this.addToken(token); | ||
} | ||
|
||
addToken(token: string) { | ||
if (token === NATIVE_TOKEN_UID) { | ||
return 0; | ||
} | ||
if (this.version === CREATE_TOKEN_TX_VERSION) { | ||
throw new Error(`Cannot add a custom token to a CREATE_TOKEN_TX`); | ||
} | ||
const index = this.tokens.indexOf(token); | ||
if (index > -1) { | ||
// Token is already on the list. | ||
return index + 1; | ||
} | ||
// Token is not on the list, adding now | ||
this.tokens.push(token); | ||
return this.tokens.length; | ||
} | ||
|
||
addInput(position: number, ...inputs: Input[]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. chore: Change the name to better inform that multiple inputs can be added, and that the expected parameter is actually an array and not an object. Initial suggestions would be |
||
if (position === -1) { | ||
this.inputs.push(...inputs); | ||
return; | ||
} | ||
|
||
this.inputs.splice(position, 0, ...inputs); | ||
} | ||
|
||
addOutput(position: number, ...outputs: Output[]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. chore: Same plural naming issue described above. |
||
if (position === -1) { | ||
this.outputs.push(...outputs); | ||
return; | ||
} | ||
|
||
this.outputs.splice(position, 0, ...outputs); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chore: Change the name of the method to better communicate its purpose. As of now, it is a
get
method that actually mutates the object and adds a new token.Also, when
useCreatedToken
is passed,token
is discarded, which is also slightly confusing and would be better accompanied by a method docstring to explain it.