Skip to content

Commit

Permalink
feature: add sender options (#8506)
Browse files Browse the repository at this point in the history
  • Loading branch information
iomekam authored Nov 10, 2023
1 parent cdd20f7 commit 00cd6ae
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/pegasus/src/courier.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ export const makeCourierMaker =
transferProtocol,
}) => {
/** @type {Sender} */
const send = async (zcfSeat, depositAddress, memo) => {
const send = async (zcfSeat, depositAddress, memo, opts) => {
const tryToSend = async () => {
const amount = zcfSeat.getAmountAllocated('Transfer', localBrand);
const transferPacket = await E(transferProtocol).makeTransferPacket({
value: amount.value,
remoteDenom: sendDenom,
depositAddress,
memo,
opts,
});

// Retain the payment. We must not proceed on failure.
Expand Down
11 changes: 6 additions & 5 deletions packages/pegasus/src/ics20.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import { assert, details as X, Fail } from '@agoric/assert';
const ICS20_TRANSFER_SUCCESS_RESULT = 'AQ==';

// ibc-go as late as v3 requires the `sender` to be nonempty, but doesn't
// actually use it on the receiving side. We don't need it on the sending side,
// either, so we can just omit it.
export const DUMMY_SENDER_ADDRESS = 'pegasus';
// actually use it on the receiving side.
export const DEFAULT_SENDER_ADDRESS = 'pegasus';

/**
* @param {string} s
Expand Down Expand Up @@ -51,7 +50,7 @@ const safeJSONParseObject = s => {
*/
export const parseICS20TransferPacket = async packet => {
const ics20Packet = safeJSONParseObject(packet);
const { amount, denom, receiver, memo } = ics20Packet;
const { amount, denom, receiver, memo, opts } = ics20Packet;

assert.typeof(denom, 'string', X`Denom ${denom} must be a string`);
assert.typeof(receiver, 'string', X`Receiver ${receiver} must be a string`);
Expand All @@ -74,6 +73,7 @@ export const parseICS20TransferPacket = async packet => {
remoteDenom: denom,
value,
memo,
opts,
});
};

Expand All @@ -89,6 +89,7 @@ export const makeICS20TransferPacket = async ({
remoteDenom,
depositAddress,
memo,
opts: { sender = DEFAULT_SENDER_ADDRESS },
}) => {
// We're using Nat as a dynamic check for overflow.
const stringValue = String(Nat(value));
Expand All @@ -99,7 +100,7 @@ export const makeICS20TransferPacket = async ({
amount: stringValue,
denom: remoteDenom,
receiver: depositAddress,
sender: DUMMY_SENDER_ADDRESS,
sender,
memo,
};

Expand Down
5 changes: 3 additions & 2 deletions packages/pegasus/src/pegasus.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,10 @@ const makePegasus = (zcf, board, namesByAddress) => {
* @param {ERef<Peg>} pegP the peg over which to transfer
* @param {DepositAddress} depositAddress the remote receiver's address
* @param {string} [memo] the memo to attach to ics transfer packet
* @param {SenderOptions} [opts] additional sender options
* @returns {Promise<Invitation>} to transfer, make an offer of { give: { Transfer: pegAmount } } to this invitation
*/
async makeInvitationToTransfer(pegP, depositAddress, memo = '') {
async makeInvitationToTransfer(pegP, depositAddress, memo = '', opts = {}) {
// Verify the peg.
const peg = await pegP;
const denomState = pegToDenomState.get(peg);
Expand All @@ -481,7 +482,7 @@ const makePegasus = (zcf, board, namesByAddress) => {
*/
const offerHandler = zcfSeat => {
assertProposalShape(zcfSeat, TRANSFER_PROPOSAL_SHAPE);
send(zcfSeat, depositAddress, memo);
send(zcfSeat, depositAddress, memo, opts);
};

return zcf.makeInvitation(offerHandler, `pegasus ${sendDenom} transfer`);
Expand Down
8 changes: 7 additions & 1 deletion packages/pegasus/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @property {Denom} remoteDenom
* @property {DepositAddress} depositAddress
* @property {string} memo
* @property {SenderOptions} opts
*/

/**
Expand Down Expand Up @@ -47,7 +48,7 @@
*/

/**
* @typedef {(zcfSeat: ZCFSeat, depositAddress: DepositAddress, memo: string) => Promise<void>} Sender
* @typedef {(zcfSeat: ZCFSeat, depositAddress: DepositAddress, memo: string, opt: SenderOptions) => Promise<void>} Sender
* Successive transfers are not guaranteed to be processed in the order in which they were sent.
* @typedef {(parts: PacketParts) => Promise<Bytes>} Receiver
* @typedef {object} Courier
Expand Down Expand Up @@ -111,3 +112,8 @@
* @property {ConnectionHandler} handler
* @property {Subscription<PegasusConnection>} subscription
*/

/**
* @typedef {object} SenderOptions
* @property {string} [sender] the sender address attached to the packet to receive any refund
*/
3 changes: 2 additions & 1 deletion packages/pegasus/test/test-peg.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async function testRemotePeg(t) {
denom: 'portdef/chanabc/uatom',
memo: 'I am a memo!',
receiver: 'markaccount',
sender: 'pegasus',
sender: 'agoric1jmd7lwdyykrxm5h83nlhg74fctwnky04ufpqtc',
},
'expected transfer packet',
);
Expand Down Expand Up @@ -247,6 +247,7 @@ async function testRemotePeg(t) {
pegP,
'markaccount',
'I am a memo!',
{ sender: 'agoric1jmd7lwdyykrxm5h83nlhg74fctwnky04ufpqtc' },
);
const seat = await E(zoe).offer(
transferInvitation,
Expand Down

0 comments on commit 00cd6ae

Please sign in to comment.