Skip to content

Commit 85784c1

Browse files
committed
feat(sync26): support unbonding in AccountInfo
oops! cosmosAPI wasn't checked in until now?!
1 parent 8576c8b commit 85784c1

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

packages/sync26/cosmosAPI.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* https://github.com/cosmos/cosmos-sdk/blob/main/client/docs/swagger-ui/swagger.yaml
3+
*/
4+
const makeLCD = (fetch, endpoint) => {
5+
const { freeze } = Object;
6+
7+
const getJSON = path => fetch(endpoint + path).then(res => res.json());
8+
9+
return freeze({
10+
account: addr =>
11+
freeze({
12+
balances: () => getJSON(`/cosmos/bank/v1beta1/balances/${addr}`),
13+
delegations: () =>
14+
getJSON(`/cosmos/staking/v1beta1/delegations/${addr}`),
15+
unbonding: () =>
16+
getJSON(
17+
`/cosmos/staking/v1beta1/delegators/${addr}/unbonding_delegations`,
18+
),
19+
rewards: () =>
20+
getJSON(`/cosmos/distribution/v1beta1/delegators/${addr}/rewards`),
21+
}),
22+
denom: denom =>
23+
freeze({
24+
meta: () => getJSON(`/cosmos/bank/v1beta1/denoms_metadata/${denom}`),
25+
}),
26+
});
27+
};
28+
29+
const testLCD = async (
30+
addr = 'agoric1ut8wc2l52layqq8lmxfvd9vwhdge2x5qsqrhrl',
31+
endpoint = 'https://api.agoric.nodestake.top',
32+
) => {
33+
const fetch = makeFetch();
34+
const lcd = makeLCD(fetch, endpoint);
35+
36+
// odd: denom ubld: key not found
37+
// const bldInfo = await lcd.denom('ubld').meta();
38+
// console.log(bldInfo);
39+
const acct = lcd.account(addr);
40+
const rewards = await acct.rewards();
41+
console.log(rewards);
42+
};
43+
44+
const sum = xs => xs.reduce((acc, cur) => acc + cur, 0);
45+
46+
async function AccountInfo(addr, hd = true, denom = 'ubld', _nonce, io = {}) {
47+
const {
48+
doc = SpreadsheetApp.getActive(),
49+
endpoint = doc.getRangeByName('AgoricLCD').getValue(),
50+
fetch = makeFetch(),
51+
lcd = makeLCD(fetch, endpoint),
52+
acct = lcd.account(addr),
53+
} = io;
54+
const { balances } = await acct.balances();
55+
if (balances.length > 1) throw Error('unsupported');
56+
if (balances.length > 0 && balances[0].denom !== denom)
57+
throw Error('unsupported');
58+
const balance = balances.length === 0 ? 0 : Number(balances[0].amount);
59+
const { delegation_responses } = await acct.delegations();
60+
const delegation = sum(
61+
delegation_responses.map(({ balance }) => Number(balance.amount)),
62+
);
63+
const rewards = await acct.rewards();
64+
if (rewards.total.length > 1) throw Error('unsupported');
65+
const reward =
66+
rewards.total.length === 1 ? Number(rewards.total[0].amount) : 0;
67+
const { unbonding_responses } = await acct.unbonding();
68+
const unbonding = sum(
69+
unbonding_responses.flatMap(r => r.entries.map(e => Number(e.balance))),
70+
);
71+
const record = { balance, delegation, reward, unbonding };
72+
const values = Object.values(record).map(micro => micro / 1000000);
73+
if (!hd) return [values];
74+
return [Object.keys(record), values];
75+
}
76+
77+
async function testAccountInfo(io = {}) {
78+
const {
79+
doc = SpreadsheetApp.getActive(),
80+
addr = doc.getRangeByName('testAddr').getValue(),
81+
} = io;
82+
const rows = await AccountInfo(addr, true, 'ubld', 1, { doc });
83+
console.log(rows);
84+
}
85+
86+
async function PendingReward(addr) {
87+
console.warn('AMBIENT: SpreadsheetApp');
88+
const doc = SpreadsheetApp.getActive();
89+
const endpoint = doc.getRangeByName('AgoricLCD').getValue();
90+
91+
const fetch = makeFetch();
92+
const lcd = makeLCD(fetch, endpoint);
93+
const acct = lcd.account(addr);
94+
const rewards = await acct.rewards();
95+
return rewards.total.map(({ denom, amount }) => [Number(amount), denom]);
96+
}
97+
98+
async function testPendingReward(
99+
addr = 'agoric1ut8wc2l52layqq8lmxfvd9vwhdge2x5qsqrhrl',
100+
) {
101+
const rows = await PendingReward(addr);
102+
console.log(rows);
103+
}

0 commit comments

Comments
 (0)