Skip to content

Commit

Permalink
wallet-adapters,wallet-standard: support chain and account transactio…
Browse files Browse the repository at this point in the history
…n parameters (MystenLabs#8496)

* allow dapps/wallet-kit specify the chain and account for a transaction

part of APPS-281
  • Loading branch information
pchrysochoidis authored Feb 24, 2023
1 parent c8ea6a1 commit c718dee
Show file tree
Hide file tree
Showing 30 changed files with 338 additions and 280 deletions.
14 changes: 14 additions & 0 deletions .changeset/strange-fireants-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@mysten/wallet-adapter-wallet-standard": minor
"@mysten/wallet-adapter-unsafe-burner": minor
"@mysten/wallet-adapter-base": minor
"@mysten/wallet-kit-core": minor
"@mysten/wallet-standard": minor
"@mysten/wallet-kit": minor
---

wallet-standard: changes sui:signAndExecuteTransaction and sui:signTransaction features to support account and chain options
wallet-adapter-wallet-standard: change signAndExecuteTransaction and signTransaction signatures to support account and chain options
wallet-adapter-wallet-standard: ensure version compatibility for of the wallet signAndExecuteTransaction and signTransaction features before using them (same major version)
wallet-kit-core/wallet-kit: expose accounts as ReadonlyWalletAccount instead of only the address
wallet-kit-core: signTransaction and signAndExecuteTransaction methods mirror the ones in standard adapter
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ export function ModuleFunction({
const execute = useMutation({
mutationFn: async ({ params, types }: TypeOf<typeof argsSchema>) => {
const result = await signAndExecuteTransaction({
kind: 'moveCall',
data: {
packageObjectId: packageId,
module: moduleName,
function: functionName,
arguments: params || [],
typeArguments: types || [],
gasBudget: 2000,
transaction: {
kind: 'moveCall',
data: {
packageObjectId: packageId,
module: moduleName,
function: functionName,
arguments: params || [],
typeArguments: types || [],
gasBudget: 2000,
},
},
});
if (getExecutionStatusType(result) === 'failure') {
Expand Down
4 changes: 2 additions & 2 deletions apps/wallet/src/dapp-interface/WalletStandardInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ export class SuiWallet implements Wallet {
on: this.#on,
},
'sui:signTransaction': {
version: '1.0.0',
version: '2.0.0',
signTransaction: this.#signTransaction,
},
'sui:signAndExecuteTransaction': {
version: '1.1.0',
version: '2.0.0',
signAndExecuteTransaction: this.#signAndExecuteTransaction,
},
'suiWallet:stake': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export function AddDelegation({ validator, amount }: Props) {
throw new Error("No coins found.");
}

const totalBalance = coins.reduce((acc, coin) => (acc += BigInt(coin.balance)), 0n);
const totalBalance = coins.reduce(
(acc, coin) => (acc += BigInt(coin.balance)),
0n
);

const mistAmount = toMist(amount);

Expand All @@ -62,8 +65,8 @@ export function AddDelegation({ validator, amount }: Props) {

const stakeCoin = await manageCoins(coins, mistAmount, gasRequired);

await signAndExecuteTransaction(
{
await signAndExecuteTransaction({
transaction: {
kind: "moveCall",
data: {
packageObjectId: SUI_FRAMEWORK_ADDRESS,
Expand All @@ -79,10 +82,10 @@ export function AddDelegation({ validator, amount }: Props) {
],
},
},
{
// requestType: "WaitForEffectsCert",
}
);
// options: {
// requestType: "WaitForEffectsCert",
// },
});
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export function CancelDelegation({ stake }: Props) {
const { signAndExecuteTransaction } = useWalletKit();

const withdrawDelegation = useMutation(["unstake-validator"], async () => {
await signAndExecuteTransaction(
{
await signAndExecuteTransaction({
transaction: {
kind: "moveCall",
data: {
packageObjectId: SUI_FRAMEWORK_ADDRESS,
Expand All @@ -38,10 +38,10 @@ export function CancelDelegation({ stake }: Props) {
],
},
},
{
// requestType: "WaitForEffectsCert",
}
);
// options: {
// requestType: "WaitForEffectsCert",
// },
});
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export function WithdrawDelegation({ stake, delegation }: Props) {
const { currentAccount, signAndExecuteTransaction } = useWalletKit();

const withdrawDelegation = useMutation(["unstake-validator"], async () => {
await signAndExecuteTransaction(
{
await signAndExecuteTransaction({
transaction: {
kind: "moveCall",
data: {
packageObjectId: SUI_FRAMEWORK_ADDRESS,
Expand All @@ -40,10 +40,10 @@ export function WithdrawDelegation({ stake, delegation }: Props) {
],
},
},
{
// requestType: "WaitForEffectsCert",
}
);
// options: {
// requestType: "WaitForEffectsCert",
// },
});
});

return (
Expand Down
6 changes: 3 additions & 3 deletions dapps/frenemies/src/network/queries/coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const DEC = 9;
export function useBalance() {
const { currentAccount } = useWalletKit();
return useQuery(
["account-balance", currentAccount],
["account-balance", currentAccount?.address],
async () => {
const { totalBalance } = await provider.getBalance(
currentAccount!,
currentAccount?.address!,
SUI_TYPE_ARG
);

Expand All @@ -24,7 +24,7 @@ export function useBalance() {
};
},
{
enabled: !!currentAccount,
enabled: !!currentAccount?.address,
refetchInterval: 60 * 1000,
staleTime: 2000,
}
Expand Down
26 changes: 14 additions & 12 deletions dapps/frenemies/src/network/queries/scorecard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,20 @@ export function useRefreshScorecard() {
if (!leaderboard) throw new Error("Missing leaderboard");

await signAndExecuteTransaction({
kind: "moveCall",
data: {
packageObjectId: config.VITE_PKG,
module: "frenemies",
function: "update",
typeArguments: [],
gasBudget: Number(GAS_BUDGET),
arguments: [
normalizeSuiAddress(scorecard.reference.objectId),
SUI_SYSTEM_ID,
normalizeSuiAddress(leaderboard.reference.objectId),
],
transaction: {
kind: "moveCall",
data: {
packageObjectId: config.VITE_PKG,
module: "frenemies",
function: "update",
typeArguments: [],
gasBudget: Number(GAS_BUDGET),
arguments: [
normalizeSuiAddress(scorecard.reference.objectId),
SUI_SYSTEM_ID,
normalizeSuiAddress(leaderboard.reference.objectId),
],
},
},
});
});
Expand Down
6 changes: 3 additions & 3 deletions dapps/frenemies/src/network/queries/use-raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export function useRawObject<T>(objectId: string, bcsType: string) {
function useObjectsOwnedByAddress() {
const { currentAccount } = useWalletKit();
return useQuery(
["owned", currentAccount],
async () => provider.getObjectsOwnedByAddress(currentAccount!),
["owned", currentAccount?.address],
async () => provider.getObjectsOwnedByAddress(currentAccount?.address!),
{
enabled: !!currentAccount,
enabled: !!currentAccount?.address,
refetchInterval: 2 * 60 * 1000,
}
);
Expand Down
23 changes: 14 additions & 9 deletions dapps/frenemies/src/routes/Claim.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ function Connected() {
const submitWinner = useMutation(
["submit-winner"],
async (values: z.infer<typeof Schema>) => {
if (!currentAccount) {
throw new Error("Missing account");
}
const res = await fetch(
import.meta.env.DEV
? "http://127.0.0.1:3003/frenemies"
Expand All @@ -98,7 +101,7 @@ function Connected() {
"content-type": "application/json",
},
body: JSON.stringify({
address: currentAccount!,
address: currentAccount.address,
name: values.name,
email: values.email,
}),
Expand All @@ -112,14 +115,16 @@ function Connected() {
const data = await res.json();

await signAndExecuteTransaction({
kind: "moveCall",
data: {
packageObjectId: config.VITE_NOOP,
module: "noop",
function: "noop_w_metadata",
typeArguments: [],
gasBudget: Number(GAS_BUDGET),
arguments: [data.bytes],
transaction: {
kind: "moveCall",
data: {
packageObjectId: config.VITE_NOOP,
module: "noop",
function: "noop_w_metadata",
typeArguments: [],
gasBudget: Number(GAS_BUDGET),
arguments: [data.bytes],
},
},
});
}
Expand Down
24 changes: 13 additions & 11 deletions dapps/frenemies/src/routes/Migrate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ export function Migrate() {
["migrate-scorecard"],
async () => {
await signAndExecuteTransaction({
kind: "moveCall",
data: {
packageObjectId: config.VITE_PKG,
module: "frenemies",
function: "migrate",
typeArguments: [],
arguments: [
legacyScorecard.data!.reference.objectId,
config.VITE_MIGRATION,
],
gasBudget: GAS_BUDGET,
transaction: {
kind: "moveCall",
data: {
packageObjectId: config.VITE_PKG,
module: "frenemies",
function: "migrate",
typeArguments: [],
arguments: [
legacyScorecard.data!.reference.objectId,
config.VITE_MIGRATION,
],
gasBudget: GAS_BUDGET,
},
},
});
},
Expand Down
32 changes: 17 additions & 15 deletions dapps/frenemies/src/routes/Setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function Setup() {
{ pkg: config.VITE_PKG, registry: config.VITE_REGISTRY },
].map(({ pkg, registry }) =>
provider.devInspectTransaction(
currentAccount,
currentAccount.address,
{
kind: "moveCall",
data: {
Expand Down Expand Up @@ -114,21 +114,23 @@ export function Setup() {
});

await signAndExecuteTransaction({
kind: "moveCall",
data: {
packageObjectId: config.VITE_PKG,
module: "frenemies",
function: "register",
arguments: [
username,
config.VITE_REGISTRY,
config.VITE_OLD_REGISTRY,
SUI_SYSTEM_ID,
],
typeArguments: [],
transaction: {
kind: "moveCall",
data: {
packageObjectId: config.VITE_PKG,
module: "frenemies",
function: "register",
arguments: [
username,
config.VITE_REGISTRY,
config.VITE_OLD_REGISTRY,
SUI_SYSTEM_ID,
],
typeArguments: [],

// TODO: Fix in sui.js - add option to use bigint...
gasBudget: Number(GAS_BUDGET),
// TODO: Fix in sui.js - add option to use bigint...
gasBudget: Number(GAS_BUDGET),
},
},
});
},
Expand Down
18 changes: 10 additions & 8 deletions dapps/frenemies/src/utils/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function useGetLatestCoins() {
return async () => {
if (!currentAccount) throw new Error("Wallet not connected");
const { data } = await provider.getCoins(
currentAccount,
currentAccount.address,
SUI_TYPE_ARG,
undefined,
1000
Expand Down Expand Up @@ -59,13 +59,15 @@ export function useManageCoin() {
const inputCoins = getCoins(coins, totalAmount);

const result = await signAndExecuteTransaction({
kind: "paySui",
data: {
inputCoins: inputCoins.map((coin) => coin.coinObjectId),
recipients: [currentAccount, currentAccount],
// TODO: Update SDK to accept bigint
amounts: [Number(amount), Number(gasFee)],
gasBudget: computeGasBudgetForPay(inputCoins.length),
transaction: {
kind: "paySui",
data: {
inputCoins: inputCoins.map((coin) => coin.coinObjectId),
recipients: [currentAccount.address, currentAccount.address],
// TODO: Update SDK to accept bigint
amounts: [Number(amount), Number(gasFee)],
gasBudget: computeGasBudgetForPay(inputCoins.length),
},
},
});

Expand Down
Loading

0 comments on commit c718dee

Please sign in to comment.