forked from BitGo/BitGoJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk-coin-sui): support paySui and payAllSui transaction types
Ticket: BG-61478
- Loading branch information
1 parent
9580a35
commit 2df4f9f
Showing
14 changed files
with
441 additions
and
185 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { BCS, decodeStr, encodeStr, getSuiMoveConfig } from '@mysten/bcs'; | ||
import { Buffer } from 'buffer'; | ||
|
||
export const bcs = new BCS(getSuiMoveConfig()); | ||
|
||
bcs | ||
.registerType( | ||
'utf8string', | ||
(writer, str) => { | ||
const bytes = Array.from(Buffer.from(str)); | ||
return writer.writeVec(bytes, (writer, el) => writer.write8(el)); | ||
}, | ||
(reader) => { | ||
const bytes = reader.readVec((reader) => reader.read8()); | ||
return Buffer.from(bytes).toString('utf-8'); | ||
} | ||
) | ||
.registerType( | ||
'ObjectDigest', | ||
(writer, str) => { | ||
const bytes = Array.from(decodeStr(str, 'base64')); | ||
return writer.writeVec(bytes, (writer, el) => writer.write8(el)); | ||
}, | ||
(reader) => { | ||
const bytes = reader.readVec((reader) => reader.read8()); | ||
return encodeStr(new Uint8Array(bytes), 'base64'); | ||
} | ||
); | ||
|
||
bcs.registerStructType('SuiObjectRef', { | ||
objectId: 'address', | ||
version: 'u64', | ||
digest: 'ObjectDigest', | ||
}); | ||
|
||
bcs.registerStructType('PayTx', { | ||
coins: 'vector<SuiObjectRef>', | ||
recipients: 'vector<address>', | ||
amounts: 'vector<u64>', | ||
}); | ||
|
||
bcs.registerStructType('PaySuiTx', { | ||
coins: 'vector<SuiObjectRef>', | ||
recipients: 'vector<address>', | ||
amounts: 'vector<u64>', | ||
}); | ||
|
||
bcs.registerStructType('PayAllSuiTx', { | ||
coins: 'vector<SuiObjectRef>', | ||
recipient: 'address', | ||
amount: 'u64', // Even though Mysten sdk doesn't register amount field, adding this so that we can get the amount when deserializing | ||
}); | ||
|
||
bcs | ||
.registerEnumType('TypeTag', { | ||
bool: null, | ||
u8: null, | ||
u64: null, | ||
u128: null, | ||
address: null, | ||
signer: null, | ||
vector: 'TypeTag', | ||
struct: 'StructTag', | ||
u16: null, | ||
u32: null, | ||
u256: null, | ||
}) | ||
.registerStructType('StructTag', { | ||
address: 'address', | ||
module: 'string', | ||
name: 'string', | ||
typeParams: 'vector<TypeTag>', | ||
}); | ||
|
||
bcs.registerEnumType('Transaction', { | ||
TransferObject: 'TransferObjectTx', | ||
Publish: 'PublishTx', | ||
Call: 'MoveCallTx', | ||
TransferSui: 'TransferSuiTx', | ||
Pay: 'PayTx', | ||
PaySui: 'PaySuiTx', | ||
PayAllSui: 'PayAllSuiTx', | ||
}); | ||
|
||
bcs.registerEnumType('TransactionKind', { | ||
Single: 'Transaction', | ||
Batch: 'vector<Transaction>', | ||
}); | ||
|
||
bcs.registerStructType('TransactionData', { | ||
kind: 'TransactionKind', | ||
sender: 'address', | ||
gasPayment: 'SuiObjectRef', | ||
gasPrice: 'u64', | ||
gasBudget: 'u64', | ||
}); |
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,13 @@ | ||
export const UNAVAILABLE_TEXT = 'UNAVAILABLE'; | ||
export const TYPE_TAG = Array.from('TransactionData::').map((e) => e.charCodeAt(0)); | ||
|
||
// Need to keep in sync with | ||
// https://github.com/MystenLabs/sui/blob/f32877f2e40d35a008710c232e49b57aab886462/crates/sui-types/src/messages.rs#L338 | ||
export const SUI_GAS_PRICE = 1; | ||
export const SUI_ADDRESS_LENGTH = 20; | ||
|
||
export enum SuiTransactionType { | ||
Pay = 'Pay', | ||
PaySui = 'PaySui', | ||
PayAllSui = 'PayAllSui', | ||
} |
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,24 +1,68 @@ | ||
import { TransactionExplanation as BaseTransactionExplanation, TransactionType } from '@bitgo/sdk-core'; | ||
import { SuiObjectRef } from './transaction'; | ||
import { SuiTransactionType } from './constants'; | ||
|
||
export interface TransactionExplanation extends BaseTransactionExplanation { | ||
type: TransactionType; | ||
} | ||
|
||
export interface PayTx { | ||
coins: SuiObjectRef[]; | ||
recipients: string[]; | ||
amounts: number[]; | ||
export type SuiObjectRef = { | ||
/** Hex code as string representing the object id */ | ||
objectId: string; | ||
/** Object version */ | ||
version: number; | ||
/** Base64 string representing the object digest */ | ||
digest: string; | ||
}; | ||
|
||
export type TxDetails = PayTxDetails | PaySuiTxDetails | PayAllSuiTxDetails; | ||
|
||
export interface PayTxDetails { | ||
Pay: { | ||
coins: SuiObjectRef[]; | ||
recipients: string[]; | ||
amounts: number[]; | ||
}; | ||
} | ||
|
||
export interface PaySuiTxDetails { | ||
PaySui: { | ||
coins: SuiObjectRef[]; | ||
recipients: string[]; | ||
amounts: number[]; | ||
}; | ||
} | ||
|
||
export interface PayAllSuiTxDetails { | ||
PayAllSui: { | ||
coins: SuiObjectRef[]; | ||
recipient: string; | ||
amount: number; | ||
}; | ||
} | ||
|
||
/** | ||
* The transaction data returned from the toJson() function of a transaction | ||
*/ | ||
export interface TxData { | ||
id?: string; | ||
kind: { Single: TxDetails }; | ||
sender: string; | ||
gasPayment: SuiObjectRef; | ||
gasBudget: number; | ||
gasPrice: number; | ||
payTx: PayTx; | ||
} | ||
|
||
export interface PayTx { | ||
coins: SuiObjectRef[]; | ||
recipients: string[]; | ||
amounts: number[]; | ||
} | ||
|
||
export interface SuiTransaction { | ||
type: SuiTransactionType; | ||
sender: string; | ||
payTx: PayTx; | ||
gasBudget: number; | ||
gasPrice: number; | ||
gasPayment: SuiObjectRef; | ||
} |
Oops, something went wrong.