Skip to content

Commit

Permalink
call transferSui for Sui token sending (MystenLabs#3661)
Browse files Browse the repository at this point in the history
  • Loading branch information
gegaowp authored Aug 1, 2022
1 parent 5f356a0 commit c5042f5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
7 changes: 0 additions & 7 deletions wallet/src/ui/app/pages/home/transfer-coin/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ export function createValidationSchema(
}
}
)
.test(
'num-gas-coins-check',
`Need at least 2 ${GAS_SYMBOL} coins to transfer a ${GAS_SYMBOL} coin`,
() => {
return coinType !== GAS_TYPE_ARG || totalGasCoins >= 2;
}
)
.label('Amount'),
});
}
36 changes: 34 additions & 2 deletions wallet/src/ui/app/redux/slices/sui-objects/Coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const COIN_TYPE_ARG_REGEX = /^0x2::coin::Coin<(.+)>$/;
export const DEFAULT_GAS_BUDGET_FOR_SPLIT = 1000;
export const DEFAULT_GAS_BUDGET_FOR_MERGE = 500;
export const DEFAULT_GAS_BUDGET_FOR_TRANSFER = 100;
export const DEFAULT_GAS_BUDGET_FOR_TRANSFER_SUI = 100;
export const DEFAULT_GAS_BUDGET_FOR_STAKE = 1000;
export const GAS_TYPE_ARG = '0x2::sui::SUI';
export const GAS_SYMBOL = 'SUI';
Expand Down Expand Up @@ -83,6 +84,33 @@ export class Coin {
});
}

/**
* Transfer `amount` of Coin<Sui> to `recipient`.
*
* @param signer A signer with connection to the gateway:e.g., new RawSigner(keypair, new JsonRpcProvider(endpoint))
* @param coins A list of Sui Coins owned by the signer
* @param amount The amount to be transferred
* @param recipient The sui address of the recipient
*/
public static async transferSui(
signer: RawSigner,
coins: SuiMoveObject[],
amount: bigint,
recipient: SuiAddress
): Promise<TransactionResponse> {
const coin = await Coin.prepareCoinWithEnoughBalance(
signer,
coins,
amount + BigInt(DEFAULT_GAS_BUDGET_FOR_TRANSFER_SUI)
);
return await signer.transferSui({
suiObjectId: Coin.getID(coin),
gasBudget: DEFAULT_GAS_BUDGET_FOR_TRANSFER_SUI,
recipient: recipient,
amount: Number(amount),
});
}

/**
* Stake `amount` of Coin<T> to `validator`. Technically it means user delegates `amount` of Coin<T> to `validator`,
* such that `validator` will stake the `amount` of Coin<T> for the user.
Expand Down Expand Up @@ -114,7 +142,11 @@ export class Coin {
coins: SuiMoveObject[],
amount: bigint
): Promise<ObjectId> {
const coin = await Coin.selectCoinForSplit(signer, coins, amount);
const coin = await Coin.prepareCoinWithEnoughBalance(
signer,
coins,
amount
);
const coinID = Coin.getID(coin);
const balance = Coin.getBalance(coin);
if (balance === amount) {
Expand All @@ -131,7 +163,7 @@ export class Coin {
}
}

private static async selectCoinForSplit(
private static async prepareCoinWithEnoughBalance(
signer: RawSigner,
coins: SuiMoveObject[],
amount: bigint
Expand Down
20 changes: 14 additions & 6 deletions wallet/src/ui/app/redux/slices/transactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@ export const sendTokens = createAsyncThunk<
isSuiMoveObject(anObj.data) && anObj.data.type === coinType
)
.map(({ data }) => data as SuiMoveObject);
const response = await Coin.transferCoin(
api.getSignerInstance(keypairVault.getKeyPair()),
coins,
amount,
recipientAddress
);
const response =
Coin.getCoinSymbol(tokenTypeArg) === 'SUI'
? await Coin.transferSui(
api.getSignerInstance(keypairVault.getKeyPair()),
coins,
amount,
recipientAddress
)
: await Coin.transferCoin(
api.getSignerInstance(keypairVault.getKeyPair()),
coins,
amount,
recipientAddress
);

// TODO: better way to sync latest objects
dispatch(fetchAllOwnedObjects());
Expand Down

0 comments on commit c5042f5

Please sign in to comment.