-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implements transaction builder (#22)
- Loading branch information
Showing
10 changed files
with
238 additions
and
99 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './encode.utils'; | ||
export * from './message.utils'; | ||
export * from './storage.utils'; | ||
export * from './transaction-message.utils'; | ||
export * from './transaction.utils'; |
100 changes: 100 additions & 0 deletions
100
packages/sdk/src/core/utils/transaction-message.utils.ts
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,100 @@ | ||
import { Any, MsgAddPackage, MsgCall, MsgEndpoint, MsgSend } from '@gnolang/gno-js-client'; | ||
import { MsgRun } from '@gnolang/gno-js-client/bin/proto/gno/vm'; | ||
|
||
import { AddPackageMessage, MsgCallMessage, MsgRunMessage, MsgSendMessage, TransactionMessage } from '../types'; | ||
|
||
export function makeAddPackageMessage(value: MsgAddPackage): AddPackageMessage { | ||
return { | ||
type: MsgEndpoint.MSG_ADD_PKG, | ||
value, | ||
}; | ||
} | ||
|
||
export function makeMsgCallMessage(value: MsgCall): MsgCallMessage { | ||
return { | ||
type: MsgEndpoint.MSG_CALL, | ||
value, | ||
}; | ||
} | ||
|
||
export function makeMsgSendMessage(value: MsgSend): MsgSendMessage { | ||
return { | ||
type: MsgEndpoint.MSG_SEND, | ||
value, | ||
}; | ||
} | ||
|
||
export function makeMsgRunMessage(value: MsgRun): MsgRunMessage { | ||
return { | ||
type: MsgEndpoint.MSG_RUN, | ||
value, | ||
}; | ||
} | ||
|
||
export function encodeTransactionMessage(message: TransactionMessage): Uint8Array { | ||
if (isAddPackageMessage(message)) { | ||
return MsgAddPackage.encode(message.value).finish(); | ||
} | ||
|
||
if (isMsgCallMessage(message)) { | ||
return MsgCall.encode(message.value).finish(); | ||
} | ||
|
||
if (isMsgSendMessage(message)) { | ||
return MsgSend.encode(message.value).finish(); | ||
} | ||
|
||
if (isMsgRunMessage(message)) { | ||
return MsgRun.encode(message.value).finish(); | ||
} | ||
|
||
throw new Error('Unknown message type'); | ||
} | ||
|
||
export function decodeTransactionMessage(message: Any): TransactionMessage { | ||
if (message.typeUrl === MsgEndpoint.MSG_ADD_PKG) { | ||
return { | ||
type: MsgEndpoint.MSG_ADD_PKG, | ||
value: MsgAddPackage.decode(message.value), | ||
}; | ||
} | ||
|
||
if (message.typeUrl === MsgEndpoint.MSG_CALL) { | ||
return { | ||
type: MsgEndpoint.MSG_CALL, | ||
value: MsgCall.decode(message.value), | ||
}; | ||
} | ||
|
||
if (message.typeUrl === MsgEndpoint.MSG_SEND) { | ||
return { | ||
type: MsgEndpoint.MSG_SEND, | ||
value: MsgSend.decode(message.value), | ||
}; | ||
} | ||
|
||
if (message.typeUrl === MsgEndpoint.MSG_RUN) { | ||
return { | ||
type: MsgEndpoint.MSG_RUN, | ||
value: MsgRun.decode(message.value), | ||
}; | ||
} | ||
|
||
throw new Error('Unknown message type'); | ||
} | ||
|
||
function isAddPackageMessage(message: TransactionMessage): message is AddPackageMessage { | ||
return message.type === MsgEndpoint.MSG_ADD_PKG; | ||
} | ||
|
||
function isMsgCallMessage(message: TransactionMessage): message is MsgCallMessage { | ||
return message.type === MsgEndpoint.MSG_CALL; | ||
} | ||
|
||
function isMsgSendMessage(message: TransactionMessage): message is MsgSendMessage { | ||
return message.type === MsgEndpoint.MSG_SEND; | ||
} | ||
|
||
function isMsgRunMessage(message: TransactionMessage): message is MsgRunMessage { | ||
return message.type === MsgEndpoint.MSG_RUN; | ||
} |
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 |
---|---|---|
@@ -1,74 +1,65 @@ | ||
import { Tx } from '@gnolang/tm2-js-client'; | ||
import { TransactionMessage } from '../types'; | ||
import { Any, defaultTxFee } from '@gnolang/gno-js-client'; | ||
import { Tx, TxFee } from '@gnolang/tm2-js-client'; | ||
|
||
export const defaultGasFee = { | ||
amount: 1000000, | ||
denom: 'ugnot', | ||
}; | ||
import { TransactionMessage } from '../types'; | ||
import { encodeTransactionMessage } from './transaction-message.utils'; | ||
|
||
export const defaultGasWanted = 1_000_000; | ||
export const defaultGasWanted = 10_000_000; | ||
|
||
export class TransactionBuilder { | ||
private _messages: TransactionMessage[] = []; | ||
private _fees: { amount: string; denom: string }[] = []; | ||
private _chainId: string = ''; | ||
private _memo: string = ''; | ||
private _accountNumber: string = ''; | ||
private _sequence: string = ''; | ||
private _gasWanted: string = ''; | ||
private _gasWanted: number = defaultGasWanted; | ||
private _gasFee: string = defaultTxFee; | ||
|
||
messages(...messages: TransactionMessage[]): TransactionBuilder { | ||
this._messages = messages; | ||
return this; | ||
} | ||
|
||
fees(...fees: { amount: number; denom: string }[]): TransactionBuilder { | ||
this._fees = fees.map((fee) => ({ amount: fee.amount.toString(), denom: fee.denom })); | ||
fee(amount: number, denom: string): TransactionBuilder { | ||
this._gasFee = `${amount}${denom}`; | ||
return this; | ||
} | ||
|
||
gasWanted(amount: number): TransactionBuilder { | ||
this._gasWanted = amount.toString(); | ||
this._gasWanted = amount; | ||
return this; | ||
} | ||
|
||
chainId(chainId: string): TransactionBuilder { | ||
this._chainId = chainId; | ||
memo(memo: string): TransactionBuilder { | ||
this._memo = memo; | ||
return this; | ||
} | ||
|
||
accountNumber(accountNumber: number): TransactionBuilder { | ||
this._accountNumber = accountNumber.toString(); | ||
return this; | ||
private get txMessages(): Any[] { | ||
return this._messages.map((message) => { | ||
return Any.create({ | ||
typeUrl: message.type, | ||
value: encodeTransactionMessage(message), | ||
}); | ||
}); | ||
} | ||
|
||
sequence(sequence: number): TransactionBuilder { | ||
this._sequence = sequence.toString(); | ||
return this; | ||
} | ||
|
||
memo(memo: string): TransactionBuilder { | ||
this._memo = memo; | ||
return this; | ||
private get txFee(): TxFee { | ||
return TxFee.create({ | ||
gasFee: this._gasFee, | ||
gasWanted: this._gasWanted, | ||
}); | ||
} | ||
|
||
build(): Tx { | ||
const txDocument = { | ||
msgs: this._messages, | ||
fee: { | ||
amount: this._fees, | ||
gas: this._gasWanted, | ||
}, | ||
chain_id: this._chainId, | ||
account_number: this._accountNumber, | ||
sequence: this._sequence, | ||
return { | ||
messages: this.txMessages, | ||
fee: this.txFee, | ||
memo: this._memo, | ||
signatures: [], | ||
}; | ||
|
||
return Tx.fromJSON(txDocument); | ||
} | ||
|
||
public static create(): TransactionBuilder { | ||
return new TransactionBuilder().gasWanted(defaultGasWanted).fees(defaultGasFee); | ||
const builder = new TransactionBuilder(); | ||
|
||
return builder.gasWanted(defaultGasWanted); | ||
} | ||
} |
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
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
14 changes: 2 additions & 12 deletions
14
packages/sdk/src/providers/adena-wallet/types/transactions.ts
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
Oops, something went wrong.