-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathauto-stake-it-tap-kit.js
157 lines (146 loc) · 4.9 KB
/
auto-stake-it-tap-kit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { M, mustMatch } from '@endo/patterns';
import { E } from '@endo/far';
import { VowShape } from '@agoric/vow';
import { makeTracer } from '@agoric/internal';
import { atob } from '@endo/base64';
import { ChainAddressShape } from '../typeGuards.js';
const trace = makeTracer('AutoStakeItTap');
/**
* @import {IBCChannelID, VTransferIBCEvent} from '@agoric/vats';
* @import {VowTools} from '@agoric/vow';
* @import {Zone} from '@agoric/zone';
* @import {TargetApp} from '@agoric/vats/src/bridge-target.js';
* @import {ChainAddress, CosmosValidatorAddress, Denom, OrchestrationAccount, StakingAccountActions} from '@agoric/orchestration';
* @import {FungibleTokenPacketData} from '@agoric/cosmic-proto/ibc/applications/transfer/v2/packet.js';
* @import {TypedPattern} from '@agoric/internal';
*/
/**
* @typedef {{
* stakingAccount: ERef<OrchestrationAccount<any> & StakingAccountActions>;
* localAccount: ERef<OrchestrationAccount<{ chainId: 'agoric' }>>;
* validator: CosmosValidatorAddress;
* localChainAddress: ChainAddress;
* remoteChainAddress: ChainAddress;
* sourceChannel: IBCChannelID;
* remoteDenom: Denom;
* localDenom: Denom;
* }} StakingTapState
*/
/** @type {TypedPattern<StakingTapState>} */
const StakingTapStateShape = {
stakingAccount: M.remotable('CosmosOrchestrationAccount'),
localAccount: M.remotable('LocalOrchestrationAccount'),
validator: ChainAddressShape,
localChainAddress: ChainAddressShape,
remoteChainAddress: ChainAddressShape,
sourceChannel: M.string(),
remoteDenom: M.string(),
localDenom: M.string(),
};
harden(StakingTapStateShape);
/**
* @param {Zone} zone
* @param {VowTools} vowTools
*/
const prepareStakingTapKit = (zone, { watch }) => {
return zone.exoClassKit(
'StakingTapKit',
{
tap: M.interface('AutoStakeItTap', {
receiveUpcall: M.call(M.record()).returns(
M.or(VowShape, M.undefined()),
),
}),
transferWatcher: M.interface('TransferWatcher', {
onFulfilled: M.call(M.undefined())
.optional(M.bigint())
.returns(VowShape),
}),
},
/** @param {StakingTapState} initialState */
initialState => {
mustMatch(initialState, StakingTapStateShape);
return harden(initialState);
},
{
tap: {
/**
* Transfers from localAccount to stakingAccount, then delegates from
* the stakingAccount to `validator` if the expected token (remoteDenom)
* is received.
*
* @param {VTransferIBCEvent} event
*/
receiveUpcall(event) {
trace('receiveUpcall', event);
// ignore packets from unknown channels
if (event.packet.source_channel !== this.state.sourceChannel) {
return;
}
const tx = /** @type {FungibleTokenPacketData} */ (
JSON.parse(atob(event.packet.data))
);
trace('receiveUpcall packet data', tx);
const { remoteDenom, localChainAddress } = this.state;
// ignore outgoing transfers
if (tx.receiver !== localChainAddress.value) {
return;
}
// only interested in transfers of `remoteDenom`
if (tx.denom !== remoteDenom) {
return;
}
const { localAccount, localDenom, remoteChainAddress } = this.state;
return watch(
E(localAccount).transfer(
{
denom: localDenom,
value: BigInt(tx.amount),
},
remoteChainAddress,
),
this.facets.transferWatcher,
BigInt(tx.amount),
);
},
},
transferWatcher: {
/**
* @param {void} _result
* @param {bigint} value the qty of uatom to delegate
*/
onFulfilled(_result, value) {
const { stakingAccount, validator, remoteDenom } = this.state;
return watch(
E(stakingAccount).delegate(validator, {
denom: remoteDenom,
value,
}),
);
},
},
},
);
};
/**
* Provides a {@link TargetApp} that reacts to an incoming IBC transfer by:
*
* 1. transferring the funds to the staking account specified at initialization
* 2. delegating the funds to the validator specified at initialization
*
* XXX consider a facet with a method for changing the validator
*
* XXX consider logic for multiple stakingAccounts + denoms
*
* @param {Zone} zone
* @param {VowTools} vowTools
* @returns {(
* ...args: Parameters<ReturnType<typeof prepareStakingTapKit>>
* ) => ReturnType<ReturnType<typeof prepareStakingTapKit>>['tap']}
*/
export const prepareStakingTap = (zone, vowTools) => {
const makeKit = prepareStakingTapKit(zone, vowTools);
return (...args) => makeKit(...args).tap;
};
/** @typedef {ReturnType<typeof prepareStakingTap>} MakeStakingTap */
/** @typedef {ReturnType<MakeStakingTap>} StakingTap */