Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zksend] Add method for sending non-sui balances #15687

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[zksend] Add method for sending non-sui balances
  • Loading branch information
hayes-mysten committed Jan 12, 2024
commit c8924f994de062e975191f2f6da2de040419d352
5 changes: 5 additions & 0 deletions .changeset/fresh-dots-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/zksend': patch
---

Add method for sending non-sui balances
80 changes: 52 additions & 28 deletions sdk/zksend/src/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const SUI_COIN_OBJECT_TYPE = normalizeStructTag('0x2::coin::Coin<0x2::sui::SUI>'
interface CreateZkSendLinkOptions {
transactionBlock?: TransactionBlock;
calculateGas?: (options: {
mist: bigint;
balances: Map<string, bigint>;
objects: TransactionObjectInput[];
gasEstimateFromDryRun: bigint;
}) => Promise<bigint> | bigint;
Expand All @@ -54,9 +54,11 @@ export class ZkSendLinkBuilder {
#keypair: Keypair;
#client: SuiClient;
#objects = new Set<TransactionObjectInput>();
#mist = 0n;
#balances = new Map<string, bigint>();
#sender: string;

#coinsByType = new Map<string, CoinStruct[]>();

constructor({
host = DEFAULT_ZK_SEND_LINK_OPTIONS.host,
path = DEFAULT_ZK_SEND_LINK_OPTIONS.path,
Expand All @@ -72,10 +74,14 @@ export class ZkSendLinkBuilder {
}

addClaimableMist(amount: bigint) {
this.#mist += amount;
this.addClaimableBalance(SUI_COIN_TYPE, amount);
}

addClaimableBalance(coinType: string, amount: bigint) {
this.#balances.set(normalizeStructTag(coinType), (this.#balances.get(coinType) ?? 0n) + amount);
}

addClaimableObject(id: TransactionObjectInput) {
addClaimableObject(id: string) {
this.#objects.add(id);
}

Expand Down Expand Up @@ -107,7 +113,7 @@ export class ZkSendLinkBuilder {
const gasEstimateFromDryRun = await this.#estimateClaimGasFee();
const baseGasAmount = calculateGas
? await calculateGas({
mist: this.#mist,
balances: this.#balances,
objects: [...this.#objects],
gasEstimateFromDryRun,
})
Expand All @@ -120,19 +126,25 @@ export class ZkSendLinkBuilder {

const address = this.#keypair.toSuiAddress();
const objectsToTransfer = [...this.#objects].map((id) => txb.object(id));
const [gas] = txb.splitCoins(txb.gas, [roundedGasAmount]);
objectsToTransfer.push(gas);

txb.setSenderIfNotSet(this.#sender);

if (this.#mist) {
const [gas, sui] = txb.splitCoins(txb.gas, [roundedGasAmount, this.#mist]);
objectsToTransfer.push(gas, sui);
} else {
const [gas] = txb.splitCoins(txb.gas, [roundedGasAmount]);
objectsToTransfer.push(gas);
for (const [coinType, amount] of this.#balances) {
if (coinType === SUI_COIN_TYPE) {
const [sui] = txb.splitCoins(txb.gas, [amount]);
objectsToTransfer.push(sui);
} else {
const coins = (await this.#getCoinsByType(coinType)).map((coin) => coin.coinObjectId);
const merged =
coins.length > 0 ? txb.mergeCoins(coins[0], coins.slice(1)) : txb.object(coins[0]);
const [split] = txb.splitCoins(merged, [amount]);
objectsToTransfer.push(split);
}
}

if (objectsToTransfer.length > 0) {
txb.transferObjects(objectsToTransfer, address);
}
txb.transferObjects(objectsToTransfer, address);

return txb;
}
Expand All @@ -143,24 +155,21 @@ export class ZkSendLinkBuilder {
txb.setGasPayment([]);
txb.transferObjects([txb.gas], this.#keypair.toSuiAddress());

if (this.#mist) {
const allCoins = await this.#client.getCoins({
owner: this.#sender,
coinType: SUI_COIN_TYPE,
limit: 1,
});
const idsToTransfer = [...this.#objects];

if (!allCoins.data.length) {
throw new Error('Sending account does not contain any Sui');
for (const [coinType] of this.#balances) {
const coins = await this.#getCoinsByType(coinType);

if (!coins.length) {
throw new Error(`Sending account does not contain any coins of type ${coinType}`);
}

idsToTransfer.push(coins[0].coinObjectId);
}

if (idsToTransfer.length > 0) {
txb.transferObjects(
[allCoins.data[0].coinObjectId, ...this.#objects].map((id) => txb.object(id)),
this.#keypair.toSuiAddress(),
);
} else if (this.#objects.size > 0) {
txb.transferObjects(
[...this.#objects].map((id) => txb.object(id)),
idsToTransfer.map((id) => txb.object(id)),
this.#keypair.toSuiAddress(),
);
}
Expand All @@ -175,6 +184,21 @@ export class ZkSendLinkBuilder {
BigInt(result.effects.gasUsed.storageRebate)
);
}

async #getCoinsByType(coinType: string) {
if (this.#coinsByType.has(coinType)) {
return this.#coinsByType.get(coinType)!;
}

const coins = await this.#client.getCoins({
coinType,
owner: this.#sender,
});

this.#coinsByType.set(coinType, coins.data);

return coins.data;
}
}

export interface ZkSendLinkOptions {
Expand Down
Loading