Skip to content

Commit

Permalink
feat(sdk-coin-sui): support paySui and payAllSui transaction types
Browse files Browse the repository at this point in the history
Ticket: BG-61478
  • Loading branch information
linyah9912 committed Nov 16, 2022
1 parent 9580a35 commit 2df4f9f
Show file tree
Hide file tree
Showing 14 changed files with 441 additions and 185 deletions.
2 changes: 1 addition & 1 deletion modules/sdk-coin-sui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"dependencies": {
"@bitgo/sdk-core": "^3.0.0",
"@bitgo/statics": "^11.0.0",
"@mysten/bcs": "^0.3.0",
"@mysten/bcs": "^0.4.0",
"bignumber.js": "^9.0.0",
"bs58": "^4.0.1",
"js-sha3": "^0.8.0",
Expand Down
96 changes: 96 additions & 0 deletions modules/sdk-coin-sui/src/lib/bcs.ts
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',
});
13 changes: 13 additions & 0 deletions modules/sdk-coin-sui/src/lib/constants.ts
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',
}
56 changes: 50 additions & 6 deletions modules/sdk-coin-sui/src/lib/iface.ts
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;
}
Loading

0 comments on commit 2df4f9f

Please sign in to comment.